【问题标题】:Trouble with MPI_AllgatherMPI_Allgather 的问题
【发布时间】:2015-04-25 07:18:52
【问题描述】:

我尝试使用MPI_Allgather 的行和列广播在 MPI 中实现矩阵矩阵乘法。尽管注释掉的代码部分运行良好,但另一个(在它下面)不起作用(即给出信号 -5 并且 mpi 进程被杀死)。请问有什么帮助吗? 谢谢

#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


    【解决方案1】:

    我发现了错误。我应该在 MyRowPartners 中使用新排名而不是 myID 在 MPI_ALLgather 和矩阵索引中。不过还是谢谢..

    【讨论】:

    • 当您在 StackOverflow 上回答自己的问题时,几天后您仍然可以将答案标记为正确。我鼓励你这样做,尽管这个问题可能会被关闭,因为答案不太可能帮助其他人。这个问题与您的情况非常具体。
    猜你喜欢
    • 2015-04-01
    • 2012-04-10
    • 2020-02-10
    • 2019-05-30
    • 2015-11-26
    • 2017-06-04
    • 2023-01-13
    • 2017-10-10
    • 2011-08-17
    相关资源
    最近更新 更多