Input: if class is same as last referenced returned cached clzinfo else do a lookup [CODESPLIT] protected FSTClazzInfo getFstClazzInfo ( FSTClazzInfo . FSTFieldInfo referencee , Class clazz ) { FSTClazzInfo serializationInfo = null ; FSTClazzInfo lastInfo = referencee . lastInfo ; if ( lastInfo != null && lastInfo . getClazz ( ) == clazz && lastInfo . conf == conf ) { serializationInfo = lastInfo ; } else { serializationInfo = getClassInfoRegistry ( ) . getCLInfo ( clazz , conf ) ; referencee . lastInfo = serializationInfo ; } return serializationInfo ; }


Input: write identical to other version but take field values from hashmap ( because of annoying putField / getField feature ) [CODESPLIT] protected void writeCompatibleObjectFields ( Object toWrite , Map fields , FSTClazzInfo . FSTFieldInfo [ ] fieldInfo ) throws IOException { int booleanMask = 0 ; int boolcount = 0 ; for ( int i = 0 ; i < fieldInfo . length ; i ++ ) { try { FSTClazzInfo . FSTFieldInfo subInfo = fieldInfo [ i ] ; boolean isarr = subInfo . isArray ( ) ; Class subInfType = subInfo . getType ( ) ; if ( subInfType != boolean . class || isarr ) { if ( boolcount > 0 ) { getCodec ( ) . writeFByte ( booleanMask << ( 8 - boolcount ) ) ; boolcount = 0 ; booleanMask = 0 ; } } if ( subInfo . isIntegral ( ) && ! isarr ) { if ( subInfType == boolean . class ) { if ( boolcount == 8 ) { getCodec ( ) . writeFByte ( booleanMask << ( 8 - boolcount ) ) ; boolcount = 0 ; booleanMask = 0 ; } boolean booleanValue = ( ( Boolean ) fields . get ( subInfo . getName ( ) ) ) . booleanValue ( ) ; booleanMask = booleanMask << 1 ; booleanMask = ( booleanMask | ( booleanValue ? 1 : 0 ) ) ; boolcount ++ ; } else if ( subInfType == int . class ) { getCodec ( ) . writeFInt ( ( ( Number ) fields . get ( subInfo . getName ( ) ) ) . intValue ( ) ) ; } else if ( subInfType == long . class ) { getCodec ( ) . writeFLong ( ( ( Number ) fields . get ( subInfo . getName ( ) ) ) . longValue ( ) ) ; } else if ( subInfType == byte . class ) { getCodec ( ) . writeFByte ( ( ( Number ) fields . get ( subInfo . getName ( ) ) ) . byteValue ( ) ) ; } else if ( subInfType == char . class ) { getCodec ( ) . writeFChar ( ( char ) ( ( Number ) fields . get ( subInfo . getName ( ) ) ) . intValue ( ) ) ; } else if ( subInfType == short . class ) { getCodec ( ) . writeFShort ( ( ( Number ) fields . get ( subInfo . getName ( ) ) ) . shortValue ( ) ) ; } else if ( subInfType == float . class ) { getCodec ( ) . writeFFloat ( ( ( Number ) fields . get ( subInfo . getName ( ) ) ) . floatValue ( ) ) ; } else if ( subInfType == double . class ) { getCodec ( ) . writeFDouble ( ( ( Number ) fields . get ( subInfo . getName ( ) ) ) . doubleValue ( ) ) ; } } else { // object Object subObject = fields . get ( subInfo . getName ( ) ) ; writeObjectWithContext ( subInfo , subObject ) ; } } catch ( Exception ex ) { FSTUtil . < RuntimeException > rethrow ( ex ) ; } } if ( boolcount > 0 ) { getCodec ( ) . writeFByte ( booleanMask << ( 8 - boolcount ) ) ; } }


Input: incoming array is already registered [CODESPLIT] protected void writeArray ( FSTClazzInfo . FSTFieldInfo referencee , Object array ) throws IOException { if ( array == null ) { getCodec ( ) . writeClass ( Object . class ) ; getCodec ( ) . writeFInt ( - 1 ) ; return ; } final int len = Array . getLength ( array ) ; Class < ? > componentType = array . getClass ( ) . getComponentType ( ) ; getCodec ( ) . writeClass ( array . getClass ( ) ) ; getCodec ( ) . writeFInt ( len ) ; if ( ! componentType . isArray ( ) ) { if ( getCodec ( ) . isPrimitiveArray ( array , componentType ) ) { getCodec ( ) . writePrimitiveArray ( array , 0 , len ) ; } else { // objects Object arr [ ] = ( Object [ ] ) array ; Class lastClz = null ; FSTClazzInfo lastInfo = null ; for ( int i = 0 ; i < len ; i ++ ) { Object toWrite = arr [ i ] ; if ( toWrite != null ) { lastInfo = writeObjectWithContext ( referencee , toWrite , lastClz == toWrite . getClass ( ) ? lastInfo : null ) ; lastClz = toWrite . getClass ( ) ; } else writeObjectWithContext ( referencee , toWrite , null ) ; } } } else { // multidim array. FIXME shared refs to subarrays are not tested !!! Object [ ] arr = ( Object [ ] ) array ; FSTClazzInfo . FSTFieldInfo ref1 = new FSTClazzInfo . FSTFieldInfo ( referencee . getPossibleClasses ( ) , null , conf . getCLInfoRegistry ( ) . isIgnoreAnnotations ( ) ) ; for ( int i = 0 ; i < len ; i ++ ) { Object subArr = arr [ i ] ; boolean needsWrite = true ; if ( getCodec ( ) . isTagMultiDimSubArrays ( ) ) { if ( subArr == null ) { needsWrite = ! getCodec ( ) . writeTag ( NULL , null , 0 , null , this ) ; } else { needsWrite = ! getCodec ( ) . writeTag ( ARRAY , subArr , 0 , subArr , this ) ; } } if ( needsWrite ) { writeArray ( ref1 , subArr ) ; getCodec ( ) . writeArrayEnd ( ) ; } } } }


Input: if out == null = > automatically create / reuse a bytebuffer [CODESPLIT] public void resetForReUse ( OutputStream out ) { if ( closed ) throw new RuntimeException ( "Can't reuse closed stream" ) ; getCodec ( ) . reset ( null ) ; if ( out != null ) { getCodec ( ) . setOutstream ( out ) ; } objects . clearForWrite ( conf ) ; }


Input: @return a copy of written bytes . Warning : if the stream has been flushed this will fail with an exception . a flush is triggered after each 1st level writeObject . [CODESPLIT] public byte [ ] getCopyOfWrittenBuffer ( ) { if ( ! getCodec ( ) . isByteArrayBased ( ) ) { return getBuffer ( ) ; } byte res [ ] = new byte [ getCodec ( ) . getWritten ( ) ] ; byte [ ] buffer = getBuffer ( ) ; System . arraycopy ( buffer , 0 , res , 0 , getCodec ( ) . getWritten ( ) ) ; return res ; }


Input: some jdk classes hash for ObjectStream so provide the same instance always [CODESPLIT] protected ObjectInputStream getObjectInputStream ( final Class cl , final FSTClazzInfo clInfo , final FSTClazzInfo . FSTFieldInfo referencee , final Object toRead ) throws IOException { ObjectInputStream wrapped = new ObjectInputStream ( ) { @ Override public Object readObjectOverride ( ) throws IOException , ClassNotFoundException { try { byte b = FSTObjectInput . this . readByte ( ) ; if ( b != FSTObjectOutput . SPECIAL_COMPATIBILITY_OBJECT_TAG ) { Constructor < ? > [ ] constructors = OptionalDataException . class . getDeclaredConstructors ( ) ; FSTObjectInput . this . pushBack ( 1 ) ; for ( int i = 0 ; i < constructors . length ; i ++ ) { Constructor constructor = constructors [ i ] ; Class [ ] typeParameters = constructor . getParameterTypes ( ) ; if ( typeParameters != null && typeParameters . length == 1 && typeParameters [ 0 ] == int . class ) { constructor . setAccessible ( true ) ; OptionalDataException ode ; try { ode = ( OptionalDataException ) constructor . newInstance ( 0 ) ; throw ode ; } catch ( InvocationTargetException e ) { break ; } } } throw new EOFException ( "if your code relies on this, think" ) ; } return FSTObjectInput . this . readObjectInternal ( referencee . getPossibleClasses ( ) ) ; } catch ( IllegalAccessException e ) { throw new IOException ( e ) ; } catch ( InstantiationException e ) { throw new IOException ( e ) ; } } @ Override public Object readUnshared ( ) throws IOException , ClassNotFoundException { try { return FSTObjectInput . this . readObjectInternal ( referencee . getPossibleClasses ( ) ) ; // fixme } catch ( IllegalAccessException e ) { throw new IOException ( e ) ; } catch ( InstantiationException e ) { throw new IOException ( e ) ; } } @ Override public void defaultReadObject ( ) throws IOException , ClassNotFoundException { try { int tag = readByte ( ) ; if ( tag == 77 ) // came from writeFields { fieldMap = ( HashMap < String , Object > ) FSTObjectInput . this . readObjectInternal ( HashMap . class ) ; // object has been written with writeFields, is no read with defaultReadObjects, // need to autoapply map to object vars. // this might be redundant in case readObject() pulls a getFields() .. (see bitset testcase) for ( Iterator < String > iterator = fieldMap . keySet ( ) . iterator ( ) ; iterator . hasNext ( ) ; ) { String key = iterator . next ( ) ; FSTClazzInfo . FSTFieldInfo fieldInfo = clInfo . getFieldInfo ( key , null ) ; // in case fieldName is not unique => cannot recover/fix if ( fieldInfo != null ) { fieldInfo . setObjectValue ( toRead , fieldMap . get ( key ) ) ; } } } else { FSTObjectInput . this . readObjectFields ( referencee , clInfo , clInfo . getCompInfo ( ) . get ( cl ) . getFieldArray ( ) , toRead , 0 , 0 ) ; // FIXME: only fields of current class } } catch ( Exception e ) { throw new IOException ( e ) ; } } HashMap < String , Object > fieldMap ; @ Override public GetField readFields ( ) throws IOException , ClassNotFoundException { int tag = readByte ( ) ; try { FSTClazzInfo . FSTCompatibilityInfo fstCompatibilityInfo = clInfo . getCompInfo ( ) . get ( cl ) ; if ( tag == 99 ) { // came from defaultwriteobject // Note: in case number and names of instance fields of reader/writer are different, // this fails as code below implicitely assumes, fields of writer == fields of reader // unfortunately one can use defaultWriteObject at writer side but use getFields at reader side // in readObject(). if then fields differ, code below reads BS and fails. // Its impossible to fix that except by always using putField + getField for // JDK compatibility classes, however this will waste lots of performance. As // it would be necessary to *always* write full metainformation (a map of fieldName => value pairs) // see #53 fieldMap = new HashMap < String , Object > ( ) ; FSTObjectInput . this . readCompatibleObjectFields ( referencee , clInfo , fstCompatibilityInfo . getFieldArray ( ) , fieldMap ) ; getCodec ( ) . readVersionTag ( ) ; // consume dummy version tag as created by defaultWriteObject } else if ( tag == 66 ) { // has been written from writeObjectCompatible without writeMethod fieldMap = new HashMap < String , Object > ( ) ; FSTObjectInput . this . readCompatibleObjectFields ( referencee , clInfo , fstCompatibilityInfo . getFieldArray ( ) , fieldMap ) ; getCodec ( ) . readVersionTag ( ) ; // consume dummy version tag as created by defaultWriteObject } else { fieldMap = ( HashMap < String , Object > ) FSTObjectInput . this . readObjectInternal ( HashMap . class ) ; } } catch ( Exception e ) { FSTUtil . < RuntimeException > rethrow ( e ) ; } return new GetField ( ) { @ Override public ObjectStreamClass getObjectStreamClass ( ) { return ObjectStreamClass . lookup ( cl ) ; } @ Override public boolean defaulted ( String name ) throws IOException { return fieldMap . get ( name ) == null ; } @ Override public boolean get ( String name , boolean val ) throws IOException { if ( fieldMap . get ( name ) == null ) { return val ; } return ( ( Boolean ) fieldMap . get ( name ) ) . booleanValue ( ) ; } @ Override public byte get ( String name , byte val ) throws IOException { if ( fieldMap . get ( name ) == null ) { return val ; } return ( ( Byte ) fieldMap . get ( name ) ) . byteValue ( ) ; } @ Override public char get ( String name , char val ) throws IOException { if ( fieldMap . get ( name ) == null ) { return val ; } return ( ( Character ) fieldMap . get ( name ) ) . charValue ( ) ; } @ Override public short get ( String name , short val ) throws IOException { if ( fieldMap . get ( name ) == null ) { return val ; } return ( ( Short ) fieldMap . get ( name ) ) . shortValue ( ) ; } @ Override public int get ( String name , int val ) throws IOException { if ( fieldMap . get ( name ) == null ) { return val ; } return ( ( Integer ) fieldMap . get ( name ) ) . intValue ( ) ; } @ Override public long get ( String name , long val ) throws IOException { if ( fieldMap . get ( name ) == null ) { return val ; } return ( ( Long ) fieldMap . get ( name ) ) . longValue ( ) ; } @ Override public float get ( String name , float val ) throws IOException { if ( fieldMap . get ( name ) == null ) { return val ; } return ( ( Float ) fieldMap . get ( name ) ) . floatValue ( ) ; } @ Override public double get ( String name , double val ) throws IOException { if ( fieldMap . get ( name ) == null ) { return val ; } return ( ( Double ) fieldMap . get ( name ) ) . doubleValue ( ) ; } @ Override public Object get ( String name , Object val ) throws IOException { Object res = fieldMap . get ( name ) ; if ( res == null ) { return val ; } return res ; } } ; } @ Override public void registerValidation ( ObjectInputValidation obj , int prio ) throws NotActiveException , InvalidObjectException { if ( callbacks == null ) { callbacks = new ArrayList < CallbackEntry > ( ) ; } callbacks . add ( new CallbackEntry ( obj , prio ) ) ; } @ Override public int read ( ) throws IOException { return getCodec ( ) . readFByte ( ) ; } @ Override public int read ( byte [ ] buf , int off , int len ) throws IOException { return FSTObjectInput . this . read ( buf , off , len ) ; } @ Override public int available ( ) throws IOException { return FSTObjectInput . this . available ( ) ; } @ Override public void close ( ) throws IOException { } @ Override public boolean readBoolean ( ) throws IOException { return FSTObjectInput . this . readBoolean ( ) ; } @ Override public byte readByte ( ) throws IOException { return getCodec ( ) . readFByte ( ) ; } @ Override public int readUnsignedByte ( ) throws IOException { return FSTObjectInput . this . readUnsignedByte ( ) ; } @ Override public char readChar ( ) throws IOException { return getCodec ( ) . readFChar ( ) ; } @ Override public short readShort ( ) throws IOException { return getCodec ( ) . readFShort ( ) ; } @ Override public int readUnsignedShort ( ) throws IOException { return FSTObjectInput . this . readUnsignedShort ( ) ; } @ Override public int readInt ( ) throws IOException { return getCodec ( ) . readFInt ( ) ; } @ Override public long readLong ( ) throws IOException { return getCodec ( ) . readFLong ( ) ; } @ Override public float readFloat ( ) throws IOException { return getCodec ( ) . readFFloat ( ) ; } @ Override public double readDouble ( ) throws IOException { return getCodec ( ) . readFDouble ( ) ; } @ Override public void readFully ( byte [ ] buf ) throws IOException { FSTObjectInput . this . readFully ( buf ) ; } @ Override public void readFully ( byte [ ] buf , int off , int len ) throws IOException { FSTObjectInput . this . readFully ( buf , off , len ) ; } @ Override public int skipBytes ( int len ) throws IOException { return FSTObjectInput . this . skipBytes ( len ) ; } @ Override public String readUTF ( ) throws IOException { return getCodec ( ) . readStringUTF ( ) ; } @ Override public String readLine ( ) throws IOException { return FSTObjectInput . this . readLine ( ) ; } @ Override public int read ( byte [ ] b ) throws IOException { return FSTObjectInput . this . read ( b ) ; } @ Override public long skip ( long n ) throws IOException { return FSTObjectInput . this . skip ( n ) ; } @ Override public void mark ( int readlimit ) { throw new RuntimeException ( "not implemented" ) ; } @ Override public void reset ( ) throws IOException { FSTObjectInput . this . reset ( ) ; } @ Override public boolean markSupported ( ) { return false ; } } ; if ( fakeWrapper == null ) { fakeWrapper = new MyObjectStream ( ) ; } fakeWrapper . push ( wrapped ) ; return fakeWrapper ; }


Input: modify content of this StructString . The length of the new String must not exceed the length of internal char array [CODESPLIT] public void setString ( String s ) { if ( s == null ) { setLen ( 0 ) ; return ; } if ( s . length ( ) > charsLen ( ) ) { throw new RuntimeException ( "String length exceeds buffer size. String len " + s . length ( ) + " charsLen:" + charsLen ( ) ) ; } for ( int i = 0 ; i < s . length ( ) ; i ++ ) { chars ( i , s . charAt ( i ) ) ; } len = s . length ( ) ; }


Input: does not write class tag and length [CODESPLIT] @ Override public void writePrimitiveArray ( Object array , int start , int length ) throws IOException { out . writeArray ( array , start , length ) ; }


Input: resets stream ( positions are lost ) [CODESPLIT] @ Override public void flush ( ) throws IOException { if ( outputStream != null ) { outputStream . write ( out . getBytez ( ) , 0 , out . getWritten ( ) ) ; offset = out . getWritten ( ) ; out . resetPosition ( ) ; } }


Input: set this struct pointer to base array at given offset ( = bufoff + index ) [CODESPLIT] public void baseOn ( byte base [ ] , long offset , FSTStructFactory fac ) { ___bytes = new HeapBytez ( base ) ; ___offset = offset ; ___fac = fac ; }


Input: set this struct pointer to base array at given index [CODESPLIT] public void baseOn ( byte base [ ] , int index ) { ___bytes = new HeapBytez ( base ) ; ___offset = index ; if ( ___fac == null ) ___fac = FSTStructFactory . getInstance ( ) ; }


Input: move offsets and memorybase to given pointer and return the pointer . Eases dereferencing of nested structs without object creation . If the given pointer is null a new one will be created ( alloc ) <pre > MyStruct st = FSTStructFactory . getInstance () . createEmptyStructPointer ( FSTStruct . class ) ; otherStruct . getEmbeddedMyStruct () . to ( st ) ; < / pre > [CODESPLIT] public < T extends FSTStruct > T detachTo ( T pointer ) { if ( pointer == null ) { return detach ( ) ; } if ( isOffHeap ( ) ) { pointer . ___fac = ___fac ; pointer . ___bytes = ___bytes ; pointer . ___elementSize = ___elementSize ; pointer . ___offset = ___offset ; return pointer ; } return ( T ) this ; }


Input: returns a complete copy of this object allocating a new Bytez capable of holding the data . [CODESPLIT] public FSTStruct createCopy ( ) { if ( ! isOffHeap ( ) ) { throw new RuntimeException ( "must be offheap to call this" ) ; } byte b [ ] = new byte [ getByteSize ( ) ] ; HeapBytez res = new HeapBytez ( b ) ; getBytes ( res , 0 ) ; return ___fac . createStructWrapper ( res , 0 ) ; }


Input: works only if change tracking is enabled [CODESPLIT] public FSTStructChange finishChangeTracking ( ) { tracker . snapshotChanges ( ( int ) getOffset ( ) , getBase ( ) ) ; FSTStructChange res = tracker ; tracker = null ; return res ; }


Input: @return a temporary per thread instance of ByteBuffer pointing to this structs data . The length of the buffer is same as the length of this struct . [CODESPLIT] public ByteBuffer asByteBufferTemporary ( ) { if ( getBase ( ) instanceof MallocBytez ) { MallocBytez base = ( MallocBytez ) getBase ( ) ; ByteBuffer bb = tmpBuf . get ( ) ; try { address . setLong ( bb , base . getBaseAdress ( ) + getOffset ( ) ) ; capacity . setInt ( bb , getByteSize ( ) ) ; } catch ( IllegalAccessException e ) { FSTUtil . < RuntimeException > rethrow ( e ) ; } bb . limit ( ( int ) ( getOffset ( ) + getByteSize ( ) ) ) ; bb . position ( ( int ) getOffset ( ) ) ; return tmpBuf . get ( ) ; } else { // assume HeapBytez. Allocates return ByteBuffer . wrap ( getBase ( ) . asByteArray ( ) , ( int ) getOffset ( ) , getByteSize ( ) ) ; } }


Input: create a new struct array of same type as template [CODESPLIT] public < X extends FSTStruct > StructArray < X > newArray ( int size , X templ ) { return newArray ( size , templ , alloc ) ; }


Input: create a new struct array of same type as template [CODESPLIT] public < X extends FSTStruct > StructArray < X > newArray ( int size , X templ , BytezAllocator alloc ) { StructArray < X > aTemplate = new StructArray < X > ( size , templ ) ; int siz = getFactory ( ) . calcStructSize ( aTemplate ) ; try { if ( siz < chunkSize ) return newStruct ( aTemplate ) ; else { return getFactory ( ) . toStruct ( aTemplate , alloc ) ; } } catch ( Throwable e ) { throw new RuntimeException ( e ) ; } }


Input: collects all changes and rebases . [CODESPLIT] public void snapshotChanges ( int originBase , Bytez origin ) { int sumLen = 0 ; for ( int i = 0 ; i < curIndex ; i ++ ) { sumLen += changeLength [ i ] ; } snapshot = new byte [ sumLen ] ; int targetIdx = 0 ; for ( int i = 0 ; i < curIndex ; i ++ ) { int changeOffset = changeOffsets [ i ] ; int len = changeLength [ i ] ; for ( int ii = 0 ; ii < len ; ii ++ ) { snapshot [ targetIdx ++ ] = origin . get ( changeOffset + ii ) ; } } rebase ( originBase ) ; }


Input: add an object to the register return handle if already present . Called during write only [CODESPLIT] public int registerObjectForWrite ( Object o , int streamPosition , FSTClazzInfo clzInfo , int reUseType [ ] ) { if ( disabled ) { return Integer . MIN_VALUE ; } //        System.out.println("REGISTER AT WRITE:"+streamPosition+" "+o.getClass().getSimpleName()); //        final Class clazz = o.getClass(); if ( clzInfo == null ) { // array oder enum oder primitive // unused ? //            clzInfo = reg.getCLInfo(clazz); } else if ( clzInfo . isFlat ( ) ) { return Integer . MIN_VALUE ; } int handle = objects . putOrGet ( o , streamPosition ) ; if ( handle >= 0 ) { //            if ( idToObject.get(handle) == null ) { // (*) (can get improved) //                idToObject.add(handle, o); //            } reUseType [ 0 ] = 0 ; return handle ; } return Integer . MIN_VALUE ; }


Input: Warning : Concurrent Modification ( e . g . add remove elements during iteration ) is NOT SUPPORTED and NOT CHECKED . Collect keys to change inside an iteration and perform changes on the map after iteration is finished . [CODESPLIT] public Iterator < V > values ( ) { final Iterator < ByteSource > iter = binaryValues ( ) ; return new Iterator ( ) { @ Override public boolean hasNext ( ) { return iter . hasNext ( ) ; } @ Override public V next ( ) { BytezByteSource next = ( BytezByteSource ) iter . next ( ) ; return decodeValue ( next ) ; } @ Override public void remove ( ) { throw new RuntimeException ( "unimplemented" ) ; } } ; }


Input: throws FSTBufferTooSmallExcpetion in case object does not fit into given range Zero Copy method [CODESPLIT] @ Override public int toByteArray ( Object o , byte arr [ ] , int startIndex , int availableSize ) { out . resetForReUse ( ) ; writeTarget . setBase ( arr , startIndex , availableSize ) ; try { out . writeObject ( o ) ; } catch ( IOException e ) { FSTUtil . < RuntimeException > rethrow ( e ) ; } int written = out . getWritten ( ) ; return written ; }


Input: throws FSTBufferTooSmallExcpetion in case object does not fit into given range [CODESPLIT] @ Override public Object toObject ( byte arr [ ] , int startIndex , int availableSize ) { try { in . resetForReuse ( null ) ; readTarget . setBase ( arr , startIndex , availableSize ) ; Object o = in . readObject ( ) ; return o ; } catch ( Exception e ) { FSTUtil . < RuntimeException > rethrow ( e ) ; } return null ; }


Input: map given Object to a target type . ( needs support in coerceWriting also ) Note one could add a pluggable Serializer / Coercer pattern here if required . Skipped for now for simplicity . [CODESPLIT] public Object coerceReading ( Class type , Object readObject ) { if ( type == null ) return readObject ; // make hashmaps from arrays. warning: for optimal performance, use direct arrays[] only in your serialized classes if ( Map . class . isAssignableFrom ( type ) && readObject . getClass ( ) . isArray ( ) ) { try { Map c = ( Map ) type . newInstance ( ) ; int len = Array . getLength ( readObject ) ; for ( int i = 0 ; i < len ; i += 2 ) { c . put ( Array . get ( readObject , i ) , Array . get ( readObject , i + 1 ) ) ; } return c ; } catch ( Exception e ) { logger . log ( FSTLogger . Level . INFO , "Exception thrown by newInstance" , e ) ; } } else // make collections from arrays. warning: for optimal performance, use direct arrays[] only in your serialized classes if ( Collection . class . isAssignableFrom ( type ) && readObject . getClass ( ) . isArray ( ) ) { try { if ( type . isInterface ( ) ) { if ( List . class . isAssignableFrom ( type ) ) { type = ArrayList . class ; } else if ( Map . class . isAssignableFrom ( type ) ) { type = HashMap . class ; } } Collection c = ( Collection ) type . newInstance ( ) ; int len = Array . getLength ( readObject ) ; for ( int i = 0 ; i < len ; i ++ ) { c . add ( Array . get ( readObject , i ) ) ; } return c ; } catch ( Exception e ) { logger . log ( FSTLogger . Level . ERROR , "Exception thrown by newInstance" , e ) ; } } else if ( Date . class . isAssignableFrom ( type ) && readObject instanceof String ) { try { return dateTimeInstance . parse ( ( String ) readObject ) ; } catch ( ParseException pe ) { logger . log ( FSTLogger . Level . ERROR , "Failed to parse date" , pe ) ; } } else if ( ( type == char . class || Character . class . isAssignableFrom ( type ) ) && readObject instanceof String ) { return ( ( String ) readObject ) . charAt ( 0 ) ; } return readObject ; }


Input: add bytes to the queue . Again by using ( reusable ) Wrapper classes any kind of memory ( offheap byte arrays nio bytebuffer memory mapped ) can be added . [CODESPLIT] public void add ( ByteSource source , long sourceoff , long sourcelen ) { if ( sourcelen >= remaining ( ) ) { grow ( sourcelen + 1 ) ; //issue85 add ( source , sourceoff , sourcelen ) ; return ; } for ( int i = 0 ; i < sourcelen ; i ++ ) { storage . put ( addIndex ++ , source . get ( i + sourceoff ) ) ; if ( addIndex >= storage . length ( ) ) addIndex -= storage . length ( ) ; } }


Input: read up to destlen bytes ( if available ) . Note you can use HeapBytez ( implements ByteSink ) in order to read to a regular byte array . HeapBytez wrapper can be reused to avoid unnecessary allocation . Also possible is ByteBufferBasicBytes to read into a ByteBuffer and MallocBytes or MMFBytes to read into Unsafe alloc ed off heap memory or persistent mem mapped memory regions . [CODESPLIT] public long poll ( ByteSink destination , long destoff , long destlen ) { long count = 0 ; try { while ( pollIndex != addIndex && count < destlen ) { destination . put ( destoff + count ++ , storage . get ( pollIndex ++ ) ) ; if ( pollIndex >= storage . length ( ) ) { pollIndex = 0 ; } } } catch ( Exception e ) { logger . log ( FSTLogger . Level . ERROR , "Failed to poll" , e ) ; } return count ; }


Input: convenience method to read len byte array . Throws an excpetion if not enough data is present [CODESPLIT] public byte [ ] readByteArray ( int len ) { if ( available ( ) < len ) { throw new RuntimeException ( "not enough data available, check available() > len before calling" ) ; } byte b [ ] = new byte [ len ] ; int count = 0 ; while ( pollIndex != addIndex && count < len ) { b [ count ++ ] = storage . get ( pollIndex ++ ) ; if ( pollIndex >= storage . length ( ) ) { pollIndex = 0 ; } } return b ; }


Input: read an int . throws an exception if not enough data is present [CODESPLIT] public int readInt ( ) { if ( available ( ) < 4 ) { throw new RuntimeException ( "not enough data available, check available() > 4 before calling" ) ; } int ch1 = poll ( ) ; int ch2 = poll ( ) ; int ch3 = poll ( ) ; int ch4 = poll ( ) ; return ( ch4 << 24 ) + ( ch3 << 16 ) + ( ch2 << 8 ) + ( ch1 << 0 ) ; }


Input: unread len bytes [CODESPLIT] public void back ( int len ) { if ( pollIndex >= len ) pollIndex -= len ; else pollIndex = pollIndex + capacity ( ) - len ; }


Input: for read = > always increase handle ( wg . replaceObject ) [CODESPLIT] public void registerClass ( Class c , FSTConfiguration conf ) { if ( getIdFromClazz ( c ) != Integer . MIN_VALUE ) { return ; } registerClassNoLookup ( c , null , conf ) ; }


Input: Set thread pool enabled . This thread pool is not for the service threads it is for the user service method . If your service method takes a long time or will be blocked please set this property to be true . [CODESPLIT] public void setThreadPoolEnabled ( boolean value ) { if ( value && ( threadPool == null ) ) { threadPool = Executors . newCachedThreadPool ( ) ; } threadPoolEnabled = value ; }


Input: Set embedded cassandra up and spawn it in a new thread . [CODESPLIT] public static void startEmbeddedCassandra ( File file , String tmpDir , long timeout ) throws IOException , ConfigurationException { if ( cassandraDaemon != null ) { /* nothing to do Cassandra is already started */ return ; } checkConfigNameForRestart ( file . getAbsolutePath ( ) ) ; log . debug ( "Starting cassandra..." ) ; log . debug ( "Initialization needed" ) ; System . setProperty ( "cassandra.config" , "file:" + file . getAbsolutePath ( ) ) ; System . setProperty ( "cassandra-foreground" , "true" ) ; System . setProperty ( "cassandra.native.epoll.enabled" , "false" ) ; // JNA doesnt cope with relocated netty System . setProperty ( "cassandra.unsafesystem" , "true" ) ; // disable fsync for a massive speedup on old platters // If there is no log4j config set already, set the default config if ( System . getProperty ( "log4j.configuration" ) == null ) { copy ( DEFAULT_LOG4J_CONFIG_FILE , tmpDir ) ; System . setProperty ( "log4j.configuration" , "file:" + tmpDir + DEFAULT_LOG4J_CONFIG_FILE ) ; } DatabaseDescriptor . daemonInitialization ( ) ; cleanupAndLeaveDirs ( ) ; final CountDownLatch startupLatch = new CountDownLatch ( 1 ) ; ExecutorService executor = Executors . newSingleThreadExecutor ( ) ; executor . execute ( ( ) -> { cassandraDaemon = new CassandraDaemon ( ) ; cassandraDaemon . activate ( ) ; startupLatch . countDown ( ) ; } ) ; try { if ( ! startupLatch . await ( timeout , MILLISECONDS ) ) { log . error ( "Cassandra daemon did not start after " + timeout + " ms. Consider increasing the timeout" ) ; throw new AssertionError ( "Cassandra daemon did not start within timeout" ) ; } Runtime . getRuntime ( ) . addShutdownHook ( new Thread ( ( ) -> { if ( session != null ) session . close ( ) ; if ( cluster != null ) cluster . close ( ) ; } ) ) ; } catch ( InterruptedException e ) { log . error ( "Interrupted waiting for Cassandra daemon to start:" , e ) ; throw new AssertionError ( e ) ; } finally { executor . shutdown ( ) ; } }


Input: truncate data in keyspace except specified tables [CODESPLIT] public static void cleanDataEmbeddedCassandra ( String keyspace , String ... excludedTables ) { if ( session != null ) { cleanDataWithNativeDriver ( keyspace , excludedTables ) ; } }


Input: Copies a resource from within the jar to a directory . [CODESPLIT] private static Path copy ( String resource , String directory ) throws IOException { mkdir ( directory ) ; String fileName = resource . substring ( resource . lastIndexOf ( "/" ) + 1 ) ; InputStream from = EmbeddedCassandraServerHelper . class . getResourceAsStream ( resource ) ; Path copyName = Paths . get ( directory , fileName ) ; Files . copy ( from , copyName ) ; return copyName ; }


Input: Print all of the thread s information and stack traces . [CODESPLIT] public static void printThreadInfo ( PrintWriter stream , String title ) { final int STACK_DEPTH = 20 ; boolean contention = threadBean . isThreadContentionMonitoringEnabled ( ) ; long [ ] threadIds = threadBean . getAllThreadIds ( ) ; stream . println ( "Process Thread Dump: " + title ) ; stream . println ( threadIds . length + " active threads" ) ; for ( long tid : threadIds ) { ThreadInfo info = threadBean . getThreadInfo ( tid , STACK_DEPTH ) ; if ( info == null ) { stream . println ( "  Inactive" ) ; continue ; } stream . println ( "Thread " + getTaskName ( info . getThreadId ( ) , info . getThreadName ( ) ) + ":" ) ; Thread . State state = info . getThreadState ( ) ; stream . println ( "  State: " + state ) ; stream . println ( "  Blocked count: " + info . getBlockedCount ( ) ) ; stream . println ( "  Waited count: " + info . getWaitedCount ( ) ) ; if ( contention ) { stream . println ( "  Blocked time: " + info . getBlockedTime ( ) ) ; stream . println ( "  Waited time: " + info . getWaitedTime ( ) ) ; } if ( state == Thread . State . WAITING ) { stream . println ( "  Waiting on " + info . getLockName ( ) ) ; } else if ( state == Thread . State . BLOCKED ) { stream . println ( "  Blocked on " + info . getLockName ( ) ) ; stream . println ( "  Blocked by " + getTaskName ( info . getLockOwnerId ( ) , info . getLockOwnerName ( ) ) ) ; } stream . println ( "  Stack:" ) ; for ( StackTraceElement frame : info . getStackTrace ( ) ) { stream . println ( "    " + frame . toString ( ) ) ; } } stream . flush ( ) ; }


Input: Return the correctly - typed { @link Class } of the given object . [CODESPLIT] @ SuppressWarnings ( "unchecked" ) public static < T > Class < T > getClass ( T o ) { return ( Class < T > ) o . getClass ( ) ; }


Input: Check if a remote port is taken [CODESPLIT] public static boolean remotePortTaken ( String node , int port , int timeout ) { Socket s = null ; try { s = new Socket ( ) ; s . setReuseAddress ( true ) ; SocketAddress sa = new InetSocketAddress ( node , port ) ; s . connect ( sa , timeout * 1000 ) ; } catch ( IOException e ) { if ( e . getMessage ( ) . equals ( "Connection refused" ) ) { return false ; } if ( e instanceof SocketTimeoutException || e instanceof UnknownHostException ) { throw e ; } } finally { if ( s != null ) { if ( s . isConnected ( ) ) { return true ; } else { } try { s . close ( ) ; } catch ( IOException e ) { } } return false ; } }


Input: Returns an empty subscriber state with - 1 as total updates master as false and server state as empty [CODESPLIT] public static SubscriberState empty ( ) { return SubscriberState . builder ( ) . serverState ( "empty" ) . streamId ( - 1 ) . parameterUpdaterStatus ( Collections . emptyMap ( ) ) . totalUpdates ( - 1 ) . isMaster ( false ) . build ( ) ; }


Input: gemm performs a matrix - matrix operation c : = alpha * op ( a ) * op ( b ) + beta * c where c is an m - by - n matrix op ( a ) is an m - by - k matrix op ( b ) is a k - by - n matrix . [CODESPLIT] @ Override public void gemm ( char Order , char TransA , char TransB , double alpha , INDArray A , INDArray B , double beta , INDArray C ) { if ( Nd4j . getExecutioner ( ) . getProfilingMode ( ) == OpExecutioner . ProfilingMode . ALL ) OpProfiler . getInstance ( ) . processBlasCall ( true , A , B , C ) ; GemmParams params = new GemmParams ( A , B , C ) ; int charOder = Order ; if ( A . data ( ) . dataType ( ) == DataBuffer . Type . DOUBLE ) { DefaultOpExecutioner . validateDataType ( DataBuffer . Type . DOUBLE , params . getA ( ) , params . getB ( ) , params . getC ( ) ) ; dgemm ( Order , params . getTransA ( ) , params . getTransB ( ) , params . getM ( ) , params . getN ( ) , params . getK ( ) , 1.0 , params . getA ( ) , params . getLda ( ) , params . getB ( ) , params . getLdb ( ) , 0 , C , params . getLdc ( ) ) ; } else if ( A . data ( ) . dataType ( ) == DataBuffer . Type . FLOAT ) { DefaultOpExecutioner . validateDataType ( DataBuffer . Type . FLOAT , params . getA ( ) , params . getB ( ) , params . getC ( ) ) ; sgemm ( Order , params . getTransA ( ) , params . getTransB ( ) , params . getM ( ) , params . getN ( ) , params . getK ( ) , 1.0f , params . getA ( ) , params . getLda ( ) , params . getB ( ) , params . getLdb ( ) , 0 , C , params . getLdc ( ) ) ; } else { DefaultOpExecutioner . validateDataType ( DataBuffer . Type . HALF , params . getA ( ) , params . getB ( ) , params . getC ( ) ) ; hgemm ( Order , params . getTransA ( ) , params . getTransB ( ) , params . getM ( ) , params . getN ( ) , params . getK ( ) , 1.0f , params . getA ( ) , params . getLda ( ) , params . getB ( ) , params . getLdb ( ) , 0 , C , params . getLdc ( ) ) ; } OpExecutionerUtil . checkForAny ( C ) ; }


Input: her2k performs a rank - 2k update of an n - by - n Hermitian matrix c that is one of the following operations : c : = alpha * a * conjg ( b ) + conjg ( alpha ) * b * conjg ( a ) + beta * c for trans = N or n c : = alpha * conjg ( b ) * a + conjg ( alpha ) * conjg ( a ) * b + beta * c for trans = C or c where c is an n - by - n Hermitian matrix ; a and b are n - by - k matrices if trans = N or n a and b are k - by - n matrices if trans = C or c . [CODESPLIT] @ Override public void symm ( char Order , char Side , char Uplo , double alpha , INDArray A , INDArray B , double beta , INDArray C ) { if ( Nd4j . getExecutioner ( ) . getProfilingMode ( ) == OpExecutioner . ProfilingMode . ALL ) OpProfiler . getInstance ( ) . processBlasCall ( false , A , B , C ) ; // FIXME: int cast if ( A . data ( ) . dataType ( ) == DataBuffer . Type . DOUBLE ) { DefaultOpExecutioner . validateDataType ( DataBuffer . Type . DOUBLE , A , B , C ) ; dsymm ( Order , Side , Uplo , ( int ) C . rows ( ) , ( int ) C . columns ( ) , alpha , A , ( int ) A . size ( 0 ) , B , ( int ) B . size ( 0 ) , beta , C , ( int ) C . size ( 0 ) ) ; } else { DefaultOpExecutioner . validateDataType ( DataBuffer . Type . FLOAT , A , B , C ) ; ssymm ( Order , Side , Uplo , ( int ) C . rows ( ) , ( int ) C . columns ( ) , ( float ) alpha , A , ( int ) A . size ( 0 ) , B , ( int ) B . size ( 0 ) , ( float ) beta , C , ( int ) C . size ( 0 ) ) ; } OpExecutionerUtil . checkForAny ( C ) ; }


Input: syrk performs a rank - n update of an n - by - n symmetric matrix c that is one of the following operations : c : = alpha * a * a + beta * c for trans = N or n c : = alpha * a * a + beta * c for trans = T or t C or c where c is an n - by - n symmetric matrix ; a is an n - by - k matrix if trans = N or n a is a k - by - n matrix if trans = T or t C or c . [CODESPLIT] @ Override public void syrk ( char Order , char Uplo , char Trans , double alpha , INDArray A , double beta , INDArray C ) { if ( Nd4j . getExecutioner ( ) . getProfilingMode ( ) == OpExecutioner . ProfilingMode . ALL ) OpProfiler . getInstance ( ) . processBlasCall ( false , A , C ) ; // FIXME: int cast if ( A . data ( ) . dataType ( ) == DataBuffer . Type . DOUBLE ) { DefaultOpExecutioner . validateDataType ( DataBuffer . Type . DOUBLE , A , C ) ; dsyrk ( Order , Uplo , Trans , ( int ) C . rows ( ) , 1 , alpha , A , ( int ) A . size ( 0 ) , beta , C , ( int ) C . size ( 0 ) ) ; } else { DefaultOpExecutioner . validateDataType ( DataBuffer . Type . FLOAT , A , C ) ; ssyrk ( Order , Uplo , Trans , ( int ) C . rows ( ) , 1 , ( float ) alpha , A , ( int ) A . size ( 0 ) , ( float ) beta , C , ( int ) C . size ( 0 ) ) ; } OpExecutionerUtil . checkForAny ( C ) ; }


Input: gemm performs a matrix - matrix operation c : = alpha * op ( a ) * op ( b ) + beta * c where c is an m - by - n matrix op ( a ) is an m - by - k matrix op ( b ) is a k - by - n matrix . [CODESPLIT] @ Override public void gemm ( char Order , char TransA , char TransB , IComplexNumber alpha , IComplexNDArray A , IComplexNDArray B , IComplexNumber beta , IComplexNDArray C ) { if ( Nd4j . getExecutioner ( ) . getProfilingMode ( ) == OpExecutioner . ProfilingMode . ALL ) OpProfiler . getInstance ( ) . processBlasCall ( true , A , B , C ) ; GemmParams params = new GemmParams ( A , B , C ) ; if ( A . data ( ) . dataType ( ) == DataBuffer . Type . DOUBLE ) { zgemm ( Order , TransA , TransB , params . getM ( ) , params . getN ( ) , params . getK ( ) , alpha . asDouble ( ) , A . ordering ( ) == NDArrayFactory . C ? B : A , params . getLda ( ) , B . ordering ( ) == NDArrayFactory . C ? A : B , params . getLdb ( ) , beta . asDouble ( ) , C , params . getLdc ( ) ) ; } else cgemm ( Order , TransA , TransB , params . getM ( ) , params . getN ( ) , params . getK ( ) , alpha . asFloat ( ) , A . ordering ( ) == NDArrayFactory . C ? B : A , params . getLda ( ) , B . ordering ( ) == NDArrayFactory . C ? A : B , params . getLdb ( ) , beta . asFloat ( ) , C , params . getLdc ( ) ) ; }


Input: hemm performs one of the following matrix - matrix operations : c : = alpha * a * b + beta * c for side = L or l c : = alpha * b * a + beta * c for side = R or r where a is a Hermitian matrix b and c are m - by - n matrices . [CODESPLIT] @ Override public void hemm ( char Order , char Side , char Uplo , IComplexNumber alpha , IComplexNDArray A , IComplexNDArray B , IComplexNumber beta , IComplexNDArray C ) { // FIXME: int cast if ( A . data ( ) . dataType ( ) == DataBuffer . Type . DOUBLE ) zhemm ( Order , Side , Uplo , ( int ) B . rows ( ) , ( int ) B . columns ( ) , alpha . asDouble ( ) , A , ( int ) A . size ( 0 ) , B , ( int ) B . size ( 0 ) , beta . asDouble ( ) , C , ( int ) C . size ( 0 ) ) ; else chemm ( Order , Side , Uplo , ( int ) B . rows ( ) , ( int ) B . columns ( ) , alpha . asFloat ( ) , A , ( int ) A . size ( 0 ) , B , ( int ) B . size ( 0 ) , beta . asFloat ( ) , C , ( int ) C . size ( 0 ) ) ; }


Input: herk performs a rank - n update of a Hermitian matrix that is one of the following operations : c : = alpha * a * conjug ( a ) + beta * c for trans = N or n c : = alpha * conjug ( a ) * a + beta * c for trans = C or c where c is an n - by - n Hermitian matrix ; a is an n - by - k matrix if trans = N or n a is a k - by - n matrix if trans = C or c . [CODESPLIT] @ Override public void herk ( char Order , char Uplo , char Trans , IComplexNumber alpha , IComplexNDArray A , IComplexNumber beta , IComplexNDArray C ) { // FIXME: int cast if ( A . data ( ) . dataType ( ) == DataBuffer . Type . DOUBLE ) zherk ( Order , Uplo , Trans , ( int ) A . rows ( ) , ( int ) A . columns ( ) , alpha . asDouble ( ) , A , ( int ) A . size ( 0 ) , beta . asDouble ( ) , C , ( int ) C . size ( 0 ) ) ; else cherk ( Order , Uplo , Trans , ( int ) A . rows ( ) , ( int ) A . columns ( ) , alpha . asFloat ( ) , A , ( int ) A . size ( 0 ) , beta . asFloat ( ) , C , ( int ) C . size ( 0 ) ) ; }


Input: her2k performs a rank - 2k update of an n - by - n Hermitian matrix c that is one of the following operations : c : = alpha * a * conjg ( b ) + conjg ( alpha ) * b * conjg ( a ) + beta * c for trans = N or n c : = alpha * conjg ( b ) * a + conjg ( alpha ) * conjg ( a ) * b + beta * c for trans = C or c where c is an n - by - n Hermitian matrix ; a and b are n - by - k matrices if trans = N or n a and b are k - by - n matrices if trans = C or c . [CODESPLIT] @ Override public void symm ( char Order , char Side , char Uplo , IComplexNumber alpha , IComplexNDArray A , IComplexNDArray B , IComplexNumber beta , IComplexNDArray C ) { // FIXME: int cast if ( A . data ( ) . dataType ( ) == DataBuffer . Type . DOUBLE ) zsymm ( Order , Side , Uplo , ( int ) A . rows ( ) , ( int ) A . columns ( ) , alpha . asDouble ( ) , A , ( int ) A . size ( 0 ) , B , ( int ) B . size ( 0 ) , beta . asDouble ( ) , C , ( int ) C . size ( 0 ) ) ; else csymm ( Order , Side , Uplo , ( int ) A . rows ( ) , ( int ) A . columns ( ) , alpha . asFloat ( ) , A , ( int ) A . size ( 0 ) , B , ( int ) B . size ( 0 ) , beta . asFloat ( ) , C , ( int ) C . size ( 0 ) ) ; }


Input: syrk performs a rank - n update of an n - by - n symmetric matrix c that is one of the following operations : c : = alpha * a * a + beta * c for trans = N or n c : = alpha * a * a + beta * c for trans = T or t C or c where c is an n - by - n symmetric matrix ; a is an n - by - k matrix if trans = N or n a is a k - by - n matrix if trans = T or t C or c . [CODESPLIT] @ Override public void syrk ( char Order , char Uplo , char Trans , IComplexNumber alpha , IComplexNDArray A , IComplexNumber beta , IComplexNDArray C ) { // FIXME: int cast if ( A . data ( ) . dataType ( ) == DataBuffer . Type . DOUBLE ) zsyrk ( Order , Uplo , Trans , ( int ) A . rows ( ) , ( int ) A . columns ( ) , alpha . asDouble ( ) , A , ( int ) A . size ( 0 ) , beta . asDouble ( ) , C , ( int ) C . size ( 0 ) ) ; else csyrk ( Order , Uplo , Trans , ( int ) A . rows ( ) , ( int ) A . columns ( ) , alpha . asFloat ( ) , A , ( int ) A . size ( 0 ) , beta . asFloat ( ) , C , ( int ) C . size ( 0 ) ) ; }


Input: yr2k performs a rank - 2k update of an n - by - n symmetric matrix c that is one of the following operations : c : = alpha * a * b + alpha * b * a + beta * c for trans = N or n c : = alpha * a * b + alpha * b * a + beta * c for trans = T or t where c is an n - by - n symmetric matrix ; a and b are n - by - k matrices if trans = N or n a and b are k - by - n matrices if trans = T or t . [CODESPLIT] @ Override public void syr2k ( char Order , char Uplo , char Trans , IComplexNumber alpha , IComplexNDArray A , IComplexNDArray B , IComplexNumber beta , IComplexNDArray C ) { // FIXME: int cast if ( A . data ( ) . dataType ( ) == DataBuffer . Type . DOUBLE ) zsyr2k ( Order , Uplo , Trans , ( int ) A . rows ( ) , ( int ) A . columns ( ) , alpha . asDouble ( ) , A , ( int ) A . size ( 0 ) , B , ( int ) B . size ( 0 ) , beta . asDouble ( ) , C , ( int ) C . size ( 0 ) ) ; else csyr2k ( Order , Uplo , Trans , ( int ) A . rows ( ) , ( int ) A . columns ( ) , alpha . asFloat ( ) , A , ( int ) A . size ( 0 ) , B , ( int ) B . size ( 0 ) , beta . asFloat ( ) , C , ( int ) C . size ( 0 ) ) ; }


Input: syr2k performs a rank - 2k update of an n - by - n symmetric matrix c that is one of the following operations : c : = alpha * a * b + alpha * b * a + beta * c for trans = N or n c : = alpha * a * b + alpha * b * a + beta * c for trans = T or t where c is an n - by - n symmetric matrix ; a and b are n - by - k matrices if trans = N or n a and b are k - by - n matrices if trans = T or t . [CODESPLIT] @ Override public void trmm ( char Order , char Side , char Uplo , char TransA , char Diag , IComplexNumber alpha , IComplexNDArray A , IComplexNDArray B , IComplexNDArray C ) { // FIXME: int cast if ( A . data ( ) . dataType ( ) == DataBuffer . Type . DOUBLE ) ztrmm ( Order , Side , Uplo , TransA , Diag , ( int ) A . rows ( ) , ( int ) A . columns ( ) , alpha . asDouble ( ) , A , ( int ) A . size ( 0 ) , B , ( int ) B . size ( 0 ) , C , ( int ) C . size ( 0 ) ) ; else ctrmm ( Order , Side , Uplo , TransA , Diag , ( int ) A . rows ( ) , ( int ) A . columns ( ) , alpha . asFloat ( ) , A , ( int ) A . size ( 0 ) , B , ( int ) B . size ( 0 ) , C , ( int ) C . size ( 0 ) ) ; }


Input: ?trsm solves one of the following matrix equations : op ( a ) * x = alpha * b or x * op ( a ) = alpha * b where x and b are m - by - n general matrices and a is triangular ; op ( a ) must be an m - by - m matrix if side = L or l op ( a ) must be an n - by - n matrix if side = R or r . For the definition of op ( a ) see Matrix Arguments . The routine overwrites x on b . [CODESPLIT] @ Override public void trsm ( char Order , char Side , char Uplo , char TransA , char Diag , IComplexNumber alpha , IComplexNDArray A , IComplexNDArray B ) { // FIXME: int cast if ( A . data ( ) . dataType ( ) == DataBuffer . Type . DOUBLE ) ztrsm ( Order , Side , Uplo , TransA , Diag , ( int ) A . rows ( ) , ( int ) A . columns ( ) , alpha . asDouble ( ) , A , ( int ) A . size ( 0 ) , B , ( int ) B . size ( 0 ) ) ; else ctrsm ( Order , Side , Uplo , TransA , Diag , ( int ) A . rows ( ) , ( int ) A . columns ( ) , alpha . asFloat ( ) , A , ( int ) A . size ( 0 ) , B , ( int ) B . size ( 0 ) ) ; }


Input: Get the onnx op descriptors by name [CODESPLIT] public static Map < String , OpDescriptor > onnxOpDescriptors ( ) throws Exception { try ( InputStream is = new ClassPathResource ( "onnxops.json" ) . getInputStream ( ) ) { ObjectMapper objectMapper = new ObjectMapper ( ) ; OnnxDescriptor opDescriptor = objectMapper . readValue ( is , OnnxDescriptor . class ) ; Map < String , OpDescriptor > descriptorMap = new HashMap <> ( ) ; for ( OpDescriptor descriptor : opDescriptor . getDescriptors ( ) ) { descriptorMap . put ( descriptor . getName ( ) , descriptor ) ; } return descriptorMap ; } }


Input: Get the allocation mode from the context [CODESPLIT] public static DataBuffer . AllocationMode getAllocationModeFromContext ( String allocMode ) { switch ( allocMode ) { case "heap" : return DataBuffer . AllocationMode . HEAP ; case "javacpp" : return DataBuffer . AllocationMode . JAVACPP ; case "direct" : return DataBuffer . AllocationMode . DIRECT ; default : return DataBuffer . AllocationMode . JAVACPP ; } }


Input: Set the allocation mode for the nd4j context The value must be one of : heap java cpp or direct or an [CODESPLIT] public static void setAllocationModeForContext ( String allocationModeForContext ) { if ( ! allocationModeForContext . equals ( "heap" ) && ! allocationModeForContext . equals ( "javacpp" ) && ! allocationModeForContext . equals ( "direct" ) ) throw new IllegalArgumentException ( "Allocation mode must be one of: heap,javacpp, or direct" ) ; Nd4jContext . getInstance ( ) . getConf ( ) . put ( "alloc" , allocationModeForContext ) ; }


Input: This method releases previously allocated memory chunk [CODESPLIT] @ Override public void release ( @ NonNull Pointer pointer , MemoryKind kind ) { Pointer . free ( pointer ) ; }


Input: Calculate the output shape for this op [CODESPLIT] public List < long [ ] > calculateOutputShape ( ) { List < long [ ] > ret = new ArrayList <> ( ) ; if ( larg ( ) . getShape ( ) != null && rarg ( ) . getShape ( ) != null ) ret . add ( Shape . broadcastOutputShape ( larg ( ) . getShape ( ) , rarg ( ) . getShape ( ) ) ) ; else if ( larg ( ) . getShape ( ) != null ) ret . add ( larg ( ) . getShape ( ) ) ; return ret ; }


Input: { [CODESPLIT] public double cumulativeProbability ( double x ) { if ( x <= lower ) { return 0 ; } if ( x >= upper ) { return 1 ; } return ( x - lower ) / ( upper - lower ) ; }


Input: This method executes given graph and returns results [CODESPLIT] @ Override public INDArray [ ] executeGraph ( SameDiff sd , ExecutorConfiguration configuration ) { Map < Integer , Node > intermediate = new HashMap <> ( ) ; ByteBuffer buffer = convertToFlatBuffers ( sd , configuration , intermediate ) ; BytePointer bPtr = new BytePointer ( buffer ) ; log . info ( "Buffer length: {}" , buffer . limit ( ) ) ; Pointer res = NativeOpsHolder . getInstance ( ) . getDeviceNativeOps ( ) . executeFlatGraphFloat ( null , bPtr ) ; if ( res == null ) throw new ND4JIllegalStateException ( "Graph execution failed" ) ; // FIXME: this is BAD PagedPointer pagedPointer = new PagedPointer ( res , 1024 * 1024L ) ; FlatResult fr = FlatResult . getRootAsFlatResult ( pagedPointer . asBytePointer ( ) . asByteBuffer ( ) ) ; log . info ( "VarMap: {}" , sd . variableMap ( ) ) ; INDArray [ ] results = new INDArray [ fr . variablesLength ( ) ] ; for ( int e = 0 ; e < fr . variablesLength ( ) ; e ++ ) { FlatVariable var = fr . variables ( e ) ; log . info ( "Var received: id: [{}:{}/<{}>];" , var . id ( ) . first ( ) , var . id ( ) . second ( ) , var . name ( ) ) ; FlatArray ndarray = var . ndarray ( ) ; INDArray val = Nd4j . createFromFlatArray ( ndarray ) ; results [ e ] = val ; if ( var . name ( ) != null && sd . variableMap ( ) . containsKey ( var . name ( ) ) ) { //log.info("VarName: {}; Exists: {}; NDArrayInfo: {};", var.opName(), sd.variableMap().containsKey(var.opName()), sd.getVertexToArray().containsKey(var.opName())); //              log.info("storing: {}; array: {}", var.name(), val); sd . associateArrayWithVariable ( val , sd . variableMap ( ) . get ( var . name ( ) ) ) ; } else { //log.info("Original id: {}; out: {}; out2: {}", original, sd.getVertexIdxToInfo().get(original), graph.getVariableForVertex(original)); if ( sd . variableMap ( ) . get ( var . name ( ) ) != null ) { sd . associateArrayWithVariable ( val , sd . getVariable ( var . name ( ) ) ) ; } else { //                log.info("BAD"); //sd.var("",val); throw new ND4JIllegalStateException ( "Unknown variable received as result: [" + var . name ( ) + "]" ) ; } } } return results ; }


Input: And [CODESPLIT] public static boolean and ( IComplexNDArray n , Condition cond ) { boolean ret = true ; IComplexNDArray linear = n . linearView ( ) ; for ( int i = 0 ; i < linear . length ( ) ; i ++ ) { ret = ret && cond . apply ( linear . getComplex ( i ) ) ; } return ret ; }


Input: Or over the whole ndarray given some condition [CODESPLIT] public static boolean or ( IComplexNDArray n , Condition cond ) { boolean ret = false ; IComplexNDArray linear = n . linearView ( ) ; for ( int i = 0 ; i < linear . length ( ) ; i ++ ) { ret = ret || cond . apply ( linear . getComplex ( i ) ) ; } return ret ; }


Input: And over the whole ndarray given some condition [CODESPLIT] public static boolean and ( final INDArray n , final Condition cond ) { if ( cond instanceof BaseCondition ) { long val = ( long ) Nd4j . getExecutioner ( ) . exec ( new MatchCondition ( n , cond ) , Integer . MAX_VALUE ) . getDouble ( 0 ) ; if ( val == n . lengthLong ( ) ) return true ; else return false ; } else { boolean ret = true ; final AtomicBoolean a = new AtomicBoolean ( ret ) ; Shape . iterate ( n , new CoordinateFunction ( ) { @ Override public void process ( long [ ] ... coord ) { if ( a . get ( ) ) a . compareAndSet ( true , a . get ( ) && cond . apply ( n . getDouble ( coord [ 0 ] ) ) ) ; } } ) ; return a . get ( ) ; } }


Input: And over the whole ndarray given some condition with respect to dimensions [CODESPLIT] public static boolean [ ] and ( final INDArray n , final Condition condition , int ... dimension ) { if ( ! ( condition instanceof BaseCondition ) ) throw new UnsupportedOperationException ( "Only static Conditions are supported" ) ; MatchCondition op = new MatchCondition ( n , condition ) ; INDArray arr = Nd4j . getExecutioner ( ) . exec ( op , dimension ) ; boolean [ ] result = new boolean [ ( int ) arr . length ( ) ] ; long tadLength = Shape . getTADLength ( n . shape ( ) , dimension ) ; for ( int i = 0 ; i < arr . length ( ) ; i ++ ) { if ( arr . getDouble ( i ) == tadLength ) result [ i ] = true ; else result [ i ] = false ; } return result ; }


Input: Or over the whole ndarray given some condition with respect to dimensions [CODESPLIT] public static boolean [ ] or ( final INDArray n , final Condition condition , int ... dimension ) { if ( ! ( condition instanceof BaseCondition ) ) throw new UnsupportedOperationException ( "Only static Conditions are supported" ) ; MatchCondition op = new MatchCondition ( n , condition ) ; INDArray arr = Nd4j . getExecutioner ( ) . exec ( op , dimension ) ; // FIXME: int cast boolean [ ] result = new boolean [ ( int ) arr . length ( ) ] ; for ( int i = 0 ; i < arr . length ( ) ; i ++ ) { if ( arr . getDouble ( i ) > 0 ) result [ i ] = true ; else result [ i ] = false ; } return result ; }


Input: Based on the matching elements op to based on condition to with function function [CODESPLIT] public static void applyWhere ( final INDArray to , final Condition condition , final Function < Number , Number > function ) { // keep original java implementation for dynamic Shape . iterate ( to , new CoordinateFunction ( ) { @ Override public void process ( long [ ] ... coord ) { if ( condition . apply ( to . getDouble ( coord [ 0 ] ) ) ) to . putScalar ( coord [ 0 ] , function . apply ( to . getDouble ( coord [ 0 ] ) ) . doubleValue ( ) ) ; } } ) ; }


Input: This method sets provided number to all elements which match specified condition [CODESPLIT] public static void applyWhere ( final INDArray to , final Condition condition , final Number number ) { if ( condition instanceof BaseCondition ) { // for all static conditions we go native Nd4j . getExecutioner ( ) . exec ( new CompareAndSet ( to , number . doubleValue ( ) , condition ) ) ; } else { final double value = number . doubleValue ( ) ; final Function < Number , Number > dynamic = new Function < Number , Number > ( ) { @ Override public Number apply ( Number number ) { return value ; } } ; Shape . iterate ( to , new CoordinateFunction ( ) { @ Override public void process ( long [ ] ... coord ) { if ( condition . apply ( to . getDouble ( coord [ 0 ] ) ) ) to . putScalar ( coord [ 0 ] , dynamic . apply ( to . getDouble ( coord [ 0 ] ) ) . doubleValue ( ) ) ; } } ) ; } }


Input: Choose from the inputs based on the given condition . This returns a row vector of all elements fulfilling the condition listed within the array for input [CODESPLIT] public static INDArray chooseFrom ( @ NonNull INDArray [ ] input , @ NonNull Condition condition ) { Choose choose = new Choose ( input , condition ) ; Nd4j . getExecutioner ( ) . exec ( choose ) ; int secondOutput = choose . getOutputArgument ( 1 ) . getInt ( 0 ) ; if ( secondOutput < 1 ) { return null ; } return choose . getOutputArgument ( 0 ) ; }


Input: Choose from the inputs based on the given condition . This returns a row vector of all elements fulfilling the condition listed within the array for input . The double and integer arguments are only relevant for scalar operations ( like when you have a scalar you are trying to compare each element in your input against ) [CODESPLIT] public static INDArray chooseFrom ( @ NonNull INDArray [ ] input , @ NonNull List < Double > tArgs , @ NonNull List < Integer > iArgs , @ NonNull Condition condition ) { Choose choose = new Choose ( input , iArgs , tArgs , condition ) ; Nd4j . getExecutioner ( ) . exec ( choose ) ; int secondOutput = choose . getOutputArgument ( 1 ) . getInt ( 0 ) ; if ( secondOutput < 1 ) { return null ; } INDArray ret = choose . getOutputArgument ( 0 ) . get ( NDArrayIndex . interval ( 0 , secondOutput ) ) ; ret = ret . reshape ( ret . length ( ) ) ; return ret ; }


Input: Based on the matching elements op to based on condition to with function function [CODESPLIT] public static void applyWhere ( final INDArray to , final Condition condition , final Function < Number , Number > function , final Function < Number , Number > alternativeFunction ) { Shape . iterate ( to , new CoordinateFunction ( ) { @ Override public void process ( long [ ] ... coord ) { if ( condition . apply ( to . getDouble ( coord [ 0 ] ) ) ) { to . putScalar ( coord [ 0 ] , function . apply ( to . getDouble ( coord [ 0 ] ) ) . doubleValue ( ) ) ; } else { to . putScalar ( coord [ 0 ] , alternativeFunction . apply ( to . getDouble ( coord [ 0 ] ) ) . doubleValue ( ) ) ; } } } ) ; }


Input: Based on the matching elements op to based on condition to with function function [CODESPLIT] public static void applyWhere ( IComplexNDArray to , Condition condition , Function < IComplexNumber , IComplexNumber > function ) { IComplexNDArray linear = to . linearView ( ) ; for ( int i = 0 ; i < linear . linearView ( ) . length ( ) ; i ++ ) { if ( condition . apply ( linear . getDouble ( i ) ) ) { linear . putScalar ( i , function . apply ( linear . getComplex ( i ) ) ) ; } } }


Input: This method returns first index matching given condition [CODESPLIT] public static INDArray firstIndex ( INDArray array , Condition condition ) { if ( ! ( condition instanceof BaseCondition ) ) throw new UnsupportedOperationException ( "Only static Conditions are supported" ) ; FirstIndex idx = new FirstIndex ( array , condition ) ; Nd4j . getExecutioner ( ) . exec ( idx ) ; return Nd4j . scalar ( ( double ) idx . getFinalResult ( ) ) ; }


Input: This method converts this FunctionProperties instance to FlatBuffers representation [CODESPLIT] public int asFlatProperties ( FlatBufferBuilder bufferBuilder ) { int iname = bufferBuilder . createString ( name ) ; int ii = FlatProperties . createIVector ( bufferBuilder , Ints . toArray ( i ) ) ; int il = FlatProperties . createLVector ( bufferBuilder , Longs . toArray ( l ) ) ; int id = FlatProperties . createDVector ( bufferBuilder , Doubles . toArray ( d ) ) ; int arrays [ ] = new int [ a . size ( ) ] ; int cnt = 0 ; for ( val array : a ) { int off = array . toFlatArray ( bufferBuilder ) ; arrays [ cnt ++ ] = off ; } int ia = FlatProperties . createAVector ( bufferBuilder , arrays ) ; return FlatProperties . createFlatProperties ( bufferBuilder , iname , ii , il , id , ia ) ; }


Input: This method creates new FunctionProperties instance from FlatBuffers representation [CODESPLIT] public static FunctionProperties fromFlatProperties ( FlatProperties properties ) { val props = new FunctionProperties ( ) ; for ( int e = 0 ; e < properties . iLength ( ) ; e ++ ) props . getI ( ) . ( properties . i ( ) ) ; for ( int e = 0 ; e < properties . lLength ( ) ; e ++ ) props . getL ( ) . ( properties . l ( ) ) ; for ( int e = 0 ; e < properties . dLength ( ) ; e ++ ) props . getD ( ) . ( properties . ( e ) ) ; for ( int e = 0 ; e < properties . iLength ( ) ; e ++ ) props . getA ( ) . ( Nd4j . createFromFlatArray ( properties . ( ) ) ) ; return props ; }


Input: This method converts multiple FunctionProperties to FlatBuffers representation [CODESPLIT] public static int asFlatProperties ( FlatBufferBuilder bufferBuilder , Collection < FunctionProperties > properties ) { int props [ ] = new int [ properties . size ( ) ] ; int cnt = 0 ; for ( val p : properties ) props [ cnt ++ ] = p . asFlatProperties ( bufferBuilder ) ; return FlatNode . createPropertiesVector ( bufferBuilder , props ) ; }


Input: This method updates state with given Throwable [CODESPLIT] public void set ( Throwable t ) { try { lock . writeLock ( ) . lock ( ) ; this . t = t ; } finally { lock . writeLock ( ) . unlock ( ) ; } }


Input: This method updates state only if it wasn t set before [CODESPLIT] public void setIfFirst ( Throwable t ) { try { lock . writeLock ( ) . lock ( ) ; if ( this . t == null ) this . t = t ; } finally { lock . writeLock ( ) . unlock ( ) ; } }


Input: Normalize a value ( val - min ) / ( max - min ) [CODESPLIT] public static double normalize ( double val , double min , double max ) { if ( max < min ) throw new IllegalArgumentException ( "Max must be greather than min" ) ; return ( val - min ) / ( max - min ) ; }


Input: This will merge the coordinates of the given coordinate system . [CODESPLIT] public static List < Double > mergeCoords ( List < Double > x , List < Double > y ) { if ( x . size ( ) != y . size ( ) ) throw new IllegalArgumentException ( "Sample sizes must be the same for each data applyTransformToDestination." ) ; List < Double > ret = new ArrayList < Double > ( ) ; for ( int i = 0 ; i < x . size ( ) ; i ++ ) { ret . add ( x . get ( i ) ) ; ret . add ( y . get ( i ) ) ; } return ret ; }


Input: This returns the coordinate split in a list of coordinates such that the values for ret [ 0 ] are the x values and ret [ 1 ] are the y values [CODESPLIT] public static List < double [ ] > coordSplit ( double [ ] vector ) { if ( vector == null ) return null ; List < double [ ] > ret = new ArrayList < double [ ] > ( ) ; /* x coordinates */ double [ ] xVals = new double [ vector . length / 2 ] ; /* y coordinates */ double [ ] yVals = new double [ vector . length / 2 ] ; /* current points */ int xTracker = 0 ; int yTracker = 0 ; for ( int i = 0 ; i < vector . length ; i ++ ) { //even value, x coordinate if ( i % 2 == 0 ) xVals [ xTracker ++ ] = vector [ i ] ; //y coordinate else yVals [ yTracker ++ ] = vector [ i ] ; } ret . add ( xVals ) ; ret . add ( yVals ) ; return ret ; }


Input: This will partition the given whole variable data applyTransformToDestination in to the specified chunk number . [CODESPLIT] public static List < List < Double > > partitionVariable ( List < Double > arr , int chunk ) { int count = 0 ; List < List < Double > > ret = new ArrayList < List < Double > > ( ) ; while ( count < arr . size ( ) ) { List < Double > sublist = arr . subList ( count , count + chunk ) ; count += chunk ; ret . add ( sublist ) ; } //All data sets must be same size for ( List < Double > lists : ret ) { if ( lists . size ( ) < chunk ) ret . remove ( lists ) ; } return ret ; }


Input: This returns the coordinate split in a list of coordinates such that the values for ret [ 0 ] are the x values and ret [ 1 ] are the y values [CODESPLIT] public static List < double [ ] > coordSplit ( List < Double > vector ) { if ( vector == null ) return null ; List < double [ ] > ret = new ArrayList < double [ ] > ( ) ; /* x coordinates */ double [ ] xVals = new double [ vector . size ( ) / 2 ] ; /* y coordinates */ double [ ] yVals = new double [ vector . size ( ) / 2 ] ; /* current points */ int xTracker = 0 ; int yTracker = 0 ; for ( int i = 0 ; i < vector . size ( ) ; i ++ ) { //even value, x coordinate if ( i % 2 == 0 ) xVals [ xTracker ++ ] = vector . get ( i ) ; //y coordinate else yVals [ yTracker ++ ] = vector . get ( i ) ; } ret . add ( xVals ) ; ret . add ( yVals ) ; return ret ; }


Input: Convert an onnx type to the proper nd4j type [CODESPLIT] public DataBuffer . Type nd4jTypeFromOnnxType ( OnnxProto3 . TensorProto . DataType dataType ) { switch ( dataType ) { case DOUBLE : return DataBuffer . Type . DOUBLE ; case FLOAT : return DataBuffer . Type . FLOAT ; case FLOAT16 : return DataBuffer . Type . HALF ; case INT32 : case INT64 : return DataBuffer . Type . INT ; default : return DataBuffer . Type . UNKNOWN ; } }


Input:
This method initiates shutdown sequence for this instance . [CODESPLIT] public void shutdown ( ) { /**
         * Probably we don't need this method in practice
         */ if ( initLocker . get ( ) && shutdownLocker . compareAndSet ( false , true ) ) { // do shutdown log . info ( "Shutting down transport..." ) ; // we just sending out ShutdownRequestMessage //transport.sendMessage(new ShutdownRequestMessage()); transport . shutdown ( ) ; executor . shutdown ( ) ; } }


Input:
Returns titles line as string appointed by title index ( 0 .. 5 ) .
 Columns are separated with char | .
 If title index is < 0 returns ? .
 If title index is > 5 returns ? .
 [CODESPLIT] public String getTitleLine ( int mtLv , int title_I ) { // String info = "" ; // if ( title_I < 0 ) return "?" ; if ( title_I > 5 ) return "?" ; // info = "" ; info += BTools . getMtLvESS ( mtLv ) ; info += BTools . getMtLvISS ( ) ; info += "|" ; // InfoValues i_IV ; // String i_ValuesS = "" ; // int i_VSLen = - 1 ; // String i_TitleS = "" ; // for ( int i = 0 ; i < ivL . size ( ) ; i ++ ) { // i_IV = ivL . get ( i ) ; // i_ValuesS = i_IV . getValues ( ) ; // i_VSLen = i_ValuesS . length ( ) ; // i_TitleS = ( title_I < i_IV . titleA . length ) ? i_IV . titleA [ title_I ] : "" ; // i_TitleS = i_TitleS + BTools . getSpaces ( i_VSLen ) ; // info += i_TitleS . substring ( 0 , i_VSLen - 1 ) ; // info += "|" ; } // return info ; }


Input:
Returns values line as string .
 Columns are separated with char | .
 [CODESPLIT] public String getValuesLine ( int mtLv ) { // String info = "" ; // info += BTools . getMtLvESS ( mtLv ) ; info += BTools . getMtLvISS ( ) ; info += "|" ; // InfoValues i_IV ; // for ( int i = 0 ; i < ivL . size ( ) ; i ++ ) { // i_IV = ivL . get ( i ) ; // info += i_IV . getValues ( ) ; } // return info ; }


Input: Get the current device architecture [CODESPLIT] public int getCurrentDeviceArchitecture ( ) { int deviceId = Nd4j . getAffinityManager ( ) . getDeviceForCurrentThread ( ) ; if ( ! arch . containsKey ( deviceId ) ) { int major = NativeOpsHolder . getInstance ( ) . getDeviceNativeOps ( ) . getDeviceMajor ( new CudaPointer ( deviceId ) ) ; int minor = NativeOpsHolder . getInstance ( ) . getDeviceNativeOps ( ) . getDeviceMinor ( new CudaPointer ( deviceId ) ) ; Integer cc = Integer . parseInt ( new String ( "" + major + minor ) ) ; arch . put ( deviceId , cc ) ; return cc ; } return arch . get ( deviceId ) ; }


Input:
Returns a subset of this array based on the specified indexes [CODESPLIT] @ Override public INDArray get ( INDArrayIndex ... indexes ) { sort ( ) ; if ( indexes . length == 1 && indexes [ 0 ] instanceof NDArrayIndexAll || ( indexes . length == 2 && ( isRowVector ( ) && indexes [ 0 ] instanceof PointIndex && indexes [ 0 ] . offset ( ) == 0 && indexes [ 1 ] instanceof NDArrayIndexAll || isColumnVector ( ) && indexes [ 1 ] instanceof PointIndex && indexes [ 0 ] . offset ( ) == 0 && indexes [ 0 ] instanceof NDArrayIndexAll ) ) ) return this ; indexes = NDArrayIndex . resolve ( shapeInfoDataBuffer ( ) , indexes ) ; ShapeOffsetResolution resolution = new ShapeOffsetResolution ( this ) ; resolution . exec ( indexes ) ; if ( indexes . length < 1 ) throw new IllegalStateException ( "Invalid index found of zero length" ) ; // FIXME: LONG int [ ] shape = LongUtils . toInts ( resolution . getShapes ( ) ) ; int numSpecifiedIndex = 0 ; for ( int i = 0 ; i < indexes . length ; i ++ ) if ( indexes [ i ] instanceof SpecifiedIndex ) numSpecifiedIndex ++ ; if ( shape != null && numSpecifiedIndex > 0 ) { Generator < List < List < Long >>> gen = SpecifiedIndex . iterateOverSparse ( indexes ) ; INDArray ret = Nd4j . createSparseCOO ( new double [ ] { } , new int [ ] [ ] { } , shape ) ; int count = 0 ; int maxValue = ArrayUtil . prod ( shape ( ) ) ; while ( count < maxValue ) { try { List < List < Long >> next = gen . next ( ) ; List < Integer > coordsCombo = new ArrayList <> ( ) ; List < Integer > cooIdx = new ArrayList <> ( ) ; for ( int i = 0 ; i < next . size ( ) ; i ++ ) { if ( next . get ( i ) . size ( ) != 2 ) throw new IllegalStateException ( "Illegal entry returned" ) ; coordsCombo . add ( next . get ( i ) . get ( 0 ) . intValue ( ) ) ; cooIdx . add ( next . get ( i ) . get ( 1 ) . intValue ( ) ) ; } count ++ ; /*
                    * if the coordinates are in the original array
                    *   -> add it in the new sparse ndarray
                    * else
                    *   -> do nothing
                    * */ int [ ] idx = Ints . toArray ( coordsCombo ) ; if ( ! isZero ( idx ) ) { double val = getDouble ( idx ) ; ret . putScalar ( filterOutFixedDimensions ( resolution . getFixed ( ) , cooIdx ) , val ) ; } } catch ( NoSuchElementException e ) { break ; } } return ret ; } int numNewAxis = 0 ; for ( int i = 0 ; i < indexes . length ; i ++ ) if ( indexes [ i ] instanceof NewAxis ) numNewAxis ++ ; if ( numNewAxis != 0 ) { } INDArray ret = subArray ( resolution ) ; return ret ; }


Input: Adjust the flags array according on the current context : - In case of a vector or a scalar we need to keep flags to 0 - There must always be at least 2 non - flags dimensions - We must keep the flags dimensions of the original array [CODESPLIT] private int [ ] updateFlags ( int [ ] viewFlags , int [ ] newShape ) { // Check if flags is well-formed int count = 0 ; for ( int i = 0 ; i < viewFlags . length ; i ++ ) { if ( viewFlags [ i ] == 0 ) { count ++ ; } } for ( int dim = 0 ; dim < viewFlags . length ; dim ++ ) { if ( viewFlags [ dim ] == 1 ) { if ( newShape [ dim ] == 1 && count < 2 ) { viewFlags [ dim ] = 0 ; } } } // Take the original Fixed into account int [ ] extendedFlags = new int [ underlyingRank ( ) ] ; int notFixedDim = 0 ; for ( int dim = 0 ; dim < underlyingRank ( ) ; dim ++ ) { int [ ] temp = flags ( ) ; if ( flags ( ) [ dim ] == 0 ) { extendedFlags [ dim ] = viewFlags [ notFixedDim ] ; notFixedDim ++ ; } else { extendedFlags [ dim ] = 1 ; } } return extendedFlags ; }


Input: Rearrange matrix columns into blocks [CODESPLIT] public static INDArray col2im ( INDArray col , int sy , int sx , int ph , int pw , int h , int w ) { if ( col . rank ( ) != 6 ) throw new IllegalArgumentException ( "col2im input array must be rank 6" ) ; INDArray output = Nd4j . create ( new long [ ] { col . size ( 0 ) , col . size ( 1 ) , h , w } ) ; Col2Im col2Im = Col2Im . builder ( ) . inputArrays ( new INDArray [ ] { col } ) . outputs ( new INDArray [ ] { output } ) . conv2DConfig ( Conv2DConfig . builder ( ) . sy ( sy ) . sx ( sx ) . dw ( 1 ) . dh ( 1 ) . kh ( h ) . kw ( w ) . ph ( ph ) . pw ( pw ) . build ( ) ) . build ( ) ; Nd4j . getExecutioner ( ) . exec ( col2Im ) ; return col2Im . outputArguments ( ) [ 0 ] ; }


Input: Pooling 2d implementation [CODESPLIT] public static INDArray pooling2D ( INDArray img , int kh , int kw , int sy , int sx , int ph , int pw , int dh , int dw , boolean isSameMode , Pooling2D . Pooling2DType type , Pooling2D . Divisor divisor , double extra , int virtualHeight , int virtualWidth , INDArray out ) { Pooling2D pooling = Pooling2D . builder ( ) . arrayInputs ( new INDArray [ ] { img } ) . arrayOutputs ( new INDArray [ ] { out } ) . config ( Pooling2DConfig . builder ( ) . dh ( dh ) . dw ( dw ) . extra ( extra ) . kh ( kh ) . kw ( kw ) . ph ( ph ) . pw ( pw ) . isSameMode ( isSameMode ) . sx ( sx ) . sy ( sy ) . virtualHeight ( virtualHeight ) . virtualWidth ( virtualWidth ) . type ( type ) . divisor ( divisor ) . build ( ) ) . build ( ) ; Nd4j . getExecutioner ( ) . exec ( pooling ) ; return out ; }


Input: Implement column formatted images @param img the image to process @param kh the kernel height @param kw the kernel width @param sy the stride along y @param sx the stride along x @param ph the padding width @param pw the padding height @param pval the padding value ( not used ) @param isSameMode whether padding mode is same @return the column formatted image [CODESPLIT] public static INDArray im2col ( INDArray img , int kh , int kw , int sy , int sx , int ph , int pw , int pval , boolean isSameMode ) { INDArray output = null ; if ( isSameMode ) { int oH = ( int ) Math . ceil ( img . size ( 2 ) * 1.f / sy ) ; int oW = ( int ) Math . ceil ( img . size ( 3 ) * 1.f / sx ) ; output = Nd4j . createUninitialized ( new long [ ] { img . size ( 0 ) , img . size ( 1 ) , kh , kw , oH , oW } , ' ' ) ; } else { // FIXME: int cast int oH = ( ( int ) img . size ( 2 ) - ( kh + ( kh - 1 ) * ( 1 - 1 ) ) + 2 * ph ) / sy + 1 ; int oW = ( ( int ) img . size ( 3 ) - ( kw + ( kw - 1 ) * ( 1 - 1 ) ) + 2 * pw ) / sx + 1 ; output = Nd4j . createUninitialized ( new long [ ] { img . size ( 0 ) , img . size ( 1 ) , kh , kw , oH , oW } , ' ' ) ; } Im2col im2col = Im2col . builder ( ) . inputArrays ( new INDArray [ ] { img } ) . outputs ( new INDArray [ ] { output } ) . conv2DConfig ( Conv2DConfig . builder ( ) . kh ( kh ) . pw ( pw ) . ph ( ph ) . sy ( sy ) . sx ( sx ) . kw ( kw ) . kh ( kh ) . dw ( 1 ) . dh ( 1 ) . isSameMode ( isSameMode ) . build ( ) ) . build ( ) ; Nd4j . getExecutioner ( ) . exec ( im2col ) ; return im2col . outputArguments ( ) [ 0 ] ; }


Input: ND Convolution [CODESPLIT] public static IComplexNDArray convn ( IComplexNDArray input , IComplexNDArray kernel , Type type , int [ ] axes ) { return Nd4j . getConvolution ( ) . convn ( input , kernel , type , axes ) ; }


Input: Instantiate a compression descriptor from the given bytebuffer [CODESPLIT] public static CompressionDescriptor fromByteBuffer ( ByteBuffer byteBuffer ) { CompressionDescriptor compressionDescriptor = new CompressionDescriptor ( ) ; //compression opType int compressionTypeOrdinal = byteBuffer . getInt ( ) ; CompressionType compressionType = CompressionType . values ( ) [ compressionTypeOrdinal ] ; compressionDescriptor . setCompressionType ( compressionType ) ; //compression algo int compressionAlgoOrdinal = byteBuffer . getInt ( ) ; CompressionAlgorithm compressionAlgorithm = CompressionAlgorithm . values ( ) [ compressionAlgoOrdinal ] ; compressionDescriptor . setCompressionAlgorithm ( compressionAlgorithm . name ( ) ) ; //from here everything is longs compressionDescriptor . setOriginalLength ( byteBuffer . getLong ( ) ) ; compressionDescriptor . setCompressedLength ( byteBuffer . getLong ( ) ) ; compressionDescriptor . setNumberOfElements ( byteBuffer . getLong ( ) ) ; compressionDescriptor . setOriginalElementSize ( byteBuffer . getLong ( ) ) ; return compressionDescriptor ; }


Input: Return a direct allocated bytebuffer from the compression codec . The size of the bytebuffer is calculated to be : 40 : 8 + 32 two ints representing their enum values for the compression algorithm and opType [CODESPLIT] public ByteBuffer toByteBuffer ( ) { //2 ints  at 4 bytes a piece, this includes the compression algorithm //that we convert to enum int enumSize = 2 * 4 ; //4 longs at 8 bytes a piece int sizesLength = 4 * 8 ; ByteBuffer directAlloc = ByteBuffer . allocateDirect ( enumSize + sizesLength ) . order ( ByteOrder . nativeOrder ( ) ) ; directAlloc . putInt ( compressionType . ordinal ( ) ) ; directAlloc . putInt ( CompressionAlgorithm . valueOf ( compressionAlgorithm ) . ordinal ( ) ) ; directAlloc . putLong ( originalLength ) ; directAlloc . putLong ( compressedLength ) ; directAlloc . putLong ( numberOfElements ) ; directAlloc . putLong ( originalElementSize ) ; directAlloc . rewind ( ) ; return directAlloc ; }


Input: Helper method to create batch from list of aggregates for cases when list of aggregates is higher then batchLimit [CODESPLIT] public static < U extends Aggregate > List < Batch < U > > getBatches ( List < U > list , int partitionSize ) { List < List < U >> partitions = Lists . partition ( list , partitionSize ) ; List < Batch < U > > split = new ArrayList <> ( ) ; for ( List < U > partition : partitions ) { split . add ( new Batch < U > ( partition ) ) ; } return split ; }


Input: Calculate the update based on the given gradient [CODESPLIT] @ Override public void applyUpdater ( INDArray gradient , int iteration , int epoch ) { if ( m == null || u == null ) throw new IllegalStateException ( "Updater has not been initialized with view state" ) ; //m = B_1 * m + (1-B_1)*grad m . muli ( config . getBeta1 ( ) ) . addi ( gradient . mul ( 1 - config . getBeta1 ( ) ) ) ; //u = max(B_2 * u, |grad|) u . muli ( config . getBeta2 ( ) ) ; Transforms . abs ( gradient , false ) ; //In-place should be OK here, original gradient values aren't used again later Nd4j . getExecutioner ( ) . exec ( new OldMax ( u , gradient , u , u . length ( ) ) ) ; double beta1t = FastMath . pow ( config . getBeta1 ( ) , iteration + 1 ) ; double learningRate = config . getLearningRate ( iteration , epoch ) ; double alphat = learningRate / ( 1.0 - beta1t ) ; if ( Double . isNaN ( alphat ) || Double . isInfinite ( alphat ) || alphat == 0.0 ) { alphat = config . getEpsilon ( ) ; } u . addi ( 1e-32 ) ; // prevent NaNs in params gradient . assign ( m ) . muli ( alphat ) . divi ( u ) ; }


Input: input arrays must have same number of dimensions [CODESPLIT] protected static void validateConcat ( int dimension , INDArray ... arrs ) { if ( arrs [ 0 ] . isScalar ( ) ) { for ( int i = 1 ; i < arrs . length ; i ++ ) if ( ! arrs [ i ] . isScalar ( ) ) throw new IllegalArgumentException ( "All arrays must have same dimensions" ) ; } else { int dims = arrs [ 0 ] . shape ( ) . length ; long [ ] shape = ArrayUtil . removeIndex ( arrs [ 0 ] . shape ( ) , dimension ) ; for ( int i = 1 ; i < arrs . length ; i ++ ) { assert Arrays . equals ( shape , ArrayUtil . removeIndex ( arrs [ i ] . shape ( ) , dimension ) ) ; assert arrs [ i ] . shape ( ) . length == dims ; } } }


Input: Sets the data opType [CODESPLIT] @ Override public void setDType ( DataBuffer . Type dtype ) { assert dtype == DataBuffer . Type . DOUBLE || dtype == DataBuffer . Type . FLOAT || dtype == DataBuffer . Type . INT : "Invalid opType passed, must be float or double" ; // this.dtype = dtype; }


Input: Generate a linearly spaced vector [CODESPLIT] @ Override public INDArray linspace ( int lower , int upper , int num ) { double [ ] data = new double [ num ] ; for ( int i = 0 ; i < num ; i ++ ) { double t = ( double ) i / ( num - 1 ) ; data [ i ] = lower * ( 1 - t ) + t * upper ; } //edge case for scalars INDArray ret = Nd4j . create ( data . length ) ; if ( ret . isScalar ( ) ) return ret ; for ( int i = 0 ; i < ret . length ( ) ; i ++ ) ret . putScalar ( i , data [ i ] ) ; return ret ; }


Input: Returns a vector with all of the elements in every nd array equal to the sum of the lengths of the ndarrays [CODESPLIT] @ Override public INDArray toFlattened ( Collection < INDArray > matrices ) { int length = 0 ; for ( INDArray m : matrices ) length += m . length ( ) ; INDArray ret = Nd4j . create ( 1 , length ) ; int linearIndex = 0 ; for ( INDArray d : matrices ) { ret . put ( new INDArrayIndex [ ] { NDArrayIndex . interval ( linearIndex , linearIndex + d . length ( ) ) } , d ) ; linearIndex += d . length ( ) ; } return ret ; }


Input: Returns a column vector where each entry is the nth bilinear product of the nth slices of the two tensors . [CODESPLIT] @ Override public INDArray bilinearProducts ( INDArray curr , INDArray in ) { assert curr . shape ( ) . length == 3 ; if ( in . columns ( ) != 1 ) { throw new AssertionError ( "Expected a column vector" ) ; } if ( in . rows ( ) != curr . size ( curr . shape ( ) . length - 1 ) ) { throw new AssertionError ( "Number of rows in the input does not match number of columns in tensor" ) ; } if ( curr . size ( curr . shape ( ) . length - 2 ) != curr . size ( curr . shape ( ) . length - 1 ) ) { throw new AssertionError ( "Can only perform this operation on a SimpleTensor with square slices" ) ; } INDArray ret = Nd4j . create ( curr . slices ( ) , 1 ) ; INDArray inT = in . transpose ( ) ; for ( int i = 0 ; i < curr . slices ( ) ; i ++ ) { INDArray slice = curr . slice ( i ) ; INDArray inTTimesSlice = inT . mmul ( slice ) ; ret . putScalar ( i , Nd4j . getBlasWrapper ( ) . dot ( inTTimesSlice , in ) ) ; } return ret ; }


Input: Reverses the passed in matrix such that m [ 0 ] becomes m [ m . length - 1 ] etc [CODESPLIT] @ Override public INDArray reverse ( INDArray reverse ) { // FIXME: native method should be used instead INDArray rev = reverse . linearView ( ) ; INDArray ret = Nd4j . create ( rev . shape ( ) ) ; int count = 0 ; for ( long i = rev . length ( ) - 1 ; i >= 0 ; i -- ) { ret . putScalar ( count ++ , rev . getFloat ( i ) ) ; } return ret . reshape ( reverse . shape ( ) ) ; }


Input: Merge the vectors and append a bias . Each vector must be either row or column vectors . An exception is thrown for inconsistency ( mixed row and column vectors ) [CODESPLIT] @ Override public INDArray appendBias ( INDArray ... vectors ) { int size = 0 ; for ( INDArray vector : vectors ) { size += vector . rows ( ) ; } INDArray result = Nd4j . create ( size + 1 , vectors [ 0 ] . columns ( ) ) ; int index = 0 ; for ( INDArray vector : vectors ) { INDArray put = toFlattened ( vector , Nd4j . ones ( 1 ) ) ; result . put ( new INDArrayIndex [ ] { NDArrayIndex . interval ( index , index + vector . rows ( ) + 1 ) , NDArrayIndex . interval ( 0 , vectors [ 0 ] . columns ( ) ) } , put ) ; index += vector . rows ( ) ; } return result ; }


Input: Create a random ( uniform 0 - 1 ) NDArray with the specified shape and order [CODESPLIT] @ Override public INDArray rand ( char order , long rows , long columns ) { return Nd4j . getRandom ( ) . nextDouble ( order , new long [ ] { rows , columns } ) ; }


Input: Generate a random normal N ( 0 1 ) with the specified order and shape [CODESPLIT] @ Override public INDArray randn ( char order , long rows , long columns ) { return Nd4j . getRandom ( ) . nextGaussian ( order , new long [ ] { rows , columns } ) ; }


Input: Creates an ndarray with the specified data [CODESPLIT] @ Override public IComplexNDArray createComplex ( double [ ] data ) { assert data . length % 2 == 0 : "Length of data must be even. A complex ndarray is made up of pairs of real and imaginary components" ; return createComplex ( data , new int [ ] { 1 , data . length / 2 } ) ; }


Input: Creates an 1 x num ndarray with the specified value [CODESPLIT] @ Override public IComplexNDArray complexValueOf ( int num , double value ) { IComplexNDArray ones = complexOnes ( num ) ; ones . assign ( Nd4j . createDouble ( value , 0.0 ) ) ; return ones ; }


Input: Creates an shape ndarray with the specified value [CODESPLIT] @ Override public IComplexNDArray complexValueOf ( int [ ] shape , double value ) { IComplexNDArray ones = complexOnes ( shape ) ; ones . assign ( Nd4j . scalar ( value ) ) ; return ones ; }


Input: concatenate ndarrays along a dimension [CODESPLIT] @ Override public INDArray concat ( int dimension , INDArray ... toConcat ) { if ( toConcat . length == 1 ) return toConcat [ 0 ] ; int sumAlongDim = 0 ; boolean allC = toConcat [ 0 ] . ordering ( ) == ' ' ; long [ ] outputShape = ArrayUtil . copy ( toConcat [ 0 ] . shape ( ) ) ; outputShape [ dimension ] = sumAlongDim ; for ( int i = 0 ; i < toConcat . length ; i ++ ) { sumAlongDim += toConcat [ i ] . size ( dimension ) ; allC = allC && toConcat [ i ] . ordering ( ) == ' ' ; for ( int j = 0 ; j < toConcat [ i ] . rank ( ) ; j ++ ) { if ( j != dimension && toConcat [ i ] . size ( j ) != outputShape [ j ] && ! toConcat [ i ] . isVector ( ) ) { throw new IllegalArgumentException ( "Illegal concatenation at array " + i + " and shape element " + j ) ; } } } long [ ] sortedStrides = Nd4j . getStrides ( outputShape ) ; INDArray ret = Nd4j . create ( outputShape , sortedStrides ) ; allC &= ( ret . ordering ( ) == ' ' ) ; if ( toConcat [ 0 ] . isScalar ( ) ) { INDArray retLinear = ret . linearView ( ) ; for ( int i = 0 ; i < retLinear . length ( ) ; i ++ ) retLinear . putScalar ( i , toConcat [ i ] . getDouble ( 0 ) ) ; return ret ; } if ( dimension == 0 && allC ) { int currBuffer = 0 ; int currBufferOffset = 0 ; for ( int i = 0 ; i < ret . length ( ) ; i ++ ) { ret . data ( ) . put ( i , toConcat [ currBuffer ] . data ( ) . getDouble ( toConcat [ currBuffer ] . offset ( ) + currBufferOffset ++ ) ) ; if ( currBufferOffset >= toConcat [ currBuffer ] . length ( ) ) { currBuffer ++ ; currBufferOffset = 0 ; } } return ret ; } int arrOffset = 0 ; // FIXME: int cast INDArray [ ] retAlongDimensionArrays = new INDArray [ ( int ) ret . tensorssAlongDimension ( dimension ) ] ; for ( int i = 0 ; i < retAlongDimensionArrays . length ; i ++ ) retAlongDimensionArrays [ i ] = ret . tensorAlongDimension ( i , dimension ) ; for ( INDArray arr : toConcat ) { long arrTensorLength = - 1 ; if ( arr . tensorssAlongDimension ( dimension ) != ret . tensorssAlongDimension ( dimension ) ) throw new IllegalStateException ( "Illegal concatenate. Tensors along dimension must be same length." ) ; for ( int i = 0 ; i < arr . tensorssAlongDimension ( dimension ) ; i ++ ) { INDArray retLinear = retAlongDimensionArrays [ i ] ; INDArray arrTensor = arr . tensorAlongDimension ( i , dimension ) ; arrTensorLength = arrTensor . length ( ) ; for ( int j = 0 ; j < arrTensor . length ( ) ; j ++ ) { int idx = j + arrOffset ; retLinear . putScalar ( idx , arrTensor . getDouble ( j ) ) ; } } //bump the sliding window arrOffset += arrTensorLength ; } return ret ; }


Input: Create an ndarray of ones [CODESPLIT] @ Override public INDArray ones ( int [ ] shape ) { //ensure shapes that wind up being scalar end up with the write shape if ( shape . length == 1 && shape [ 0 ] == 0 ) { shape = new int [ ] { 1 , 1 } ; } INDArray ret = create ( shape ) ; ret . assign ( 1 ) ; return ret ; }


Input: Create an ndarray of ones [CODESPLIT] @ Override public IComplexNDArray complexOnes ( int [ ] shape ) { IComplexNDArray ret = createComplex ( shape ) ; ret . assign ( 1 ) ; return ret ; }


Input: Creates a complex ndarray with the specified shape [CODESPLIT] public IComplexNDArray createComplex ( float [ ] data , int [ ] shape , int [ ] stride , long offset ) { //ensure shapes that wind up being scalar end up with the write shape if ( shape . length == 1 && shape [ 0 ] == 0 ) { shape = new int [ ] { 1 , 1 } ; } return createComplex ( Nd4j . createBuffer ( data ) , shape , stride , offset , Nd4j . order ( ) ) ; }


Input: Create an ndrray with the specified shape [CODESPLIT] @ Override public INDArray create ( float [ ] data , int [ ] shape ) { //ensure shapes that wind up being scalar end up with the write shape if ( shape . length == 1 && shape [ 0 ] == 0 ) { shape = new int [ ] { 1 , 1 } ; } return create ( data , shape , Nd4j . getStrides ( shape ) , 0 ) ; }


Input: Create an ndrray with the specified shape [CODESPLIT] @ Override public IComplexNDArray createComplex ( double [ ] data , int [ ] shape ) { //ensure shapes that wind up being scalar end up with the write shape if ( shape . length == 1 && shape [ 0 ] == 0 ) { shape = new int [ ] { 1 , 1 } ; } return createComplex ( data , shape , Nd4j . getComplexStrides ( shape ) , 0 ) ; }


Input: Create an ndrray with the specified shape [CODESPLIT] @ Override public IComplexNDArray createComplex ( float [ ] data , int [ ] shape , int [ ] stride ) { return createComplex ( data , shape , stride , 0 ) ; }


Input: Creates a complex ndarray with the specified shape [CODESPLIT] public IComplexNDArray createComplex ( int [ ] shape , int [ ] stride , long offset ) { if ( Nd4j . dataType ( ) == DataBuffer . Type . DOUBLE ) return createComplex ( new double [ ArrayUtil . prod ( shape ) * 2 ] , shape , stride , offset ) ; if ( Nd4j . dataType ( ) == DataBuffer . Type . FLOAT || Nd4j . dataType ( ) == DataBuffer . Type . HALF ) return createComplex ( new float [ ArrayUtil . prod ( shape ) * 2 ] , shape , stride , offset ) ; throw new IllegalStateException ( "Illegal data opType " + Nd4j . dataType ( ) ) ; }


Input: Create a scalar ndarray with the specified offset [CODESPLIT] @ Override public IComplexNDArray complexScalar ( Number value , long offset ) { if ( Nd4j . dataType ( ) == DataBuffer . Type . DOUBLE ) return scalar ( createDouble ( value . doubleValue ( ) , 0 ) , offset ) ; if ( Nd4j . dataType ( ) == DataBuffer . Type . FLOAT || Nd4j . dataType ( ) == DataBuffer . Type . INT || Nd4j . dataType ( ) == DataBuffer . Type . HALF ) return scalar ( createFloat ( value . floatValue ( ) , 0 ) , offset ) ; throw new IllegalStateException ( "Illegal data opType " + Nd4j . dataType ( ) ) ; }


Input: Create a scalar nd array with the specified value and offset [CODESPLIT] @ Override public INDArray scalar ( float value ) { if ( Nd4j . dataType ( ) == DataBuffer . Type . FLOAT || Nd4j . dataType ( ) == DataBuffer . Type . HALF ) return create ( new float [ ] { value } , new int [ ] { 1 , 1 } , new int [ ] { 1 , 1 } , 0 ) ; else if ( Nd4j . dataType ( ) == DataBuffer . Type . DOUBLE ) return scalar ( ( double ) value ) ; else return scalar ( ( int ) value ) ; }


Input: Create a scalar nd array with the specified value and offset [CODESPLIT] @ Override public INDArray scalar ( double value ) { if ( Nd4j . dataType ( ) == DataBuffer . Type . DOUBLE ) return create ( new double [ ] { value } , new int [ ] { 1 , 1 } , new int [ ] { 1 , 1 } , 0 ) ; else return scalar ( ( float ) value ) ; }


Input: Create a scalar ndarray with the specified offset [CODESPLIT] @ Override public IComplexNDArray scalar ( IComplexNumber value , long offset ) { if ( Nd4j . dataType ( ) == DataBuffer . Type . DOUBLE ) return scalar ( value . asDouble ( ) , offset ) ; if ( Nd4j . dataType ( ) == DataBuffer . Type . FLOAT || Nd4j . dataType ( ) == DataBuffer . Type . HALF ) return scalar ( value . asFloat ( ) , offset ) ; throw new IllegalStateException ( "Illegal data opType " + Nd4j . dataType ( ) ) ; }


Input: Create a scalar nd array with the specified value and offset [CODESPLIT] @ Override public IComplexNDArray scalar ( IComplexFloat value ) { return createComplex ( new float [ ] { value . realComponent ( ) , value . imaginaryComponent ( ) } , new int [ ] { 1 } , new int [ ] { 1 } , 0 ) ; }


Input: Returns true if the updater has accumulated enough ndarrays to replicate to the workers [CODESPLIT] @ Override public boolean shouldReplicate ( ) { long now = System . currentTimeMillis ( ) ; long diff = Math . abs ( now - lastSynced ) ; return diff > syncTime ; }


Input: Subtract two complex numbers in - place [CODESPLIT] @ Override public IComplexNumber subi ( IComplexNumber c , IComplexNumber result ) { return result . set ( realComponent ( ) . doubleValue ( ) - c . realComponent ( ) . doubleValue ( ) , imaginaryComponent ( ) . doubleValue ( ) - c . imaginaryComponent ( ) . doubleValue ( ) ) ; }


Input: Multiply two complex numbers inplace [CODESPLIT] @ Override public IComplexNumber muli ( IComplexNumber c , IComplexNumber result ) { double newR = real * c . realComponent ( ) . doubleValue ( ) - imag * c . imaginaryComponent ( ) . doubleValue ( ) ; double newI = real * c . imaginaryComponent ( ) . doubleValue ( ) + imag * c . realComponent ( ) . doubleValue ( ) ; result . set ( newR , newI ) ; return result ; }


Input: Divide two complex numbers in - place [CODESPLIT] @ Override public IComplexNumber divi ( IComplexNumber c , IComplexNumber result ) { double d = c . realComponent ( ) . doubleValue ( ) * c . realComponent ( ) . doubleValue ( ) + c . imaginaryComponent ( ) . doubleValue ( ) * c . imaginaryComponent ( ) . doubleValue ( ) ; double newR = ( realComponent ( ) * c . realComponent ( ) . doubleValue ( ) + imaginaryComponent ( ) * c . imaginaryComponent ( ) . doubleValue ( ) ) / d ; double newI = ( imaginaryComponent ( ) * c . realComponent ( ) . doubleValue ( ) - realComponent ( ) * c . imaginaryComponent ( ) . doubleValue ( ) ) / d ; result . set ( newR , newI ) ; return result ; }


Input: Create complex number where the [CODESPLIT] public static IComplexNumber [ ] [ ] complexNumbersFor ( float [ ] [ ] realComponents ) { IComplexNumber [ ] [ ] ret = new IComplexNumber [ realComponents . length ] [ realComponents [ 0 ] . length ] ; for ( int i = 0 ; i < realComponents . length ; i ++ ) for ( int j = 0 ; j < realComponents [ i ] . length ; j ++ ) ret [ i ] [ j ] = Nd4j . createComplexNumber ( realComponents [ i ] [ j ] , 0 ) ; return ret ; }


Input: Create complex number where the [CODESPLIT] public static IComplexNumber [ ] complexNumbersFor ( float [ ] realComponents ) { IComplexNumber [ ] ret = new IComplexNumber [ realComponents . length ] ; for ( int i = 0 ; i < realComponents . length ; i ++ ) ret [ i ] = Nd4j . createComplexNumber ( realComponents [ i ] , 0 ) ; return ret ; }


Input: Return the sin value of the given complex number [CODESPLIT] public static IComplexNumber atan ( IComplexNumber num ) { Complex c = new Complex ( num . realComponent ( ) . doubleValue ( ) , num . imaginaryComponent ( ) . doubleValue ( ) ) . atan ( ) ; return Nd4j . createDouble ( c . getReal ( ) , c . getImaginary ( ) ) ; }


Input: Return the ceiling value of the given complex number [CODESPLIT] public static IComplexNumber ceil ( IComplexNumber num ) { Complex c = new Complex ( FastMath . ceil ( num . realComponent ( ) . doubleValue ( ) ) , FastMath . ceil ( num . imaginaryComponent ( ) . doubleValue ( ) ) ) ; return Nd4j . createDouble ( c . getReal ( ) , c . getImaginary ( ) ) ; }


Input: Return the log value of the given complex number [CODESPLIT] public static IComplexNumber neg ( IComplexNumber num ) { Complex c = new Complex ( num . realComponent ( ) . doubleValue ( ) , num . imaginaryComponent ( ) . doubleValue ( ) ) . negate ( ) ; return Nd4j . createDouble ( c . getReal ( ) , c . getImaginary ( ) ) ; }


Input: Return the absolute value of the given complex number [CODESPLIT] public static IComplexNumber abs ( IComplexNumber num ) { double c = new Complex ( num . realComponent ( ) . doubleValue ( ) , num . imaginaryComponent ( ) . doubleValue ( ) ) . abs ( ) ; return Nd4j . createDouble ( c , 0 ) ; }


Input: Raise a complex number to a power [CODESPLIT] public static IComplexNumber pow ( IComplexNumber num , IComplexNumber power ) { Complex c = new Complex ( num . realComponent ( ) . doubleValue ( ) , num . imaginaryComponent ( ) . doubleValue ( ) ) . pow ( new Complex ( power . realComponent ( ) . doubleValue ( ) , power . imaginaryComponent ( ) . doubleValue ( ) ) ) ; if ( c . isNaN ( ) ) c = new Complex ( Nd4j . EPS_THRESHOLD , 0.0 ) ; return Nd4j . createDouble ( c . getReal ( ) , c . getImaginary ( ) ) ; }


Input: Returns the exp of a complex number : Let r be the realComponent component and i be the imaginary Let ret be the complex number returned ret - > exp ( r ) * cos ( i ) exp ( r ) * sin ( i ) where the first number is the realComponent component and the second number is the imaginary component [CODESPLIT] public static IComplexNumber exp ( IComplexNumber d ) { if ( d instanceof IComplexFloat ) return exp ( ( IComplexFloat ) d ) ; return exp ( ( IComplexDouble ) d ) ; }


Input: Returns the exp of a complex number : Let r be the realComponent component and i be the imaginary Let ret be the complex number returned ret - > exp ( r ) * cos ( i ) exp ( r ) * sin ( i ) where the first number is the realComponent component and the second number is the imaginary component [CODESPLIT] public static IComplexDouble exp ( IComplexDouble d ) { return Nd4j . createDouble ( FastMath . exp ( d . realComponent ( ) ) * FastMath . cos ( d . imaginaryComponent ( ) ) , FastMath . exp ( d . realComponent ( ) ) * FastMath . sin ( d . imaginaryComponent ( ) ) ) ; }


Input: Returns the exp of a complex number : Let r be the realComponent component and i be the imaginary Let ret be the complex number returned ret - > exp ( r ) * cos ( i ) exp ( r ) * sin ( i ) where the first number is the realComponent component and the second number is the imaginary component [CODESPLIT] public static IComplexFloat exp ( IComplexFloat d ) { return Nd4j . createFloat ( ( float ) FastMath . exp ( d . realComponent ( ) ) * ( float ) FastMath . cos ( d . imaginaryComponent ( ) ) , ( float ) FastMath . exp ( d . realComponent ( ) ) * ( float ) FastMath . sin ( d . imaginaryComponent ( ) ) ) ; }


Input: get a context [CODESPLIT] public Aeron . Context getContext ( ) { Aeron . Context ctx = new Aeron . Context ( ) . publicationConnectionTimeout ( - 1 ) . availableImageHandler ( AeronUtil :: printAvailableImage ) . unavailableImageHandler ( AeronUtil :: printUnavailableImage ) . aeronDirectoryName ( mediaDriverDirectoryName ) . keepAliveInterval ( 100000 ) . errorHandler ( e -> log . error ( e . toString ( ) , e ) ) ; return ctx ; }


Input: This method frees specific chunk of memory described by AllocationPoint passed in [CODESPLIT] @ Override public void free ( AllocationPoint point ) { if ( point . getAllocationStatus ( ) == AllocationStatus . DEVICE ) { if ( point . isConstant ( ) ) return ; AllocationShape shape = point . getShape ( ) ; int deviceId = point . getDeviceId ( ) ; long address = point . getDevicePointer ( ) . address ( ) ; long reqMemory = AllocationUtils . getRequiredMemory ( shape ) ; // we don't cache too big objects if ( reqMemory > CudaEnvironment . getInstance ( ) . getConfiguration ( ) . getMaximumDeviceCacheableLength ( ) || deviceCachedAmount . get ( deviceId ) . get ( ) >= CudaEnvironment . getInstance ( ) . getConfiguration ( ) . getMaximumHostCache ( ) ) { //log.info("DEVICE_{} memory purging: {} bytes; MS: {}; MT: {}", deviceId, reqMemory, MAX_GPU_ALLOCATION, MAX_GPU_CACHE); super . free ( point ) ; return ; } //            log.info("Saving HOST memory into cache..."); ensureDeviceCacheHolder ( deviceId , shape ) ; CacheHolder cache = deviceCache . get ( deviceId ) . get ( shape ) ; if ( point . getDeviceId ( ) != deviceId ) throw new RuntimeException ( "deviceId changed!" ) ; // memory chunks < threshold will be cached no matter what if ( reqMemory <= FORCED_CACHE_THRESHOLD ) { cache . put ( new CudaPointer ( point . getDevicePointer ( ) . address ( ) ) ) ; return ; } else { long cacheEntries = cache . size ( ) ; long cacheHeight = deviceCache . get ( deviceId ) . size ( ) ; // total memory allocated within this bucket long cacheDepth = cacheEntries * reqMemory ; //if (cacheDepth < MAX_CACHED_MEMORY / cacheHeight) { cache . put ( new CudaPointer ( point . getDevicePointer ( ) . address ( ) ) ) ; return ; //} else { //    super.free(point); // } } } super . free ( point ) ; }


Input: Returns a subset of this array based on the specified indexes [CODESPLIT] @ Override public INDArray get ( INDArrayIndex ... indexes ) { //check for row/column vector and point index being 0 if ( indexes . length == 1 && indexes [ 0 ] instanceof NDArrayIndexAll || ( indexes . length == 2 && ( isRowVector ( ) && indexes [ 0 ] instanceof PointIndex && indexes [ 0 ] . offset ( ) == 0 && indexes [ 1 ] instanceof NDArrayIndexAll || isColumnVector ( ) && indexes [ 1 ] instanceof PointIndex && indexes [ 0 ] . offset ( ) == 0 && indexes [ 0 ] instanceof NDArrayIndexAll ) ) ) return this ; indexes = NDArrayIndex . resolve ( shapeInfoDataBuffer ( ) , indexes ) ; ShapeOffsetResolution resolution = new ShapeOffsetResolution ( this ) ; resolution . exec ( indexes ) ; if ( indexes . length < 1 ) throw new IllegalStateException ( "Invalid index found of zero length" ) ; // FIXME: LONG int [ ] shape = LongUtils . toInts ( resolution . getShapes ( ) ) ; int numSpecifiedIndex = 0 ; for ( int i = 0 ; i < indexes . length ; i ++ ) if ( indexes [ i ] instanceof SpecifiedIndex ) numSpecifiedIndex ++ ; if ( shape != null && numSpecifiedIndex > 0 ) { // TODO create a new ndarray with the specified indexes return null ; } INDArray ret = subArray ( resolution ) ; return ret ; }