In C a 2-dimensional array with m rows is stored row-wise in a
contiguous memory area while it is stored in m+1 1-dimensional
arrays in Java (each 1-dimensional array is stored in a contiguous
memory area, but the whole matrix isn't stored in a contiguous
memory area). "Datatype.Vector" is implemented via JNI and calls
the C function MPI_Type_vector which expects that a matrix is
stored in a contiguous memory area. This leads to wrong results
if you want to construct a new datatype for column elements of a
matrix in Java. Jeff suggested some possible solutions in the last
weeks which I will now discuss.
1) Simulate a 2-dimensional (m,n)-array in one 1-dimensional array
with m*n elements and perform all necessary index computations
yourself. This solution works and with the new method
Datatype.Resized you can scatter all columns of a matrix to
The problem is that you don't use a multi-dimensional array
from Java. I'm not a Java expert, but I suppose that it even
contradicts the philosophy of object-oriented programming.
In my opinion this solution will also add new sources of errors
if you must implement complicated algorithms (bugs with index
computations, unreadable source code, etc.).
2) Implement a wrapper class in Java so that you don't have to deal
with index computations in your normal program.
As far as I know, it would have been necessary to use set- and
get-methods to write and read matrix elements in that case,
because Java doesn't allow operator overloading (I'm not even
sure, if it would have been possible to overload "" in the
case that overloading would have been supported). In my opinion
this approach wouldn't solve the problem, because I wouldn't be
able to apply Datatype.Vector to an object of this new class.
If I'm right, "Datatype.Vector" would be needless or I would have
to implement methods to create a column datatype and to send and
to receive column blocks in that class as well. I think, that I
would also get once more complicated and unreadable source code.
3) Implement a wrapper class which possibly needs to be written in C.
Yesterday I tried to understand how I can use JNI to solve this
problem, because I didn't have any experience with JNI before.
I can use JNI to convert a Java matrix into a C matrix and vice
versa and in this way my original Java matrix can be stored into
a contiguous memory area. However, in that case I can also use
MPI_Type_vector, because I'm already using C, so that
Datatype.Vector would be needless.
In my opinion Datatype.Vector and friends should be implemented in
a way that they can be used with multi-dimensional Java arrays,
although I'm not sure if that is possible. The current version of
their implementation is not optimal, but nevertheless better than
nothing. Does somebody have other ideas or comments?
Jeff, thank you very much for all your time and comments. Now I
will try some other methods and I let you know, if I find more