使用MPI_Gather断言失败

时间:2017-02-01 15:42:39

标签: c malloc mpi

我正在尝试编写重复执行计算的MPI C代码,并将其结果保存到单个数组中,以减少输出频率。下面的示例代码(var的大小,200,足以满足使用中的CPU数量):

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

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

float *phie, *phitemp, var[200];
int time=0, gatherphi=10, gatherfile = 200, j, iter=0, lephie, x;
int nelecrank = 2, size, rank, Tmax = 2000;
FILE *out;

MPI_Init(&argc, &argv) ;

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

lephie = gatherfile/gatherphi; // number of runs of calculation before output

// allocate memory
//printf("%d Before malloc.\n", rank);
if (rank==1) phie=(float *) malloc(nelecrank*size*gatherfile/gatherphi*sizeof(float));
phitemp=(float *) malloc(nelecrank*sizeof(float));
//printf("%d After malloc.\n", rank);

for(j=0;j<200;j++) var[j]=rank;

for(time=0;time<Tmax;time++){
if (!time%gatherphi) {// do calculation

  for (j=0;j<nelecrank;j++) { // each processor does the calculation nelecrank times
              phitemp[j]=0; 
              for (x = 0; x<=4; x++) {
                  phitemp[j]=phitemp[j]+var[rank+j*size];
                  }
              } 
         } // end of j for loop
         printf("iter: %d, %d Before MPI_Gather.\n", iter, rank);
         MPI_Gather(phitemp, nelecrank, MPI_FLOAT, phie+iter*nelecrank*size*sizeof(float), nelecrank, MPI_FLOAT, 1, MPI_COMM_WORLD);
         iter++;
     } // end of gatherphi condition

if (time % gatherfile) { //output result of calculation
  iter=0;
  if (rank==1) {
              out = fopen ("output.txt", "wt+");
              if (out == NULL) {
                  printf("Could not open output file.\n");
                  exit(1);
                  }
              else  printf("Have opened output file.\n");
              for (j=0;j<nelecrank*size*lephie;j++) {
                      fprintf(out,"%f ",*(phie+j*sizeof(float)));
                  }
              fclose(out);
              }
   } // end of file output

if (rank==1) {
      if (phie) free (phie);
      }
if (phitemp) free (phitemp);
MPI_Finalize();

return 0;
}

它给了我重复的内存分配问题,直到它最终退出。我没有经验在MPI中使用内存分配 - 你能帮忙吗?

非常感谢, 玛尔塔

1 个答案:

答案 0 :(得分:0)

基本上,phie还不够大。

malloc nelecrank*size*gatherfile/gatherphi*sizeof(float)=80*sizeof(float) phieMPI_Gather内存。

但是,您的iter*nelecrank*size*sizeof(float)+nelecrank*size*sizeof(float)使用需要iter内存。 nelecrank*Tmax-1的最大值为phie,因此(nelecrank*Tmax-1)*nelecrank*size*sizeof(float)+nelecrank*size*sizeof(float)必须为8000*size*sizeof(float),大约为iter=0

也许你想在某个地方重置 "code":"ErrorAccessDenied", "message":"Access is denied. Check credentials and try again."