【问题标题】:Send objects with MPJ express使用 MPJ express 发送对象
【发布时间】:2012-12-21 11:06:13
【问题描述】:

我是并行编程的新手,我想用 java 来做。 我想知道是否可以通过 MPI 发送和接收更复杂的对象。我正在使用 MPJ 快递。但是,每当我想发送一个对象时,我都会得到一个 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;

所以我的问题是: - 是否可以使用 MPJ Express 发送/接收更复杂的对象? - 如果是这样:我做错了什么?

【问题讨论】:

    标签: java mpi mpj-express


    【解决方案1】:

    我也是 MPJ express 的新手,但似乎封闭对象需要是原始类型 - 一些东西的数组。 (就像您在 OpenMPI 中使用 C/C++ 实现一样)。

    这种代码对我很有效:

        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 接口。

    【讨论】:

      【解决方案2】:

      您想在发送之前对其进行序列化。

      import mpi.*;
      
      import java.io.*;
      import java.nio.ByteBuffer;
      
      public class MPITest
      {
          public static void main(String[] args)
          {
              MPI.Init(args);
              int me = MPI.COMM_WORLD.Rank();
              int tasks = MPI.COMM_WORLD.Size();
      
              MPI.COMM_WORLD.Barrier();
      
              if(me == 0)
              {
                  Cat cat = new Cat("Tom", 15);
                  cat.Speak();
      
                  ByteBuffer byteBuff = ByteBuffer.allocateDirect(2000 + MPI.SEND_OVERHEAD);
                  MPI.Buffer_attach(byteBuff);
      
                  try
                  {
                      ByteArrayOutputStream bos = new ByteArrayOutputStream();
                      ObjectOutput out = null;
                      out = new ObjectOutputStream(bos);
                      out.writeObject(cat);
                      byte[] bytes = bos.toByteArray();
      
                      System.out.println("Serialized to " + bytes.length);
      
                      MPI.COMM_WORLD.Isend(bytes, 0, bytes.length, MPI.BYTE, 1, 0);
                  }
                  catch(IOException ex)
                  {
      
                  }
              }
              else
              {
                  byte[] bytes = new byte[2000];
                  Cat recv = null;
                  MPI.COMM_WORLD.Recv(bytes, 0, 2000, MPI.BYTE, MPI.ANY_SOURCE, 0);
      
                  ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
                  ObjectInput in = null;
                  try
                  {
                      in = new ObjectInputStream(bis);
                      Object obj = in.readObject();
                      recv = (Cat)obj;
      
                      recv.Speak();
                  }
                  catch(IOException ex)
                  {
      
                  }
                  catch(ClassNotFoundException cnf)
                  {
      
                  }
              }
      
              MPI.COMM_WORLD.Barrier();
              MPI.Finalize();
          }
      } 
      

      这可行,但是您可能希望使用外部化并手动执行以避免序列化例程将发送的一些额外垃圾。

      HTH 布赖恩

      【讨论】:

        【解决方案3】:
        import mpi.*;
        
        /**
         * Compile: javac -cp $MPJ_HOME/lib/mpj.jar:. ObjSend.java 
         * Execute: mpjrun.sh -np 2 -dport 11000 ObjSend
         */
        
        public class ObjSend {
            public static void main(String[] args) throws Exception {
        
                int peer ; 
        
                MPI.Init(args);
        
                int rank = MPI.COMM_WORLD.Rank() ;
                int size = MPI.COMM_WORLD.Size() ; 
        
                int tag = 100 ; 
        
                if(rank == 0) {
        
                    String [] smsg = new String[1] ; 
                    smsg[0] = "Hi from proc 0" ; 
                    peer = 1 ; 
                    MPI.COMM_WORLD.Send(smsg, 0, smsg.length, MPI.OBJECT, 
                                                                peer, tag);
                    System.out.println("proc <"+rank+"> sent a msg to "+
                                       "proc <"+peer+">") ; 
        
                } else if(rank == 1) {
        
                    String[] rmsg = new String[1] ; 
                    peer = 0 ; 
                    MPI.COMM_WORLD.Recv(rmsg, 0, rmsg.length , MPI.OBJECT, 
                                                                 peer, tag);
                    System.out.println("proc <"+rank+"> received a msg from "+
                                       "proc <"+peer+">") ; 
                    System.out.println("proc <"+rank+"> received the following "+
                                       "message: \""+rmsg[0]+"\"") ; 
        
                } 
        
                MPI.Finalize();
        
            }   
        }
        

        【讨论】:

          猜你喜欢
          • 2016-07-18
          • 2014-07-17
          • 2020-03-01
          • 2019-03-10
          • 2015-07-04
          • 2023-03-04
          • 1970-01-01
          • 2013-04-06
          • 2016-05-25
          相关资源
          最近更新 更多