Create nd4j array backed by nio ByteBuffer

Hello ! I am using nd4j for cyclic workload, I have a nio ByteBuffer that contains all the data but the problem is I have to update the INDArray by creating a new one as img_nd = Nd4j.create(imgYUVBuffer, YUVimgShape, DataType.FLOAT);

What I want is the INDArray img_nd get automatically updated on updating the ByteBuffer.

How can I achieve this ?

@MankaranSingh you can create DataBuffers from ByteBuffers, but they have to be allocated using
ByteBuffer.allocateDirect(…) or it will crash your JVM.

Something like:

ByteBuffer byteBuffer = ByteBuffer.allocateDirect(..);
DataBuffer buffer = Nd4j.createBuffer(byteBuffer,DataType.FLOAT,numberOfElementsInBuffer);
INDArray arr = Nd4j.create(buffer);

More here:

That should work for you.

Hi @agibsonccc. thanks for the reply ! This unfortunately didn’t work out for me. Updating the ByteBuffer dosent reflecrs changes on the ndarray. Could this be because my data is in float and the ndarray buffer takes in byte buffer ? The method you mentioned says that This will wrap the buffer as a reference (no copy) if the allocation opType is the same.

@MankaranSingh Could you give me something end to end reproducible I can just run on my own to see what you’re talking about? I would need to see how you’re creating your bytebuffer and how you’re testing everything.

Hi @agibsonccc, I used the exact same example as you metioned.

But I think I fixed the issue. what I did was,

    float imgYUVBuffer[] = new float[512*384*1];
    long[] YUVimgShape = {384, 512, 1};
    DataBuffer imgYUVDataBuffer = Nd4j.createBuffer(imgYUVBuffer);
    INDArray img_nd = Nd4j.create(imgYUVDataBuffer, YUVimgShape);
    FloatBuffer imgYUVByteBuffer = imgYUVDataBuffer.asNioFloat();

now editing imgYUVByteBuffer directly edits the ndarray. I know this is a bit of a hack but it worked.

If you’d like, I will submit the code that didn’t work in a bit after creating a minimal runnable script.

@MankaranSingh I would need how you created the original bytebuffer as well. That matters a lot. You gave me the ByteBuffer already existing. On heap buffers won’t work with nd4j.