【发布时间】: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