使用 C++ MPI Iprobe 单元化局部变量“buf”

C++ MPI Iprobe unitialized local variable 'buf' used

提问人:Юрий Дзбановский 提问时间:10/10/2021 最后编辑:double-beepЮрий Дзбановский 更新时间:11/18/2021 访问量:107

问:

我是 C++ 和 MPI 的新手。接到任务并阅读了很多。我仍然相信我正确地编写了所有内容,但仍然无法执行而不会出现任何错误。我的代码:

#include <iostream>
#include <mpi.h>

using namespace std;

int main() {
int myid, numprocs, **buf, source, i; 
    int message[3] = { 0, 1, 2 };
    int myrank, data = 2002, count, TAG = 0;
    MPI_Status status; 
    MPI_Init(NULL, NULL); 
    MPI_Comm_rank(MPI_COMM_WORLD, &myrank);

    if (myrank == 0) {
        MPI_Send(&data, 1, MPI_INT, 2, TAG, MPI_COMM_WORLD);
    }
    else if (myrank == 1) {
        MPI_Send(&message, 3, MPI_INT, 2, TAG, MPI_COMM_WORLD);
    }
    else {
        MPI_Probe(MPI_ANY_SOURCE, TAG, MPI_COMM_WORLD, &status);
        source = status.MPI_SOURCE; 
        MPI_Get_count(&status, MPI_INT, &count);

        for (i = 0; i < count; i++) {
            //buf[i] = new int[count * sizeof(int)];
             buf[i] = (int *)malloc(count * sizeof(int));
        } 
        MPI_Recv(&buf[0], count, MPI_INT, source, TAG, MPI_COMM_WORLD, &status);
        for (i = 0; i < count; i++) {
            printf("received: %d\n", buf[i]);
        }
    }
    MPI_Finalize();

    return 0;
}

错误:

Error   C4700   uninitialized local variable 'buf' used

我不明白它为什么要初始化它。我给了内存空间,只想进一步填充它。似乎我没有解开一些 C++ 简单的操作或 smth。初始化类似

int **buf = nullptr;

还尝试过:

buf[i] = new int[count * sizeof(int)];

没有任何区别。请给我一个提示。

C++ malloc mpi 新运算符

评论

0赞 Gilles Gouaillardet 10/10/2021
buf确实是未初始化的。
0赞 Gilles Gouaillardet 10/10/2021
declare 然后 .就风格/品味而言,您可以int * bufbuf = (int *)malloc(count * sizeof(int)MPI_Recv(buf, ...)
0赞 Юрий Дзбановский 10/10/2021
@GillesGouaillardet谢谢你的答案。现在它给了我.似乎是因为它之前知道“count”变量值?Error C4703 potentially uninitialized local pointer variable 'buf' useds inside of "else", but how could I do it earlier if I haven
0赞 Юрий Дзбановский 10/10/2021
@GillesGouaillardet我让它工作,没有错误,但现在应用程序只是冻结并且没有给出任何结果。buf = new int[count * sizeof(int)];MPI_Recv(buf, count, MPI_INT, source, TAG, MPI_COMM_WORLD, &status);
0赞 Gilles Gouaillardet 10/10/2021
我假设您启动了 3 个 MPI 任务。排名 2 应该收到两条消息(来自排名 0 和 1),但它目前只收到一条。

答:

0赞 Lina 11/18/2021 #1

好吧,我认为您需要检查计数值。

示例 (hint(?))

count = 0;

再试一次

评论

0赞 overseas 11/19/2021
这既不是警告所描述的,也不是程序中的错误,因为计数是通过 GetCount 启动的。这是关于 buf 未初始化,正如评论已经暗示的那样。