Java Code Examples for java.nio.DoubleBuffer#put()

The following examples show how to use java.nio.DoubleBuffer#put() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BufferUtils.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Creates a new DoubleBuffer with the same contents as the given
 * DoubleBuffer. The new DoubleBuffer is separate 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;
}
 
Example 2
Source File: CCDKernelCuda.java    From OSPREY3 with GNU General Public License v2.0 6 votes vote down vote up
public void runAsync(DoubleMatrix1D x, ObjectiveFunction.DofBounds dofBounds) {
	
	// make sure the energy function is still valid
	if (ffenergy.getFullSubset().handleChemicalChanges() != ffSequenceNumber) {
		throw new Error("don't re-use kernel instances after chemical changes. This is a bug");
	}
	
	// upload x and bounds
	DoubleBuffer buf = xAndBounds.getHostBuffer();
	buf.clear();
	int numDofs = dofInfos.size();
	for (int d=0; d<numDofs; d++) {
		buf.put(Math.toRadians(x.get(d)));
		buf.put(Math.toRadians(dofBounds.getMin(d)));
		buf.put(Math.toRadians(dofBounds.getMax(d)));
	}
	xAndBounds.uploadAsync();
	
	// run the kernel
	func.runAsync();
}
 
Example 3
Source File: BufferUtils.java    From aion-germany with GNU General Public License v3.0 6 votes vote down vote up
/**
 * 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 (buf.isDirect()) {
		copy = createDoubleBuffer(buf.limit());
	}
	else {
		copy = DoubleBuffer.allocate(buf.limit());
	}
	copy.put(buf);

	return copy;
}
 
Example 4
Source File: TensorFlowModel.java    From samantha with MIT License 6 votes vote down vote up
public Map<String, String> getStringifiedSparseTensor(List<LearningInstance> instances) {
    Map<String, Integer> numFeatures = getNumSparseFeatures(instances);
    Map<String, String> tensorMap = new HashMap<>();
    for (Map.Entry<String, Integer> entry : numFeatures.entrySet()) {
        String name = entry.getKey();
        int numFeature = entry.getValue();
        DoubleBuffer valBuffer = DoubleBuffer.allocate(numFeature);
        IntBuffer idxBuffer = IntBuffer.allocate(numFeature);
        for (LearningInstance instance : instances) {
            TensorFlowInstance tfins = (TensorFlowInstance) instance;
            double[] doubleValues = tfins.getName2Values().get(name);
            int[] intValues = tfins.getName2Indices().get(name);
            for (int i=0; i<doubleValues.length; i++) {
                valBuffer.put(doubleValues[i]);
            }
            for (int i=0; i<intValues.length; i++) {
                idxBuffer.put(intValues[i]);
            }
        }
        tensorMap.put(name + VALUE_APPENDIX, StringUtils.join(valBuffer.array(), ','));
        tensorMap.put(name + INDEX_APPENDIX, StringUtils.join(idxBuffer.array(), ','));
    }
    return tensorMap;
}
 
Example 5
Source File: BufferUtils.java    From Ultraino with MIT License 6 votes vote down vote up
/**
 * Creates a new DoubleBuffer with the same contents as the given
 * DoubleBuffer. The new DoubleBuffer is separate 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;
}
 
Example 6
Source File: BufferUtils.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * 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 (buf.isDirect()) {
        copy = createDoubleBuffer(buf.limit());
    } else {
        copy = DoubleBuffer.allocate(buf.limit());
    }
    copy.put(buf);

    return copy;
}
 
Example 7
Source File: StreamingGeometryGenerator.java    From BIMserver with GNU Affero General Public License v3.0 5 votes vote down vote up
void setTransformationMatrix(VirtualObject geometryInfo, double[] transformationMatrix) throws BimserverDatabaseException {
	ByteBuffer byteBuffer = ByteBuffer.allocate(16 * 8);
	byteBuffer.order(ByteOrder.nativeOrder());
	DoubleBuffer asDoubleBuffer = byteBuffer.asDoubleBuffer();
	for (double d : transformationMatrix) {
		asDoubleBuffer.put(d);
	}
	geometryInfo.setAttribute(GeometryPackage.eINSTANCE.getGeometryInfo_Transformation(), byteBuffer.array());
}
 
Example 8
Source File: ResidueForcefieldEnergyCuda.java    From OSPREY3 with GNU General Public License v2.0 5 votes vote down vote up
private double getEnergy(CUBuffer<IntBuffer> indices) {
	
	// check broken-ness first. easy peasy
	if (isBroken) {
		return Double.POSITIVE_INFINITY;
	}
	
	// make sure this thread can use the cuda context
	getStream().getContext().attachCurrentThread();
	
	// capture the current molecule state
	DoubleBuffer coordsbuf = coords.getHostBuffer();
	coordsbuf.clear();
	for (Residue res : residues) {
		coordsbuf.put(res.coords);
	}
	coordsbuf.clear();
	coords.uploadAsync();
	
	// launch kernel
	func.setArgs(Pointer.to(
		coords.getDevicePointer(),
		data.getDevicePointer(),
		Pointer.to(new int[] { indices.getHostBuffer().limit() }),
		indices.getDevicePointer(),
		energy.getDevicePointer()
	));
	func.runAsync();
	
	// download the energy
	DoubleBuffer buf = energy.downloadSync();
	buf.rewind();
	return buf.get();
}
 
Example 9
Source File: TestableModel.java    From vespa with Apache License 2.0 5 votes vote down vote up
private org.tensorflow.Tensor<?> tensorFlowDoubleInputArgument(int d0Size, int d1Size) {
    DoubleBuffer fb1 = DoubleBuffer.allocate(d0Size * d1Size);
    int i = 0;
    for (int d0 = 0; d0 < d0Size; d0++)
        for (int d1 = 0; d1 < d1Size; ++d1)
            fb1.put(i++, (float)(d1 * 1.0 / d1Size));
    return org.tensorflow.Tensor.create(new long[]{ d0Size, d1Size }, fb1);
}
 
Example 10
Source File: TestableTensorFlowModel.java    From vespa with Apache License 2.0 5 votes vote down vote up
/** Must be the same as vespaInputArgument() */
private org.tensorflow.Tensor<?> tensorFlowDoubleInputArgument() {
    DoubleBuffer fb = DoubleBuffer.allocate(d0Size * d1Size);
    int i = 0;
    for (int d0 = 0; d0 < d0Size; d0++)
        for (int d1 = 0; d1 < d1Size; ++d1)
            fb.put(i++, (d1 * 1.0 / d1Size));
    return org.tensorflow.Tensor.create(new long[]{ d0Size, d1Size }, fb);
}
 
Example 11
Source File: DoubleTensorTypeSupport.java    From datacollector with Apache License 2.0 5 votes vote down vote up
@Override
public void writeField(DoubleBuffer buffer, Field field) {
  Utils.checkState(
      field.getType() == Field.Type.DOUBLE,
      Utils.format("Not a Double scalar, it is {}", field.getType())
  );
  buffer.put(field.getValueAsDouble());
}
 
Example 12
Source File: DoubleDataChunkTest.java    From powsybl-core with Mozilla Public License 2.0 5 votes vote down vote up
@Test
public void baseTest() throws IOException {
    UncompressedDoubleDataChunk chunk = new UncompressedDoubleDataChunk(1, new double[] {1d, 2d, 3d});
    assertEquals(1, chunk.getOffset());
    assertEquals(3, chunk.getLength());
    assertArrayEquals(new double[]{1d, 2d, 3d}, chunk.getValues(), 0d);
    assertEquals(24, chunk.getEstimatedSize());
    assertFalse(chunk.isCompressed());
    assertEquals(1d, chunk.getCompressionFactor(), 0d);
    DoubleBuffer buffer = DoubleBuffer.allocate(4);
    for (int i = 0; i < 4; i++) {
        buffer.put(i, Double.NaN);
    }
    chunk.fillBuffer(buffer, 0);
    assertArrayEquals(new double[] {Double.NaN, 1d, 2d, 3d}, buffer.array(), 0d);
    String jsonRef = String.join(System.lineSeparator(),
            "{",
            "  \"offset\" : 1,",
            "  \"values\" : [ 1.0, 2.0, 3.0 ]",
            "}");
    assertEquals(jsonRef, JsonUtil.toJson(chunk::writeJson));
    RegularTimeSeriesIndex index = RegularTimeSeriesIndex.create(Interval.parse("2015-01-01T00:00:00Z/2015-01-01T00:45:00Z"),
                                                                 Duration.ofMinutes(15));
    assertEquals(ImmutableList.of(new DoublePoint(1, Instant.parse("2015-01-01T00:15:00Z").toEpochMilli(), 1d),
                                  new DoublePoint(2, Instant.parse("2015-01-01T00:30:00Z").toEpochMilli(), 2d),
                                  new DoublePoint(3, Instant.parse("2015-01-01T00:45:00Z").toEpochMilli(), 3d)),
            chunk.stream(index).collect(Collectors.toList()));
}
 
Example 13
Source File: StoredDoubleTimeSeries.java    From powsybl-core with Mozilla Public License 2.0 5 votes vote down vote up
@Override
public double[] toArray() {
    DoubleBuffer buffer = DoubleBuffer.allocate(metadata.getIndex().getPointCount());
    for (int i = 0; i < metadata.getIndex().getPointCount(); i++) {
        buffer.put(i, Double.NaN);
    }
    fillBuffer(buffer, 0);
    return buffer.array();
}
 
Example 14
Source File: BinUtils.java    From BIMserver with GNU Affero General Public License v3.0 5 votes vote down vote up
public static byte[] doubleToByteArray(Double inDouble) {
	byte[] bArray = new byte[8];
	ByteBuffer bBuffer = ByteBuffer.wrap(bArray);
	DoubleBuffer lBuffer = bBuffer.asDoubleBuffer();
	lBuffer.put(inDouble);
	return bArray;
}
 
Example 15
Source File: ArrayDate.java    From MeteoInfo with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public ByteBuffer getDataAsByteBuffer() {
    ByteBuffer bb = ByteBuffer.allocate((int) (8 * getSize()));
    DoubleBuffer ib = bb.asDoubleBuffer();
    ib.put((double[]) get1DJavaArray(double.class)); // make sure its in canonical order
    return bb;
}
 
Example 16
Source File: GeometryUtils.java    From BIMserver with GNU Affero General Public License v3.0 5 votes vote down vote up
public static byte[] doubleArrayToByteArray(double[] vertices) {
	if (vertices == null) {
		return null;
	}
	ByteBuffer buffer = ByteBuffer.wrap(new byte[vertices.length * 4]);
	buffer.order(ByteOrder.LITTLE_ENDIAN);
	DoubleBuffer asDoubleBuffer = buffer.asDoubleBuffer();
	for (double d : vertices) {
		asDoubleBuffer.put(d);
	}
	return buffer.array();
}
 
Example 17
Source File: ArrayDouble.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public ByteBuffer getDataAsByteBuffer() {
  ByteBuffer bb = ByteBuffer.allocate((int) (8 * getSize()));
  DoubleBuffer ib = bb.asDoubleBuffer();
  ib.put((double[]) get1DJavaArray(DataType.DOUBLE)); // make sure its in canonical order
  return bb;
}
 
Example 18
Source File: ArrayDouble.java    From MeteoInfo with GNU Lesser General Public License v3.0 4 votes vote down vote up
public ByteBuffer getDataAsByteBuffer() {
    ByteBuffer bb = ByteBuffer.allocate((int) (8 * getSize()));
    DoubleBuffer ib = bb.asDoubleBuffer();
    ib.put((double[]) get1DJavaArray(double.class)); // make sure its in canonical order
    return bb;
}
 
Example 19
Source File: TypedArrayFunctions.java    From es6draft with MIT License 4 votes vote down vote up
private static final void put(IntToDoubleFunction src, int srcPos, DoubleBuffer dest, int destPos, int length) {
    for (int n = destPos, k = srcPos; k < srcPos + length;) {
        dest.put(n++, src.applyAsDouble(k++));
    }
}
 
Example 20
Source File: ByteBufferTest.java    From openjdk-jdk9 with GNU General Public License v2.0 votes vote down vote up
void putDouble(DoubleBuffer v, double x) { putLongX(pos, Double.doubleToRawLongBits(x)); pos += 8; v.put(x); }