我尝试使用#include <stdlib.h>
#include <string.h>
#include "mpi.h"
#define N 4 // NxN is origininal(big) matrices size to multiply
int SqrtRoot(int a)
{
switch(a)
{
case 1: return 1 ;
case 4: return 2;
case 9: return 3;
case 16: return 4;
default: return -1;
}
}
int main( argc, argv )
int argc;
char **argv;
{
int myID, p;
double t1,t2;
double *RowwiseA, *ColWiseB; // 1D array of size N*N/sqrt(p) to hold the 2D data of
// processors own datatogether with data of its rowwise neighbours
double* MyResult; // my result of size of nxn(n = N/p) is also stored 1D
int mydatastartindex;
int i,j,n;
MPI_Comm MyRowPartners, MyColPartners;
MPI_Init( &argc, &argv );
MPI_Comm_size( MPI_COMM_WORLD, &p );
MPI_Comm_rank( MPI_COMM_WORLD, &myID );
MPI_Comm_split(MPI_COMM_WORLD,myID/SqrtRoot(p), myID,&MyRowPartners);
MPI_Comm_split(MPI_COMM_WORLD,myID%SqrtRoot(p), myID,&MyColPartners);
int DataPerProcess = N*N/(p);
RowwiseA = (double*) malloc(N*N/SqrtRoot(p) * sizeof(double));
ColWiseB = (double*) malloc(N*N/SqrtRoot(p) * sizeof(double));
MyResult = (double*) malloc(N*N/p * sizeof(double));
mydatastartindex = myID * DataPerProcess;
n = SqrtRoot(N*N/p); // one deimension of local square matrix of nxn
// initialize my dat
for(i = 0; i < n; i++){
for(j = 0; j < n; j++) {
RowwiseA [mydatastartindex + i*n + j] = (i + j) * (myID + 1);
ColWiseB [mydatastartindex + i*n + j] = (i - j) * (myID + 1);
}
}
// ------------ THIS ONE WORKS ------------------
//double* sum = (double*)malloc(4*sizeof(double));
//
//double mydata = myID*1.0f;
//MPI_Allgather(
// &mydata ,
// 1,
// MPI_DOUBLE,
// sum,
// 1,
// MPI_DOUBLE,
// MyRowPartners
// );
//
//--------------------------------------------------
//--------THIS ONE DOES NOT WORK----------------------
MPI_Allgather(
&RowwiseA[mydatastartindex],
DataPerProcess,
MPI_DOUBLE,
RowwiseA ,
DataPerProcess,
MPI_DOUBLE,
MyRowPartners
);
//-----------------------------------------------------
return 0;
}
的行方向和列方式广播在MPI中实现矩阵矩阵乘法。虽然注释掉代码部分运行良好,但是其他代码(在它下面)不起作用(即给出信号-5和mpi进程被杀死)。有什么帮助吗?
感谢
ord
答案 0 :(得分:0)
我发现了这个错误。我应该在MyRowPartners中使用新的排名而不是myID 在MPI_ALLgather和矩阵索引中。谢谢..