ดูเหมือนว่า MPI_Gather จะไม่ทำงาน

ฉันจำเป็นต้องใช้ฟังก์ชัน MPI_Gather ในโปรแกรมการคูณเมทริกซ์แต่กำลังประสบปัญหาในช่วงสองสามวันที่ผ่านมา

ดังนั้นฉันจึงเขียนโปรแกรม MPI ง่ายๆ โดยใช้ฟังก์ชันรวบรวมเพียงอย่างเดียวและพยายามทำให้มันรัน... สำหรับสิ่งนี้ ฉันอ้างถึงหนังสือ 'Introduction to Parallel Programming by Peter Pacheco'

โปรแกรมออกจากการทำงานโดยตรง โดยทำให้ฉันไม่มีผลลัพธ์หรือข้อผิดพลาดเลย... ฉันยังไม่สามารถเข้าใจข้อผิดพลาดได้จนถึงตอนนี้

   /******************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;

    }

ใครก็ได้โปรดช่วยฉันด้วย

ขอบคุณ.

อนาฆะ มธุสุดานันท์


c mpi
person user1715985    schedule 03.10.2012    source แหล่งที่มา
comment
ข้อผิดพลาดของคุณคืออะไร? เราจะช่วยได้อย่างไรโดยไม่รู้ว่าคุณได้รับข้อผิดพลาดอะไรสำหรับโค้ดที่คอมไพล์และทำงานได้ดีบนระบบของฉัน   -  person pyCthon    schedule 04.10.2012


คำตอบ (1)


โปรแกรมของคุณทำงานได้ดีสำหรับฉัน โดยให้ผลลัพธ์เป็น:

    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;

}
person Massimiliano    schedule 03.10.2012
comment
เราสามารถใช้การรวบรวมแบบแทนที่เพื่อใช้บัฟเฟอร์เพียงตัวเดียวในระดับหลัก - person Hristo Iliev; 04.10.2012