Discussion:
Creating an array of groups/communicators
(too old to reply)
l***@yahoo.com
2008-01-29 10:24:54 UTC
Permalink
Hi all,
I'm wondering if it is possible to create an array of groups/
communicators. Say I have 9 processors, and I want to create 8 groups,
as shown below

(new_group : processors in that group)
new_group[0] : 0 1 2 3 4 5 6 7 8
new_group[1] : 1 2 3 4 5 6 7 8
new_group[2] : 2 3 4 5 6 7 8
new_group[3] : 3 4 5 6 7 8
new_group[4] : 4 5 6 7 8
new_group[5] : 5 6 7 8
new_group[6] : 6 7 8
new_group[7] : 7 8

The reason why I need the processors to be grouped this way is so I
can broadcast from the first processor in each group to the rest of
the processors, and the number of processors and the processors
involved in each broadcast varies as shown.

Here's what I have:

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

#define p 9

int main(int argc, char** argv)
{
int rank, i, j, k, **ranks, no_procs[p-1];
MPI_Group orig_group, new_group[p-1];
MPI_Comm new_comm[p-1];
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_group(MPI_COMM_WORLD, &orig_group);

ranks = (int**)calloc(p-1, sizeof(int*));
for (i = 0 ; i < (p-1) ; i++)
ranks[i] = (int*)calloc(p-i, sizeof(int));

for (i = 0 ; i < (p-1) ; i++)
{
no_procs[i] = p-i;
for (j = i ; j < p ; j++)
ranks[i][j] = j;
}

for (i = 0 ; i < (p-1) ; i++)
{
MPI_Group_incl(orig_group, no_procs[i], ranks[i],
&new_group[i]);
MPI_Comm_create(MPI_COMM_WORLD, new_group[i], &new_comm[i]);
}

for (i = 0 ; i < (p-1) ; i++)
free(ranks[i]);
free(ranks);
MPI_Finalize();
return 0;
}

I keep getting the segmentation fault error right before the
MPI_Group_incl(orig_group, no_procs[i], ranks[i], &new_group[i]);
line. What am I getting wrong here?

Thank you.

Regards,
Rayne
Christian
2008-01-29 12:59:34 UTC
Permalink
Post by l***@yahoo.com
I keep getting the segmentation fault error right before the
MPI_Group_incl(orig_group, no_procs[i], ranks[i], &new_group[i]);
line. What am I getting wrong here?
You are simply not doing a correct 'ranks[]' initialization.
Replacing "ranks[i][j] = j;" with "ranks[i][j-i] = j;" should fix your
problem.

BTW, to me your approach seems to be a bit intricate (but unnecessary)
in several ways ...

Christian
l***@yahoo.com
2008-01-30 03:59:48 UTC
Permalink
Post by Christian
Post by l***@yahoo.com
I keep getting the segmentation fault error right before the
MPI_Group_incl(orig_group, no_procs[i], ranks[i], &new_group[i]);
line. What am I getting wrong here?
You are simply not doing a correct 'ranks[]' initialization.
Replacing "ranks[i][j] = j;" with "ranks[i][j-i] = j;" should fix your
problem.
Thank you!
Post by Christian
BTW, to me your approach seems to be a bit intricate (but unnecessary)
in several ways ...
Christian
Do you have any suggestions? My main aim is to, say, have processor 2
broadcast "X" to processors 3 to 8, without processors 0 and 1
getting "X", and then have say, processor 5 broadcast "Y" to
processors 6 to 8 without processors 0 to 4 getting "Y". There will in
fact be MANY of such broadcasts throughout the program (the program
above is just to test out the group/communicator part of my actual
program.)

Loading...