问题描述
我是并行编程的新手,我想用Java来做.我想知道是否可以通过MPI发送和接收更复杂的对象.我正在使用MPJ Express.但是,每当我要发送对象时,我都会收到ClassCastException.
I'm new to parallel programming and I want to do it in java.I am wondering if it is possible to send and receive more complex ojects via MPI. I'm using MPJ express. However whenever I want to send a object I get a ClassCastException.
MPI.Init(args);
myrank = MPI.COMM_WORLD.Rank();
numprocs = MPI.COMM_WORLD.Size();
Vector<CustomClass> chr = new Vector<CustomClass>();
if (myrank == 0 ) { //am I the master?
for (int i = 1; i < numprocs; i++) {
MPI.COMM_WORLD.Send(chr, 0, chr.size(), MPI.OBJECT, i, 99); //Here's where the
exception occurs
}
}
else {
Vector<BasicRegion> chr_received = new Vector<BasicRegion>();
MPI.COMM_WORLD.Recv(chr_received, 0, 1, MPI.OBJECT, 0, 99 );
}
例外:
mpi.MPIException:mpi.MPIException:java.lang.ClassCastException:java.util.Vector无法转换为[Ljava.lang.Object;
mpi.MPIException: mpi.MPIException: java.lang.ClassCastException: java.util.Vector cannot be cast to [Ljava.lang.Object;
所以我的问题是:-是否可以使用MPJ Express发送/接收更复杂的对象?-如果是这样:我在做什么错了?
So my questions are:- is it possible to send/receive more complex objects with MPJ Express?- if so: what am I doing wrong?
推荐答案
我也是MPJ express的新手,但是似乎封闭对象必须是原始类型-某种东西的数组. (就像您在OpenMPI中使用C/C ++实现一样).
I am new to MPJ express as well, but seems the enclosing object needs to be primitive type - array of something. (like as you do with the C/C++ implementation in OpenMPI).
这种代码对我来说很有效:
This kind of code worked for me well:
Node t[] = new Node[4];
...
count[0] = t.length;
MPI.COMM_WORLD.Send(count, 0, 1, MPI.INT, 1, 98);
MPI.COMM_WORLD.Send(t, 0, t.length, MPI.OBJECT, 1, 99);
} else if( myRank == 1 ) {
int count[] = new int[1];
MPI.COMM_WORLD.Recv( count, 0, 1, MPI.INT, 0, 98);
Status mps = MPI.COMM_WORLD.Recv( t, 0, count[0], MPI.OBJECT, 0, 99 );
...
当然,您必须具有实现Serializable接口的自定义类.
And of course, you have to have that custom class implementing Serializable interface.
这篇关于使用MPJ Express发送对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!