【问题标题】:C++ double type in MPIMPI 中的 C++ 双精度类型
【发布时间】:2016-03-28 04:05:25
【问题描述】:

在 MPI 中发生了一些奇怪的事情,我不太明白。我有以下简单的代码:

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 类型出了点问题。因为如果我将“double”更改为“int”,我会得到预期的输出:

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

有什么建议吗?

【问题讨论】:

    标签: c++ double mpi send


    【解决方案1】:

    您使用了错误的格式说明符,%d 用于 int。对于double,您应该使用%f%e%a%g,例如参见文档here

    由于问题被标记为,您最好只使用 iostreams 进行输出。

    【讨论】:

    • 不可能!多么可笑的错误!我应该在晚上停止编码:D 谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-05
    相关资源
    最近更新 更多