use of org.bytedeco.javacpp.Pointer in project nd4j by deeplearning4j.
the class GridExecutionerTest method testOpPointerizeReduce1.
/**
* Reduce along dimensions
*
* @throws Exception
*/
@Test
public void testOpPointerizeReduce1() throws Exception {
CudaGridExecutioner executioner = new CudaGridExecutioner();
INDArray array = Nd4j.create(10, 10);
Sum opA = new Sum(array);
// we need exec here, to init Op.Z for specific dimension
executioner.exec(opA, 1);
GridPointers pointers = executioner.pointerizeOp(opA, 1);
assertEquals(opA.opNum(), pointers.getOpNum());
assertEquals(Op.Type.REDUCE, pointers.getType());
CudaContext context = (CudaContext) AtomicAllocator.getInstance().getDeviceContext().getContext();
Pointer x = AtomicAllocator.getInstance().getPointer(array, context);
Pointer xShapeInfo = AtomicAllocator.getInstance().getPointer(array.shapeInfoDataBuffer(), context);
Pointer z = AtomicAllocator.getInstance().getPointer(opA.z(), context);
Pointer zShapeInfo = AtomicAllocator.getInstance().getPointer(opA.z().shapeInfoDataBuffer(), context);
DataBuffer dimBuff = Nd4j.getConstantHandler().getConstantBuffer(new int[] { 1 });
Pointer ptrBuff = AtomicAllocator.getInstance().getPointer(dimBuff, context);
assertEquals(x, pointers.getX());
assertEquals(null, pointers.getY());
assertNotEquals(null, pointers.getZ());
assertEquals(z, pointers.getZ());
assertEquals(10, opA.z().length());
assertEquals(10, pointers.getZLength());
/* // We dont really care about EWS here, since we're testing TAD-based operation
assertEquals(1, pointers.getXStride());
assertEquals(-1, pointers.getYStride());
assertEquals(1, pointers.getZStride());
*/
assertEquals(xShapeInfo, pointers.getXShapeInfo());
assertEquals(null, pointers.getYShapeInfo());
assertEquals(zShapeInfo, pointers.getZShapeInfo());
assertEquals(ptrBuff, pointers.getDimensions());
assertEquals(1, pointers.getDimensionsLength());
assertNotEquals(null, pointers.getTadShape());
assertNotEquals(null, pointers.getTadOffsets());
assertEquals(null, pointers.getExtraArgs());
}
use of org.bytedeco.javacpp.Pointer in project nd4j by deeplearning4j.
the class CpuTADManager method getTADOnlyShapeInfo.
@Override
public Pair<DataBuffer, DataBuffer> getTADOnlyShapeInfo(INDArray array, int[] dimension) {
if (dimension != null && dimension.length > 1)
Arrays.sort(dimension);
if (dimension == null || dimension.length >= 1 && dimension[0] == Integer.MAX_VALUE) {
return new Pair<>(array.shapeInfoDataBuffer(), null);
} else {
TadDescriptor descriptor = new TadDescriptor(array, dimension);
if (!cache.containsKey(descriptor)) {
int dimensionLength = dimension.length;
// FIXME: this is fast triage, remove it later
// dimensionLength <= 1 ? 2 : dimensionLength;
int targetRank = array.rank();
long offsetLength;
long tadLength = 1;
for (int i = 0; i < dimensionLength; i++) {
tadLength *= array.shape()[dimension[i]];
}
offsetLength = array.lengthLong() / tadLength;
DataBuffer outputBuffer = new IntBuffer(targetRank * 2 + 4);
DataBuffer offsetsBuffer = new LongBuffer(offsetLength);
DataBuffer dimensionBuffer = constantHandler.getConstantBuffer(dimension);
Pointer dimensionPointer = dimensionBuffer.addressPointer();
Pointer xShapeInfo = array.shapeInfoDataBuffer().addressPointer();
Pointer targetPointer = outputBuffer.addressPointer();
Pointer offsetsPointer = offsetsBuffer.addressPointer();
nativeOps.tadOnlyShapeInfo((IntPointer) xShapeInfo, (IntPointer) dimensionPointer, dimension.length, (IntPointer) targetPointer, new LongPointerWrapper(offsetsPointer));
// If the line below will be uncommented, shapes from JVM will be used on native side
// outputBuffer = array.tensorAlongDimension(0, dimension).shapeInfoDataBuffer();
Pair<DataBuffer, DataBuffer> pair = new Pair<>(outputBuffer, offsetsBuffer);
if (counter.get() < MAX_ENTRIES) {
counter.incrementAndGet();
cache.put(descriptor, pair);
bytes.addAndGet((outputBuffer.length() * 4) + (offsetsBuffer.length() * 8));
}
return pair;
}
return cache.get(descriptor);
}
}
use of org.bytedeco.javacpp.Pointer in project nd4j by deeplearning4j.
the class TestNDArrayCreation method testBufferCreation.
@Test
public void testBufferCreation() {
DataBuffer dataBuffer = Nd4j.createBuffer(new double[] { 1, 2 });
Pointer pointer = dataBuffer.pointer();
FloatPointer floatPointer = new FloatPointer(pointer);
DataBuffer dataBuffer1 = Nd4j.createBuffer(floatPointer, 2);
assertEquals(2, dataBuffer1.length());
assertEquals(1.0, dataBuffer1.getDouble(0), 1e-1);
assertEquals(2.0, dataBuffer1.getDouble(1), 1e-1);
INDArray arr = Nd4j.create(dataBuffer1);
System.out.println(arr);
}
use of org.bytedeco.javacpp.Pointer in project nd4j by deeplearning4j.
the class NativeGraphExecutioner method executeGraph.
/**
* This method executes given graph and returns results
*
* @param sd
* @return
*/
@Override
public INDArray[] executeGraph(SameDiff sd, ExecutorConfiguration configuration) {
Map<Integer, Node> intermediate = new HashMap<>();
ByteBuffer buffer = convertToFlatBuffers(sd, configuration, intermediate);
BytePointer bPtr = new BytePointer(buffer);
log.info("Buffer length: {}", buffer.limit());
Pointer res = NativeOpsHolder.getInstance().getDeviceNativeOps().executeFlatGraphFloat(null, bPtr);
if (res == null)
throw new ND4JIllegalStateException("Graph execution failed");
// FIXME: this is BAD
PagedPointer pagedPointer = new PagedPointer(res, 1024 * 1024L);
FlatResult fr = FlatResult.getRootAsFlatResult(pagedPointer.asBytePointer().asByteBuffer());
log.info("VarMap: {}", sd.variableMap());
INDArray[] results = new INDArray[fr.variablesLength()];
for (int e = 0; e < fr.variablesLength(); e++) {
FlatVariable var = fr.variables(e);
log.info("Var received: id: [{}:{}/<{}>];", var.id().first(), var.id().second(), var.name());
FlatArray ndarray = var.ndarray();
INDArray val = Nd4j.createFromFlatArray(ndarray);
results[e] = val;
if (var.name() != null && sd.variableMap().containsKey(var.name())) {
// log.info("VarName: {}; Exists: {}; NDArrayInfo: {};", var.opName(), sd.variableMap().containsKey(var.opName()), sd.getVertexToArray().containsKey(var.opName()));
// log.info("storing: {}; array: {}", var.name(), val);
sd.associateArrayWithVariable(val, sd.variableMap().get(var.name()));
} else {
// log.info("Original id: {}; out: {}; out2: {}", original, sd.getVertexIdxToInfo().get(original), graph.getVariableForVertex(original));
if (sd.variableMap().get(var.name()) != null) {
sd.associateArrayWithVariable(val, sd.getVariable(var.name()));
} else {
throw new ND4JIllegalStateException("Unknown variable received as result: [" + var.name() + "]");
}
}
}
return results;
}
use of org.bytedeco.javacpp.Pointer in project nd4j by deeplearning4j.
the class BasicContextPool method getDeviceBuffers.
/**
* This method is used to allocate
* @param context
* @param deviceId
*/
protected void getDeviceBuffers(CudaContext context, int deviceId) {
// ((CudaExecutioner) Nd4j.getExecutioner()).getNativeOps();
NativeOps nativeOps = NativeOpsHolder.getInstance().getDeviceNativeOps();
// we hardcode sizeOf to sizeOf(double)
int sizeOf = 8;
Pointer reductionPointer = nativeOps.mallocDevice(16385 * sizeOf * 2, new CudaPointer(deviceId), 0);
if (reductionPointer == null)
throw new IllegalStateException("Can't allocate [DEVICE] reduction buffer memory!");
nativeOps.memsetAsync(reductionPointer, 0, 16385 * sizeOf * 2, 0, context.getOldStream());
context.syncOldStream();
Pointer allocationPointer = nativeOps.mallocDevice(1024 * 1024, new CudaPointer(deviceId), 0);
if (allocationPointer == null)
throw new IllegalStateException("Can't allocate [DEVICE] allocation buffer memory!");
Pointer scalarPointer = nativeOps.mallocHost(1 * sizeOf, 0);
if (scalarPointer == null)
throw new IllegalStateException("Can't allocate [HOST] scalar buffer memory!");
context.setBufferScalar(scalarPointer);
context.setBufferAllocation(allocationPointer);
context.setBufferReduction(reductionPointer);
Pointer specialPointer = nativeOps.mallocDevice(1024 * 1024 * sizeOf, new CudaPointer(deviceId), 0);
if (specialPointer == null)
throw new IllegalStateException("Can't allocate [DEVICE] special buffer memory!");
nativeOps.memsetAsync(specialPointer, 0, 65536 * sizeOf, 0, context.getOldStream());
context.setBufferSpecial(specialPointer);
}
Aggregations