2013-11-28 51 views
0

我看過這個,但它並沒有真正幫助,因爲我正在處理基元,而不是對象。這只是一個額外部分的一部分,是一個項目。這很好,但我想讓它工作。MPJ Express發送和接收 - ClassCastException

編譯好了,但是當我運行它時,我在第20行得到了一個ClassCastException異常。

/* Java Version (mine) */  
public class PingPongJava { 

    public static void main(String args[]) throws Exception { 
     int me; 
     int size; 
     int recv_buf = 0; 
     int buf = 0; 
     int send_buf = 101; 
     double tic = 0, toc = 0; 

     MPI.Init(args); 
     me = MPI.COMM_WORLD.Rank(); 
     size = MPI.COMM_WORLD.Size(); 

     tic = MPI.Wtime(); 
     for (int i = 0; i < 50; i++){ 
      if (me == 0){ 
      MPI.COMM_WORLD.Send(send_buf, 0,1,MPI.INT, 17, 100); 
      MPI.COMM_WORLD.Recv(recv_buf, 0,1, MPI.INT, 23, 100); 

      } 
      else{ 
      MPI.COMM_WORLD.Recv(buf, 0, 1, MPI.INT, 17, 100); 
      MPI.COMM_WORLD.Send(buf, 0,1,MPI.INT, 23, 100); 
      } 
     } 
     toc = MPI.Wtime(); 
     if (me == 0){ 
      System.out.println("Time taken is " + (toc-tic)/100); 
      } 

     MPI.Finalize(); 
    } 
    } 

我從這個C版轉換

/* Point-to-point communication. 
*/ 
#include <mpi.h> 
#include <stdio.h> 

int main(int argc, char **argv){ 
    int rank, size, i, recv_buf, send_buf=101, buf; 
    MPI_Status status; 
    double tic, toc; 

    MPI_Init(&argc, &argv); 
    MPI_Comm_rank (MPI_COMM_WORLD, &rank); 
    MPI_Comm_size (MPI_COMM_WORLD, &size); 

    tic = MPI_Wtime(); 
    for(i=0;i<50;i++){ 
    if(rank==0){ 
     MPI_Send(&send_buf, 1, MPI_INT, 1, 17 , MPI_COMM_WORLD); 
     MPI_Recv(&recv_buf, 1, MPI_INT, 1, 23, MPI_COMM_WORLD, &status); 
    }else{ 
     MPI_Recv(&buf, 1, MPI_INT, 0, 17, MPI_COMM_WORLD, &status); 
     MPI_Send(&buf, 1, MPI_INT, 0, 23, MPI_COMM_WORLD); 
    } 
    } 
    toc = MPI_Wtime(); 

    if(rank==0) 
    printf("Average time for a single message: %lf seconds \n", (toc-tic)/100.0); 

    MPI_Finalize(); 
    return 0; 
} 

回答

1

首先,你必須使用數組類型變量recv_buf,BUF,send_buf。次要的,Send,Recv簽名中的最後兩個參數是錯誤的。它們必須是:「..目標,標籤)」在發送和「..源,標籤)」在接收。我在下面的代碼中糾正了你的錯誤。

public class PingPongJava { 

    public static void main(String args[]) throws Exception { 
     int me; 
     int size; 
     int[] recv_buf = { 0 }; 
     int[] buf = {0}; 
     int[] send_buf = { 101 }; 
     double tic = 0, toc = 0; 

     MPI.Init(args); 

     me = MPI.COMM_WORLD.Rank(); 
     size = MPI.COMM_WORLD.Size(); 

     tic = MPI.Wtime(); 

     for (int i = 0; i < 50; i++) { 
      if (me == 0) { 
       MPI.COMM_WORLD.Send(send_buf, 0, 1, MPI.INT, 1, 17); 
       MPI.COMM_WORLD.Recv(recv_buf, 0, 1, MPI.INT, 1, 23); 
      } else { 
       MPI.COMM_WORLD.Recv(buf, 0, 1, MPI.INT, 0, 17); 
       MPI.COMM_WORLD.Send(buf, 0, 1, MPI.INT, 0, 23); 
      } 
     } 
     toc = MPI.Wtime(); 

     if (me == 0) { 
      System.out.println("Time taken is " + (toc - tic)/100); 
     } 

     MPI.Finalize(); 
    } 
}