我是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: %dn", buf[i]);
}
}
MPI_Finalize();
return 0;
}
错误:
Error C4700 uninitialized local variable 'buf' used
我不明白为什么它希望它被初始化。我给了记忆空间,只是想进一步填满它。好像我不懂一些c++的简单操作之类的。初始化像
这样的东西int **buf = nullptr;
也尝试:
buf[i] = new int[count * sizeof(int)];
没有任何影响。请给我一个提示。
嗯,我认为你需要检查count的值。
例子(提示(?))
count = 0;
再试一次