uncomplicate / neanderthal

Fast Clojure Matrix Library
http://neanderthal.uncomplicate.org
Eclipse Public License 1.0
1.06k stars 56 forks source link

Getting pointer to matrix/vector? #40

Closed howarth closed 6 years ago

howarth commented 6 years ago

Hello, I'd like to use this library but require the following functionality. I can do the work of figuring out how to do it, but I wanted to ask you if it's even possible.

Given a vector or matrix created by, for example, (dv 10) or (dge 2 3) could I access the pointer to the location of that matrix in memory? I have some code that loads data from disk into vectors or matrices across different languages because in MATLAB/NumPy/Julia all matrices are the same in memory (so that BLAS/etc can use them)

I.e. I want to be able to write the following

(def my-matrix (dge 2 3))
(def matrix-pointer (function-i-will-write-to-get-pointer a))
(load-data matrix-pointer ... )
; my-matrix should now have the data loaded
(do-some-calculation my-matrix) 
blueberry commented 6 years ago

Yes. You can get the raw buffer with the uncomplicate.neanderthal.block/buffer function. Please note that there is no such thing as "pointer" in Java, but only references and values. That buffer is native, but you can only get the C pointer of that array when you drop into the JNI layer.

howarth commented 6 years ago

Ok, thank you! I'm confused because Block.java shows that buffer returns an Object. I normally program in MATLAB/Python so I'm pretty noob with respect to java/c.

So would

env->GetDirectBufferAddress(buffer)

be what I use to get the pointer in the JNI layer?

Also, what is offset in Block.java?

howarth commented 6 years ago

Or is it the case that when I pass the Object returned from Block.buffer down to the JNI layer the variable that holds that Object reference is itself the pointer? Sorry if these questions don't make sense.

blueberry commented 6 years ago

For the example that you provide (main memory dge/dv) you'll get DirectByteBuffer and you can get its direct pointer with GetDirectBufferAddress. If you have another type of Buffer, that won't work.

offset is, well, the offset from the address of the buffer to the first element that is used in the vector or matrix. Also note that neanderthal supports many matrix layouts, so not all bytes in that byte buffer (C array) are always relevant.

howarth commented 6 years ago

Ok, when you say different layouts, are you talking about Native, CL, and CU?

blueberry commented 6 years ago

No. GE, TR, etc.