下面列出了java.nio.DoubleBuffer#put ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* 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;
}
/**
* 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;
}
/**
* 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;
}
/**
* 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;
}
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;
}
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();
}
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();
}
@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;
}
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;
}
@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();
}
@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()));
}
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());
}
@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());
}
/** 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);
}
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);
}
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();
}
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;
}
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++));
}
}
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;
}
void putDouble(DoubleBuffer v, double x) { putLongX(pos, Double.doubleToRawLongBits(x)); pos += 8; v.put(x); }