2013-01-07 12 views
1

저는 병렬 프로그래밍에 익숙하지 않아 자바로하고 싶습니다. MPI를 통해보다 복잡한 객체를 보내고받을 수 있는지 궁금합니다. MPJ Express를 사용하고 있습니다. 그러나 개체를 보내고 싶을 때마다 ClassCastException이 발생합니다.MPJ로 표현한 객체 보내기

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로 좀 더 복잡한 개체를주고받을 수 있습니까? - 그렇다면 무엇이 잘못 되었나요?

답변

4

나는 MPJ에 익숙하다. 그러나 둘러싸는 객체는 원시 타입 일 필요가있다. (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 인터페이스를 가지고 있습니다.

1

보내기 전에 직렬화하려고합니다.

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 브라이언

0
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(); 

    } 
}