如何使用MPI发送和接收一维数组的边界数据?

时间:2014-12-12 17:28:01

标签: pointers parallel-processing segmentation-fault mpi distributed-computing

晚上好,
我正在尝试修复此MPI代码的问题。

Master可以向从属设备发送一维数据块,但是当工作人员与邻居交换数据时程序崩溃。我可能会认为这是因为我应该创建一个vector-datatype来向右发送最后一个元素和第一个元素离开了。我怎么能这样做?

这是代码的一部分,它给我带来了问题:

numworkers = numtasks-1;
if (taskid == MASTER) {
         printf("Master\n");
double *Q=malloc(m * n * cell_size *  sizeof(double));
/*Set initial Gauss hump*/ 
for (k=0;k<3;k++)     
for (i = 0; i < m; i++) {
 for (j = 0; j < n; j++) {
  Q(k,i,j) = 4.0;
  }
 }
  printf("gauss done.\n");
  averow = (m*n*cell_size)/numworkers;
  extra = (m*n*cell_size)%numworkers;
  MPI_Type_contiguous(rows, MPI_DOUBLE, &rowtype);!
  MPI_Type_commit(&rowtype);

  printf ("Starting program with %d worker tasks.\n", numworkers);
  printf("Grid size: X= %d  Y= %d  Time steps= %d\n",m,n,STEPS);

  for (i=1; i<=numworkers; i++) // Distribute work to workers.
  {rows = (i <= extra) ? averow+1 : averow; 
        if (i == 1) // neighbors
        left = NONE;
     else
        left = i - 1;
     if (i == numworkers)
        right = NONE;
     else
        right = i + 1;
     /*  Now send startup information to each worker  */
     dest = i;
     MPI_Send(&rows, 1, MPI_INT, dest, BEGIN, MPI_COMM_WORLD);
     MPI_Send(&left, 1, MPI_INT, dest, BEGIN, MPI_COMM_WORLD);
     MPI_Send(&right, 1, MPI_INT, dest, BEGIN, MPI_COMM_WORLD);
     MPI_Send(Q,1, rowtype, dest, BEGIN, 
              MPI_COMM_WORLD);
     printf("Sent to task %d: rows= %d offset= %d ",dest,rows,offset);
     printf("left= %d right= %d\n",left,right);
     offset = offset + rows;
  }
   for (i=1; i<=numworkers; i++)
  {
     source = i;
     msgtype = DONE;
   MPI_Irecv(&rows, 1, MPI_INT, source, msgtype, MPI_COMM_WORLD, &request);         
   MPI_Irecv(Q, 1,rowtype, source,
              msgtype, MPI_COMM_WORLD, &request);
   }
  printf("Solver took\n");
  free(Q);
  MPI_Type_free(&rowtype);
  MPI_Finalize();}

  if (taskid != MASTER) 
  {MPI_Recv(&rows, 1, MPI_INT, MASTER, BEGIN, MPI_COMM_WORLD, &status);
  double *Q =malloc(rows*  sizeof(double));     
  MPI_Recv(&left, 1, MPI_INT, MASTER, BEGIN, MPI_COMM_WORLD, &status);
  MPI_Recv(&right, 1, MPI_INT, MASTER, BEGIN, MPI_COMM_WORLD, &status);
  MPI_Recv(Q, rows, MPI_DOUBLE, MASTER, BEGIN, MPI_COMM_WORLD, &status);
   printf("ricevo da master rows %d \n",rows);      
  printf("Task %d received work. Beginning time steps...\n",taskid);
  for (it = 1; it <= numworkers; it++)
  {{if (left != NONE)  //boundary data exchange
     {MPI_Send(Q, 1, MPI_DOUBLE, left,
                 RTAG, MPI_COMM_WORLD);
        source = left;
        msgtype = LTAG;
        MPI_Recv(Q, 1, MPI_DOUBLE, source,
                  msgtype, MPI_COMM_WORLD, &status);}
     if (right != NONE)
     {
        MPI_Send(Q, 1, MPI_DOUBLE, right,
                  LTAG, MPI_COMM_WORLD);
        source = right;
        msgtype = RTAG;
        MPI_Recv(Q, 1, MPI_DOUBLE, source, msgtype,
                  MPI_COMM_WORLD, &status);}
     /* Now call update to update the value of grid points*/ 
   Q=Q+1;
     printf("update fatto."); }
  /* Finally, send my portion of final results back to master */
  MPI_Send(&offset, 1, MPI_INT, MASTER, DONE, MPI_COMM_WORLD);
  MPI_Send(&rows, 1, MPI_INT, MASTER, DONE, MPI_COMM_WORLD);
  MPI_Send(Q, rows, MPI_DOUBLE, MASTER, DONE, 
           MPI_COMM_WORLD);
  free(Q); 
  MPI_Type_free(&rowtype); 
  MPI_Finalize();
  }

Q定义为:

  #define Q(i,j,k) Q[((k) + n * ((j) + m * (i)))]

用于调试:我正在使用带有Putty的大学服务器。我该怎么调试这个程序?

非常感谢你,我希望有人会帮助我!

1 个答案:

答案 0 :(得分:0)

我尝试使用Non-Blocking,现在它可以工作了。该程序崩溃,因为任务没有以正确的方式接收和发送数据。我也改变了数据的大小,这是我没有看到的另一个错误。而不是

if (left != NONE)  //boundary data exchange
 {MPI_Send(Q, 1, MPI_DOUBLE, left,
             RTAG, MPI_COMM_WORLD);
    source = left;
    msgtype = LTAG;
    MPI_Recv(Q, 1, MPI_DOUBLE, source,
              msgtype, MPI_COMM_WORLD, &status);}
 if (right != NONE)
 {
    MPI_Send(Q, 1, MPI_DOUBLE, right,
              LTAG, MPI_COMM_WORLD);
    source = right;
    msgtype = RTAG;
    MPI_Recv(Q, 1, MPI_DOUBLE, source, msgtype,
              MPI_COMM_WORLD, &status);}

我用过:

if (left != NONE)  //boundary data exchange
     {MPI_Isend(Q, rows, MPI_DOUBLE, left,
                 RTAG, MPI_COMM_WORLD,&req1);
        source = left;
        msgtype = LTAG;
        MPI_Irecv(Q, rows, MPI_DOUBLE, source,
                  msgtype, MPI_COMM_WORLD, &req2);
 printf("boundary left done");
 MPI_Wait(&req1,&status);
 MPI_Wait(&req2,&status);
 }

     if (right != NONE)
     {
        MPI_Isend(Q, rows, MPI_DOUBLE, right,
                  LTAG, MPI_COMM_WORLD,&req3);
        source = right;
        msgtype = RTAG;
        MPI_Irecv(Q, rows, MPI_DOUBLE, source, msgtype,
                  MPI_COMM_WORLD, &req4);
      printf("boundary right done");
      MPI_Wait(&req3,&status);
      MPI_Wait(&req4,&status);}

谢谢大家的帮助!