下面列出了java.nio.DoubleBuffer#rewind ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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);
}
/**
* 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 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;
}
@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;
}
@Override
public double downloadEnergySync() {
// IMPORTANT!! rewind the output buffer before downloading energies
// otherwise we get weird segfaults in nvidia's opencl driver that are next to impossible to diagnose!
energies.getBuffer().rewind();
downloadBufferSync(energies);
DoubleBuffer buf = energies.getBuffer();
// 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();
for (int i=0; i<n; i++) {
energy += buf.get();
}
return energy;
}
/**
* 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 and initializes a 2D {@link NDArray}.
*
* @param data the float array that needs to be set
* @return a new instance of {@link NDArray}
*/
default NDArray create(double[][] data) {
DoubleBuffer buffer = DoubleBuffer.allocate(data.length * data[0].length);
for (double[] d : data) {
buffer.put(d);
}
buffer.rewind();
return create(buffer, new Shape(data.length, data[0].length));
}
/**
* Create a new DoubleBuffer of an appropriate size to hold the specified number of doubles only if the given buffer if not already the right size.
*
* @param buf
* the buffer to first check and rewind
* @param size
* number of doubles that need to be held by the newly created buffer
* @return the requested new DoubleBuffer
*/
public static DoubleBuffer createDoubleBuffer(DoubleBuffer buf, int size) {
if (buf != null && buf.limit() == size) {
buf.rewind();
return buf;
}
buf = createDoubleBuffer(size);
return buf;
}
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();
}
/**
* Create a new DoubleBuffer of an appropriate size to hold the specified
* number of doubles only if the given buffer if not already the right size.
*
* @param buf
* the buffer to first check and rewind
* @param size
* number of doubles that need to be held by the newly created
* buffer
* @return the requested new DoubleBuffer
*/
public static DoubleBuffer createDoubleBuffer(DoubleBuffer buf, int size) {
if (buf != null && buf.limit() == size) {
buf.rewind();
return buf;
}
buf = createDoubleBuffer(size);
return buf;
}
/**
* Create a new DoubleBuffer of an appropriate size to hold the specified
* number of doubles only if the given buffer if not already the right size.
*
* @param buf
* the buffer to first check and rewind
* @param size
* number of doubles that need to be held by the newly created
* buffer
* @return the requested new DoubleBuffer
*/
public static DoubleBuffer createDoubleBuffer(DoubleBuffer buf, int size) {
if (buf != null && buf.limit() == size) {
buf.rewind();
return buf;
}
buf = createDoubleBuffer(size);
return buf;
}
/**
* Create a new DoubleBuffer of an appropriate size to hold the specified
* number of doubles only if the given buffer if not already the right size.
*
* @param buf
* the buffer to first check and rewind
* @param size
* number of doubles that need to be held by the newly created
* buffer
* @return the requested new DoubleBuffer
*/
public static DoubleBuffer createDoubleBuffer(DoubleBuffer buf, int size) {
if (buf != null && buf.limit() == size) {
buf.rewind();
return buf;
}
buf = createDoubleBuffer(size);
return buf;
}