signature:
public class StringUtils { /** * Matches a template string against a pattern , replaces matched tokens with
 * the supplied replacements , and returns the result . The regular expression
 * must use a capturing group . The value of the first capturing group is used
 * to look up the replacement . If no replacement is found for the token , then
 * it is replaced with the empty string .
 * For example , assume template is " % foo % _ % bar % _ % baz % " , pattern is " % ( . * ? ) % " ,
 * and replacements contains 2 entries , mapping " foo " to " zoo " and " baz " to
 * " zaz " . The result returned would be " zoo _ _ zaz " .
 * @ param template String template to receive replacements
 * @ param pattern Pattern to match for identifying tokens , must use a capturing
 * group
 * @ param replacements Map < String , String > mapping tokens identified by the
 * capturing group to their replacement values
 * @ return String template with replacements */
public static String replaceTokens ( String template , Pattern pattern , Map < String , String > replacements ) { } }

implementation: StringBuffer sb = new StringBuffer ( ) ; Matcher matcher = pattern . matcher ( template ) ; while ( matcher . find ( ) ) { String replacement = replacements . get ( matcher . group ( 1 ) ) ; if ( replacement == null ) { replacement = "" ; } matcher . appendReplacement ( sb , Matcher . quoteReplacement ( replacement ) ) ; } matcher . appendTail ( sb ) ; return sb . toString ( ) ;


signature:
public class StringUtils { /** * Get stack trace for a given thread . */
public static String getStackTrace ( Thread t ) { } }

implementation: final StackTraceElement [ ] stackTrace = t . getStackTrace ( ) ; StringBuilder str = new StringBuilder ( ) ; for ( StackTraceElement e : stackTrace ) { str . append ( e . toString ( ) + "\n" ) ; } return str . toString ( ) ;


signature:
public class StringUtils { /** * Compare strings locale - freely by using String # equalsIgnoreCase .
 * @ param s1 Non - null string to be converted
 * @ param s2 string to be converted
 * @ return the str , converted to uppercase . */
public static boolean equalsIgnoreCase ( String s1 , String s2 ) { } }

implementation:
Preconditions . checkNotNull ( s1 ) ; // don ' t check non - null against s2 to make the semantics same as
// s1 . equals ( s2)
return s1 . equalsIgnoreCase ( s2 ) ;


signature:
public class StringUtils { /** * < p > Checks if the String contains only unicode letters . < / p >
 * < p > < code > null < / code > will return < code > false < / code > .
 * An empty String ( length ( ) = 0 ) will return < code > true < / code > . < / p >
 * < pre >
 * StringUtils . isAlpha ( null ) = false
 * StringUtils . isAlpha ( " " ) = true
 * StringUtils . isAlpha ( " " ) = false
 * StringUtils . isAlpha ( " abc " ) = true
 * StringUtils . isAlpha ( " ab2c " ) = false
 * StringUtils . isAlpha ( " ab - c " ) = false
 * < / pre >
 * @ param str the String to check , may be null
 * @ return < code > true < / code > if only contains letters , and is non - null */
public static boolean isAlpha ( String str ) { } }

implementation: if ( str == null ) { return false ; } int sz = str . length ( ) ; for ( int i = 0 ; i < sz ; i ++ ) { if ( ! Character . isLetter ( str . charAt ( i ) ) ) { return false ; } } return true ;


signature:
public class InvertMatrix { /** * Inverts a matrix
 * @ param arr the array to invert
 * @ param inPlace Whether to store the result in { @ code arr }
 * @ return the inverted matrix */
public static INDArray invert ( INDArray arr , boolean inPlace ) { } }

implementation:
if ( ! arr . isSquare ( ) ) { throw new IllegalArgumentException ( "invalid array: must be square matrix" ) ; } // FIX ME : Please
/* int [ ] IPIV = new int [ arr . length ( ) + 1 ] ;
 int LWORK = arr . length ( ) * arr . length ( ) ;
 INDArray WORK = Nd4j . create ( new double [ LWORK ] ) ;
 INDArray inverse = inPlace ? arr : arr . dup ( ) ;
 Nd4j . getBlasWrapper ( ) . lapack ( ) . getrf ( arr ) ;
 Nd4j . getBlasWrapper ( ) . lapack ( ) . getri ( arr . size ( 0 ) , inverse , arr . size ( 0 ) , IPIV , WORK , LWORK , 0 ) ; */
RealMatrix rm = CheckUtil . convertToApacheMatrix ( arr ) ; RealMatrix rmInverse = new LUDecomposition ( rm ) . getSolver ( ) . getInverse ( ) ; INDArray inverse = CheckUtil . convertFromApacheMatrix ( rmInverse ) ; if ( inPlace ) arr . assign ( inverse ) ; return inverse ;


signature:
public class Factorial { /** * Compute the factorial of the non - negative integer .
 * @ param n the argument to the factorial , non - negative .
 * @ return the factorial of n . */
public BigInteger at ( int n ) { } }

implementation: while ( a . size ( ) <= n ) { final int lastn = a . size ( ) - 1 ; final BigInteger nextn = BigInteger . valueOf ( lastn + 1 ) ; a . add ( a . get ( lastn ) . multiply ( nextn ) ) ; } return a . get ( n ) ;


signature:
public class AllocationPoint { /** * This method returns , if host side has actual copy of data
 * @ return true , if data is actual , false otherwise */
public boolean isActualOnHostSide ( ) { } }

implementation:
// log . info ( " isActuialOnHostSide ( ) - > Host side : [ { } ] , Device side : [ { } ] " , accessHostRead . get ( ) , accessDeviceRead . get ( ) ) ;
boolean result = accessHostWrite . get ( ) >= accessDeviceWrite . get ( ) || accessHostRead . get ( ) >= accessDeviceWrite . get ( ) ; // log . info ( " isActuialOnHostSide ( ) - > { } , shape : { } " , result , shape ) ;
return result ;


signature:
public class AllocationPoint { /** * This method returns , if device side has actual copy of data
 * @ return */
public boolean isActualOnDeviceSide ( ) { } }

implementation:
// log . info ( " isActuialOnDeviceSide ( ) - > Host side : [ { } ] , Device side : [ { } ] " , accessHostWrite . get ( ) , accessDeviceWrite . get ( ) ) ;
boolean result = accessDeviceWrite . get ( ) >= accessHostWrite . get ( ) || accessDeviceRead . get ( ) >= accessHostWrite . get ( ) ; // accessHostWrite . get ( ) < = getDeviceAccessTime ( ) ;
// log . info ( " isActuialOnDeviceSide ( ) - > { } ( { } ) , Shape : { } " , result , objectId , shape ) ;
return result ;


signature:
public class BaseShapeInfoProvider { /** * This method creates shapeInformation buffer , based on shape being passed in
 * @ param shape
 * @ return */
@ Override public Pair < DataBuffer , long [ ] > createShapeInformation ( int [ ] shape ) { } }

implementation: char order = Nd4j . order ( ) ; return createShapeInformation ( shape , order ) ;


signature:
public class BaseShapeInfoProvider { /** * This method creates shapeInformation buffer , based on shape & order being passed in
 * @ param shape
 * @ param order
 * @ return */
@ Override public Pair < DataBuffer , long [ ] > createShapeInformation ( long [ ] shape , char order ) { } }

implementation:
long [ ] stride = Nd4j . getStrides ( shape , order ) ; // this won ' t be view , so ews is 1
int ews = 1 ; return createShapeInformation ( shape , stride , 0 , ews , order ) ;


signature:
public class Shape { /** * Returns whether the given shape is a vector
 * @ param shapeInfo the shapeinfo to test
 * @ return whether the given shape is a vector */
public static boolean isVector ( DataBuffer shapeInfo ) { } }

implementation: int rank = Shape . rank ( shapeInfo ) ; if ( rank > 2 || rank < 1 ) return false ; else { int len = Shape . length ( shapeInfo ) ; DataBuffer shape = Shape . shapeOf ( shapeInfo ) ; return shape . getInt ( 0 ) == len || shape . getInt ( 1 ) == len ; }


signature:
public class Shape { /** * A port of numpy ' s reshaping algorithm that leverages
 * no copy where possible and returns
 * null if the reshape
 * couldn ' t happen without copying
 * @ param arr the array to reshape
 * @ param newShape the new shape
 * @ param isFOrder whether the array will be fortran ordered or not
 * @ return null if a reshape isn ' t possible , or a new ndarray */
public static INDArray newShapeNoCopy ( INDArray arr , long [ ] newShape , boolean isFOrder ) { } }

implementation:
int oldnd ; long [ ] olddims = ArrayUtil . copy ( arr . shape ( ) ) ; long [ ] oldstrides = ArrayUtil . copy ( arr . stride ( ) ) ; long np , op , last_stride ; int oi , oj , ok , ni , nj , nk ; long [ ] newStrides = new long [ newShape . length ] ; oldnd = 0 ; /* * Remove axes with dimension 1 from the old array . They have no effect
 * but would need special cases since their strides do not matter . */
for ( oi = 0 ; oi < arr . rank ( ) ; oi ++ ) { if ( arr . size ( oi ) != 1 ) { olddims [ oldnd ] = arr . size ( oi ) ; oldstrides [ oldnd ] = arr . stride ( oi ) ; oldnd ++ ; } } np = 1 ; for ( ni = 0 ; ni < newShape . length ; ni ++ ) { np *= newShape [ ni ] ; } op = 1 ; for ( oi = 0 ; oi < oldnd ; oi ++ ) { op *= olddims [ oi ] ; } if ( np != op ) { /* different total sizes ; no hope */
return null ; } if ( np == 0 ) { /* the current code does not handle 0 - sized arrays , so give up */
return null ; } /* oi to oj and ni to nj give the axis ranges currently worked with */
oi = 0 ; oj = 1 ; ni = 0 ; nj = 1 ; while ( ni < newShape . length && oi < oldnd ) { np = newShape [ ni ] ; op = olddims [ oi ] ; while ( np != op ) { if ( np < op ) { /* Misses trailing 1s , these are handled later */
np *= newShape [ nj ++ ] ; } else { op *= olddims [ oj ++ ] ; } } /* Check whether the original axes can be combined */
for ( ok = oi ; ok < oj - 1 ; ok ++ ) { if ( isFOrder ) { if ( oldstrides [ ok + 1 ] != olddims [ ok ] * oldstrides [ ok ] ) { /* not contiguous enough */
return null ; } } else { /* C order */
if ( oldstrides [ ok ] != olddims [ ok + 1 ] * oldstrides [ ok + 1 ] ) { /* not contiguous enough */
return null ; } } } /* Calculate new strides for all axes currently worked with */
if ( isFOrder ) { newStrides [ ni ] = oldstrides [ oi ] ; for ( nk = ni + 1 ; nk < nj ; nk ++ ) { newStrides [ nk ] = newStrides [ nk - 1 ] * newShape [ nk - 1 ] ; } } else { /* C order */
newStrides [ nj - 1 ] = oldstrides [ oj - 1 ] ; for ( nk = nj - 1 ; nk > ni ; nk -- ) { newStrides [ nk - 1 ] = newStrides [ nk ] * newShape [ nk ] ; } } ni = nj ++ ; oi = oj ++ ; } /* * Set strides corresponding to trailing 1s of the new shape . */
if ( ni >= 1 ) { last_stride = newStrides [ ni - 1 ] ; } else { last_stride = arr . elementStride ( ) ; } if ( isFOrder && ni >= 1 ) { last_stride *= newShape [ ni - 1 ] ; } for ( nk = ni ; nk < newShape . length ; nk ++ ) { newStrides [ nk ] = last_stride ; } if ( arr instanceof IComplexNDArray ) // return Nd4j . createComplex ( arr . data ( ) , newShape , newStrides , arr . offset ( ) ) ;
throw new UnsupportedOperationException ( ) ; INDArray ret = Nd4j . create ( arr . data ( ) , newShape , newStrides , arr . offset ( ) , isFOrder ? 'f' : 'c' ) ; return ret ;


signature:
public class Shape { /** * Infer the order for the ndarray based on the
 * array ' s strides
 * @ param arr the array to get the
 * ordering for
 * @ return the ordering for the given array */
public static char getOrder ( INDArray arr ) { } }

implementation: return getOrder ( arr . shape ( ) , arr . stride ( ) , arr . elementStride ( ) ) ;


signature:
public class Shape { /** * Compute the offset for the given array
 * given the indices
 * @ param arr the array to compute the offset for
 * @ param indexes the indexes along each dimension to create the offset for
 * @ return the offset for the given array and indexes */
public static long offsetFor ( INDArray arr , int [ ] indexes ) { } }

implementation: ShapeOffsetResolution resolution = new ShapeOffsetResolution ( arr ) ; resolution . exec ( Shape . toIndexes ( indexes ) ) ; return resolution . getOffset ( ) ;


signature:
public class Shape { /** * Convert the given int indexes
 * to nd array indexes
 * @ param indices the indices to convert
 * @ return the converted indexes */
public static INDArrayIndex [ ] toIndexes ( int [ ] indices ) { } }

implementation: INDArrayIndex [ ] ret = new INDArrayIndex [ indices . length ] ; for ( int i = 0 ; i < ret . length ; i ++ ) ret [ i ] = new NDArrayIndex ( indices [ i ] ) ; return ret ;


signature:
public class Shape { /** * Get array shape from an int [ ]
 * @ param buffer Buffer to get the shape from
 * @ return Shape array */
public static int [ ] shape ( int [ ] buffer ) { } }

implementation: int [ ] ret = new int [ rank ( buffer ) ] ; for ( int i = 0 ; i < ret . length ; i ++ ) ret [ i ] = buffer [ 1 + i ] ; return ret ;


signature:
public class ArchiveUtils { /** * Extracts files to the specified destination
 * @ param file the file to extract to
 * @ param dest the destination directory
 * @ throws IOException */
public static void unzipFileTo ( String file , String dest ) throws IOException { } }

implementation:
File target = new File ( file ) ; if ( ! target . exists ( ) ) throw new IllegalArgumentException ( "Archive doesnt exist" ) ; FileInputStream fin = new FileInputStream ( target ) ; int BUFFER = 2048 ; byte data [ ] = new byte [ BUFFER ] ; if ( file . endsWith ( ".zip" ) || file . endsWith ( ".jar" ) ) { try ( ZipInputStream zis = new ZipInputStream ( fin ) ) { // get the zipped file list entry
ZipEntry ze = zis . getNextEntry ( ) ; while ( ze != null ) { String fileName = ze . getName ( ) ; File newFile = new File ( dest + File . separator + fileName ) ; if ( ze . isDirectory ( ) ) { newFile . mkdirs ( ) ; zis . closeEntry ( ) ; ze = zis . getNextEntry ( ) ; continue ; } FileOutputStream fos = new FileOutputStream ( newFile ) ; int len ; while ( ( len = zis . read ( data ) ) > 0 ) { fos . write ( data , 0 , len ) ; } fos . close ( ) ; ze = zis . getNextEntry ( ) ; log . debug ( "File extracted: " + newFile . getAbsoluteFile ( ) ) ; } zis . closeEntry ( ) ; } } else if ( file . endsWith ( ".tar.gz" ) || file . endsWith ( ".tgz" ) ) { BufferedInputStream in = new BufferedInputStream ( fin ) ; GzipCompressorInputStream gzIn = new GzipCompressorInputStream ( in ) ; TarArchiveInputStream tarIn = new TarArchiveInputStream ( gzIn ) ; TarArchiveEntry entry ; /* Read the tar entries using the getNextEntry method * */
while ( ( entry = ( TarArchiveEntry ) tarIn . getNextEntry ( ) ) != null ) { log . info ( "Extracting: " + entry . getName ( ) ) ; /* If the entry is a directory , create the directory . */
if ( entry . isDirectory ( ) ) { File f = new File ( dest + File . separator + entry . getName ( ) ) ; f . mkdirs ( ) ; } /* * If the entry is a file , write the decompressed file to the disk
 * and close destination stream . */
else { int count ; try ( FileOutputStream fos = new FileOutputStream ( dest + File . separator + entry . getName ( ) ) ; BufferedOutputStream destStream = new BufferedOutputStream ( fos , BUFFER ) ; ) { while ( ( count = tarIn . read ( data , 0 , BUFFER ) ) != - 1 ) { destStream . write ( data , 0 , count ) ; } destStream . flush ( ) ; IOUtils . closeQuietly ( destStream ) ; } } } // Close the input stream
tarIn . close ( ) ; } else if ( file . endsWith ( ".gz" ) ) { File extracted = new File ( target . getParent ( ) , target . getName ( ) . replace ( ".gz" , "" ) ) ; if ( extracted . exists ( ) ) extracted . delete ( ) ; extracted . createNewFile ( ) ; try ( GZIPInputStream is2 = new GZIPInputStream ( fin ) ; OutputStream fos = FileUtils . openOutputStream ( extracted ) ) { IOUtils . copyLarge ( is2 , fos ) ; fos . flush ( ) ; } } else { throw new IllegalStateException ( "Unable to infer file type (compression format) from source file name: " + file ) ; } target . delete ( ) ;


signature:
public class BaseDataBuffer { /** * Special method for
 * @ param i
 * @ return */
protected short getShort ( long i ) { } }

implementation: if ( dataType ( ) != Type . HALF ) throw new UnsupportedOperationException ( "getShort() is supported for Half-precision buffers only" ) ; return fromFloat ( ( ( HalfIndexer ) indexer ) . get ( offset ( ) + i ) ) ;


signature:
public class BaseDataBuffer { /** * Reallocate the native memory of the buffer
 * @ param length the new length of the buffer
 * @ return this databuffer */
@ Override public DataBuffer reallocate ( long length ) { } }

implementation:
Pointer oldPointer = pointer ; if ( isAttached ( ) ) { long capacity = length * getElementSize ( ) ; switch ( dataType ( ) ) { case DOUBLE : pointer = getParentWorkspace ( ) . alloc ( capacity , Type . DOUBLE , false ) . asDoublePointer ( ) ; indexer = DoubleIndexer . create ( ( DoublePointer ) pointer ) ; break ; case FLOAT : pointer = getParentWorkspace ( ) . alloc ( capacity , Type . FLOAT , false ) . asFloatPointer ( ) ; indexer = FloatIndexer . create ( ( FloatPointer ) pointer ) ; break ; case INT : pointer = getParentWorkspace ( ) . alloc ( capacity , Type . INT , false ) . asIntPointer ( ) ; indexer = IntIndexer . create ( ( IntPointer ) pointer ) ; break ; } workspaceGenerationId = getParentWorkspace ( ) . getGenerationId ( ) ; } else { switch ( dataType ( ) ) { case INT : pointer = new IntPointer ( length ) ; indexer = IntIndexer . create ( ( IntPointer ) pointer ) ; break ; case DOUBLE : pointer = new DoublePointer ( length ) ; indexer = DoubleIndexer . create ( ( DoublePointer ) pointer ) ; break ; case FLOAT : pointer = new FloatPointer ( length ) ; indexer = FloatIndexer . create ( ( FloatPointer ) pointer ) ; break ; } } Pointer . memcpy ( pointer , oldPointer , this . length ( ) * getElementSize ( ) ) ; // this . underlyingLength = length ;
return this ;


signature:
public class CudaExecutioner { /** * This method executes given CustomOp
 * PLEASE NOTE : You ' re responsible for input / output validation
 * PLEASE NOTE : right now this operations are executing on CPU
 * @ param op */
public void exec ( CustomOp op ) { } }

implementation:
Nd4j . getExecutioner ( ) . commit ( ) ; if ( op . opName ( ) . equalsIgnoreCase ( "im2col" ) ) { val dtype = Nd4j . dataType ( ) ; val xArr = op . inputArguments ( ) [ 0 ] ; val zArr = op . outputArguments ( ) [ 0 ] ; CudaContext context = AtomicAllocator . getInstance ( ) . getFlowController ( ) . prepareAction ( zArr , xArr ) ; if ( extraz . get ( ) == null ) extraz . set ( new PointerPointer ( 32 ) ) ; PointerPointer xShapeHost = extraz . get ( ) . put ( AddressRetriever . retrieveHostPointer ( xArr . shapeInfoDataBuffer ( ) ) , context . getOldStream ( ) , AtomicAllocator . getInstance ( ) . getDeviceIdPointer ( ) , context . getBufferAllocation ( ) , context . getBufferReduction ( ) , context . getBufferScalar ( ) , context . getBufferSpecial ( ) , null , AddressRetriever . retrieveHostPointer ( zArr . shapeInfoDataBuffer ( ) ) ) ; val x = AtomicAllocator . getInstance ( ) . getPointer ( xArr , context ) ; val z = AtomicAllocator . getInstance ( ) . getPointer ( zArr , context ) ; val xShape = AtomicAllocator . getInstance ( ) . getPointer ( xArr . shapeInfoDataBuffer ( ) , context ) ; val zShape = AtomicAllocator . getInstance ( ) . getPointer ( zArr . shapeInfoDataBuffer ( ) , context ) ; double zeroPad = 0.0 ; if ( op . tArgs ( ) != null && op . tArgs ( ) . length > 0 ) { zeroPad = op . tArgs ( ) [ 0 ] ; } val extrass = new double [ ] { op . iArgs ( ) [ 0 ] , op . iArgs ( ) [ 1 ] , op . iArgs ( ) [ 2 ] , op . iArgs ( ) [ 3 ] , op . iArgs ( ) [ 4 ] , op . iArgs ( ) [ 5 ] , op . iArgs ( ) [ 6 ] , op . iArgs ( ) [ 7 ] , op . iArgs ( ) [ 8 ] , zeroPad } ; val extraArgsBuff = Nd4j . getConstantHandler ( ) . getConstantBuffer ( extrass ) ; val extraArgs = AtomicAllocator . getInstance ( ) . getPointer ( extraArgsBuff , context ) ; if ( dtype == DataBuffer . Type . DOUBLE ) { nativeOps . execTransformDouble ( xShapeHost , 37 , ( DoublePointer ) x , ( LongPointer ) xShape , ( DoublePointer ) z , ( LongPointer ) zShape , ( DoublePointer ) extraArgs ) ; } else if ( dtype == DataBuffer . Type . FLOAT ) { nativeOps . execTransformFloat ( xShapeHost , 37 , ( FloatPointer ) x , ( LongPointer ) xShape , ( FloatPointer ) z , ( LongPointer ) zShape , ( FloatPointer ) extraArgs ) ; } else if ( dtype == DataBuffer . Type . HALF ) { nativeOps . execTransformHalf ( xShapeHost , 37 , ( ShortPointer ) x , ( LongPointer ) xShape , ( ShortPointer ) z , ( LongPointer ) zShape , ( ShortPointer ) extraArgs ) ; } // AtomicAllocator . getInstance ( ) . getAllocationPoint ( zArr ) . tickDeviceWrite ( ) ;
AtomicAllocator . getInstance ( ) . getFlowController ( ) . registerAction ( context , zArr , xArr ) ; // Nd4j . getExecutioner ( ) . commit ( ) ;
return ; } else if ( op . opName ( ) . equalsIgnoreCase ( "col2im" ) ) { val dtype = Nd4j . dataType ( ) ; val xArr = op . inputArguments ( ) [ 0 ] ; val zArr = op . outputArguments ( ) [ 0 ] ; CudaContext context = AtomicAllocator . getInstance ( ) . getFlowController ( ) . prepareAction ( zArr , xArr ) ; if ( extraz . get ( ) == null ) extraz . set ( new PointerPointer ( 32 ) ) ; PointerPointer xShapeHost = extraz . get ( ) . put ( AddressRetriever . retrieveHostPointer ( xArr . shapeInfoDataBuffer ( ) ) , context . getOldStream ( ) , AtomicAllocator . getInstance ( ) . getDeviceIdPointer ( ) , context . getBufferAllocation ( ) , context . getBufferReduction ( ) , context . getBufferScalar ( ) , context . getBufferSpecial ( ) , null , AddressRetriever . retrieveHostPointer ( zArr . shapeInfoDataBuffer ( ) ) ) ; val x = AtomicAllocator . getInstance ( ) . getPointer ( xArr , context ) ; val z = AtomicAllocator . getInstance ( ) . getPointer ( zArr , context ) ; val xShape = AtomicAllocator . getInstance ( ) . getPointer ( xArr . shapeInfoDataBuffer ( ) , context ) ; val zShape = AtomicAllocator . getInstance ( ) . getPointer ( zArr . shapeInfoDataBuffer ( ) , context ) ; val extrass = new double [ ] { op . iArgs ( ) [ 0 ] , op . iArgs ( ) [ 1 ] , op . iArgs ( ) [ 2 ] , op . iArgs ( ) [ 3 ] , op . iArgs ( ) [ 4 ] , op . iArgs ( ) [ 5 ] , op . iArgs ( ) [ 6 ] , op . iArgs ( ) [ 7 ] } ; val extraArgsBuff = Nd4j . getConstantHandler ( ) . getConstantBuffer ( extrass ) ; val extraArgs = AtomicAllocator . getInstance ( ) . getPointer ( extraArgsBuff , context ) ; if ( dtype == DataBuffer . Type . DOUBLE ) { nativeOps . execTransformDouble ( xShapeHost , 36 , ( DoublePointer ) x , ( LongPointer ) xShape , ( DoublePointer ) z , ( LongPointer ) zShape , ( DoublePointer ) extraArgs ) ; } else if ( dtype == DataBuffer . Type . FLOAT ) { nativeOps . execTransformFloat ( xShapeHost , 36 , ( FloatPointer ) x , ( LongPointer ) xShape , ( FloatPointer ) z , ( LongPointer ) zShape , ( FloatPointer ) extraArgs ) ; } else if ( dtype == DataBuffer . Type . HALF ) { nativeOps . execTransformHalf ( xShapeHost , 36 , ( ShortPointer ) x , ( LongPointer ) xShape , ( ShortPointer ) z , ( LongPointer ) zShape , ( ShortPointer ) extraArgs ) ; } // AtomicAllocator . getInstance ( ) . getAllocationPoint ( zArr ) . tickDeviceWrite ( ) ;
AtomicAllocator . getInstance ( ) . getFlowController ( ) . registerAction ( context , zArr , xArr ) ; // Nd4j . getExecutioner ( ) . commit ( ) ;
return ; } else if ( op . opName ( ) . equalsIgnoreCase ( "pooling2d" ) ) { val dtype = Nd4j . dataType ( ) ; val xArr = op . inputArguments ( ) [ 0 ] ; val zArr = op . outputArguments ( ) [ 0 ] ; CudaContext context = AtomicAllocator . getInstance ( ) . getFlowController ( ) . prepareAction ( zArr , xArr ) ; if ( extraz . get ( ) == null ) extraz . set ( new PointerPointer ( 32 ) ) ; PointerPointer xShapeHost = extraz . get ( ) . put ( AddressRetriever . retrieveHostPointer ( xArr . shapeInfoDataBuffer ( ) ) , context . getOldStream ( ) , AtomicAllocator . getInstance ( ) . getDeviceIdPointer ( ) , context . getBufferAllocation ( ) , context . getBufferReduction ( ) , context . getBufferScalar ( ) , context . getBufferSpecial ( ) , null , AddressRetriever . retrieveHostPointer ( zArr . shapeInfoDataBuffer ( ) ) ) ; val x = AtomicAllocator . getInstance ( ) . getPointer ( xArr , context ) ; val z = AtomicAllocator . getInstance ( ) . getPointer ( zArr , context ) ; val xShape = AtomicAllocator . getInstance ( ) . getPointer ( xArr . shapeInfoDataBuffer ( ) , context ) ; val zShape = AtomicAllocator . getInstance ( ) . getPointer ( zArr . shapeInfoDataBuffer ( ) , context ) ; val extrass = new double [ ] { op . iArgs ( ) [ 0 ] , op . iArgs ( ) [ 1 ] , op . iArgs ( ) [ 2 ] , op . iArgs ( ) [ 3 ] , op . iArgs ( ) [ 4 ] , op . iArgs ( ) [ 5 ] , op . iArgs ( ) [ 6 ] , op . iArgs ( ) [ 7 ] , op . iArgs ( ) [ 8 ] } ; val extraArgsBuff = Nd4j . getConstantHandler ( ) . getConstantBuffer ( extrass ) ; val extraArgs = AtomicAllocator . getInstance ( ) . getPointer ( extraArgsBuff , context ) ; if ( dtype == DataBuffer . Type . DOUBLE ) { nativeOps . execTransformDouble ( xShapeHost , 71 , ( DoublePointer ) x , ( LongPointer ) xShape , ( DoublePointer ) z , ( LongPointer ) zShape , ( DoublePointer ) extraArgs ) ; } else if ( dtype == DataBuffer . Type . FLOAT ) { nativeOps . execTransformFloat ( xShapeHost , 71 , ( FloatPointer ) x , ( LongPointer ) xShape , ( FloatPointer ) z , ( LongPointer ) zShape , ( FloatPointer ) extraArgs ) ; } else if ( dtype == DataBuffer . Type . HALF ) { nativeOps . execTransformHalf ( xShapeHost , 71 , ( ShortPointer ) x , ( LongPointer ) xShape , ( ShortPointer ) z , ( LongPointer ) zShape , ( ShortPointer ) extraArgs ) ; } // AtomicAllocator . getInstance ( ) . getAllocationPoint ( zArr ) . tickDeviceWrite ( ) ;
AtomicAllocator . getInstance ( ) . getFlowController ( ) . registerAction ( context , zArr , xArr ) ; // Nd4j . getExecutioner ( ) . commit ( ) ;
return ; } Nd4j . getExecutioner ( ) . commit ( ) ; long st = profilingHookIn ( op ) ; CudaContext context = ( CudaContext ) AtomicAllocator . getInstance ( ) . getDeviceContext ( ) . getContext ( ) ; // AtomicAllocator . getInstance ( ) . getFlowController ( ) . prepareActionAllWrite ( op . outputArguments ( ) ) ;
if ( extraz . get ( ) == null ) extraz . set ( new PointerPointer ( 32 ) ) ; PointerPointer extras = extraz . get ( ) . put ( new CudaPointer ( 1 ) , context . getOldStream ( ) , context . getBufferScalar ( ) , context . getBufferReduction ( ) ) ; val outputArgs = op . outputArguments ( ) ; val inputArgs = op . inputArguments ( ) ; if ( outputArgs . length == 0 && ! op . isInplaceCall ( ) ) throw new ND4JIllegalStateException ( "You can't execute non-inplace CustomOp without outputs being specified" ) ; val lc = op . opName ( ) . toLowerCase ( ) ; val hash = op . opHash ( ) ; val inputShapes = new PointerPointer < > ( inputArgs . length * 2 ) ; val inputBuffers = new PointerPointer < > ( inputArgs . length * 2 ) ; int cnt = 0 ; for ( val in : inputArgs ) { val hp = AtomicAllocator . getInstance ( ) . getHostPointer ( in . shapeInfoDataBuffer ( ) ) ; inputBuffers . put ( cnt , AtomicAllocator . getInstance ( ) . getHostPointer ( in ) ) ; inputShapes . put ( cnt , hp ) ; val dp = AtomicAllocator . getInstance ( ) . getPointer ( in . shapeInfoDataBuffer ( ) , context ) ; inputBuffers . put ( cnt + inputArgs . length , AtomicAllocator . getInstance ( ) . getPointer ( in , context ) ) ; inputShapes . put ( cnt + inputArgs . length , dp ) ; if ( op . isInplaceCall ( ) ) AtomicAllocator . getInstance ( ) . getAllocationPoint ( in ) . tickHostWrite ( ) ; cnt ++ ; } val outputShapes = new PointerPointer < > ( outputArgs . length * 2 ) ; val outputBuffers = new PointerPointer < > ( outputArgs . length * 2 ) ; cnt = 0 ; for ( val out : outputArgs ) { outputBuffers . put ( cnt , AtomicAllocator . getInstance ( ) . getHostPointer ( out ) ) ; outputShapes . put ( cnt , AtomicAllocator . getInstance ( ) . getHostPointer ( out . shapeInfoDataBuffer ( ) ) ) ; outputBuffers . put ( cnt + outputArgs . length , AtomicAllocator . getInstance ( ) . getPointer ( out , context ) ) ; outputShapes . put ( cnt + outputArgs . length , AtomicAllocator . getInstance ( ) . getPointer ( out . shapeInfoDataBuffer ( ) , context ) ) ; AtomicAllocator . getInstance ( ) . getAllocationPoint ( out ) . tickHostWrite ( ) ; cnt ++ ; } if ( Nd4j . dataType ( ) == DataBuffer . Type . FLOAT ) { val tArgs = op . tArgs ( ) . length > 0 ? new FloatPointer ( op . tArgs ( ) . length ) : null ; val iArgs = op . iArgs ( ) . length > 0 ? new LongPointer ( op . iArgs ( ) . length ) : null ; cnt = 0 ; for ( val t : op . tArgs ( ) ) tArgs . put ( cnt ++ , ( float ) t ) ; cnt = 0 ; for ( val i : op . iArgs ( ) ) iArgs . put ( cnt ++ , i ) ; val status = OpStatus . byNumber ( nativeOps . execCustomOpFloat ( extras , hash , inputBuffers , inputShapes , inputArgs . length , outputBuffers , outputShapes , outputArgs . length , tArgs , op . tArgs ( ) . length , iArgs , op . iArgs ( ) . length , op . isInplaceCall ( ) ) ) ; if ( status != OpStatus . ND4J_STATUS_OK ) throw new ND4JIllegalStateException ( "Op execution failed: " + status ) ; } else if ( Nd4j . dataType ( ) == DataBuffer . Type . DOUBLE ) { val tArgs = op . tArgs ( ) . length > 0 ? new DoublePointer ( op . tArgs ( ) . length ) : null ; val iArgs = op . iArgs ( ) . length > 0 ? new LongPointer ( op . iArgs ( ) . length ) : null ; cnt = 0 ; for ( val t : op . tArgs ( ) ) tArgs . put ( cnt ++ , t ) ; for ( val i : op . iArgs ( ) ) iArgs . put ( cnt ++ , i ) ; val status = OpStatus . byNumber ( nativeOps . execCustomOpDouble ( extras , hash , inputBuffers , inputShapes , inputArgs . length , outputBuffers , outputShapes , outputArgs . length , tArgs , op . tArgs ( ) . length , iArgs , op . iArgs ( ) . length , op . isInplaceCall ( ) ) ) ; if ( status != OpStatus . ND4J_STATUS_OK ) throw new ND4JIllegalStateException ( "Op execution failed: " + status ) ; } else if ( Nd4j . dataType ( ) == DataBuffer . Type . HALF ) { val tArgs = op . tArgs ( ) . length > 0 ? new ShortPointer ( op . tArgs ( ) . length ) : null ; val iArgs = op . iArgs ( ) . length > 0 ? new LongPointer ( op . iArgs ( ) . length ) : null ; cnt = 0 ; for ( val t : op . tArgs ( ) ) tArgs . put ( cnt ++ , ArrayUtil . toHalf ( ( float ) t ) ) ; cnt = 0 ; for ( val i : op . iArgs ( ) ) iArgs . put ( cnt ++ , i ) ; val status = OpStatus . byNumber ( nativeOps . execCustomOpHalf ( extras , hash , inputBuffers , inputShapes , inputArgs . length , outputBuffers , outputShapes , outputArgs . length , tArgs , op . tArgs ( ) . length , iArgs , op . iArgs ( ) . length , op . isInplaceCall ( ) ) ) ; if ( status != OpStatus . ND4J_STATUS_OK ) throw new ND4JIllegalStateException ( "Op execution failed: " + status ) ; } // AtomicAllocator . getInstance ( ) . getFlowController ( ) . prepareActionAllWrite ( op . outputArguments ( ) ) ;
profilingHookOut ( op , st ) ;


signature:
public class NioUtil { /** * Copy from the given from buffer
 * to the to buffer at the specified
 * offsets and strides
 * @ param n
 * @ param bufferType
 * @ param from the origin buffer
 * @ param fromOffset the starting offset
 * @ param fromStride the stride at which to copy from the origin
 * @ param to the destination buffer
 * @ param toOffset the starting point
 * @ param toStride the to stride */
public static void copyAtStride ( int n , BufferType bufferType , ByteBuffer from , int fromOffset , int fromStride , ByteBuffer to , int toOffset , int toStride ) { } }

implementation:
// TODO : implement shape copy for cases where stride = = 1
ByteBuffer fromView = from ; ByteBuffer toView = to ; fromView . order ( ByteOrder . nativeOrder ( ) ) ; toView . order ( ByteOrder . nativeOrder ( ) ) ; switch ( bufferType ) { case INT : IntBuffer fromInt = fromView . asIntBuffer ( ) ; IntBuffer toInt = toView . asIntBuffer ( ) ; for ( int i = 0 ; i < n ; i ++ ) { int put = fromInt . get ( fromOffset + i * fromStride ) ; toInt . put ( toOffset + i * toStride , put ) ; } break ; case FLOAT : FloatBuffer fromFloat = fromView . asFloatBuffer ( ) ; FloatBuffer toFloat = toView . asFloatBuffer ( ) ; for ( int i = 0 ; i < n ; i ++ ) { float put = fromFloat . get ( fromOffset + i * fromStride ) ; toFloat . put ( toOffset + i * toStride , put ) ; } break ; case DOUBLE : DoubleBuffer fromDouble = fromView . asDoubleBuffer ( ) ; DoubleBuffer toDouble = toView . asDoubleBuffer ( ) ; for ( int i = 0 ; i < n ; i ++ ) { toDouble . put ( toOffset + i * toStride , fromDouble . get ( fromOffset + i * fromStride ) ) ; } break ; default : throw new IllegalArgumentException ( "Only floats and double supported" ) ; }


signature:
public class StatusServer { /** * Start a server based on the given subscriber .
 * Note that for the port to start the server on , you should
 * set the statusServerPortField on the subscriber
 * either manually or via command line . The
 * server defaults to port 9000.
 * The end points are :
 * / opType : returns the opType information ( master / slave )
 * / started : if it ' s a master node , it returns master : started / stopped and responder : started / stopped
 * / connectioninfo : See the SlaveConnectionInfo and MasterConnectionInfo classes for fields .
 * / ids : the list of ids for all of the subscribers
 * @ param statusStorage the subscriber to base
 * the status server on
 * @ return the started server */
public static Server startServer ( StatusStorage statusStorage , int statusServerPort ) { } }

implementation:
log . info ( "Starting server on port " + statusServerPort ) ; RoutingDsl dsl = new RoutingDsl ( ) ; dsl . GET ( "/ids/" ) . routeTo ( new F . Function0 < Result > ( ) { @ Override public Result apply ( ) throws Throwable { List < Integer > ids = statusStorage . ids ( ) ; return ok ( toJson ( ids ) ) ; } } ) ; dsl . GET ( "/state/:id" ) . routeTo ( new F . Function < String , Result > ( ) { @ Override public Result apply ( String id ) throws Throwable { return ok ( toJson ( statusStorage . getState ( Integer . parseInt ( id ) ) ) ) ; } } ) ; dsl . GET ( "/opType/:id" ) . routeTo ( new F . Function < String , Result > ( ) { @ Override public Result apply ( String id ) throws Throwable { return ok ( toJson ( ServerTypeJson . builder ( ) . type ( statusStorage . getState ( Integer . parseInt ( id ) ) . serverType ( ) ) ) ) ; } } ) ; dsl . GET ( "/started/:id" ) . routeTo ( new F . Function < String , Result > ( ) { @ Override public Result apply ( String id ) throws Throwable { return statusStorage . getState ( Integer . parseInt ( id ) ) . isMaster ( ) ? ok ( toJson ( MasterStatus . builder ( ) . master ( statusStorage . getState ( Integer . parseInt ( id ) ) . getServerState ( ) ) // note here that a responder is is + 1
. responder ( statusStorage . getState ( Integer . parseInt ( id ) + 1 ) . getServerState ( ) ) . responderN ( statusStorage . getState ( Integer . parseInt ( id ) ) . getTotalUpdates ( ) ) . build ( ) ) ) : ok ( toJson ( SlaveStatus . builder ( ) . slave ( statusStorage . getState ( Integer . parseInt ( id ) ) . serverType ( ) ) . build ( ) ) ) ; } } ) ; dsl . GET ( "/connectioninfo/:id" ) . routeTo ( new F . Function < String , Result > ( ) { @ Override public Result apply ( String id ) throws Throwable { return ok ( toJson ( statusStorage . getState ( Integer . parseInt ( id ) ) . getConnectionInfo ( ) ) ) ; } } ) ; dsl . POST ( "/updatestatus/:id" ) . routeTo ( new F . Function < String , Result > ( ) { @ Override public Result apply ( String id ) throws Throwable { SubscriberState subscriberState = Json . fromJson ( request ( ) . body ( ) . asJson ( ) , SubscriberState . class ) ; statusStorage . updateState ( subscriberState ) ; return ok ( toJson ( subscriberState ) ) ; } } ) ; Server server = Server . forRouter ( dsl . build ( ) , Mode . PROD , statusServerPort ) ; return server ;


signature:
public class ProtectedCudaConstantHandler { /** * This method moves specified dataBuffer to CUDA constant memory space .
 * PLEASE NOTE : CUDA constant memory is limited to 48KB per device .
 * @ param dataBuffer
 * @ return */
@ Override public synchronized long moveToConstantSpace ( DataBuffer dataBuffer ) { } }

implementation:
// now , we move things to constant memory
Integer deviceId = AtomicAllocator . getInstance ( ) . getDeviceId ( ) ; ensureMaps ( deviceId ) ; AllocationPoint point = AtomicAllocator . getInstance ( ) . getAllocationPoint ( dataBuffer ) ; long requiredMemoryBytes = AllocationUtils . getRequiredMemory ( point . getShape ( ) ) ; // logger . info ( " shape : " + point . getShape ( ) ) ;
// and release device memory : )
long currentOffset = constantOffsets . get ( deviceId ) . get ( ) ; CudaContext context = ( CudaContext ) AtomicAllocator . getInstance ( ) . getDeviceContext ( ) . getContext ( ) ; if ( currentOffset + requiredMemoryBytes >= MAX_CONSTANT_LENGTH || requiredMemoryBytes > MAX_BUFFER_LENGTH ) { if ( point . getAllocationStatus ( ) == AllocationStatus . HOST && CudaEnvironment . getInstance ( ) . getConfiguration ( ) . getMemoryModel ( ) == Configuration . MemoryModel . DELAYED ) { AtomicAllocator . getInstance ( ) . getMemoryHandler ( ) . alloc ( AllocationStatus . DEVICE , point , point . getShape ( ) , false ) ; } val profD = PerformanceTracker . getInstance ( ) . helperStartTransaction ( ) ; if ( NativeOpsHolder . getInstance ( ) . getDeviceNativeOps ( ) . memcpyAsync ( point . getPointers ( ) . getDevicePointer ( ) , point . getPointers ( ) . getHostPointer ( ) , requiredMemoryBytes , 1 , context . getSpecialStream ( ) ) == 0 ) { throw new ND4JIllegalStateException ( "memcpyAsync failed" ) ; } flowController . commitTransfer ( context . getSpecialStream ( ) ) ; PerformanceTracker . getInstance ( ) . helperRegisterTransaction ( point . getDeviceId ( ) , profD , point . getNumberOfBytes ( ) , MemcpyDirection . HOST_TO_DEVICE ) ; point . setConstant ( true ) ; point . tickDeviceWrite ( ) ; point . tickHostRead ( ) ; point . setDeviceId ( deviceId ) ; protector . persistDataBuffer ( dataBuffer ) ; return 0 ; } long bytes = requiredMemoryBytes ; // hack for misalignment avoidance for 16bit data opType
if ( dataBuffer . dataType ( ) == DataBuffer . Type . HALF ) { if ( bytes % 4 != 0 ) { bytes += 2 ; } } else if ( Nd4j . dataType ( ) == DataBuffer . Type . DOUBLE || dataBuffer . dataType ( ) == DataBuffer . Type . LONG ) { // for double data opType , we must be assured , that all DOUBLE pointers are starting from even addresses , to avoid banks spills
long div = bytes / 4 ; if ( div % 2 != 0 ) bytes += 4 ; // for possible changes of dtype in the same jvm , we skip few bytes in constant memory
div = currentOffset / 4 ; while ( div % 2 != 0 ) { currentOffset = constantOffsets . get ( deviceId ) . addAndGet ( 4 ) ; div = currentOffset / 4 ; // just break out , if we ' re stepped beyond constant memory space
if ( currentOffset > MAX_CONSTANT_LENGTH ) break ; } } currentOffset = constantOffsets . get ( deviceId ) . getAndAdd ( bytes ) ; if ( currentOffset >= MAX_CONSTANT_LENGTH ) { if ( point . getAllocationStatus ( ) == AllocationStatus . HOST && CudaEnvironment . getInstance ( ) . getConfiguration ( ) . getMemoryModel ( ) == Configuration . MemoryModel . DELAYED ) { AtomicAllocator . getInstance ( ) . getMemoryHandler ( ) . alloc ( AllocationStatus . DEVICE , point , point . getShape ( ) , false ) ; } val profD = PerformanceTracker . getInstance ( ) . helperStartTransaction ( ) ; if ( NativeOpsHolder . getInstance ( ) . getDeviceNativeOps ( ) . memcpyAsync ( point . getPointers ( ) . getDevicePointer ( ) , point . getPointers ( ) . getHostPointer ( ) , requiredMemoryBytes , 1 , context . getSpecialStream ( ) ) == 0 ) { throw new ND4JIllegalStateException ( "memcpyAsync failed" ) ; } flowController . commitTransfer ( context . getSpecialStream ( ) ) ; PerformanceTracker . getInstance ( ) . helperRegisterTransaction ( point . getDeviceId ( ) , profD , point . getNumberOfBytes ( ) , MemcpyDirection . HOST_TO_DEVICE ) ; point . setConstant ( true ) ; point . tickDeviceWrite ( ) ; point . tickHostRead ( ) ; point . setDeviceId ( deviceId ) ; protector . persistDataBuffer ( dataBuffer ) ; return 0 ; } NativeOpsHolder . getInstance ( ) . getDeviceNativeOps ( ) . memcpyConstantAsync ( currentOffset , point . getPointers ( ) . getHostPointer ( ) , requiredMemoryBytes , 1 , context . getSpecialStream ( ) ) ; flowController . commitTransfer ( context . getSpecialStream ( ) ) ; long cAddr = deviceAddresses . get ( deviceId ) . address ( ) + currentOffset ; // if ( resetHappened )
// logger . info ( " copying to constant : { } , bufferLength : { } , bufferDtype : { } , currentOffset : { } , currentAddres : { } " , requiredMemoryBytes , dataBuffer . length ( ) , dataBuffer . dataType ( ) , currentOffset , cAddr ) ;
point . setAllocationStatus ( AllocationStatus . CONSTANT ) ; point . getPointers ( ) . setDevicePointer ( new CudaPointer ( cAddr ) ) ; point . setConstant ( true ) ; point . tickDeviceWrite ( ) ; point . setDeviceId ( deviceId ) ; point . tickHostRead ( ) ; protector . persistDataBuffer ( dataBuffer ) ; return cAddr ;


signature:
public class ProtectedCudaConstantHandler { /** * PLEASE NOTE : This method implementation is hardware - dependant .
 * PLEASE NOTE : This method does NOT allow concurrent use of any array
 * @ param dataBuffer
 * @ return */
@ Override public DataBuffer relocateConstantSpace ( DataBuffer dataBuffer ) { } }

implementation:
// we always assume that data is sync , and valid on host side
Integer deviceId = AtomicAllocator . getInstance ( ) . getDeviceId ( ) ; ensureMaps ( deviceId ) ; if ( dataBuffer instanceof CudaIntDataBuffer ) { int [ ] data = dataBuffer . asInt ( ) ; return getConstantBuffer ( data ) ; } else if ( dataBuffer instanceof CudaFloatDataBuffer ) { float [ ] data = dataBuffer . asFloat ( ) ; return getConstantBuffer ( data ) ; } else if ( dataBuffer instanceof CudaDoubleDataBuffer ) { double [ ] data = dataBuffer . asDouble ( ) ; return getConstantBuffer ( data ) ; } else if ( dataBuffer instanceof CudaHalfDataBuffer ) { float [ ] data = dataBuffer . asFloat ( ) ; return getConstantBuffer ( data ) ; } else if ( dataBuffer instanceof CudaLongDataBuffer ) { long [ ] data = dataBuffer . asLong ( ) ; return getConstantBuffer ( data ) ; } throw new IllegalStateException ( "Unknown CudaDataBuffer opType" ) ;


signature:
public class ProtectedCudaConstantHandler { /** * This method returns DataBuffer with contant equal to input array .
 * PLEASE NOTE : This method assumes that you ' ll never ever change values within result DataBuffer
 * @ param array
 * @ return */
@ Override public DataBuffer getConstantBuffer ( float [ ] array ) { } }

implementation:
// logger . info ( " getConstantBuffer ( float [ ] ) called " ) ;
ArrayDescriptor descriptor = new ArrayDescriptor ( array ) ; Integer deviceId = AtomicAllocator . getInstance ( ) . getDeviceId ( ) ; ensureMaps ( deviceId ) ; if ( ! buffersCache . get ( deviceId ) . containsKey ( descriptor ) ) { // we create new databuffer
// logger . info ( " Creating new constant buffer . . . " ) ;
DataBuffer buffer = Nd4j . createBufferDetached ( array ) ; if ( constantOffsets . get ( deviceId ) . get ( ) + ( array . length * Nd4j . sizeOfDataType ( ) ) < MAX_CONSTANT_LENGTH ) { buffer . setConstant ( true ) ; // now we move data to constant memory , and keep happy
moveToConstantSpace ( buffer ) ; buffersCache . get ( deviceId ) . put ( descriptor , buffer ) ; bytes . addAndGet ( array . length * Nd4j . sizeOfDataType ( ) ) ; } return buffer ; } // else logger . info ( " Reusing constant buffer . . . " ) ;
return buffersCache . get ( deviceId ) . get ( descriptor ) ;


signature:
public class KafkaConnectionInformation { /** * Returns a kafka connection uri
 * @ return a kafka connection uri
 * represented by this connection information */
public String kafkaUri ( ) { } }

implementation: return String . format ( "kafka://%s?topic=%s&groupId=%s&zookeeperHost=%s&zookeeperPort=%d&serializerClass=%s&keySerializerClass=%s" , kafkaBrokerList , topicName , groupId , zookeeperHost , zookeeperPort , StringEncoder . class . getName ( ) , StringEncoder . class . getName ( ) ) ;


signature:
public class Transforms { /** * Cosine similarity
 * @ param d1 the first vector
 * @ param d2 the second vector
 * @ return the cosine similarities between the 2 arrays */
public static double cosineSim ( @ NonNull INDArray d1 , @ NonNull INDArray d2 ) { } }

implementation: return Nd4j . getExecutioner ( ) . execAndReturn ( new CosineSimilarity ( d1 , d2 , d1 . length ( ) ) ) . getFinalResult ( ) . doubleValue ( ) ;


signature:
public class Transforms { /** * Atan2 operation , new INDArray instance will be returned
 * Note the order of x and y parameters is opposite to that of java . lang . Math . atan2
 * @ param x the abscissa coordinate
 * @ param y the ordinate coordinate
 * @ return the theta from point ( r , theta ) when converting ( x , y ) from to cartesian to polar coordinates */
public static INDArray atan2 ( @ NonNull INDArray x , @ NonNull INDArray y ) { } }

implementation: return Nd4j . getExecutioner ( ) . execAndReturn ( new OldAtan2Op ( x , y , Nd4j . createUninitialized ( x . shape ( ) , x . ordering ( ) ) ) ) ;


signature:
public class Transforms { /** * Ceiling function
 * @ param ndArray
 * @ param copyOnOps
 * @ return */
public static INDArray ceiling ( INDArray ndArray , boolean copyOnOps ) { } }

implementation: return exec ( copyOnOps ? new Ceil ( ndArray , ndArray . dup ( ) ) : new Ceil ( ndArray , ndArray ) ) ;


signature:
public class Transforms { /** * Sin function
 * @ param in
 * @ param copy
 * @ return */
public static INDArray sin ( INDArray in , boolean copy ) { } }

implementation: return Nd4j . getExecutioner ( ) . execAndReturn ( new Sin ( ( copy ? in . dup ( ) : in ) ) ) ;


signature:
public class Transforms { /** * Sin function
 * @ param in
 * @ param copy
 * @ return */
public static INDArray atanh ( INDArray in , boolean copy ) { } }

implementation: return Nd4j . getExecutioner ( ) . execAndReturn ( new ATanh ( ( copy ? in . dup ( ) : in ) ) ) ;


signature:
public class Transforms { /** * Sinh function
 * @ param in
 * @ param copy
 * @ return */
public static INDArray sinh ( INDArray in , boolean copy ) { } }

implementation: return Nd4j . getExecutioner ( ) . execAndReturn ( new Sinh ( ( copy ? in . dup ( ) : in ) ) ) ;


signature:
public class Transforms { /** * Hard tanh
 * @ param ndArray the input
 * @ param dup whether to duplicate the ndarray and return it as the result
 * @ return the output */
public static INDArray hardTanh ( INDArray ndArray , boolean dup ) { } }

implementation: return exec ( dup ? new HardTanh ( ndArray , ndArray . dup ( ) ) : new HardTanh ( ndArray ) ) ;


signature:
public class Transforms { /** * Hard tanh
 * @ param ndArray the input
 * @ param dup whether to duplicate the ndarray and return it as the result
 * @ return the output */
public static INDArray hardTanhDerivative ( INDArray ndArray , boolean dup ) { } }

implementation: return exec ( dup ? new HardTanhDerivative ( ndArray , ndArray . dup ( ) ) : new HardTanhDerivative ( ndArray ) ) ;


signature:
public class Transforms { /** * Element - wise power function - x ^ y , performed element - wise
 * @ param ndArray the ndarray to raise to the power of
 * @ param power the power to raise by
 * @ param dup if true :
 * @ return the ndarray raised to this power */
public static INDArray pow ( INDArray ndArray , INDArray power , boolean dup ) { } }

implementation: INDArray result = ( dup ? Nd4j . create ( ndArray . shape ( ) , ndArray . ordering ( ) ) : ndArray ) ; return exec ( new Pow ( ndArray , power , result , ndArray . length ( ) , 0 ) ) ;


signature:
public class Transforms { /** * Log on arbitrary base
 * @ param ndArray
 * @ param base
 * @ return */
public static INDArray log ( INDArray ndArray , double base , boolean duplicate ) { } }

implementation: return Nd4j . getExecutioner ( ) . exec ( new LogX ( duplicate ? ndArray . dup ( ndArray . ordering ( ) ) : ndArray , base ) ) . z ( ) ;


signature:
public class Transforms { /** * Eps function
 * @ param ndArray
 * @ return */
public static INDArray lessThanOrEqual ( INDArray first , INDArray ndArray , boolean dup ) { } }

implementation: return exec ( dup ? new OldLessThanOrEqual ( first . dup ( ) , ndArray ) : new OldLessThanOrEqual ( first , ndArray ) ) ;


signature:
public class Transforms { /** * Eps function
 * @ param ndArray
 * @ return */
public static INDArray greaterThanOrEqual ( INDArray first , INDArray ndArray , boolean dup ) { } }

implementation: return exec ( dup ? new OldGreaterThanOrEqual ( first . dup ( ) , ndArray ) : new OldGreaterThanOrEqual ( first , ndArray ) ) ;


signature:
public class Transforms { /** * Eps function
 * @ param ndArray
 * @ return */
public static INDArray eps ( INDArray ndArray , boolean dup ) { } }

implementation: return exec ( dup ? new Eps ( ndArray . dup ( ) ) : new Eps ( ndArray ) ) ;


signature:
public class Transforms { /** * Maximum function with a scalar
 * @ param ndArray tbe ndarray
 * @ param k
 * @ param dup
 * @ return */
public static INDArray max ( INDArray ndArray , double k , boolean dup ) { } }

implementation: return exec ( dup ? new ScalarMax ( ndArray . dup ( ) , k ) : new ScalarMax ( ndArray , k ) ) ;


signature:
public class Transforms { /** * Element wise maximum function between 2 INDArrays
 * @ param first
 * @ param second
 * @ param dup
 * @ return */
public static INDArray max ( INDArray first , INDArray second , boolean dup ) { } }

implementation: if ( dup ) { first = first . dup ( ) ; } return exec ( new OldMax ( second , first , first , first . length ( ) ) ) ;


signature:
public class Transforms { /** * Minimum function with a scalar
 * @ param ndArray tbe ndarray
 * @ param k
 * @ param dup
 * @ return */
public static INDArray min ( INDArray ndArray , double k , boolean dup ) { } }

implementation: return exec ( dup ? new ScalarMin ( ndArray . dup ( ) , k ) : new ScalarMin ( ndArray , k ) ) ;


signature:
public class Transforms { /** * Element wise minimum function between 2 INDArrays
 * @ param first
 * @ param second
 * @ param dup
 * @ return */
public static INDArray min ( INDArray first , INDArray second , boolean dup ) { } }

implementation: if ( dup ) { first = first . dup ( ) ; } return exec ( new OldMin ( second , first , first , first . length ( ) ) ) ;


signature:
public class Transforms { /** * Stabilize to be within a range of k
 * @ param ndArray tbe ndarray
 * @ param k
 * @ param dup
 * @ return */
public static INDArray stabilize ( INDArray ndArray , double k , boolean dup ) { } }

implementation: return exec ( dup ? new Stabilize ( ndArray , ndArray . dup ( ) , k ) : new Stabilize ( ndArray , k ) ) ;


signature:
public class Transforms { /** * Abs function
 * @ param ndArray
 * @ param dup
 * @ return */
public static INDArray abs ( INDArray ndArray , boolean dup ) { } }

implementation: return exec ( dup ? new Abs ( ndArray , ndArray . dup ( ) ) : new Abs ( ndArray ) ) ;


signature:
public class Transforms { /** * Exp function
 * @ param ndArray
 * @ param dup
 * @ return */
public static INDArray exp ( INDArray ndArray , boolean dup ) { } }

implementation: return exec ( dup ? new Exp ( ndArray , ndArray . dup ( ) ) : new Exp ( ndArray ) ) ;


signature:
public class Transforms { /** * Elementwise exponential - 1 function
 * @ param ndArray
 * @ param dup
 * @ return */
public static INDArray expm1 ( INDArray ndArray , boolean dup ) { } }

implementation: return exec ( dup ? new Expm1 ( ndArray , ndArray . dup ( ) ) : new Expm1 ( ndArray ) ) ;


signature:
public class Transforms { /** * Identity function
 * @ param ndArray
 * @ param dup
 * @ return */
public static INDArray identity ( INDArray ndArray , boolean dup ) { } }

implementation: return exec ( dup ? new OldIdentity ( ndArray , ndArray . dup ( ) ) : new OldIdentity ( ndArray ) ) ;


signature:
public class Transforms { /** * Sqrt function
 * @ param ndArray
 * @ param dup
 * @ return */
public static INDArray sqrt ( INDArray ndArray , boolean dup ) { } }

implementation: return exec ( dup ? new Sqrt ( ndArray , ndArray . dup ( ) ) : new Sqrt ( ndArray ) ) ;


signature:
public class Transforms { /** * Tanh function
 * @ param ndArray
 * @ param dup
 * @ return */
public static INDArray tanh ( INDArray ndArray , boolean dup ) { } }

implementation: return exec ( dup ? new Tanh ( ndArray , ndArray . dup ( ) ) : new Tanh ( ndArray ) ) ;


signature:
public class Transforms { /** * Log function
 * @ param ndArray
 * @ param dup
 * @ return */
public static INDArray log ( INDArray ndArray , boolean dup ) { } }

implementation: return exec ( dup ? new Log ( ndArray , ndArray . dup ( ) ) : new Log ( ndArray ) ) ;


signature:
public class Transforms { /** * Log of x + 1 function
 * @ param ndArray
 * @ param dup
 * @ return */
public static INDArray log1p ( INDArray ndArray , boolean dup ) { } }

implementation: return exec ( dup ? new Log1p ( ndArray , ndArray . dup ( ) ) : new Log1p ( ndArray ) ) ;


signature:
public class Transforms { /** * Apply the given elementwise op
 * @ param op the factory to create the op
 * @ return the new ndarray */
private static INDArray exec ( ScalarOp op ) { } }

implementation: if ( op . x ( ) . isCleanedUp ( ) ) throw new IllegalStateException ( "NDArray already freed" ) ; return Nd4j . getExecutioner ( ) . exec ( op ) . z ( ) ;


signature:
public class Transforms { /** * Apply the given elementwise op
 * @ param op the factory to create the op
 * @ return the new ndarray */
private static INDArray exec ( TransformOp op ) { } }

implementation: if ( op . x ( ) . isCleanedUp ( ) ) throw new IllegalStateException ( "NDArray already freed" ) ; return Nd4j . getExecutioner ( ) . execAndReturn ( op ) ;


signature:
public class Transforms { /** * Raises a square matrix to a power < i > n < / i > , which can be positive , negative , or zero .
 * The behavior is similar to the numpy matrix _ power ( ) function . The algorithm uses
 * repeated squarings to minimize the number of mmul ( ) operations needed
 * < p > If < i > n < / i > is zero , the identity matrix is returned . < / p >
 * < p > If < i > n < / i > is negative , the matrix is inverted and raised to the abs ( n ) power . < / p >
 * @ param in A square matrix to raise to an integer power , which will be changed if dup is false .
 * @ param n The integer power to raise the matrix to .
 * @ param dup If dup is true , the original input is unchanged .
 * @ return The result of raising < i > in < / i > to the < i > n < / i > th power . */
public static INDArray mpow ( INDArray in , int n , boolean dup ) { } }

implementation:
assert in . rows ( ) == in . columns ( ) ; if ( n == 0 ) { if ( dup ) return Nd4j . eye ( in . rows ( ) ) ; else return in . assign ( Nd4j . eye ( in . rows ( ) ) ) ; } INDArray temp ; if ( n < 0 ) { temp = InvertMatrix . invert ( in , ! dup ) ; n = - n ; } else temp = in . dup ( ) ; INDArray result = temp . dup ( ) ; if ( n < 4 ) { for ( int i = 1 ; i < n ; i ++ ) { result . mmuli ( temp ) ; } if ( dup ) return result ; else return in . assign ( result ) ; } else { // lets try to optimize by squaring itself a bunch of times
int squares = ( int ) ( Math . log ( n ) / Math . log ( 2.0 ) ) ; for ( int i = 0 ; i < squares ; i ++ ) result = result . mmul ( result ) ; int diff = ( int ) Math . round ( n - Math . pow ( 2.0 , squares ) ) ; for ( int i = 0 ; i < diff ; i ++ ) result . mmuli ( temp ) ; if ( dup ) return result ; else return in . assign ( result ) ; }


signature:
public class TwoPointApproximation { /** * Prepare the boundaries for processing
 * @ param bounds the bounds
 * @ param x the input in to the approximation
 * @ return the lower and upper bounds as an array of ndarrays
 * ( in that order ) of the same shape as x */
public static INDArray [ ] prepareBounds ( INDArray bounds , INDArray x ) { } }

implementation: return new INDArray [ ] { Nd4j . valueArrayOf ( x . shape ( ) , bounds . getDouble ( 0 ) ) , Nd4j . valueArrayOf ( x . shape ( ) , bounds . getDouble ( 1 ) ) } ;


signature:
public class TwoPointApproximation { /** * Adjust final scheme to presence of bounds
 * Returns ( in this order ) :
 * adjusted hypothesis , whether to use onesided as an int mask array
 * @ param x the point to estimate the derivative
 * @ param h the finite difference steps
 * @ param numSteps Number of h steps in 1 direction
 * to implement finite difference scheme .
 * @ param lowerBound Lower bounds for independent variable variable
 * @ param upperBound Upper bounds for independent variable
 * @ return */
public static INDArray [ ] adjustSchemeToBounds ( INDArray x , INDArray h , int numSteps , INDArray lowerBound , INDArray upperBound ) { } }

implementation: INDArray oneSided = Nd4j . onesLike ( h ) ; if ( and ( lowerBound . eq ( Double . NEGATIVE_INFINITY ) , upperBound . eq ( Double . POSITIVE_INFINITY ) ) . sumNumber ( ) . doubleValue ( ) > 0 ) { return new INDArray [ ] { h , oneSided } ; } INDArray hTotal = h . mul ( numSteps ) ; INDArray hAdjusted = h . dup ( ) ; INDArray lowerDist = x . sub ( lowerBound ) ; INDArray upperBound2 = upperBound . sub ( x ) ; INDArray central = and ( greaterThanOrEqual ( lowerDist , hTotal ) , greaterThanOrEqual ( upperBound2 , hTotal ) ) ; INDArray forward = and ( greaterThanOrEqual ( upperBound , lowerDist ) , not ( central ) ) ; hAdjusted . put ( forward , min ( h . get ( forward ) , upperBound2 . get ( forward ) . mul ( 0.5 ) . divi ( numSteps ) ) ) ; oneSided . put ( forward , Nd4j . scalar ( 1.0 ) ) ; INDArray backward = and ( upperBound2 . lt ( lowerBound ) , not ( central ) ) ; hAdjusted . put ( backward , min ( h . get ( backward ) , lowerDist . get ( backward ) . mul ( 0.5 ) . divi ( numSteps ) ) ) ; oneSided . put ( backward , Nd4j . scalar ( 1.0 ) ) ; INDArray minDist = min ( upperBound2 , lowerDist ) . divi ( numSteps ) ; INDArray adjustedCentral = and ( not ( central ) , lessThanOrEqual ( abs ( hAdjusted ) , minDist ) ) ; hAdjusted . put ( adjustedCentral , minDist . get ( adjustedCentral ) ) ; oneSided . put ( adjustedCentral , Nd4j . scalar ( 0.0 ) ) ; return new INDArray [ ] { hAdjusted , oneSided } ;


signature:
public class MultipleEpochsIterator { /** * Returns the next element in the iteration .
 * @ return the next element in the iteration */
@ Override public DataSet next ( ) { } }

implementation: if ( ! iter . hasNext ( ) && passes < numPasses ) { passes ++ ; batch = 0 ; log . info ( "Epoch " + passes + " batch " + batch ) ; iter . reset ( ) ; } batch ++ ; DataSet next = iter . next ( ) ; if ( preProcessor != null ) preProcessor . preProcess ( next ) ; return next ;


signature:
public class BaseConvolution { /** * ND Convolution
 * @ param input the input to transform
 * @ param kernel the kernel to transform with
 * @ param type the opType of convolution
 * @ return the convolution of the given input and kernel */
@ Override public IComplexNDArray convn ( IComplexNDArray input , IComplexNDArray kernel , Convolution . Type type ) { } }

implementation: return convn ( input , kernel , type , ArrayUtil . range ( 0 , input . shape ( ) . length ) ) ;


signature:
public class JcublasLapack { /** * CHOLESKY DECOMP */
@ Override public void spotrf ( byte uplo , int N , INDArray A , INDArray INFO ) { } }

implementation:
INDArray a = A ; if ( Nd4j . dataType ( ) != DataBuffer . Type . FLOAT ) log . warn ( "DOUBLE potrf called in FLOAT environment" ) ; if ( A . ordering ( ) == 'c' ) a = A . dup ( 'f' ) ; if ( Nd4j . getExecutioner ( ) instanceof GridExecutioner ) ( ( GridExecutioner ) Nd4j . getExecutioner ( ) ) . flushQueue ( ) ; // Get context for current thread
CudaContext ctx = ( CudaContext ) allocator . getDeviceContext ( ) . getContext ( ) ; // setup the solver handles for cuSolver calls
cusolverDnHandle_t handle = ctx . getSolverHandle ( ) ; cusolverDnContext solverDn = new cusolverDnContext ( handle ) ; // synchronized on the solver
synchronized ( handle ) { int result = cusolverDnSetStream ( new cusolverDnContext ( handle ) , new CUstream_st ( ctx . getOldStream ( ) ) ) ; if ( result != 0 ) throw new BlasException ( "solverSetStream failed" ) ; // transfer the INDArray into GPU memory
CublasPointer xAPointer = new CublasPointer ( a , ctx ) ; // this output - indicates how much memory we ' ll need for the real operation
DataBuffer worksizeBuffer = Nd4j . getDataBufferFactory ( ) . createInt ( 1 ) ; int stat = cusolverDnSpotrf_bufferSize ( solverDn , uplo , N , ( FloatPointer ) xAPointer . getDevicePointer ( ) , N , ( IntPointer ) worksizeBuffer . addressPointer ( ) // we intentionally use host pointer here
) ; if ( stat != CUSOLVER_STATUS_SUCCESS ) { throw new BlasException ( "cusolverDnSpotrf_bufferSize failed" , stat ) ; } int worksize = worksizeBuffer . getInt ( 0 ) ; // Now allocate memory for the workspace , the permutation matrix and a return code
Pointer workspace = new Workspace ( worksize * Nd4j . sizeOfDataType ( ) ) ; // Do the actual decomp
stat = cusolverDnSpotrf ( solverDn , uplo , N , ( FloatPointer ) xAPointer . getDevicePointer ( ) , N , new CudaPointer ( workspace ) . asFloatPointer ( ) , worksize , new CudaPointer ( allocator . getPointer ( INFO , ctx ) ) . asIntPointer ( ) ) ; if ( stat != CUSOLVER_STATUS_SUCCESS ) { throw new BlasException ( "cusolverDnSpotrf failed" , stat ) ; } } allocator . registerAction ( ctx , a ) ; allocator . registerAction ( ctx , INFO ) ; if ( a != A ) A . assign ( a ) ; if ( uplo == 'U' ) { A . assign ( A . transpose ( ) ) ; INDArrayIndex ix [ ] = new INDArrayIndex [ 2 ] ; for ( int i = 1 ; i < Math . min ( A . rows ( ) , A . columns ( ) ) ; i ++ ) { ix [ 0 ] = NDArrayIndex . point ( i ) ; ix [ 1 ] = NDArrayIndex . interval ( 0 , i ) ; A . put ( ix , 0 ) ; } } else { INDArrayIndex ix [ ] = new INDArrayIndex [ 2 ] ; for ( int i = 0 ; i < Math . min ( A . rows ( ) , A . columns ( ) - 1 ) ; i ++ ) { ix [ 0 ] = NDArrayIndex . point ( i ) ; ix [ 1 ] = NDArrayIndex . interval ( i + 1 , A . columns ( ) ) ; A . put ( ix , 0 ) ; } } log . info ( "A: {}" , A ) ;


signature:
public class CpuLapack { /** * Q R DECOMP */
@ Override public void sgeqrf ( int M , int N , INDArray A , INDArray R , INDArray INFO ) { } }

implementation:
INDArray tau = Nd4j . create ( N ) ; int status = LAPACKE_sgeqrf ( getColumnOrder ( A ) , M , N , ( FloatPointer ) A . data ( ) . addressPointer ( ) , getLda ( A ) , ( FloatPointer ) tau . data ( ) . addressPointer ( ) ) ; if ( status != 0 ) { throw new BlasException ( "Failed to execute sgeqrf" , status ) ; } // Copy R ( upper part of Q ) into result
if ( R != null ) { R . assign ( A . get ( NDArrayIndex . interval ( 0 , A . columns ( ) ) , NDArrayIndex . all ( ) ) ) ; INDArrayIndex ix [ ] = new INDArrayIndex [ 2 ] ; for ( int i = 1 ; i < Math . min ( A . rows ( ) , A . columns ( ) ) ; i ++ ) { ix [ 0 ] = NDArrayIndex . point ( i ) ; ix [ 1 ] = NDArrayIndex . interval ( 0 , i ) ; R . put ( ix , 0 ) ; } } status = LAPACKE_sorgqr ( getColumnOrder ( A ) , M , N , N , ( FloatPointer ) A . data ( ) . addressPointer ( ) , getLda ( A ) , ( FloatPointer ) tau . data ( ) . addressPointer ( ) ) ; if ( status != 0 ) { throw new BlasException ( "Failed to execute sorgqr" , status ) ; }


signature:
public class AllocationUtils { /** * This method returns AllocationShape for specific array , that takes in account its real shape : offset , length , etc
 * @ param array
 * @ return */
public static AllocationShape buildAllocationShape ( INDArray array ) { } }

implementation: AllocationShape shape = new AllocationShape ( ) ; shape . setStride ( array . elementWiseStride ( ) ) ; shape . setOffset ( array . originalOffset ( ) ) ; shape . setDataType ( array . data ( ) . dataType ( ) ) ; shape . setLength ( array . length ( ) ) ; shape . setDataType ( array . data ( ) . dataType ( ) ) ; return shape ;


signature:
public class AllocationUtils { /** * This method returns AllocationShape for the whole DataBuffer .
 * @ param buffer
 * @ return */
public static AllocationShape buildAllocationShape ( DataBuffer buffer ) { } }

implementation: AllocationShape shape = new AllocationShape ( ) ; shape . setStride ( 1 ) ; shape . setOffset ( buffer . originalOffset ( ) ) ; shape . setDataType ( buffer . dataType ( ) ) ; shape . setLength ( buffer . length ( ) ) ; return shape ;


signature:
public class Paths { /** * Check if a file exists in the path
 * @ param name the name of the file
 * @ return true if the name exists
 * false otherwise */
public static boolean nameExistsInPath ( String name ) { } }

implementation: String path = System . getenv ( PATH_ENV_VARIABLE ) ; String [ ] dirs = path . split ( File . pathSeparator ) ; for ( String dir : dirs ) { File dirFile = new File ( dir ) ; if ( ! dirFile . exists ( ) ) continue ; if ( dirFile . isFile ( ) && dirFile . getName ( ) . equals ( name ) ) return true ; else { Iterator < File > files = FileUtils . iterateFiles ( dirFile , null , false ) ; while ( files . hasNext ( ) ) { File curr = files . next ( ) ; if ( curr . getName ( ) . equals ( name ) ) return true ; } } } return false ;


signature:
public class BaseNDArrayProxy { /** * Custom deserialization for Java serialization */
protected void read ( ObjectInputStream s ) throws IOException , ClassNotFoundException { } }

implementation: data = Nd4j . createBuffer ( length , false ) ; data . read ( s ) ;


signature:
public class AsynchronousFlowController { /** * This method ensures the events in the beginning of FIFO queues are finished */
protected void sweepTail ( ) { } }

implementation:
Integer deviceId = allocator . getDeviceId ( ) ; int cnt = 0 ; // we get number of issued commands for specific device
long lastCommandId = deviceClocks . get ( deviceId ) . get ( ) ; for ( int l = 0 ; l < configuration . getCommandLanesNumber ( ) ; l ++ ) { Queue < cudaEvent_t > queue = eventsBarrier . get ( deviceId ) . get ( l ) ; if ( queue . size ( ) >= MAX_EXECUTION_QUEUE || laneClocks . get ( deviceId ) . get ( l ) . get ( ) < lastCommandId - MAX_EXECUTION_QUEUE ) { cudaEvent_t event = queue . poll ( ) ; if ( event != null && ! event . isDestroyed ( ) ) { event . synchronize ( ) ; event . destroy ( ) ; cnt ++ ; } } } deviceClocks . get ( deviceId ) . incrementAndGet ( ) ; // log . info ( " Events sweeped : [ { } ] " , cnt ) ;


signature:
public class JCublasNDArrayFactory { /** * Create a complex ndarray from the passed in indarray
 * @ param data the data to wrap
 * @ param shape
 * @ return the complex ndarray with the specified ndarray as the
 * real components */
@ Override public IComplexNDArray createComplex ( IComplexNumber [ ] data , int [ ] shape ) { } }

implementation: return new JCublasComplexNDArray ( data , shape , Nd4j . getComplexStrides ( shape , Nd4j . order ( ) ) ) ;


signature:
public class JCublasNDArrayFactory { /** * Create a complex ndarray from the passed in indarray
 * @ param arrs the arr to wrap
 * @ param shape
 * @ return the complex ndarray with the specified ndarray as the
 * real components */
@ Override public IComplexNDArray createComplex ( List < IComplexNDArray > arrs , int [ ] shape ) { } }

implementation: return new JCublasComplexNDArray ( arrs , shape ) ;


signature:
public class JCublasNDArrayFactory { /** * Symmetric in place shuffle of an ndarray
 * along a specified set of dimensions . Each array in list should have it ' s own dimension at the same index of dimensions array
 * @ param arrays the ndarrays to shuffle
 * @ param dimensions the dimensions to do the shuffle
 * @ return */
@ Override public void shuffle ( List < INDArray > arrays , Random rnd , List < int [ ] > dimensions ) { } }

implementation:
// no dimension - no shuffle
if ( dimensions == null || dimensions . size ( ) == 0 ) throw new RuntimeException ( "Dimension can't be null or 0-length" ) ; if ( arrays == null || arrays . size ( ) == 0 ) throw new RuntimeException ( "No input arrays provided" ) ; if ( dimensions . size ( ) > 1 && arrays . size ( ) != dimensions . size ( ) ) throw new IllegalStateException ( "Number of dimensions do not match number of arrays to shuffle" ) ; Nd4j . getExecutioner ( ) . push ( ) ; // first we build TAD for input array and dimensions
AtomicAllocator allocator = AtomicAllocator . getInstance ( ) ; CudaContext context = null ; for ( int x = 0 ; x < arrays . size ( ) ; x ++ ) { context = allocator . getFlowController ( ) . prepareAction ( arrays . get ( x ) ) ; } int tadLength = 1 ; for ( int i = 0 ; i < dimensions . get ( 0 ) . length ; i ++ ) { tadLength *= arrays . get ( 0 ) . shape ( ) [ dimensions . get ( 0 ) [ i ] ] ; } val numTads = arrays . get ( 0 ) . length ( ) / tadLength ; val map = ArrayUtil . buildInterleavedVector ( rnd , ( int ) numTads ) ; val shuffle = new CudaIntDataBuffer ( map ) ; Pointer shuffleMap = allocator . getPointer ( shuffle , context ) ; PointerPointer extras = new PointerPointer ( null , // not used
context . getOldStream ( ) , allocator . getDeviceIdPointer ( ) ) ; long [ ] xPointers = new long [ arrays . size ( ) ] ; long [ ] xShapes = new long [ arrays . size ( ) ] ; long [ ] tadShapes = new long [ arrays . size ( ) ] ; long [ ] tadOffsets = new long [ arrays . size ( ) ] ; for ( int i = 0 ; i < arrays . size ( ) ; i ++ ) { INDArray array = arrays . get ( i ) ; Pointer x = AtomicAllocator . getInstance ( ) . getPointer ( array , context ) ; Pointer xShapeInfo = AtomicAllocator . getInstance ( ) . getPointer ( array . shapeInfoDataBuffer ( ) , context ) ; TADManager tadManager = Nd4j . getExecutioner ( ) . getTADManager ( ) ; int [ ] dimension = dimensions . size ( ) > 1 ? dimensions . get ( i ) : dimensions . get ( 0 ) ; Pair < DataBuffer , DataBuffer > tadBuffers = tadManager . getTADOnlyShapeInfo ( array , dimension ) ; // log . info ( " Original shape : { } ; dimension : { } ; TAD shape : { } " , array . shapeInfoDataBuffer ( ) . asInt ( ) , dimension , tadBuffers . getFirst ( ) . asInt ( ) ) ;
Pointer tadShapeInfo = AtomicAllocator . getInstance ( ) . getPointer ( tadBuffers . getFirst ( ) , context ) ; DataBuffer offsets = tadBuffers . getSecond ( ) ; if ( offsets . length ( ) != numTads ) throw new ND4JIllegalStateException ( "Can't symmetrically shuffle arrays with non-equal number of TADs" ) ; Pointer tadOffset = AtomicAllocator . getInstance ( ) . getPointer ( offsets , context ) ; xPointers [ i ] = x . address ( ) ; xShapes [ i ] = xShapeInfo . address ( ) ; tadShapes [ i ] = tadShapeInfo . address ( ) ; tadOffsets [ i ] = tadOffset . address ( ) ; } CudaDoubleDataBuffer tempX = new CudaDoubleDataBuffer ( arrays . size ( ) ) ; CudaDoubleDataBuffer tempShapes = new CudaDoubleDataBuffer ( arrays . size ( ) ) ; CudaDoubleDataBuffer tempTAD = new CudaDoubleDataBuffer ( arrays . size ( ) ) ; CudaDoubleDataBuffer tempOffsets = new CudaDoubleDataBuffer ( arrays . size ( ) ) ; AtomicAllocator . getInstance ( ) . memcpyBlocking ( tempX , new LongPointer ( xPointers ) , xPointers . length * 8 , 0 ) ; AtomicAllocator . getInstance ( ) . memcpyBlocking ( tempShapes , new LongPointer ( xShapes ) , xPointers . length * 8 , 0 ) ; AtomicAllocator . getInstance ( ) . memcpyBlocking ( tempTAD , new LongPointer ( tadShapes ) , xPointers . length * 8 , 0 ) ; AtomicAllocator . getInstance ( ) . memcpyBlocking ( tempOffsets , new LongPointer ( tadOffsets ) , xPointers . length * 8 , 0 ) ; if ( Nd4j . dataType ( ) == DataBuffer . Type . DOUBLE ) { nativeOps . shuffleDouble ( extras , new PointerPointer ( allocator . getPointer ( tempX , context ) ) , new PointerPointer ( allocator . getPointer ( tempShapes , context ) ) , new PointerPointer ( allocator . getPointer ( tempX , context ) ) , new PointerPointer ( allocator . getPointer ( tempShapes , context ) ) , arrays . size ( ) , ( IntPointer ) shuffleMap , new PointerPointer ( allocator . getPointer ( tempTAD , context ) ) , new PointerPointer ( allocator . getPointer ( tempOffsets , context ) ) ) ; } else if ( Nd4j . dataType ( ) == DataBuffer . Type . FLOAT ) { nativeOps . shuffleFloat ( extras , new PointerPointer ( allocator . getPointer ( tempX , context ) ) , new PointerPointer ( allocator . getPointer ( tempShapes , context ) ) , new PointerPointer ( allocator . getPointer ( tempX , context ) ) , new PointerPointer ( allocator . getPointer ( tempShapes , context ) ) , arrays . size ( ) , ( IntPointer ) shuffleMap , new PointerPointer ( allocator . getPointer ( tempTAD , context ) ) , new PointerPointer ( allocator . getPointer ( tempOffsets , context ) ) ) ; } else { // HALFs
nativeOps . shuffleHalf ( extras , new PointerPointer ( allocator . getPointer ( tempX , context ) ) , new PointerPointer ( allocator . getPointer ( tempShapes , context ) ) , new PointerPointer ( allocator . getPointer ( tempX , context ) ) , new PointerPointer ( allocator . getPointer ( tempShapes , context ) ) , arrays . size ( ) , ( IntPointer ) shuffleMap , new PointerPointer ( allocator . getPointer ( tempTAD , context ) ) , new PointerPointer ( allocator . getPointer ( tempOffsets , context ) ) ) ; } for ( int f = 0 ; f < arrays . size ( ) ; f ++ ) { allocator . getFlowController ( ) . registerAction ( context , arrays . get ( f ) ) ; } // just to keep reference
shuffle . address ( ) ; tempX . dataType ( ) ; tempShapes . dataType ( ) ; tempOffsets . dataType ( ) ; tempTAD . dataType ( ) ;


signature:
public class JCublasNDArrayFactory { /** * Create from an in memory numpy pointer
 * @ param pointer the pointer to the
 * numpy array
 * @ return an ndarray created from the in memory
 * numpy pointer */
@ Override public INDArray createFromNpyPointer ( Pointer pointer ) { } }

implementation:
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 ( 4 * length ) ; shapeBufferPointer . limit ( 4 * length ) ; shapeBufferPointer . position ( 0 ) ; val intPointer = new LongPointer ( shapeBufferPointer ) ; DataBuffer shapeBuffer = Nd4j . createBuffer ( shapeBufferPointer , DataBuffer . Type . LONG , length , LongRawIndexer . create ( intPointer ) ) ; dataPointer . position ( 0 ) ; dataPointer . limit ( dataBufferElementSize * Shape . length ( shapeBuffer ) ) ; dataPointer . capacity ( dataBufferElementSize * Shape . length ( shapeBuffer ) ) ; // we don ' t care about pointers here , they will be copied in BaseCudaDataBuffer method , and indexer will be recreated
if ( dataBufferElementSize == ( Float . SIZE / 8 ) ) { data = Nd4j . createBuffer ( dataPointer , DataBuffer . Type . FLOAT , Shape . length ( shapeBuffer ) , FloatIndexer . create ( new FloatPointer ( dataPointer ) ) ) ; } else if ( dataBufferElementSize == ( Double . SIZE / 8 ) ) { data = Nd4j . createBuffer ( dataPointer , DataBuffer . Type . DOUBLE , Shape . length ( shapeBuffer ) , DoubleIndexer . create ( new DoublePointer ( dataPointer ) ) ) ; } INDArray ret = Nd4j . create ( data , Shape . shape ( shapeBuffer ) , Shape . strideArr ( shapeBuffer ) , Shape . offset ( shapeBuffer ) , Shape . order ( shapeBuffer ) ) ; return ret ;


signature:
public class JCublasNDArrayFactory { /** * Create from a given numpy file .
 * @ param file the file to create the ndarray from
 * @ return the created ndarray */
@ Override public INDArray createFromNpyFile ( File file ) { } }

implementation:
/* Pointer pointer = nativeOps . numpyFromFile ( new BytePointer ( file . getAbsolutePath ( ) . getBytes ( ) ) ) ;
 log . info ( " Pointer here : { } " , pointer . address ( ) ) ;
 return createFromNpyPointer ( pointer ) ; */
byte [ ] pathBytes = file . getAbsolutePath ( ) . getBytes ( Charset . forName ( "UTF-8" ) ) ; String otherBytes = new String ( pathBytes ) ; System . out . println ( otherBytes ) ; ByteBuffer directBuffer = ByteBuffer . allocateDirect ( pathBytes . length ) . order ( ByteOrder . nativeOrder ( ) ) ; directBuffer . put ( pathBytes ) ; directBuffer . rewind ( ) ; directBuffer . position ( 0 ) ; Pointer pointer = nativeOps . numpyFromFile ( new BytePointer ( directBuffer ) ) ; INDArray result = createFromNpyPointer ( pointer ) ; // releasing original pointer here
nativeOps . releaseNumpy ( pointer ) ; return result ;


signature:
public class CudaCachingZeroProvider { /** * This method provides PointersPair to memory chunk specified by AllocationShape
 * PLEASE NOTE : This method can actually ignore malloc request , and give out previously cached free memory chunk with equal shape .
 * @ param shape shape of desired memory chunk
 * @ param point target AllocationPoint structure
 * @ param location either HOST or DEVICE
 * @ return */
@ Override public PointersPair malloc ( AllocationShape shape , AllocationPoint point , AllocationStatus location ) { } }

implementation:
long reqMemory = AllocationUtils . getRequiredMemory ( shape ) ; if ( location == AllocationStatus . HOST && reqMemory < CudaEnvironment . getInstance ( ) . getConfiguration ( ) . getMaximumHostCacheableLength ( ) ) { CacheHolder cache = zeroCache . get ( shape ) ; if ( cache != null ) { Pointer pointer = cache . poll ( ) ; if ( pointer != null ) { cacheZeroHit . incrementAndGet ( ) ; // since this memory chunk is going to be used now , remove it ' s amount from
zeroCachedAmount . addAndGet ( - 1 * reqMemory ) ; PointersPair pair = new PointersPair ( ) ; pair . setDevicePointer ( new CudaPointer ( pointer . address ( ) ) ) ; pair . setHostPointer ( new CudaPointer ( pointer . address ( ) ) ) ; point . setAllocationStatus ( AllocationStatus . HOST ) ; return pair ; } } cacheZeroMiss . incrementAndGet ( ) ; if ( CudaEnvironment . getInstance ( ) . getConfiguration ( ) . isUsePreallocation ( ) && zeroCachedAmount . get ( ) < CudaEnvironment . getInstance ( ) . getConfiguration ( ) . getMaximumHostCache ( ) / 10 && reqMemory < 16 * 1024 * 1024L ) { CachePreallocator preallocator = new CachePreallocator ( shape , location , CudaEnvironment . getInstance ( ) . getConfiguration ( ) . getPreallocationCalls ( ) ) ; preallocator . start ( ) ; } cacheZeroMiss . incrementAndGet ( ) ; return super . malloc ( shape , point , location ) ; } return super . malloc ( shape , point , location ) ;


signature:
public class DummyWorkspace { /** * This method does allocation from a given Workspace
 * @ param requiredMemory allocation size , in bytes
 * @ param kind MemoryKind for allocation
 * @ param dataType dataType that is going to be used
 * @ param initialize
 * @ return */
@ Override public PagedPointer alloc ( long requiredMemory , MemoryKind kind , DataBuffer . Type dataType , boolean initialize ) { } }

implementation: throw new UnsupportedOperationException ( "DummyWorkspace shouldn't be used for allocation" ) ;


signature:
public class RRWLock { /** * This method notifies locker , that specific object was added to tracking list
 * @ param object */
@ Override public void attachObject ( Object object ) { } }

implementation: if ( ! objectLocks . containsKey ( object ) ) objectLocks . put ( object , new ReentrantReadWriteLock ( ) ) ;


signature:
public class AdamUpdater { /** * Calculate the update based on the given gradient
 * @ param gradient the gradient to get the update for
 * @ param iteration
 * @ return the gradient */
@ Override public void applyUpdater ( INDArray gradient , int iteration , int epoch ) { } }

implementation: if ( m == null || v == null ) throw new IllegalStateException ( "Updater has not been initialized with view state" ) ; double beta1 = config . getBeta1 ( ) ; double beta2 = config . getBeta2 ( ) ; double learningRate = config . getLearningRate ( iteration , epoch ) ; double epsilon = config . getEpsilon ( ) ; INDArray oneMinusBeta1Grad = gradient . mul ( 1.0 - beta1 ) ; m . muli ( beta1 ) . addi ( oneMinusBeta1Grad ) ; INDArray oneMinusBeta2GradSquared = gradient . mul ( gradient ) . muli ( 1 - beta2 ) ; v . muli ( beta2 ) . addi ( oneMinusBeta2GradSquared ) ; double beta1t = FastMath . pow ( beta1 , iteration + 1 ) ; double beta2t = FastMath . pow ( beta2 , iteration + 1 ) ; double alphat = learningRate * FastMath . sqrt ( 1 - beta2t ) / ( 1 - beta1t ) ; if ( Double . isNaN ( alphat ) || alphat == 0.0 ) alphat = epsilon ; INDArray sqrtV = Transforms . sqrt ( v . dup ( gradientReshapeOrder ) , false ) . addi ( epsilon ) ; gradient . assign ( m ) . muli ( alphat ) . divi ( sqrtV ) ;


signature:
public class Nd4jSerializer { /** * Writes the bytes for the object to the output .
 * This method should not be called directly , instead this serializer can be passed to { @ link Kryo } write methods that accept a
 * serialier .
 * @ param kryo
 * @ param output
 * @ param object May be null if { @ link # getAcceptsNull ( ) } is true . */
@ Override public void write ( Kryo kryo , Output output , INDArray object ) { } }

implementation: DataOutputStream dos = new DataOutputStream ( output ) ; try { Nd4j . write ( object , dos ) ; } catch ( IOException e ) { throw new RuntimeException ( e ) ; } // Note : output should NOT be closed manually here - may be needed elsewhere ( and closing here will cause serialization to fail )


signature:
public class Nd4jSerializer { /** * Reads bytes and returns a new object of the specified concrete opType .
 * Before Kryo can be used to read child objects , { @ link Kryo # reference ( Object ) } must be called with the parent object to
 * ensure it can be referenced by the child objects . Any serializer that uses { @ link Kryo } to read a child object may need to
 * be reentrant .
 * This method should not be called directly , instead this serializer can be passed to { @ link Kryo } read methods that accept a
 * serialier .
 * @ param kryo
 * @ param input
 * @ param type
 * @ return May be null if { @ link # getAcceptsNull ( ) } is true . */
@ Override public INDArray read ( Kryo kryo , Input input , Class < INDArray > type ) { } }

implementation: DataInputStream dis = new DataInputStream ( input ) ; try { return Nd4j . read ( dis ) ; } catch ( IOException e ) { throw new RuntimeException ( e ) ; } // Note : input should NOT be closed manually here - may be needed elsewhere ( and closing here will cause serialization to fail )


signature:
public class MasterStatus { /** * Returns true if bth
 * the master and responder are started .
 * @ return */
public boolean started ( ) { } }

implementation: return master . equals ( ServerState . STARTED . name ( ) . toLowerCase ( ) ) && responder . equals ( ServerState . STARTED . name ( ) . toLowerCase ( ) ) ;


signature:
public class CudaGridExecutioner { /** * This is one of the main entry points for ops that are executed without respect to dimension .
 * Developers note : For CudaGridExecutioner that ' s also the MetaOp / GridOp creation point .
 * @ param op
 * @ return */
@ Override public Op exec ( Op op ) { } }

implementation:
/* We pass this op to GridProcessor through check for possible MetaOp concatenation
 Also , it ' s the GriOp entry point */
checkForCompression ( op ) ; invokeWatchdog ( op ) ; if ( op instanceof Accumulation ) { exec ( ( Accumulation ) op , new int [ ] { Integer . MAX_VALUE } ) ; } else if ( op instanceof IndexAccumulation ) { exec ( ( IndexAccumulation ) op , new int [ ] { Integer . MAX_VALUE } ) ; } else if ( op instanceof ScalarOp || op instanceof TransformOp ) { // the only entry place for TADless ops
processAsGridOp ( op ) ; } else if ( op instanceof BroadcastOp ) { invoke ( ( BroadcastOp ) op ) ; } else { // logger . info ( " Random op : { } " , op . getClass ( ) . getSimpleName ( ) ) ;
pushToGrid ( new OpDescriptor ( op ) ) ; } return op ;


signature:
public class CudaGridExecutioner { /** * FIXME : remove CudaContext return opType . We just don ' t need it */
@ Override protected CudaContext invoke ( TransformOp op ) { } }

implementation: if ( op . isExecSpecial ( ) ) { flushQueue ( ) ; super . invoke ( op ) ; } else { processAsGridOp ( op , null ) ; } return null ;


signature:
public class CudaGridExecutioner { /** * This method forces all currently enqueued ops to be executed immediately
 * PLEASE NOTE : This call is always blocking , until all queued operations are finished */
@ Override public void flushQueueBlocking ( ) { } }

implementation:
flushQueue ( ) ; // logger . info ( " Blocking flush " ) ; n
( ( CudaContext ) AtomicAllocator . getInstance ( ) . getDeviceContext ( ) . getContext ( ) ) . syncOldStream ( ) ; ( ( CudaContext ) AtomicAllocator . getInstance ( ) . getDeviceContext ( ) . getContext ( ) ) . syncSpecialStream ( ) ;


signature:
public class AddressRetriever { /** * Retrieves the device pointer
 * for the given data buffer
 * @ param buffer the buffer to get the device
 * address for
 * @ return the device address for the given
 * data buffer */
public static long retrieveDeviceAddress ( DataBuffer buffer , CudaContext context ) { } }

implementation: return allocator . getPointer ( buffer , context ) . address ( ) ;


signature:
public class AddressRetriever { /** * Retrieves the device pointer
 * for the given data buffer
 * @ param buffer the buffer to get the device
 * address for
 * @ return the device pointer for the given
 * data buffer */
public static Pointer retrieveDevicePointer ( DataBuffer buffer , CudaContext context ) { } }

implementation: return allocator . getPointer ( buffer , context ) ;


signature:
public class CudaZeroHandler { /** * PLEASE NOTE : Specific implementation , on systems without special devices can return HostPointer here
 * @ param buffer
 * @ return */
@ Override public org . bytedeco . javacpp . Pointer getDevicePointer ( DataBuffer buffer , CudaContext context ) { } }

implementation:
// TODO : It would be awesome to get rid of typecasting here
// getCudaContext ( ) . syncOldStream ( ) ;
AllocationPoint dstPoint = ( ( BaseCudaDataBuffer ) buffer ) . getAllocationPoint ( ) ; // log . info ( " getDevicePointer called " ) ;
/* if ( configuration . getMemoryModel ( ) = = Configuration . MemoryModel . DELAYED & & dstPoint . getAllocationStatus ( ) = = AllocationStatus . HOST ) {
 / / if we have constant buffer ( aka shapeInfo or other constant stuff )
 if ( buffer . isConstant ( ) ) {
 Nd4j . getConstantHandler ( ) . moveToConstantSpace ( buffer ) ;
 } else {
 PointersPair pair = memoryProvider . malloc ( dstPoint . getShape ( ) , dstPoint , AllocationStatus . DEVICE ) ;
 if ( pair ! = null ) {
 Integer deviceId = getDeviceId ( ) ;
 dstPoint . getPointers ( ) . setDevicePointer ( pair . getDevicePointer ( ) ) ;
 dstPoint . setAllocationStatus ( AllocationStatus . DEVICE ) ;
 deviceAllocations . get ( deviceId ) . put ( dstPoint . getObjectId ( ) , dstPoint . getObjectId ( ) ) ;
 zeroAllocations . get ( dstPoint . getBucketId ( ) ) . remove ( dstPoint . getObjectId ( ) ) ;
 deviceMemoryTracker . addToAllocation ( Thread . currentThread ( ) . getId ( ) , deviceId , AllocationUtils . getRequiredMemory ( dstPoint . getShape ( ) ) ) ;
 dstPoint . tickHostWrite ( ) ; */
// here ' s the place , where we do care about promotion . but we only care about promotion of original buffers
if ( dstPoint . getAllocationStatus ( ) == AllocationStatus . HOST && buffer . offset ( ) == 0 && 1 < 0 ) { if ( dstPoint . getDeviceTicks ( ) > configuration . getMinimumRelocationThreshold ( ) ) { // at this point we know , that this request is done withing some existent context
long requiredMemory = AllocationUtils . getRequiredMemory ( dstPoint . getShape ( ) ) ; if ( deviceMemoryTracker . reserveAllocationIfPossible ( Thread . currentThread ( ) . getId ( ) , getDeviceId ( ) , requiredMemory ) && pingDeviceForFreeMemory ( getDeviceId ( ) , requiredMemory ) ) { // so , memory is reserved
promoteObject ( buffer ) ; } } } // if that ' s device state , we probably might want to update device memory state
if ( dstPoint . getAllocationStatus ( ) == AllocationStatus . DEVICE ) { if ( ! dstPoint . isActualOnDeviceSide ( ) ) { // log . info ( " Relocating to GPU " ) ;
relocate ( AllocationStatus . HOST , AllocationStatus . DEVICE , dstPoint , dstPoint . getShape ( ) , context ) ; } else { // log . info ( " Buffer is actual on device side : " + dstPoint . getShape ( ) ) ;
} } // else log . info ( " Not on [ DEVICE ] " ) ;
// we update memory use counter , to announce that it ' s somehow used on device
dstPoint . tickDeviceRead ( ) ; // return pointer with offset if needed . length is specified for constructor compatibility purposes
CudaPointer p = new CudaPointer ( dstPoint . getPointers ( ) . getDevicePointer ( ) , buffer . length ( ) , ( buffer . offset ( ) * buffer . getElementSize ( ) ) ) ; switch ( buffer . dataType ( ) ) { case DOUBLE : return p . asDoublePointer ( ) ; case FLOAT : return p . asFloatPointer ( ) ; case INT : return p . asIntPointer ( ) ; case HALF : return p . asShortPointer ( ) ; case LONG : return p . asLongPointer ( ) ; default : return p ; }


signature:
public class JarResource { /** * Returns requested ClassPathResource as InputStream object
 * @ return File requested at constructor call
 * @ throws FileNotFoundException */
public InputStream getInputStream ( ) throws FileNotFoundException { } }

implementation: URL url = this . getUrl ( ) ; if ( isJarURL ( url ) ) { try { url = extractActualUrl ( url ) ; ZipFile zipFile = new ZipFile ( url . getFile ( ) ) ; ZipEntry entry = zipFile . getEntry ( this . resourceName ) ; InputStream stream = zipFile . getInputStream ( entry ) ; return stream ; } catch ( Exception e ) { throw new RuntimeException ( e ) ; } } else { File srcFile = this . getFile ( ) ; return new FileInputStream ( srcFile ) ; }


signature:
public class CudaAffinityManager { /** * This method returns deviceId for given thread , identified by threadId
 * If no device was assigned to this thread before this call , it ' ll be assinged here .
 * @ param threadId
 * @ return */
@ Override public Integer getDeviceForThread ( long threadId ) { } }

implementation:
if ( getNumberOfDevices ( ) == 1 ) return 0 ; Integer aff = affinityMap . get ( threadId ) ; if ( aff == null ) { Integer deviceId = getNextDevice ( threadId ) ; affinityMap . put ( threadId , deviceId ) ; affiliated . set ( new AtomicBoolean ( false ) ) ; if ( threadId == Thread . currentThread ( ) . getId ( ) ) { NativeOpsHolder . getInstance ( ) . getDeviceNativeOps ( ) . setDevice ( new CudaPointer ( deviceId ) ) ; // logger . error ( " setDevice ( { } ) called for thread { } " , deviceId , Thread . currentThread ( ) . getName ( ) ) ;
affiliated . get ( ) . set ( true ) ; } return deviceId ; } else { if ( threadId == Thread . currentThread ( ) . getId ( ) ) { if ( affiliated . get ( ) == null ) affiliated . set ( new AtomicBoolean ( false ) ) ; if ( ! affiliated . get ( ) . get ( ) ) { NativeOpsHolder . getInstance ( ) . getDeviceNativeOps ( ) . setDevice ( new CudaPointer ( aff ) ) ; // logger . error ( " SCARY setDevice ( { } ) called for thread { } " , aff , threadId ) ;
affiliated . get ( ) . set ( true ) ; return aff ; } } return aff ; } /* return affinityMap . get ( threadId ) ; */
// return 0;


signature:
public class CudaAffinityManager { /** * This method pairs specified thread & device
 * @ param threadId
 * @ param deviceId */
@ Override public void attachThreadToDevice ( long threadId , Integer deviceId ) { } }

implementation: List < Integer > devices = new ArrayList < > ( CudaEnvironment . getInstance ( ) . getConfiguration ( ) . getAvailableDevices ( ) ) ; logger . debug ( "Manually mapping thread [{}] to device [{}], out of [{}] devices..." , threadId , deviceId , devices . size ( ) ) ; affinityMap . put ( threadId , deviceId ) ;


signature:
public class CudaAffinityManager { /** * This method returns device id available . Round - robin balancing used here .
 * @ param threadId this parameter can be anything , it ' s used for logging only .
 * @ return */
protected Integer getNextDevice ( long threadId ) { } }

implementation:
Integer device = null ; if ( ! CudaEnvironment . getInstance ( ) . getConfiguration ( ) . isForcedSingleGPU ( ) && getNumberOfDevices ( ) > 0 ) { // simple round - robin here
synchronized ( this ) { device = CudaEnvironment . getInstance ( ) . getConfiguration ( ) . getAvailableDevices ( ) . get ( devPtr . getAndIncrement ( ) ) ; // We check only for number of entries here , not their actual values
if ( devPtr . get ( ) >= CudaEnvironment . getInstance ( ) . getConfiguration ( ) . getAvailableDevices ( ) . size ( ) ) devPtr . set ( 0 ) ; logger . debug ( "Mapping thread [{}] to device [{}], out of [{}] devices..." , threadId , device , CudaEnvironment . getInstance ( ) . getConfiguration ( ) . getAvailableDevices ( ) . size ( ) ) ; } } else { device = CudaEnvironment . getInstance ( ) . getConfiguration ( ) . getAvailableDevices ( ) . get ( 0 ) ; logger . debug ( "Single device is forced, mapping to device [{}]" , device ) ; } return device ;


signature:
public class LibUtils { /** * Loads the specified library . The full name of the library
 * is created by calling { @ link LibUtils # createLibName ( String ) }
 * with the given argument . The method will attempt to load
 * the library as a as a resource ( for usage within a JAR ) ,
 * and , if this fails , using the usual System . loadLibrary
 * call .
 * @ param baseName The base name of the library
 * @ throws UnsatisfiedLinkError if the native library
 * could not be loaded . */
public static void loadLibrary ( String baseName ) { } }

implementation: String libName = LibUtils . createLibName ( baseName ) ; Throwable throwable = null ; try { loadLibraryResource ( libName ) ; return ; } catch ( Throwable t ) { throwable = t ; } try { System . loadLibrary ( libName ) ; return ; } catch ( Throwable t ) { StringWriter sw = new StringWriter ( ) ; PrintWriter pw = new PrintWriter ( sw ) ; pw . println ( "Error while loading native library \"" + libName + "\" with base name \"" + baseName + "\"" ) ; pw . println ( "Operating system name: " + System . getProperty ( "os.name" ) ) ; pw . println ( "Architecture         : " + System . getProperty ( "os.arch" ) ) ; pw . println ( "Architecture bit size: " + System . getProperty ( "sun.arch.data.model" ) ) ; if ( throwable != null ) { pw . println ( "Stack trace from the attempt to " + "load the library as a resource:" ) ; throwable . printStackTrace ( pw ) ; } pw . println ( "Stack trace from the attempt to " + "load the library as a file:" ) ; t . printStackTrace ( pw ) ; pw . flush ( ) ; pw . close ( ) ; throw new UnsatisfiedLinkError ( "Could not load the native library.\n" + sw . toString ( ) ) ; }


signature:
public class LibUtils { /** * Load the library with the given name from a resource .
 * The extension for the current OS will be appended .
 * @ param libName The library name
 * @ throws Throwable If the library could not be loaded */
public static void loadTempBinaryFile ( String libName ) throws Exception { } }

implementation: String libPrefix = createLibPrefix ( ) ; String libExtension = createLibExtension ( ) ; String fullName = libPrefix + libName ; String resourceName = fullName + "." + libExtension ; ClassPathResource resource = new ClassPathResource ( resourceName ) ; InputStream inputStream = resource . getInputStream ( ) ; if ( inputStream == null ) { throw new NullPointerException ( "No resource found with name '" + resourceName + "'" ) ; } File tempFile = new File ( System . getProperty ( "java.io.tmpdir" ) , fullName + "." + libExtension ) ; tempFile . deleteOnExit ( ) ; OutputStream outputStream = null ; try { outputStream = new FileOutputStream ( tempFile ) ; byte [ ] buffer = new byte [ 8192 ] ; while ( true ) { int read = inputStream . read ( buffer ) ; if ( read < 0 ) { break ; } outputStream . write ( buffer , 0 , read ) ; } outputStream . flush ( ) ; outputStream . close ( ) ; outputStream = null ; System . load ( tempFile . getAbsolutePath ( ) ) ; } finally { if ( outputStream != null ) { outputStream . close ( ) ; } }


signature:
public class LibUtils { /** * Load the library with the given name from a resource .
 * The extension for the current OS will be appended .
 * @ param libName The library name
 * @ throws Throwable If the library could not be loaded */
public static void loadTempBinaryFile ( Class < ? > libName ) throws Exception { } }

implementation: String os = getOsName ( ) ; String arch = getArchName ( ) ; String resourceFolder = os + "-" + arch ; String libPrefix = createLibPrefix ( ) ; String libExtension = createLibExtension ( ) ; StringBuilder sb = new StringBuilder ( ) . append ( libName . getPackage ( ) . getName ( ) . replace ( "." , "/" ) + "/" ) . append ( resourceFolder ) . append ( "/" ) . append ( libPrefix ) . append ( "jni" + libName . getSimpleName ( ) + "." ) . append ( libExtension ) ; String resourceName = sb . toString ( ) ; ClassPathResource resource = new ClassPathResource ( resourceName ) ; InputStream inputStream = resource . getInputStream ( ) ; if ( inputStream == null ) { throw new NullPointerException ( "No resource found with name '" + resourceName + "'" ) ; } String fullName = libPrefix + "jni" + libName . getSimpleName ( ) + "." + libExtension ; File tempFile = new File ( System . getProperty ( "java.io.tmpdir" ) , fullName ) ; tempFile . deleteOnExit ( ) ; OutputStream outputStream = null ; try { outputStream = new FileOutputStream ( tempFile ) ; byte [ ] buffer = new byte [ 8192 ] ; while ( true ) { int read = inputStream . read ( buffer ) ; if ( read < 0 ) { break ; } outputStream . write ( buffer , 0 , read ) ; } outputStream . flush ( ) ; outputStream . close ( ) ; outputStream = null ; System . load ( tempFile . getAbsolutePath ( ) ) ; } finally { if ( outputStream != null ) { outputStream . close ( ) ; } }


signature:
public class LibUtils { /** * Load the library with the given name from a resource .
 * The extension for the current OS will be appended .
 * @ param libName The library name
 * @ throws Throwable If the library could not be loaded */
public static void loadJavaCppResource ( String libName ) throws Throwable { } }

implementation: String libPrefix = createLibPrefix ( ) ; String libExtension = createLibExtension ( ) ; String fullName = libPrefix + libName ; String resourceName = fullName + "." + libExtension ; ClassPathResource resource = new ClassPathResource ( resourceName ) ; InputStream inputStream = resource . getInputStream ( ) ; if ( inputStream == null ) { throw new NullPointerException ( "No resource found with name '" + resourceName + "'" ) ; } File tempFile = File . createTempFile ( fullName , "." + libExtension ) ; tempFile . deleteOnExit ( ) ; OutputStream outputStream = null ; try { outputStream = new FileOutputStream ( tempFile ) ; byte [ ] buffer = new byte [ 8192 ] ; while ( true ) { int read = inputStream . read ( buffer ) ; if ( read < 0 ) { break ; } outputStream . write ( buffer , 0 , read ) ; } outputStream . flush ( ) ; outputStream . close ( ) ; outputStream = null ; System . load ( tempFile . toString ( ) ) ; } finally { if ( outputStream != null ) { outputStream . close ( ) ; } }


signature:
public class LibUtils { /** * Get the name of the os
 * for libary discovery on the classpath
 * @ return */
public static String getOsName ( ) { } }

implementation: OSType osType = calculateOS ( ) ; switch ( osType ) { case APPLE : return "macosx" ; case LINUX : return "linux" ; case SUN : return "sun" ; case WINDOWS : return "windows" ; } return "" ;


signature:
public class LibUtils { /** * Creates the name for the native library with the given base
 * name for the current operating system and architecture .
 * The resulting name will be of the form < br / >
 * baseName - OSType - ARCHType < br / >
 * where OSType and ARCHType are the < strong > lower case < / strong > Strings
 * of the respective enum constants . Example : < br / >
 * jcuda - windows - x86 < br / >
 * @ param baseName The base name of the library
 * @ return The library name */
public static String createLibName ( String baseName ) { } }

implementation: OSType osType = calculateOS ( ) ; ARCHType archType = calculateArch ( ) ; String libName = baseName ; libName += "-" + osType . toString ( ) . toLowerCase ( Locale . ENGLISH ) ; libName += "-" + archType . toString ( ) . toLowerCase ( Locale . ENGLISH ) ; return libName ;