MPI Gather 仅从根进程收集

MPI Gather only gathering from Root Process

首先,我一直在使用 this code 作为参考,它显示了在没有 MPI_Scatter 的情况下使用 MPI_Gather,因为这就是我在这里想要实现的目标。我已经为此工作了很长时间,只是无法弄清楚这个问题。这种 sobel 边缘检测算法加强了图像内部对象的轮廓。

我将post我的代码放在下面,因为没有太多,但我会先给出一个快速的代码描述。

我正在尝试将顺序程序转换为并行程序。所以所有 non-MPI 代码都是正确的。

所以我的 MPI 代码只能在某处出错。

int main(int argc, char **argv) {

     FILE *inFile, *oFile;
     int grayImage[N][N], edgeImage[N][N];
     char type[2];
     int w, h, max;
     int r, g, b, y, x, i, j, sum, sumx, sumy;
     int tid;

     int GX[3][3], GY[3][3];
     double elapsed_time;
     struct timeval tv1, tv2;
     int error = 0;
     char buffer[BUFSIZ];
     int rank, NP;

     // Code lies here for reading from the image file and storing into the grayImage array. 
     // This works so I saw no reason to include it

     /* 3x3 Sobel masks. */
     GX[0][0] = -1; GX[0][1] = 0; GX[0][2] = 1;
     GX[1][0] = -2; GX[1][1] = 0; GX[1][2] = 2;
     GX[2][0] = -1; GX[2][1] = 0; GX[2][2] = 1;

     GY[0][0] =  1; GY[0][1] =  2; GY[0][2] =  1;
     GY[1][0] =  0; GY[1][1] =  0; GY[1][2] =  0;
     GY[2][0] = -1; GY[2][1] = -2; GY[2][2] = -1;



     MPI_Init(NULL, NULL);

     MPI_Comm_size(MPI_COMM_WORLD, &NP);
     MPI_Comm_rank(MPI_COMM_WORLD, &rank);

     // This calculates the block size.MPI 
     // On 4 processors the block size for a 100x100 image would be 25x100 each

     int blksz = (int)ceil((double)N/NP);

     // This creates a local array for each processor, soon to be gathered

     int tempEdge[blksz][N];

     // this line shows it's working correctly

     printf("processor %d, width: %d, height: %d, blksz: %d, begin: %d, end: %d\n", rank, w, h, blksz, rank*blksz, (rank+1)*blksz);

     for(x=rank*blksz; x < (rank+1)*blksz && x<h; x++){

        // Any code in this loop can be ignored as it works correctly.

         for(y=0; y < w; ++y){

             sumx = 0;
             sumy = 0;
             // handle image boundaries 
             if(x==0 || x==(h-1) || y==0 || y==(w-1))
                 sum = 0;
             else{
                 //x gradient approx
                 for(i=-1; i<=1; i++)  {
                     for(j=-1; j<=1; j++){
                         sumx += (grayImage[x+i][y+j] * GX[i+1][j+1]);
                     }
                 }
                 //y gradient approx
                 for(i=-1; i<=1; i++)  {
                     for(j=-1; j<=1; j++){
                         sumy += (grayImage[x+i][y+j] * GY[i+1][j+1]);
                     }
                 }
                 //gradient magnitude approx
                 sum = (abs(sumx) + abs(sumy));
             }
             tempEdge[x][y] = clamp(sum);
         }
     }

     // Here is the line I am guessing is causing the problem

     MPI_Gather(&tempEdge, w*blksz, MPI_INT,
               &edgeImage, w*blksz, MPI_INT, 0,
               MPI_COMM_WORLD);


     // Finally, I output edgeImage to a file here.

     if(rank==0){

         // output edgeImage to File

     }

     MPI_Finalize();

     return 0;    
}

我使用的输入图片是这样的:

但输出只给出了这个:

如您所见,它只是图像的前四分之一 (N/4) 或 blksz

这意味着 MPI_Gather 仅从排名为 0 的进程收集数据?

我在这上面花了很多时间,如果有任何帮助,我们将不胜感激!

不要将其余代码中的错误归咎于 MPI 集体。您的代码在没有段错误的情况下生成损坏的图像实际上是一个奇迹。看看那部分:

int tempEdge[blksz][N];
             ~~~~~

for(x = rank*blksz; x < (rank+1)*blksz && x<h; x++){
        ~~~~~~~~~~
   for(y = 0; y < w; ++y){
      ...
      tempEdge[x][y] = clamp(sum);    (1)
               ~
   }
}

对于任何 > 0 的等级,代码写入数组末尾。将 (1) 处的语句修改为:

tempEdge[x - rank*blksz][y] = clamp(sum);

此外,删除 MPI_Gather 调用中的 &

MPI_Gather(tempEdge, w*blksz, MPI_INT,
           edgeImage, w*blksz, MPI_INT, 0,
           MPI_COMM_WORLD);

它也适用于 &,但这在技术上是不正确的。如果您坚持使用 address-of 运算符,请改用 &tempEdge[0][0]&edgeImage[0][0]