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

The following examples show how to use java.nio.DoubleBuffer#get() . 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: ForcefieldKernelCuda.java    From OSPREY3 with GNU General Public License v2.0 6 votes vote down vote up
@Override
public double downloadEnergySync() {
	
	// make sure this thread can use the cuda context
	getStream().getContext().attachCurrentThread();
	
	energies.downloadSync();
	DoubleBuffer buf = energies.getHostBuffer();
	
	// do the last bit of the energy sum on the cpu
	// add one element per work group on the gpu
	// typically, it's a factor of groupSize less than the number of atom pairs
	double energy = subset.getInternalSolvationEnergy();
	buf.rewind();
	int n = getEnergySize(subset, func.blockThreads);
	for (int i=0; i<n; i++) {
		energy += buf.get();
	}
	return energy;
}
 
Example 2
Source File: WflowPythonToJavaAdapter.java    From OpenDA with GNU Lesser General Public License v3.0 6 votes vote down vote up
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);
}
 
Example 3
Source File: StreamingGeometryGenerator.java    From BIMserver with GNU Affero General Public License v3.0 6 votes vote down vote up
void processExtendsUntranslated(VirtualObject geometryInfo, DoubleBuffer vertices, int index, GenerateGeometryResult generateGeometryResult) throws BimserverDatabaseException {
	double x = vertices.get(index);
	double y = vertices.get(index + 1);
	double z = vertices.get(index + 2);
	
	HashMapWrappedVirtualObject bounds = (HashMapWrappedVirtualObject) geometryInfo.eGet(GeometryPackage.eINSTANCE.getGeometryInfo_BoundsUntransformed());
	HashMapWrappedVirtualObject minBounds = (HashMapWrappedVirtualObject) bounds.eGet(GeometryPackage.eINSTANCE.getBounds_Min());
	HashMapWrappedVirtualObject maxBounds = (HashMapWrappedVirtualObject) bounds.eGet(GeometryPackage.eINSTANCE.getBounds_Max());
	
	minBounds.set("x", Math.min(x, (double)minBounds.eGet("x")));
	minBounds.set("y", Math.min(y, (double)minBounds.eGet("y")));
	minBounds.set("z", Math.min(z, (double)minBounds.eGet("z")));
	maxBounds.set("x", Math.max(x, (double)maxBounds.eGet("x")));
	maxBounds.set("y", Math.max(y, (double)maxBounds.eGet("y")));
	maxBounds.set("z", Math.max(z, (double)maxBounds.eGet("z")));
	
	generateGeometryResult.setUntranslatedMinX(Math.min(x, generateGeometryResult.getUntranslatedMinX()));
	generateGeometryResult.setUntranslatedMinY(Math.min(y, generateGeometryResult.getUntranslatedMinY()));
	generateGeometryResult.setUntranslatedMinZ(Math.min(z, generateGeometryResult.getUntranslatedMinZ()));
	generateGeometryResult.setUntranslatedMaxX(Math.max(x, generateGeometryResult.getUntranslatedMaxX()));
	generateGeometryResult.setUntranslatedMaxY(Math.max(y, generateGeometryResult.getUntranslatedMaxY()));
	generateGeometryResult.setUntranslatedMaxZ(Math.max(z, generateGeometryResult.getUntranslatedMaxZ()));
}
 
Example 4
Source File: CCDKernelCuda.java    From OSPREY3 with GNU General Public License v2.0 6 votes vote down vote up
public Minimizer.Result downloadResultSync() {
	
	// allocate space for the result
	int numDofs = dofInfos.size();
	Minimizer.Result result = new Minimizer.Result(DoubleFactory1D.dense.make(numDofs), 0);
	
	// wait for the kernel to finish and download the out buffer
	DoubleBuffer buf = ccdOut.downloadSync();
	
	// copy to the result
	buf.rewind();
	result.energy = buf.get();
	for (int d=0; d<numDofs; d++) {
		result.dofValues.set(d, Math.toDegrees(buf.get()));
	}
	
	return result;
}
 
Example 5
Source File: LargeAttributesTest.java    From jhdf with MIT License 6 votes vote down vote up
@Test
void testGettingObject() {
	Attribute lattr = largeAttrHdfFile.getAttribute(ATTRIBUTE_NAME);
	assertThat(lattr, notNullValue());
	assertThat(lattr.getJavaType(), is(double.class));

	ByteBuffer bb = lattr.getBuffer();
	DoubleBuffer db = bb.asDoubleBuffer();
	assertThat(db.limit(), is(equalTo(EXPECTED_DATA_LENGTH)));

	double referenceData[] = new double[EXPECTED_DATA_LENGTH];
	for (int i=0; i < EXPECTED_DATA_LENGTH; i++) referenceData[i] = (double)i;

	double obtainedData[] = new double[EXPECTED_DATA_LENGTH];
	db.get(obtainedData);

	assertThat(obtainedData, is(equalTo(referenceData)));
}
 
Example 6
Source File: DbHelper.java    From Synapse with Apache License 2.0 6 votes vote down vote up
@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;
}
 
Example 7
Source File: ByteBufferViews.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
@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);
    }

}
 
Example 8
Source File: ChannelHelper.java    From libcommon with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
	}
}
 
Example 9
Source File: GeometryGenerator.java    From BIMserver with GNU Affero General Public License v3.0 6 votes vote down vote up
private void processExtendsUntranslated(GeometryInfo geometryInfo, DoubleBuffer vertices, int index, GenerateGeometryResult generateGeometryResult) throws BimserverDatabaseException {
	double x = vertices.get(index);
	double y = vertices.get(index + 1);
	double z = vertices.get(index + 2);
	
	Vector3f minBounds = geometryInfo.getBoundsUntransformed().getMin();
	Vector3f maxBounds = geometryInfo.getBoundsUntransformed().getMax();
	
	minBounds.setX(Math.min(x, (double)minBounds.getX()));
	minBounds.setY(Math.min(y, (double)minBounds.getY()));
	minBounds.setZ(Math.min(z, (double)minBounds.getZ()));
	maxBounds.setX(Math.max(x, (double)maxBounds.getX()));
	maxBounds.setY(Math.max(y, (double)maxBounds.getY()));
	maxBounds.setZ(Math.max(z, (double)maxBounds.getZ()));
	
	generateGeometryResult.setUntranslatedMinX(Math.min(x, generateGeometryResult.getUntranslatedMinX()));
	generateGeometryResult.setUntranslatedMinY(Math.min(y, generateGeometryResult.getUntranslatedMinY()));
	generateGeometryResult.setUntranslatedMinZ(Math.min(z, generateGeometryResult.getUntranslatedMinZ()));
	generateGeometryResult.setUntranslatedMaxX(Math.max(x, generateGeometryResult.getUntranslatedMaxX()));
	generateGeometryResult.setUntranslatedMaxY(Math.max(y, generateGeometryResult.getUntranslatedMaxY()));
	generateGeometryResult.setUntranslatedMaxZ(Math.max(z, generateGeometryResult.getUntranslatedMaxZ()));
}
 
Example 10
Source File: NDArray.java    From djl with Apache License 2.0 5 votes vote down vote up
/**
 * Converts this {@code NDArray} to a double array.
 *
 * @return a double array
 * @throws IllegalStateException when {@link DataType} of this {@code NDArray} mismatches
 */
default double[] toDoubleArray() {
    if (getDataType() != DataType.FLOAT64) {
        throw new IllegalStateException(
                "DataType mismatch, Required double" + " Actual " + getDataType());
    }
    DoubleBuffer db = toByteBuffer().asDoubleBuffer();
    double[] ret = new double[db.remaining()];
    db.get(ret);
    return ret;
}
 
Example 11
Source File: PrimitiveDoubleArraySerializationProvider.java    From geowave with Apache License 2.0 5 votes vote down vote up
@Override
public double[] readField(final byte[] fieldData) {
  if ((fieldData == null) || (fieldData.length < 8)) {
    return null;
  }
  final DoubleBuffer buff = ByteBuffer.wrap(fieldData).asDoubleBuffer();
  final double[] result = new double[buff.remaining()];
  buff.get(result);
  return result;
}
 
Example 12
Source File: GeometryUtils.java    From BIMserver with GNU Affero General Public License v3.0 5 votes vote down vote up
public static double[] toDoubleArray(byte[] data) {
	ByteBuffer buffer = ByteBuffer.wrap(data);
	buffer.order(ByteOrder.LITTLE_ENDIAN);
	DoubleBuffer doubleBuffer = buffer.asDoubleBuffer();
	double[] result = new double[data.length / 8];
	for (int i=0; i<data.length / 8; i++) {
		result[i] = doubleBuffer.get();
	}
	return result;
}
 
Example 13
Source File: SearcherImpl.java    From Word2VecJava with MIT License 5 votes vote down vote up
private double[] getVectorOrNull(final String word) {
final Integer index = word2vectorOffset.get(word);
  if(index == null)
	return null;

final DoubleBuffer vectors = model.vectors.duplicate();
double[] result = new double[model.layerSize];
vectors.position(index);
vectors.get(result);
return result;
 }
 
Example 14
Source File: TerrainPicking.java    From oreon-engine with GNU General Public License v3.0 5 votes vote down vote up
public void getTerrainPosition(){
	
	if (isActive() && glfwGetMouseButton(BaseContext.getWindow().getId(),1) == GLFW_PRESS){
		Vec3f pos = new Vec3f(0,0,0);
		DoubleBuffer xPos = BufferUtils.createDoubleBuffer(1);
		DoubleBuffer yPos = BufferUtils.createDoubleBuffer(1);
		glfwGetCursorPos(BaseContext.getWindow().getId(), xPos, yPos);
		Vec2f screenPos = new Vec2f((float) xPos.get(),(float) yPos.get());
		
		GLContext.getResources().getSceneDepthMap().bind();
		glGetTexImage(GL_TEXTURE_2D,0,GL_DEPTH_COMPONENT,GL_FLOAT,depthmapBuffer);
		float depth = depthmapBuffer.get((int) (BaseContext.getWindow().getWidth() * screenPos.getY() + screenPos.getX()));
		
		// window coords
		Vec2f w = new Vec2f(screenPos.getX()/BaseContext.getWindow().getWidth(),
							screenPos.getY()/BaseContext.getWindow().getHeight());
		//ndc coords
		Vec3f ndc = new Vec3f(w.getX() * 2 - 1, w.getY() * 2 - 1, depth);
		float cw = BaseContext.getCamera().getProjectionMatrix().get(3,2) / (ndc.getZ() - BaseContext.getCamera().getProjectionMatrix().get(2,2)); 
		Vec3f clip = ndc.mul(cw);
		Vec4f clipPos = new Vec4f(clip.getX(),clip.getY(),clip.getZ(),cw);
		Vec4f worldPos =  BaseContext.getCamera().getViewProjectionMatrix().invert().mul(clipPos);
		worldPos = worldPos.div(worldPos.getW());
	
		pos.setX(worldPos.getX());
		pos.setY(worldPos.getY());
		pos.setZ(worldPos.getZ());
		
		System.out.println("TerrainPicking: " + pos);
	}
}
 
Example 15
Source File: DatasetImplTest.java    From jhdf with MIT License 5 votes vote down vote up
@Test
void testDoubleDatasetBuffer() {
	DatasetBase dataset = (DatasetBase) hdfFile.getByPath(FLOAT64_PATH);
	// We know this is a double dataset so get the double buffer
	DoubleBuffer buffer = dataset.getDataBuffer().asDoubleBuffer();
	assertThat(buffer, is(notNullValue()));
	// Check the size of the buffer is correct, i.e the number of points in this
	// data set
	assertThat(buffer.limit(), is(equalTo(21)));
	double[] data = new double[21];
	// read the data into the array
	buffer.get(data);
	// check the data is correct
	assertArrayEquals(REFERENCE_DOUBLE_DATA, data, DOUBLE_DELTA);
}
 
Example 16
Source File: ThriftBmiBridge.java    From OpenDA with GNU Lesser General Public License v3.0 5 votes vote down vote up
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;
	}
}
 
Example 17
Source File: StreamingGeometryGenerator.java    From BIMserver with GNU Affero General Public License v3.0 5 votes vote down vote up
private ByteBuffer quantizeVertices(DoubleBuffer vertices, double[] quantizationMatrix, double multiplierToMm) {
	ByteBuffer quantizedBuffer = ByteBuffer.wrap(new byte[vertices.capacity() * 2]);
	quantizedBuffer.order(ByteOrder.LITTLE_ENDIAN);
	
	double[] vertex = new double[4];
	double[] result = new double[4];
	vertex[3] = 1;
	int nrVertices = vertices.capacity();
	for (int i=0; i<nrVertices; i+=3) {
		vertex[0] = vertices.get(i);
		vertex[1] = vertices.get(i+1);
		vertex[2] = vertices.get(i+2);

		if (multiplierToMm != 1f) {
			vertex[0] = vertex[0] * multiplierToMm;
			vertex[1] = vertex[1] * multiplierToMm;
			vertex[2] = vertex[2] * multiplierToMm;
		}

		Matrix.multiplyMV(result, 0, quantizationMatrix, 0, vertex, 0);
		
		quantizedBuffer.putShort((short)result[0]);
		quantizedBuffer.putShort((short)result[1]);
		quantizedBuffer.putShort((short)result[2]);
	}

	return quantizedBuffer;
}
 
Example 18
Source File: VectorAlgebra.java    From OSPREY3 with GNU General Public License v2.0 4 votes vote down vote up
public static void copy(DoubleBuffer src, int srcOffset, double[] dest, int destOffset) {
	src.position(srcOffset);
	dest[destOffset + 0] = src.get();
	dest[destOffset + 1] = src.get();
	dest[destOffset + 2] = src.get();
}
 
Example 19
Source File: ByteBufferTest.java    From openjdk-jdk9 with GNU General Public License v2.0 votes vote down vote up
double getDouble(DoubleBuffer v) { ck(v.get(v.position()), getDoubleX(pos)); double x = v.get(); pos += 8; return x; } 
Example 20
Source File: ByteBufferTest.java    From openjdk-jdk9 with GNU General Public License v2.0 votes vote down vote up
double getDouble(DoubleBuffer vb, int i) { ck(vb.get(i / 8), getDoubleX(i)); return vb.get(i / 8); }