Hello,
On Feb 20, 2008 2:35 AM, Mosher, Chuck C.
<Chuc...@conocophillips.com> wrote:
A typical new user question, I bet:
I am working in on project (www.javaseis.org) that uses large arrays created
in Java. We currently call native methods through JNI to act on these
arrays, which results in a copy operation. Does JNA provide a means to pass
pointers to Java arrays directly to native methods, avoiding the copy ?
As I understand it, Java won't copy the array if it is stored
contiguously. However, as your array gets larger, the chances of it
not being allocated contiguously in memory increases. If it isn't
contiguous, Java has to copy it in order to return a pointer to JNI
that can actually be used by a C program.
As such, there isn't much JNA can do in this case (AFAIK).
However, there are a few alternatives. For my work, I have been
building bits and pieces of a matrix library on top of NIO buffers.
I'm mostly interested in passing these (large) buffers between HDF and
BLAS/LAPACK without copying. When you create a direct NIO buffer, Java
will never copy it. JNA's Memory class could probably be used in a
similar manner.
The NIO buffers and JNA's Memory class probably don't have quite as
nice an interface as arrays, but building an array class suited to
your application on top of this storage might be a feasible solution.
NIO buffers have one problem when you're talking about really big
arrays in that some position and offset variables that should have
been longs are ints. This is fixed in Java 7 by the BigBuffer and
friends, but it's something that JNA's Memory class probably already
addressed.
Cheers,
Albert