@ Override public INDArray createFromNpyPointer ( Pointer pointer ) { Pointer dataPointer = nativeOps . dataPointForNumpy ( pointer ) ; int dataBufferElementSize = nativeOps . elementSizeForNpyArray ( pointer ) ; DataBuffer data = null ; Pointer shapeBufferPointer = nativeOps . shapeBufferForNumpy ( pointer ) ; int length = nativeOps . lengthForShapeBufferPointer ( shapeBufferPointer ) ; shapeBufferPointer . capacity ( 8 * length ) ; shapeBufferPointer . limit ( 8 * length ) ; shapeBufferPointer . position ( 0 ) ; val intPointer = new LongPointer ( shapeBufferPointer ) ; val newPointer = new LongPointer ( length ) ; val perfD = PerformanceTracker . getInstance ( ) . helperStartTransaction ( ) ; Pointer . memcpy ( newPointer , intPointer , shapeBufferPointer . limit ( ) ) ; PerformanceTracker . getInstance ( ) . helperRegisterTransaction ( 0 , perfD , shapeBufferPointer . limit ( ) , MemcpyDirection . HOST_TO_HOST ) ; DataBuffer shapeBuffer = Nd4j . createBuffer ( newPointer , DataType . LONG , length , LongIndexer . create ( newPointer ) ) ; dataPointer . position ( 0 ) ; dataPointer . limit ( dataBufferElementSize * Shape . length ( shapeBuffer ) ) ; dataPointer . capacity ( dataBufferElementSize * Shape . length ( shapeBuffer ) ) ; if ( dataBufferElementSize == ( Float . SIZE / 8 ) ) { FloatPointer dPointer = new FloatPointer ( dataPointer . limit ( ) / dataBufferElementSize ) ; val perfX = PerformanceTracker . getInstance ( ) . helperStartTransaction ( ) ; Pointer . memcpy ( dPointer , dataPointer , dataPointer . limit ( ) ) ; PerformanceTracker . getInstance ( ) . helperRegisterTransaction ( 0 , perfX , dataPointer . limit ( ) , MemcpyDirection . HOST_TO_HOST ) ; data = Nd4j . createBuffer ( dPointer , DataType . FLOAT , Shape . length ( shapeBuffer ) , FloatIndexer . create ( dPointer ) ) ; } else if ( dataBufferElementSize == ( Double . SIZE / 8 ) ) { DoublePointer dPointer = new DoublePointer ( dataPointer . limit ( ) / dataBufferElementSize ) ; val perfX = PerformanceTracker . getInstance ( ) . helperStartTransaction ( ) ; Pointer . memcpy ( dPointer , dataPointer , dataPointer . limit ( ) ) ; PerformanceTracker . getInstance ( ) . helperRegisterTransaction ( 0 , perfX , dataPointer . limit ( ) , MemcpyDirection . HOST_TO_HOST ) ; data = Nd4j . createBuffer ( dPointer , DataType . DOUBLE , Shape . length ( shapeBuffer ) , DoubleIndexer . create ( dPointer ) ) ; } INDArray ret = Nd4j . create ( data , Shape . shape ( shapeBuffer ) , Shape . strideArr ( shapeBuffer ) , 0 , Shape . order ( shapeBuffer ) ) ; Nd4j . getAffinityManager ( ) . tagLocation ( ret , AffinityManager . Location . DEVICE ) ; return ret ; }
Create from an in memory numpy pointer . Note that this is heavily used in our python library jumpy .