/** * For direct buffers, calls {@link Pointer#Pointer(Buffer)}, while for buffers * backed with an array, allocates enough memory for the array and copies it. * * @param buffer the Buffer to reference or copy * @see #put(double[]) */ public DoublePointer(DoubleBuffer buffer) { super(buffer); if (buffer != null && !buffer.isDirect() && buffer.hasArray()) { double[] array = buffer.array(); allocateArray(array.length - buffer.arrayOffset()); put(array, buffer.arrayOffset(), array.length - buffer.arrayOffset()); position(buffer.position()); limit(buffer.limit()); } } /**
/** * Creates a double array from the provided {@link DoubleBuffer}. * * @param buffer {@link DoubleBuffer} the data source. * @return double array containing the data of the buffer. */ public static double[] getDoubleArrayFromBuffer(DoubleBuffer buffer) { double[] array = null; if (buffer.hasArray()) { array = buffer.array(); } else { buffer.rewind(); array = new double[buffer.capacity()]; buffer.get(array); } return array; }
/** * For direct buffers, calls {@link Pointer#Pointer(Buffer)}, while for buffers * backed with an array, allocates enough memory for the array and copies it. * * @param buffer the Buffer to reference or copy * @see #put(double[]) */ public DoublePointer(DoubleBuffer buffer) { super(buffer); if (buffer != null && !buffer.isDirect() && buffer.hasArray()) { double[] array = buffer.array(); allocateArray(array.length - buffer.arrayOffset()); put(array, buffer.arrayOffset(), array.length - buffer.arrayOffset()); position(buffer.position()); limit(buffer.limit()); } } /**
public static double[] decodeDoubles(String data) { ByteBuffer buffer = ByteBuffer.wrap(Base64.decodeBase64(data)); double[] ret = buffer.asDoubleBuffer().array(); KLAB.info("decompressed " + ret.length + " data: some are " + ret[212] + ", " + ret[3442]); return ret; }
double[] array = buffer.array(); int offset = buffer.arrayOffset(); int length = array.length - offset;
@Override public double[] toDoubleArray() { return toDoubleBuffer().array(); }
setDoublesAtOffset(byteOffset, values.array(), (int)(values.arrayOffset() + valuesOffset), (int)length);
/** * ByteChannelからdouble配列を読み込む * @param channel * @return * @throws IOException */ public static double[] readDoubleArray(@NonNull final ByteChannel channel) throws IOException { final int n = readInt(channel); final ByteBuffer buf = ByteBuffer.allocate(n * 8).order(ByteOrder.BIG_ENDIAN); final int readBytes = channel.read(buf); if (readBytes != n * 8) throw new IOException(); buf.clear(); final DoubleBuffer result = buf.asDoubleBuffer(); if (result.hasArray()) { return result.array(); } else { final double[] b = new double[n]; result.get(b); return b; } }
DoubleBuffer t = DoubleBuffer.wrap(buffer.array()); int elementOffset = buffer.position() + buffer.arrayOffset(); result = Pointer.to(t).withByteOffset(
/** * Creates a double array from the provided {@link DoubleBuffer}. * * @param buffer {@link DoubleBuffer} the data source. * @return double array containing the data of the buffer. */ public static double[] getDoubleArrayFromBuffer(DoubleBuffer buffer) { double[] array = null; if (buffer.hasArray()) { array = buffer.array(); } else { buffer.rewind(); array = new double[buffer.capacity()]; buffer.get(array); } return array; }
/** * Creates a new Pointer to the given buffer, taking into * account the position and array offset of the given buffer. * The buffer is assumed to be non-<code>null</code>, and * be either direct or have a backing array. * * @param buffer The buffer * @return The pointer */ private static Pointer computePointer(DoubleBuffer buffer) { Pointer result = null; if (buffer.isDirect()) { int oldPosition = buffer.position(); buffer.position(0); result = Pointer.to(buffer.slice()).withByteOffset( oldPosition * Sizeof.DOUBLE); buffer.position(oldPosition); } else if (buffer.hasArray()) { DoubleBuffer t = DoubleBuffer.wrap(buffer.array()); int elementOffset = buffer.position() + buffer.arrayOffset(); result = Pointer.to(t).withByteOffset( elementOffset * Sizeof.DOUBLE); } return result; }
static <A> A getIndirectArray(Buffer buffer, int length, Class<A> arrayClass) { if (buffer instanceof IntBuffer && arrayClass == int[].class) return (A)((IntBuffer)buffer).array(); if (buffer instanceof ShortBuffer && arrayClass == short[].class) return (A)((ShortBuffer)buffer).array(); if (buffer instanceof ByteBuffer && arrayClass == byte[].class) return (A)((ByteBuffer)buffer).array(); if (buffer instanceof FloatBuffer && arrayClass == float[].class) return (A)((FloatBuffer)buffer).array(); if (buffer instanceof LongBuffer && arrayClass == long[].class) return (A)((LongBuffer)buffer).array(); if (buffer instanceof DoubleBuffer && arrayClass == double[].class) return (A)((DoubleBuffer)buffer).array(); if (buffer instanceof CharBuffer && arrayClass == char[].class) return (A)((CharBuffer)buffer).array(); return null; } public static boolean isSubRaster(Raster raster) {
/** * Creates a new Pointer to the given buffer, taking into * account the position and array offset of the given buffer. * The buffer is assumed to be non-<code>null</code>, and * be either direct or have a backing array. * * @param buffer The buffer * @return The pointer */ private static Pointer computePointer(DoubleBuffer buffer) { Pointer result = null; if (buffer.isDirect()) { int oldPosition = buffer.position(); buffer.position(0); result = Pointer.to(buffer.slice()).withByteOffset( oldPosition * Sizeof.DOUBLE); buffer.position(oldPosition); } else if (buffer.hasArray()) { DoubleBuffer t = DoubleBuffer.wrap(buffer.array()); int elementOffset = buffer.position() + buffer.arrayOffset(); result = Pointer.to(t).withByteOffset( elementOffset * Sizeof.DOUBLE); } return result; }
static <A> A getIndirectArray(Buffer buffer, int length, Class<A> arrayClass) { if (buffer instanceof IntBuffer && arrayClass == int[].class) return (A)((IntBuffer)buffer).array(); if (buffer instanceof ShortBuffer && arrayClass == short[].class) return (A)((ShortBuffer)buffer).array(); if (buffer instanceof ByteBuffer && arrayClass == byte[].class) return (A)((ByteBuffer)buffer).array(); if (buffer instanceof FloatBuffer && arrayClass == float[].class) return (A)((FloatBuffer)buffer).array(); if (buffer instanceof LongBuffer && arrayClass == long[].class) return (A)((LongBuffer)buffer).array(); if (buffer instanceof DoubleBuffer && arrayClass == double[].class) return (A)((DoubleBuffer)buffer).array(); if (buffer instanceof CharBuffer && arrayClass == char[].class) return (A)((CharBuffer)buffer).array(); return null; } public static boolean isSubRaster(Raster raster) {
/** * Creates a new Pointer to the given buffer, taking into * account the position and array offset of the given buffer. * The buffer is assumed to be non-<code>null</code>, and * be either direct or have a backing array. * * @param buffer The buffer * @return The pointer */ private static Pointer computePointer(DoubleBuffer buffer) { Pointer result = null; if (buffer.isDirect()) { int oldPosition = buffer.position(); buffer.position(0); result = Pointer.to(buffer.slice()).withByteOffset( oldPosition * Sizeof.DOUBLE); buffer.position(oldPosition); } else if (buffer.hasArray()) { DoubleBuffer t = DoubleBuffer.wrap(buffer.array()); int elementOffset = buffer.position() + buffer.arrayOffset(); result = Pointer.to(t).withByteOffset( elementOffset * Sizeof.DOUBLE); } return result; }
public Float64Array copy(DoubleBuffer buffer) { buffer = buffer.duplicate(); Float64Array result = Float64Array.create(buffer.remaining()); double[] tmp; if (buffer.hasArray()) { tmp = buffer.array(); } else { tmp = new double[buffer.remaining()]; buffer.get(tmp); } for (int i = 0; i < tmp.length; ++i) { result.set(i, tmp[i]); } return result; }
static ExecResult<DoubleBuffer> testJava_double_aSinB(int loops, int dataSize) throws CLBuildException { DoubleBuffer aBuffer = DoubleBuffer.allocate(dataSize); DoubleBuffer bBuffer = DoubleBuffer.allocate(dataSize); DoubleBuffer outputBuffer = DoubleBuffer.allocate(dataSize); double[] a = aBuffer.array(), b = bBuffer.array(), output = outputBuffer.array(); if (warmup) { System.out.print("Warming up Java operations..."); for (int i = 0; i < 3000; i++) { java_aSinB(a, b, output, 100); } System.out.println(); } fillBuffersWithSomeDatad(aBuffer, bBuffer); gc(); long start = System.nanoTime(); for (int i = 0; i < loops; i++) { java_aSinB(a, b, output, dataSize); } long time = System.nanoTime() - start; System.out.println("Java operations : " + time + "ns"); return new ExecResult<DoubleBuffer>(outputBuffer, time / (loops * (double) dataSize)); }
/** @return Serializable thrift representation */ public Word2VecModelThrift toThrift() { double[] vectorsArray; if(vectors.hasArray()) { vectorsArray = vectors.array(); } else { vectorsArray = new double[vectors.limit()]; vectors.position(0); vectors.get(vectorsArray); } return new Word2VecModelThrift() .setVocab(vocab) .setLayerSize(layerSize) .setVectors(Doubles.asList(vectorsArray)); }
/** * For direct buffers, calls {@link Pointer#Pointer(Buffer)}, while for buffers * backed with an array, allocates enough memory for the array and copies it. * * @param buffer the Buffer to reference or copy * @see #put(double[]) */ public DoublePointer(DoubleBuffer buffer) { super(buffer); if (buffer != null && buffer.hasArray()) { double[] array = buffer.array(); allocateArray(array.length); put(array); position(buffer.position()); limit(buffer.limit()); } } /**
/** @return Serializable thrift representation */ public Word2VecModelThrift toThrift() { double[] vectorsArray; if(vectors.length == 1 && vectors[0].hasArray()) { vectorsArray = vectors[0].array(); } else { int totalSize = 0; for (DoubleBuffer buffer : vectors) { totalSize += buffer.limit(); } vectorsArray = new double[totalSize]; int copiedCount = 0; for (DoubleBuffer buffer : vectors) { int size = buffer.limit(); buffer.position(0); buffer.get(vectorsArray, copiedCount, size); copiedCount += size; } } return new Word2VecModelThrift() .setVocab(vocab) .setLayerSize(layerSize) .setVectors(Doubles.asList(vectorsArray)); }