Download Programming on Parallel Machines - matloff
Transcript
6.4. COLLECTIVE COMMUNICATIONS
1
2
3
4
5
6
7
129
MPI_Scatter(oh, nv*nv, MPI_INT, ohchunk, nv/nnodes, MPI_INT, 0,
MPI_COMM_WORLD);
mycount = 0;
for (i = 0; i < nv*nv/nnodes)
if (ohchunk[i] != 0) mycount++;
MPI_Reduce(&mycount,&numedge,1,MPI_INT,MPI_SUM,0,MPI_COMM_WORLD);
if (me == 0) printf("there are %d edges\n",numedge);
6.4.2.4
The MPI Barrier()
This implements a barrier for a given communicator. The name of the communicator is the sole argument
for the function.
Explicit barriers are less common in message-passing programs than in the shared-memory world.
6.4.3
Creating Communicators
Again, a communicator is a subset (either proper or improper) of all of our nodes. MPI includes a number
of functions for use in creating communicators. Some set up a virtual “topology” among the nodes.
For instance, many physics problems consist of solving differential equations in two- or three-dimensional
space, via approximation on a grid of points. In two dimensions, groups may consists of rows in the grid.
Here’s how we might divide an MPI run into two groups (assumes an even number of MPI processes to
begin with):
MPI_Comm_size(MPI_COMM_WORLD,&nnodes);
MPI_Comm_rank(MPI_COMM_WORLD,&me);
...
// declare variables to bind to groups
MPI_Group worldgroup, subgroup;
// declare variable to bind to a communicator
MPI_Comm subcomm;
...
int i,startrank,nn2 = nnodes/2;
int *subranks = malloc(nn2*sizeof(int));
if (me < nn2) start = 0;
else start = nn2;
for (i = 0; i < nn2; i++)
subranks[i] = i + start;
// bind the world to a group variable
MPI_Comm_group(MPI_COMM_WORLD, &worldgroup);
// take worldgroup the nn2 ranks in "subranks" and form group
// "subgroup" from them
MPI_Group_incl(worldgroup, nn2, subranks, subgroup);
// create a communicator for that new group
MPI_Comm_create(MPI_COMM_WORLD, subgroup, subcomm);
// get my rank in this new group
MPI_Group_rank (subgroup, &subme);