2015-12-22 79 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