Arrays MPI广播二维阵列
我打算用MPI学习并行编程。我有一些错误Arrays MPI广播二维阵列,arrays,mpi,broadcast,Arrays,Mpi,Broadcast,我打算用MPI学习并行编程。我有一些错误 #include "mpi.h" #include <stdio.h> #include <stdlib.h> int main(int argc, char** argv) { int procNum, procRank; int m,n; int sumProc = 0, sumAll = 0; int** arr; MPI_Status status; MPI_Init
#include "mpi.h"
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char** argv)
{
int procNum, procRank;
int m,n;
int sumProc = 0, sumAll = 0;
int** arr;
MPI_Status status;
MPI_Init ( &argc, &argv );
MPI_Comm_size ( MPI_COMM_WORLD, &procNum );
MPI_Comm_rank ( MPI_COMM_WORLD, &procRank );
if (procRank == 0)
{
printf("Type the array size \n");
scanf("%i %i", &m, &n);
}
MPI_Bcast(&m, 1, MPI_INT, 0, MPI_COMM_WORLD);
MPI_Bcast(&n, 1, MPI_INT, 0, MPI_COMM_WORLD);
arr = new int*[m];
for (int i = 0; i < m; i++)
arr[i] = new int[n];
if (procRank == 0)
{
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
arr[i][j] = rand() % 30;
printf("%i ", arr[i][j]);
}
printf("\n");
}
}
MPI_Bcast(&arr[0][0], m*n, MPI_INT, 0, MPI_COMM_WORLD);
for (int i = procRank; i < n; i += procNum)
for (int j = 0; j < m; j++)
sumProc += arr[j][i];
MPI_Reduce(&sumProc,&sumAll,1,MPI_INT,MPI_SUM,0,MPI_COMM_WORLD);
if (procRank == 0)
{
printf("sumAll = %i", sumAll);
}
delete *arr;
MPI_Finalize();
return 0;
}
这是什么问题?也许问题出在MPI_Bcast中
我补充道
for (int i = 0; i < m; i++)
MPI_Bcast(arr[i], n, MPI_INT, 0, MPI_COMM_WORLD);
它在这里解决了我的问题
arr = new int*[m];
for (int i = 0; i < m; i++)
arr[i] = new int[n];
假设所有数组在内存中都是连续的。因为它们不是,所以会得到不同的值。在我看来,在不成功的版本中,您(错误地)假设数组在内存中是连续的。如何避免这种情况?或者在std::vector或int**中使用MPI是不可能的,我只能使用int[m][n]之类的东西?
MPI_Bcast(&arr[0][0], m*n, MPI_INT, 0, MPI_COMM_WORLD);
arr = new int*[m];
for (int i = 0; i < m; i++)
arr[i] = new int[n];
MPI_Bcast(&arr[0][0], m*n, MPI_INT, 0, MPI_COMM_WORLD);