2015-12-22 101 views
3

MPI中發生了一些奇怪的事情,我不太明白。我有以下簡單的代碼:MPI中的C++雙類型

MPI_Init(&argc, &argv); 

int rank; 
int size; 

MPI_Comm_rank(MPI_COMM_WORLD, &rank); 
MPI_Comm_size(MPI_COMM_WORLD, &size); 

if (rank == 0) { 
    double ridiculous = 7.9; 

    printf("Process 0 will be sending number %d\n", ridiculous); 

    MPI_Send(&ridiculous, 1, MPI_DOUBLE, 1, 0, MPI_COMM_WORLD); 

    printf("Process 0 sent number %d\n", ridiculous); 
} 
else { 
    double received = 0.; 

    MPI_Recv(&received, 1, MPI_DOUBLE, 0, 0, MPI_COMM_WORLD, 
     MPI_STATUS_IGNORE); 

    printf("Process 1 received number %d from process 0\n", received); 
} 

MPI_Finalize(); 

我期待這樣的輸出:

Process 0 will be sending number 7.9 
Process 0 sent number 7.9 
Process 1 received number 7.9 from process 0 

但奇怪的是收到此:

Process 0 will be sending number 1112261192 
Process 0 sent number -32766 
Process 1 received number -32766 from process 0 

我沒那麼擅長此道MPI的東西,但它看起來像我雙面類型出了問題。因爲如果我將「double」更改爲「int」,我會得到預期輸出:

Process 0 will be sending number 7 
Process 0 sent number 7 
Process 1 received number 7 from process 0 

有什麼建議嗎?

回答

4

您正在使用錯誤的格式說明符,%d用於int。對於double,您應該使用%f,%e,%a%g,參見例如doc here

由於問題標記爲,所以最好只使用iostreams進行輸出。

+1

不行!多麼荒謬的錯誤!我應該在晚上停止編碼:D謝謝! – picusiaubas