2012-10-03 185 views
1

我需要在我的Matrix Multiplication程序中使用MPI_Gather功能,但在过去的几天里面临着麻烦。MPI_Gather似乎不工作

因此,我仅使用收集功能,并已试图让它跑......为此,我提到这本书的前言由彼得·帕切科到并行编程“写了一个简单的MPI程序。

程序直接退出,给我绝对没有任何结果或错误......我一直没能找出错误至今。

/******************NOTE********************** 

     The program simply uses the MPI_Gather() function. 
     The program is exiting directly. 
     I have written it for only TWO processes. 
     (-np 2) 

    ******************************************/ 

    #include<stdio.h> 
    #include"mpi.h" 

    int main() 
    { 
    int i,j,proc,rank; 
    double d[4]; 
    double local_a[2]; 


    MPI_Init(NULL,NULL); 
    MPI_Comm_size(MPI_COMM_WORLD, &proc); 
    MPI_Comm_rank(MPI_COMM_WORLD, &rank); 

    if(rank==0) 
    { 

     local_a[0]=1.0; 
     local_a[1]=2.0; 
    } 

    else 
    { 
     local_a[0]=3.0; 
     local_a[1]=4.0; 
    } 

    int local=2; 

    if(rank==0) 
    { 

     MPI_Gather(local_a,local,MPI_DOUBLE,d,local,MPI_DOUBLE,0,MPI_COMM_WORLD); 
    //MPI_Gather(&local_a,local,MPI_DOUBLE,&d,local,MPI_DOUBLE,0,MPI_COMM_WORLD); 
    //also tried the above line just to be certain. 


     printf("\n"); 
     for(j=0;j<4;j++) 
     printf("\t%f",d[j]); 
    } 
    else 
    { 
     MPI_Gather(local_a,local,MPI_DOUBLE,d,local,MPI_DOUBLE,0,MPI_COMM_WORLD); 
    //MPI_Gather(&local_a,local,MPI_DOUBLE,&d,local,MPI_DOUBLE,0,MPI_COMM_WORLD); 
    } 

    MPI_Finalize(); 

    return 0; 

    } 

任何人都可以帮我一把。

谢谢。

Anagha Madhusudanan

+0

你有什么错误?如果我们不知道在我的系统上编译并运行的代码会出现什么错误,我们可以提供帮助。 – pyCthon

回答

2

你的程序正常工作对我来说,给作为输出:

1.000000 2.000000 3.000000 4.000000 

你能分享你如何运行的详细信息,并编译可执行文件,所以我会尝试重现错误,并在编辑答案的情况下?

只是为了您的信息,你可以找到下面的程序稍加修改的版本,使得显而易见的是,接收缓冲区只能在根被分配:

#include <stdio.h> 
#include <stdlib.h> 
#include "mpi.h" 

int main() { 

    int rank  = -1; 
    int commsize = -1; 
    double sendbuffer[2]; 

    MPI_Init(NULL,NULL); 
    MPI_Comm_size(MPI_COMM_WORLD, &commsize); 
    MPI_Comm_rank(MPI_COMM_WORLD, &rank); 

    sendbuffer[0]=2.0*rank; 
    sendbuffer[1]=2.0*rank + 1; 

    int count=2; 

    if(rank==0) { 

    // Recvbuffer is significant only at root 
    double * recvbuffer = malloc(2*commsize*sizeof(double)); 
    // Gather values at root 
    MPI_Gather(sendbuffer,count,MPI_DOUBLE,recvbuffer,count,MPI_DOUBLE,0,MPI_COMM_WORLD); 
    // Print to screen 
    printf("\n"); 
    for(int jj=0; jj<2*commsize ;jj++) 
     printf("%f\n",recvbuffer[jj]); 
    // Free recvbuffer 
    free(recvbuffer); 

    } else { 
    MPI_Gather(sendbuffer,count,MPI_DOUBLE,NULL,0,MPI_DOUBLE,0,MPI_COMM_WORLD); 
    } 

    MPI_Finalize(); 

    return 0; 

} 
+0

也可以使用就地收集,以便仅使用主控级别中的一个缓冲区。 –