这是我第一次与MPI合作。我在写一段使用向量和MPI进行矩阵乘法的代码。我的程序可以成功编译,但是当我运行该程序时,我不断出现错误。有人可以帮助我解释为什么我收到此错误。 TIA
* MPI_Recv中发生错误 由进程报告[139967236014081,139964394242049] 关于MPI_COMM_WORLD MPI_ERR_RANK:无效排名 MPI_ERRORS_ARE_FATAL(此通信器中的进程现在将中止, * ,可能还有您的MPI工作)
/**********************************************************************
* MPI-based matrix multiplication AxB=C
*********************************************************************/
#include<ctime>
#include<iostream>
#include<time.h>
#include <vector>
#include <cstdlib>
#include "mpi.h"
#define N 10 /* number of rows and columns in matrix */
using namespace std;
MPI_Status status;
//double a[N][N],b[N][N],c[N][N];
int main(int argc, char *argv[])
{
int numtasks,taskid,numworkers,source,dest,rows,offset,i,j,k;
int averow, extra;
int loop = 1000;
double randomNum;
vector<vector<double> > a;
vector<vector<double> > b;
vector<vector<double> > c;
vector<vector<double> > avg_Matrix;
a.resize(N);
for (int i = 0; i < N; ++i)
a[i].resize(N);
b.resize(N);
for (int i = 0; i < N; ++i)
b[i].resize(N);
c.resize(N);
for (int i = 0; i < N; ++i)
c[i].resize(N);
avg_Matrix.resize(N);
for (int i = 0; i < N; ++i)
avg_Matrix[i].resize(N);
srand ( time(NULL) ); //Initializing random seed.
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &taskid);
MPI_Comm_size(MPI_COMM_WORLD, &numtasks);
//cout<<"Number of works"<<numworkers;
//cout<<"taskid"<<taskid;
numworkers = numtasks-1;
/*---------------------------- master ----------------------------*/
if (taskid == 0) {
for (i=0; i<N; i++) {
for (j=0; j<N; j++) {
randomNum = rand() % 400 + (-199.999); // generate randomnumbers between -199.999 to 199.999 for first matrix.
a[i][j] = randomNum;
}
}
cout<<"Printing First Matrix"<<endl;
for(i=0; i< N; i++)
{
cout<<endl;
for(j=0; j< N; j++)
{
cout<<a[i][j];
cout<<"\t";
}
}
/* second matrix. */
for(i=0; i< N; i++)
{
for(j=0; j< N; j++)
{
randomNum = rand() % 400 + (-199.999); // generate randomnumbers between -199.999 to 199.999 for first matrix.
b[i][j] = randomNum;
}
}
cout<<endl;
cout<<endl;
/* Printing the second Matrix*/
cout<<"Printing Second Matrix"<<endl;
for(i=0; i< N; i++)
{
cout<<endl;
for(j=0; j< N; j++)
{
cout<<b[i][j];
cout<<"\t";
}
}
/* send matrix data to the worker tasks */
averow = N/numworkers;
extra = N%numworkers;
offset = 0;
for (dest=1; dest<=numworkers; dest++)
{
rows = (dest <= extra) ? averow+1 : averow;
MPI_Send(&offset, 1, MPI_INT, dest, 1, MPI_COMM_WORLD);
MPI_Send(&rows, 1, MPI_INT, dest, 1, MPI_COMM_WORLD);
MPI_Send(&a[offset][0], rows*N, MPI_DOUBLE,dest,1, MPI_COMM_WORLD);
MPI_Send(&b, N*N, MPI_DOUBLE, dest, 1, MPI_COMM_WORLD);
offset = offset + rows;
}
/* wait for results from all worker tasks */
if(taskid == 0) {
for (i=1; i<=numworkers; i++)
{
source = i;
MPI_Recv(&offset, 1, MPI_INT, source, 2, MPI_COMM_WORLD, &status);
MPI_Recv(&rows, 1, MPI_INT, source, 2, MPI_COMM_WORLD, &status);
MPI_Recv(&c[offset][0], rows*N, MPI_DOUBLE, source, 2, MPI_COMM_WORLD, &status);
}
}
cout<<"Here is the result matrix:"<<endl;
for (i=0; i<N; i++) {
for (j=0; j<N; j++)
cout<<"\t"<<c[i][j];
cout<<endl;
}
}
/*---------------------------- worker----------------------------*/
if (taskid > 0) {
source = 0;
MPI_Recv(&offset, 1, MPI_INT, source, 1, MPI_COMM_WORLD, &status);
MPI_Recv(&rows, 1, MPI_INT, source, 1, MPI_COMM_WORLD, &status);
MPI_Recv(&a, rows*N, MPI_DOUBLE, source, 1, MPI_COMM_WORLD, &status);
MPI_Recv(&b, N*N, MPI_DOUBLE, source, 1, MPI_COMM_WORLD, &status);
/* Matrix multiplication */
for (k=0; k<N; k++){
for (i=0; i<rows; i++) {
for (j=0; j<N; j++){
c[i][k] = c[i][k] + a[i][j] * b[j][k];
}
}
}
MPI_Send(&offset, 1, MPI_INT, 0, 2, MPI_COMM_WORLD);
MPI_Send(&rows, 1, MPI_INT, 0, 2, MPI_COMM_WORLD);
MPI_Send(&c, rows*N, MPI_DOUBLE, 0, 2, MPI_COMM_WORLD);
}
MPI_Finalize();
return 0;
}