1

I need to convert one-dimensional and two-dimensional Java arrays of double elements through JNI to/from the ALGLIB real_1d_array and real_2d_array in C/C++, respectively. Right now, the process is as follows:

  1. Use the JNI GetDoubleArrayElements() to obtain a contiguous vector of jdouble in C/C++. On my machine, this always produces a copy, i.e. JNI sets the last argument of this call, jboolean isCopy, to JNI_TRUE.

  2. Allocate a C/C++ vector of double and copy elements from the JNI vector of jdouble to it. Note that, in general, there is no guarantee that the jdouble type is the same as the double type, so a cast to double is necessary when copying array elements one by one. (Is there a clean way to determine that jdouble is indeed the same as double, either at compile time or runtime?)

  3. Instantiate an ALGLIB real_1d_array or real_2d_array, respectively. As far as I can tell from the ALGLIB code, this creates an ae_matrix of dimensions (0, 0) internally held by the array.

  4. Copy the array elements from the C/C++ contiguous vector of double to the ALGLIB array using the setcontent() method. As far as I can tell from the code, this resizes the internallly-held ae_matrix appropriately and copies elements one by one into that matrix. It looks like in the case of a two-dimensional matrix, the first dimension is a vector of pointers to linear vectors of double, so that elements are stored non-contiguously in an array of one-dimensional arrays (similar to Java).

  5. Use ALGLIB to perform the desired computations using the real arrays.

  6. To copy results back to JAVA arrays through JNI, reverse the process. Elements of the ALGLIB real arrays are only accessible through methods. For one-dimensional arrays, there is a getcontent() method that returns a pointer to a contiguous vector of double. For two-dimensional array, through operator [] that returns a pointer to a contiguous row. As before a one-by-one element copy with a cast to jdouble is used.

As you can see, there is plenty of copying involved. For small arrays, this is not a big deal, but for larger ones this gets quite expensive in terms of memory and CPU.

Is there a way to speed things up without relying on the knowledge of internal structures of ALGLIB, which may be subject to change?

user1408140
  • 639
  • 3
  • 9
  • 20

0 Answers0