【问题标题】:mpi communicator for each element of an array of structures结构数组的每个元素的 mpi 通信器
【发布时间】:2018-04-06 16:25:54
【问题描述】:

我是 MPI 新手,不确定这是否是正确的方法。或者如果我应该以这种方式使用 MPI,但我的问题如下:

我有一个指向用户定义结构的指针数组。根据每个进程中发生的情况,数组的元素可能是 NULL,也可能是指向用户定义结构实例的指针。我现在需要数组的元素通过 MPI 相互通信。这是有问题的,因为其中一些不存在。

我应该详细说明:该结构有一个函数指针,指向需要在其中发生通信的函数。如果元素存在,则调用该函数。如果没有,就没有。

我的想法:为数组的每个元素创建一个专用的 MPI 通信器,其中包括该元素不为 NULL 的所有处理器。然后在各个元素的通信过程中引用这个通信器。

我能否创建一个 MPI 通信器“数组”,每个元素一个对应数组?然后为每个元素引用 MPI_COMM_ARRAY[i]? 还是我完全处于死胡同,根本不应该使用 NULL 作为数组条目? 什么是“干净”的编码方式?

这是对我现在所拥有的内容的简化。如果细胞偶然存在于所有进程中,它就会起作用。如果他们不这样做,它就会失败。 示例代码:

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


void * createcell();
void Cell_givenumberofvertices(void * _self, int * NbOfVertices);
void Cell_givenumberofvertices_parallel(void * _self, int * NbOfVertices);
void Cell_addvertex(void * _self);
void addvertex(void * _self);
void getnumberofvertices(void * _self, int * NbOfVertices);


struct Cell{
  unsigned NbOfVertices;
  void (* givenumberofvertices)(void * _self, int * NbOfVertices);
  void (* addvertex)(void * _self);
};

void * createcell(){
  struct Cell * self = calloc(1, sizeof(struct Cell));
  int world_size;

  MPI_Comm_size(MPI_COMM_WORLD,&world_size);

  self->NbOfVertices = 0;
  self->addvertex = Cell_addvertex;

  if(world_size==0) self->givenumberofvertices = Cell_givenumberofvertices;
  else self->givenumberofvertices = Cell_givenumberofvertices_parallel;

  return self;
}

void Cell_givenumberofvertices(void * _self, int * NbOfVertices){
   struct Cell * self = _self;
   * NbOfVertices = self->NbOfVertices;
   return;
}

void Cell_givenumberofvertices_parallel(void * _self, int * NbOfVertices){
  struct Cell * self = _self;
  int world_size, world_rank;
  int i;
  int * NbVertxOnProcess;
  int totalnumberofvertices=0;

  MPI_Comm_size(MPI_COMM_WORLD,&world_size);
  MPI_Comm_rank(MPI_COMM_WORLD,&world_rank);
  NbVertxOnProcess = (int *) malloc(world_size*sizeof(int));

  MPI_Gather(&(self->NbOfVertices),1,MPI_UNSIGNED,NbVertxOnProcess,1,MPI_INT,0,MPI_COMM_WORLD);

  for(i=0;i<world_size;i++) totalnumberofvertices+=NbVertxOnProcess[i];

   * NbOfVertices = totalnumberofvertices;
   return;
}

void Cell_addvertex(void * _self){
  struct Cell * self = _self;
  self->NbOfVertices ++;
  return;
}

 void addvertex(void * _self){
   struct Cell * self = _self;
   self->addvertex(self);
 }

 void getnumberofvertices(void * _self, int * NbOfVertices){
  struct Cell * self = _self;
  self->givenumberofvertices(self, NbOfVertices);
 }  



int main(int argc, char *argv[]) {
  void ** cells;
  int i,j;
  const int numberofcells = 100;
  const int numberofvertices = 100;
  const float domainlength = 115.4;
  float grid[numberofcells];
  float vertexcoordinates[numberofvertices];
  int world_rank;

  MPI_Init(NULL,NULL);

  /* create array of Cell pointers */
  cells = (void **) calloc(numberofcells,sizeof(void *));

  /* create grid */
  for(i=0;i<numberofcells;i++){
    grid[i]=domainlength/numberofcells*(i+1);
  }
  /* generate random vertex coordinates */
  MPI_Comm_rank(MPI_COMM_WORLD,&world_rank);
  srand((unsigned int) world_rank);
  for(i=0;i<numberofvertices;i++){
    vertexcoordinates[i]=((float)rand()/(float)(RAND_MAX)) * domainlength;
  }
  /* find the cell the vertex is in */
  for(i=0;i<numberofvertices;i++){
    for(j=0;j<numberofcells;j++){
      float lb, ub;
      if(j==0) lb=0.0;
      else lb=grid[j-1];
      ub = grid[j];
      if(lb<vertexcoordinates[i]&&vertexcoordinates[i]<ub){
         if(!cells[j]){
          cells[j]=createcell();
         }
        addvertex(cells[j]);
      }
    }
  }

  for(i=0;i<numberofcells;i++){
     if(cells[i]){
      int NbVertxInCell;
      getnumberofvertices(cells[i], &NbVertxInCell);
      printf("%i vertices in cell number %i \n",NbVertxInCell,i);
    }
  }
  MPI_Finalize();
  return 0;
}

【问题讨论】:

  • 请显示一些代码,您的解释很难理解。例如,“用户定义结构数组”被视为用户定义结构的数组。
  • 很公平,我希望现在更清楚了。问题在于函数 Cell_givenumberofvertices_parallel()

标签: c arrays data-structures mpi function-pointers


【解决方案1】:

我仍然不完全了解您想要实现的目标,也不了解这种设计的基本原理。

无论如何,这里有一些想法。

首先,请注意,在调用集体操作时,通信器中的所有任务都应调用它,否则某些任务可能会挂起。

其次,在Cell_givenumberofvertices_parallel() 中,您可以将MPI_Gather() 替换为MPI_Reduce()。而且由于主循环打印在所有行列上,我相信你真的想要MPI_Allreduce()

我还怀疑你想在struct Cell 中添加一个MPI_Comm * comm 字段, 否则,并非所有队伍都会在 MPI_COMM_WORLD 上调用集体,这将挂起。

第三,我认为不需要指针函数。

如果一个单元格只在一个任务上,那么通讯应该是MPI_COMM_SELF,你可以使用MPI_Allreduce(),这样就不需要两个子程序了。

最后但同样重要的是,我看不出函数如何随着时间的推移返回不同的值,因此可以在初始化时计算顶点总数,并将其存储为新字段。

例如,可能是

struct Cell{
  unsigned localNbOfVertices;
  unsigned totalNbOfVertices;
  MPI_Comm comm;
};

在你的主循环中,你可以

  for(i=0;i<numberofcells;i++){
     unsigned local=0, total;
     MPI_Comm comm;
     if(cells[i]) local=cells[i]->localNbOfVertices;
     MPI_Allreduce(&local, &total, 1, MPI_UNSIGNED, MPI_SUM, MPI_COMM_WORLD);
     if(cells[i] cells[i]->totalNbOfVertices = total;
     printf("%i vertices in cell number %i \n",total,i);
     if(cells[i]) MPI_Comm_split(MPI_COMM_WORLD, 0, world_rank, &cells[i]->comm); else MPI_Comm_split(MPI_COMM_WORLD, MPI_UNDEFINED, world_rank, &comm);
  }

【讨论】:

  • 是的,为了生成一个小例子,我可能过度简化了代码。在实际应用中,顶点位置随时间而变化。此外,函数 get/givenumberovertices() 只是一个简化的表示。在实际应用中,所采用的函数取决于顶点的数量及其位置。添加 MPI_Comm * comm 到 struct Cell 听起来就像我正在寻找的。但我很难理解在哪里以及如何创建新的沟通者。如果我在 createCell() 中执行此操作,我不知道该单元存在于哪些其他处理器上。
  • 我用通讯器创建更新了我的答案。初始化所有单元后,应创建单元通信器。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-12-17
  • 2023-02-05
  • 2011-06-01
  • 2021-04-16
  • 1970-01-01
  • 1970-01-01
  • 2018-05-10
相关资源
最近更新 更多