private void create() throws IOException{ if( isCreate ){ return; } ICompressor compressor = FindCompressor.get( columnBinary.compressorClassName ); byte[] binary = compressor.decompress( columnBinary.binary , binaryStart , binaryLength ); ByteBuffer wrapBuffer = ByteBuffer.wrap( binary ); int nullFlagBinaryLength = wrapBuffer.getInt(); int doubleBinaryLength = wrapBuffer.getInt(); int nullFlagBinaryStart = Integer.BYTES * 2; int doubleBinaryStart = nullFlagBinaryStart + nullFlagBinaryLength; DoubleBuffer doubleBuffer = ByteBuffer.wrap( binary , doubleBinaryStart , doubleBinaryLength ).asDoubleBuffer(); IDicManager dicManager = new DoubleDicManager( binary , nullFlagBinaryStart , nullFlagBinaryLength , doubleBuffer ); column = new PrimitiveColumn( columnBinary.columnType , columnBinary.columnName ); column.setCellManager( new BufferDirectCellManager( ColumnType.DOUBLE , dicManager , nullFlagBinaryLength ) ); isCreate = true; }
/** * Creates a clone of the given buffer. The clone's capacity is * equal to the given buffer's limit. * * @param buf The buffer to clone * @return The cloned buffer */ public static Buffer clone(Buffer buf) { if (buf instanceof FloatBuffer) { return clone((FloatBuffer) buf); } else if (buf instanceof ShortBuffer) { return clone((ShortBuffer) buf); } else if (buf instanceof ByteBuffer) { return clone((ByteBuffer) buf); } else if (buf instanceof IntBuffer) { return clone((IntBuffer) buf); } else if (buf instanceof DoubleBuffer) { return clone((DoubleBuffer) buf); } else { throw new UnsupportedOperationException(); } }
@Test(dataProvider = "doubleViewProvider") public void testDoubleGet(String desc, IntFunction<ByteBuffer> fbb, Function<ByteBuffer, DoubleBuffer> fbi) { ByteBuffer bb = allocate(fbb); DoubleBuffer vb = fbi.apply(bb); int o = bb.position(); for (int i = 0; i < vb.limit(); i++) { double fromBytes = getDoubleFromBytes(bb, o + i * 8); double fromMethodView = bb.getDouble(o + i * 8); assertValues(i, fromBytes, fromMethodView, bb); double fromBufferView = vb.get(i); assertValues(i, fromMethodView, fromBufferView, bb, vb); } for (int i = 0; i < vb.limit(); i++) { double v = getDoubleFromBytes(bb, o + i * 8); double a = bb.getDouble(); assertValues(i, v, a, bb); double b = vb.get(); assertValues(i, a, b, bb, vb); } }
@Override protected void doUpdate(ServerDenseDoubleRow[] rows, double[] scalars) { Random rand = new Random(System.currentTimeMillis()); try { rows[0].getLock().writeLock().lock(); double min = scalars[0]; double max = scalars[1]; double factor = max - min; DoubleBuffer data = rows[0].getData(); int size = rows[0].size(); for (int i = 0; i < size; i++) { data.put(i, factor * rand.nextDouble() + min); } } finally { rows[0].getLock().writeLock().unlock(); } }
private void initializeCrossline(DrawContext dc) { DoubleBuffer verts = crossLine; if (verts == null) verts = Buffers.newDirectDoubleBuffer(points.size() * 3); for (LatLon point : points) { double alt = elevation + baseHeight * exageration * (yMax - yMin) * crossLinePercent; Vec4 p1 = dc.getGlobe().computePointFromPosition(point.getLatitude(), point.getLongitude(), alt); verts.put(p1.x - referenceCenter.x); verts.put(p1.y - referenceCenter.y); verts.put(p1.z - referenceCenter.z); } this.crossLine = verts; }
/** * Creates a new DoubleBuffer with the same contents as the given * DoubleBuffer. The new DoubleBuffer is seperate from the old one and * changes are not reflected across. If you want to reflect changes, * consider using Buffer.duplicate(). * * @param buf * the DoubleBuffer to copy * @return the copy */ public static DoubleBuffer clone(DoubleBuffer buf) { if (buf == null) { return null; } buf.rewind(); DoubleBuffer copy; if (isDirect(buf)) { copy = createDoubleBuffer(buf.limit()); } else { copy = DoubleBuffer.allocate(buf.limit()); } copy.put(buf); return copy; }
public static synchronized void readPcRasterMapValues(Jep jep, String variableName, double[] dest) throws JepException { String bufferName = "readPcRasterMapValues" + dest.length; Map<Integer, DoubleBuffer> map = readPcRasterMapValuesBuffers.get(jep); if (map == null) { map = new HashMap<Integer, DoubleBuffer>(); readPcRasterMapValuesBuffers.put(jep, map); } DoubleBuffer buffer = map.get(dest.length); if (buffer == null) { buffer = buffers.get(dest.length); if (buffer == null) { buffer = createNativeDoubleBuffer(dest.length); buffers.put(dest.length, buffer); } declareDoubleArray(jep, bufferName, buffer); map.put(dest.length, buffer); } jep.eval(bufferName + "[:]=reshape(pcr2numpy(" + variableName + ", 1E31), " + dest.length + ')'); buffer.rewind(); buffer.get(dest); }
public void updateDoubleDenseToDoubleDense(int size, ByteBuf buf, ServerDenseDoubleRow row, int compressRatio) { int bitPerItem = 8 * 8 / compressRatio; DoubleBuffer data = row.getData(); LOG.debug("update double to double, size: " + size); if (size <= 0) return; double maxAbs = buf.readDouble(); int maxPoint = (int) Math.pow(2, bitPerItem - 1) - 1; for (int i = 0; i < size - 1; i++) { byte[] itemBytes = new byte[bitPerItem / 8]; buf.readBytes(itemBytes); int point = byteArray2int(itemBytes); double parsedValue = (double) point / (double) maxPoint * maxAbs; data.put(i, data.get(i) + parsedValue); } LOG.info(String.format("parse compressed %d double data, max abs: %f, max point: %d", size - 1, maxAbs, maxPoint)); }
@Nullable private static double[] byteArray2DoubleArray(byte... array) { if (array == null) { return null; } final int len = array.length; final double[] res = new double[len >> 3]; try { final DoubleBuffer buffer = ByteBuffer.wrap(array).asDoubleBuffer(); for (int i = 0, iLen = len >> 3; i < iLen; ++i) { res[i] = buffer.get(); } } catch (Exception e) { e.printStackTrace(); } return res; }
@Override public ColumnBinary toBinary(final ColumnBinaryMakerConfig commonConfig , final ColumnBinaryMakerCustomConfigNode currentConfigNode , final IColumn column ) throws IOException{ ColumnBinaryMakerConfig currentConfig = commonConfig; if( currentConfigNode != null ){ currentConfig = currentConfigNode.getCurrentConfig(); } byte[] binaryRaw = new byte[ getBinaryLength( column.size() ) ]; ByteBuffer lengthBuffer = ByteBuffer.wrap( binaryRaw ); lengthBuffer.putInt( column.size() ); lengthBuffer.putInt( column.size() * Double.BYTES ); ByteBuffer nullFlagBuffer = ByteBuffer.wrap( binaryRaw , Integer.BYTES * 2 , column.size() ); DoubleBuffer doubleBuffer = ByteBuffer.wrap( binaryRaw , ( Integer.BYTES * 2 + column.size() ) , ( column.size() * Double.BYTES ) ).asDoubleBuffer(); int rowCount = 0; for( int i = 0 ; i < column.size() ; i++ ){ ICell cell = column.get(i); if( cell.getType() == ColumnType.NULL ){ nullFlagBuffer.put( (byte)1 ); doubleBuffer.put( (double)0 ); } else{ rowCount++; PrimitiveCell byteCell = (PrimitiveCell) cell; nullFlagBuffer.put( (byte)0 ); doubleBuffer.put( byteCell.getRow().getDouble() ); } } byte[] binary = currentConfig.compressorClass.compress( binaryRaw , 0 , binaryRaw.length ); return new ColumnBinary( this.getClass().getName() , currentConfig.compressorClass.getClass().getName() , column.getColumnName() , ColumnType.DOUBLE , rowCount , binaryRaw.length , rowCount * Double.BYTES , -1 , binary , 0 , binary.length , null ); }
public DoubleBuffer asDoubleBuffer() { DoubleBuffer buffer = DoubleBuffer.allocate(4*4); for (int i = 0; i < 4; i++) for (int j = 0; j < 4; j++) buffer.put(data[i][j]); buffer.flip(); return buffer; }
private static void writeDoubleBuffer(DoubleBuffer buf) { Buffer viewedBuffer = bufferViews.get(buf); if (viewedBuffer != null) { bufferEndiannessWritten.put(viewedBuffer, buf.order()); } else { throw new AssertionError(); } }
@Override protected double doProcessRow(ServerDenseDoubleRow row) { double qSum = 0; DoubleBuffer data = row.getData(); int size = row.size(); for (int i = 0; i < size; i++) { qSum += Math.pow(data.get(i), 2); } return qSum; }
@Override protected double doProcessRow(ServerDenseDoubleRow row) { double sum = 0.0; DoubleBuffer data = row.getData(); int size = row.size(); for (int i = 0; i < size; i++) { sum += data.get(i); } return sum; }
@Override protected double doProcessRow(ServerDenseDoubleRow row) { double min = Double.POSITIVE_INFINITY; DoubleBuffer data = row.getData(); int size = row.size(); for (int i = 0; i < size; i++) { min = Math.min(min, data.get(i)); } return min; }
@Override protected double doProcessRow(ServerDenseDoubleRow row) { double max = Double.NEGATIVE_INFINITY; DoubleBuffer data = row.getData(); int size = row.size(); for (int i = 0; i < size; i++) { max = Math.max(max, data.get(i)); } return max; }
@Override protected double doProcessRow(ServerDenseDoubleRow row) { double asum = 0.0; DoubleBuffer data = row.getData(); int size = row.size(); for (int i = 0; i < size; i++) { asum += Math.abs(data.get(i)); } return asum; }
@Override protected void doUpdate(ServerDenseDoubleRow[] rows, Serialize func) { MapFunc mapper = (MapFunc) func; DoubleBuffer rowData = rows[0].getData(); int size = rows[0].size(); for (int i = 0; i < size; i++) { rowData.put(i, mapper.call(rowData.get(i))); } }
@Override protected void doUpdate(ServerDenseDoubleRow[] rows, Serialize func) { Zip3MapWithIndexFunc mapper = (Zip3MapWithIndexFunc) func; DoubleBuffer from1 = rows[0].getData(); DoubleBuffer from2 = rows[1].getData(); DoubleBuffer from3 = rows[2].getData(); DoubleBuffer to = rows[3].getData(); int startCol = (int)rows[0].getStartCol(); int size = rows[0].size(); for (int i = 0; i < size; i++) { to.put(i, mapper.call(startCol + i, from1.get(i), from2.get(i), from3.get(i))); } }
private Vector2f getCursorPos() { DoubleBuffer xpos = BufferUtils.createDoubleBuffer(1); DoubleBuffer ypos = BufferUtils.createDoubleBuffer(1); xpos.rewind(); xpos.rewind(); GLFW.glfwGetCursorPos(window.windowID, xpos, ypos); double x = xpos.get(); double y = ypos.get(); xpos.clear(); ypos.clear(); Vector2f result = new Vector2f((float) x, (float) y); return result; }
@Override protected void doUpdate(ServerDenseDoubleRow[] rows, double[] scalars) { try { rows[1].getLock().writeLock().lock(); DoubleBuffer from = rows[0].getData(); DoubleBuffer to = rows[1].getData(); int size = rows[0].size(); for (int i = 0; i < size; i++) { to.put(i, from.get(i) + scalars[0]); } } finally { rows[1].getLock().writeLock().unlock(); } }
@Override protected void doUpdate(ServerDenseDoubleRow[] rows) { try { rows[1].getLock().writeLock().lock(); DoubleBuffer from = rows[0].getData(); DoubleBuffer to = rows[1].getData(); int size = rows[0].size(); for (int i = 0; i < size; i++) { to.put(i, Math.log10(from.get(i))); } } finally { rows[1].getLock().writeLock().unlock(); } }
@Override protected void doUpdate(ServerDenseDoubleRow[] rows) { try { rows[1].getLock().writeLock().lock(); DoubleBuffer from = rows[0].getData(); DoubleBuffer to = rows[1].getData(); int size = rows[0].size(); for (int i = 0; i < size; i++) { to.put(i, Math.abs(from.get(i))); } } finally { rows[1].getLock().writeLock().unlock(); } }
@Override protected void doUpdate(ServerDenseDoubleRow[] rows) { try { rows[2].getLock().writeLock().lock(); DoubleBuffer from1 = rows[0].getData(); DoubleBuffer from2 = rows[1].getData(); DoubleBuffer to = rows[2].getData(); int size = rows[0].size(); for (int i = 0; i < size; i++) { to.put(i, Math.min(from1.get(i), from2.get(i))); } } finally { rows[2].getLock().writeLock().unlock(); } }
@Override protected void doUpdate(ServerDenseDoubleRow row, double[] other) { try { row.getLock().writeLock().lock(); DoubleBuffer data = row.getData(); int size = row.size(); for (int i = 0; i < size; i++) { data.put(i, Math.max(data.get(i), other[i])); } } finally { row.getLock().writeLock().unlock(); } }
private static double[] bufferToDoubleArray(ByteBuffer buffer) { buffer.order(ByteOrder.nativeOrder()); DoubleBuffer doubles = buffer.asDoubleBuffer(); if (doubles.hasArray()) { return doubles.array(); } else { double[] resultArray = new double[doubles.capacity()]; doubles.get(resultArray); return resultArray; } }
public static void declareDoubleArray(Jep jep, String variableName, DoubleBuffer buffer) throws JepException { jep.eval("from numpy import *"); jep.eval("from ctypes import *"); jep.eval("PyBuffer_FromMemory = pythonapi.PyBuffer_FromReadWriteMemory"); jep.eval("PyBuffer_FromMemory.restype = py_object"); jep.eval("PyBuffer_FromMemory.argtypes =[c_void_p, c_int32]"); jep.eval(variableName + "=frombuffer(PyBuffer_FromMemory(" + getAddress(buffer) + ", " + buffer.limit() * 8 + "), float64)"); }
@Test public void testGetDouble() { ByteBuffer bb = ByteBuffer.allocate(40); DoubleBuffer db = bb.asDoubleBuffer(); db.put(1.1d); db.put(2.2d); db.put(3.3d); db.put(4.4d); db.put(5.5d); byte[] bytes = bb.array(); ByteSource bs = createByteSource(bytes); double d = bs.getDouble(); assertEquals(1.1d, d, 0.0001); assertEquals(8, bs.position()); d = bs.getDouble(); assertEquals(2.2d, d, 0.0001); assertEquals(16, bs.position()); bs.position(4 * 8); d = bs.getDouble(); assertEquals(5.5d, d, 0.0001); assertEquals(40, bs.position()); try { bs.getDouble(); fail("expected BufferUnderflowException"); } catch (BufferUnderflowException expected) { } }
@Override protected void doUpdate(ServerDenseDoubleRow[] rows, double[] values) { try { rows[0].getLock().writeLock().lock(); DoubleBuffer data = rows[0].getData(); double scaleFactor = values[0]; int size = rows[0].size(); for (int i = 0; i < size; i++) { data.put(i, data.get(i) * scaleFactor); } } finally { rows[0].getLock().writeLock().unlock(); } }
/** * Called for each document * @return cosine similarity of the current document against the input inputVector */ @Override public final Object run() { final int size = inputVector.length; final byte[] bytes = binaryEmbeddingReader.get(docId).bytes; final ByteArrayDataInput input = new ByteArrayDataInput(bytes); input.readVInt(); // returns the number of values which should be 1, MUST appear hear since it affect the next calls final int len = input.readVInt(); // returns the number of bytes to read if(len != size * DOUBLE_SIZE) { return 0.0; } final int position = input.getPosition(); final DoubleBuffer doubleBuffer = ByteBuffer.wrap(bytes, position, len).asDoubleBuffer(); final double[] docVector = new double[size]; doubleBuffer.get(docVector); double docVectorNorm = 0.0f; double score = 0; for (int i = 0; i < size; i++) { // doc inputVector norm if(cosine) { docVectorNorm += docVector[i]*docVector[i]; } // dot product score += docVector[i] * inputVector[i]; } if(cosine) { // cosine similarity score if (docVectorNorm == 0 || magnitude == 0){ return 0f; } else { return score / (Math.sqrt(docVectorNorm) * magnitude); } } else { return score; } }
public static DoubleBuffer slice(DoubleBuffer buf) { DoubleBuffer buffer = buf.slice(); Buffer viewedBuffer = bufferViews.get(buf); if (viewedBuffer != null) { bufferViews.put(buffer, viewedBuffer); } return buffer; }
@Override protected void doUpdate(ServerDenseDoubleRow[] rows) { try { rows[2].getLock().writeLock().lock(); DoubleBuffer from1 = rows[0].getData(); DoubleBuffer from2 = rows[1].getData(); DoubleBuffer to = rows[2].getData(); int size = rows[0].size(); for (int i = 0; i < size; i++) { to.put(i, Math.max(from1.get(i), from2.get(i))); } } finally { rows[2].getLock().writeLock().unlock(); } }
@Override protected void doUpdate(ServerDenseDoubleRow row, double[] values) { DoubleBuffer data = row.getData(); int size = row.size(); for (int i = 0; i < size; i++) { data.put(i, values[i]); } }
public static void glBufferStorage(int target, DoubleBuffer data, int flags) { org.lwjgl.opengl.GL44.glBufferStorage(target, data, flags); if (Properties.PROFILE.enabled) { Context ctx = CURRENT_CONTEXT.get(); BufferObject bo = ctx.bufferObjectBindings.get(target); if (bo != null) { bo.size = data != null ? data.remaining() << 3 : 0L; } } }
@Override protected void doUpdate(ServerDenseDoubleRow[] rows, double[] values) { for (ServerDenseDoubleRow row : rows) { int rowId = row.getRowId(); if (rowId >= row.getStartCol() && rowId < row.getEndCol()) { try { row.getLock().writeLock().lock(); DoubleBuffer rowData = row.getData(); rowData.put(rowId - (int)row.getStartCol(), values[rowId]); } finally { row.getLock().writeLock().unlock(); } } } }
@Override protected void doUpdate(ServerDenseDoubleRow[] rows) { try { rows[1].getLock().writeLock().lock(); DoubleBuffer from = rows[0].getData(); DoubleBuffer to = rows[1].getData(); int size = rows[0].size(); for (int i = 0; i < size; i++) { to.put(i, Math.signum(from.get(i))); } } finally { rows[1].getLock().writeLock().unlock(); } }
@AndroidIncompatible // problem with equality of Type objects? public void testFreshInstance() { assertFreshInstances( String.class, CharSequence.class, Appendable.class, StringBuffer.class, StringBuilder.class, Pattern.class, MatchResult.class, Number.class, int.class, Integer.class, long.class, Long.class, short.class, Short.class, byte.class, Byte.class, boolean.class, Boolean.class, char.class, Character.class, int[].class, Object[].class, UnsignedInteger.class, UnsignedLong.class, BigInteger.class, BigDecimal.class, Throwable.class, Error.class, Exception.class, RuntimeException.class, Charset.class, Locale.class, Currency.class, List.class, Map.Entry.class, Object.class, Equivalence.class, Predicate.class, Function.class, Comparable.class, Comparator.class, Ordering.class, Class.class, Type.class, TypeToken.class, TimeUnit.class, Ticker.class, Joiner.class, Splitter.class, CharMatcher.class, InputStream.class, ByteArrayInputStream.class, Reader.class, Readable.class, StringReader.class, OutputStream.class, ByteArrayOutputStream.class, Writer.class, StringWriter.class, File.class, Buffer.class, ByteBuffer.class, CharBuffer.class, ShortBuffer.class, IntBuffer.class, LongBuffer.class, FloatBuffer.class, DoubleBuffer.class, String[].class, Object[].class, int[].class); }
@Override protected void doUpdate(ServerDenseDoubleRow[] rows, double[] scalars) { try { rows[1].getLock().writeLock().lock(); DoubleBuffer xData = rows[0].getData(); DoubleBuffer yData = rows[1].getData(); double a = scalars[0]; int size = rows[0].size(); for (int i = 0; i < size; i++) { yData.put(i, a * xData.get(i) + yData.get(i)); } } finally { rows[1].getLock().writeLock().unlock(); } }
@Override protected void doUpdate(ServerDenseDoubleRow[] rows, double[] values) { try { rows[1].getLock().writeLock().lock(); DoubleBuffer from = rows[0].getData(); DoubleBuffer to = rows[1].getData(); double value = values[0]; int size = rows[0].size(); for (int i = 0; i < size; i++) { to.put(i, from.get(i) * value); } } finally { rows[1].getLock().writeLock().unlock(); } }
public static DoubleBuffer asDoubleBuffer(ByteBuffer buf) { DoubleBuffer buffer = buf.asDoubleBuffer(); Buffer viewedBuffer = bufferViews.get(buf); if (viewedBuffer != null) { bufferViews.put(buffer, viewedBuffer); } else { bufferViews.put(buffer, buf); } return buffer; }