| [5773b57e] | 1 | /*
|
|---|
| 2 | This program demonstrates how to build communicators using
|
|---|
| 3 | MPI_Comm_split. For each process it builds a new communicator
|
|---|
| 4 | for the processes in the same row and column in a two-dimensional
|
|---|
| 5 | process structure.
|
|---|
| 6 | You have to use a square number of processes to execute the
|
|---|
| 7 | program, for instance 4 or 9 processes.
|
|---|
| 8 |
|
|---|
| 9 | Compile the program with 'mpicc rowcol.c -o rowcol'
|
|---|
| 10 | Run the program on a square number of processes.
|
|---|
| 11 | */
|
|---|
| 12 |
|
|---|
| 13 | #include <stdlib.h>
|
|---|
| 14 | #include <stdio.h>
|
|---|
| 15 | #include <math.h>
|
|---|
| 16 | #include <mpi.h>
|
|---|
| 17 |
|
|---|
| 18 | int main(int argc, char *argv[]) {
|
|---|
| 19 | int id, ntasks, q, i;
|
|---|
| 20 | int row, col;
|
|---|
| 21 | int ntasks_row, id_row; /* Nr of processes and rank in the row */
|
|---|
| 22 | int id_col; /* Rank in the column communicator */
|
|---|
| 23 | MPI_Comm row_comm, col_comm; /* Communicators for row and column */
|
|---|
| 24 | int X[10]; /* Send buffer */
|
|---|
| 25 |
|
|---|
| 26 | MPI_Init(&argc, &argv); /* Initialize MPI */
|
|---|
| 27 | MPI_Comm_size(MPI_COMM_WORLD, &ntasks); /* Get nr of tasks */
|
|---|
| 28 | MPI_Comm_rank(MPI_COMM_WORLD, &id); /* Get id of this process */
|
|---|
| 29 |
|
|---|
| 30 | /* The process grid will be of size q*q */
|
|---|
| 31 | q = (int) sqrt((double) ntasks);
|
|---|
| 32 |
|
|---|
| 33 | /* Check that we have a square number of processes */
|
|---|
| 34 | if (q*q == ntasks) {
|
|---|
| 35 |
|
|---|
| 36 | /* Calculate on which row and column this process is */
|
|---|
| 37 | row = id/q;
|
|---|
| 38 | col = id%q;
|
|---|
| 39 |
|
|---|
| 40 | /* Build communicator for processes in the same row */
|
|---|
| 41 | MPI_Comm_split(MPI_COMM_WORLD, row, id, &row_comm);
|
|---|
| 42 |
|
|---|
| 43 | /* Build communicator for processes in the same column */
|
|---|
| 44 | MPI_Comm_split(MPI_COMM_WORLD, col, id, &col_comm);
|
|---|
| 45 |
|
|---|
| 46 | /* Get nr of tasks in the row communicator */
|
|---|
| 47 | MPI_Comm_size(row_comm, &ntasks_row);
|
|---|
| 48 |
|
|---|
| 49 | /* Get own rank in row communicator */
|
|---|
| 50 | MPI_Comm_rank(row_comm, &id_row);
|
|---|
| 51 |
|
|---|
| 52 | /* Get own rank in row communicator */
|
|---|
| 53 | MPI_Comm_rank(col_comm, &id_col);
|
|---|
| 54 |
|
|---|
| 55 | printf("Process %d has rank %d in its row communicator ", id, id_row);
|
|---|
| 56 | printf("and %d in the column communicator\n", id_col);
|
|---|
| 57 |
|
|---|
| 58 | /* The first process in each row assigns values to X */
|
|---|
| 59 | if (id_row==0) {
|
|---|
| 60 | for (i=0; i<10; i++) X[i]=i;
|
|---|
| 61 | }
|
|---|
| 62 | MPI_Bcast(&X, 10, MPI_INT, 0, row_comm);
|
|---|
| 63 | }
|
|---|
| 64 |
|
|---|
| 65 | else { /* Not a square number of proceses */
|
|---|
| 66 | if (id == 0) {
|
|---|
| 67 | printf("You have to use a square number of processes\n");
|
|---|
| 68 | printf("Quitting\n");
|
|---|
| 69 | }
|
|---|
| 70 | } /* end of else-part */
|
|---|
| 71 |
|
|---|
| 72 |
|
|---|
| 73 | /* All proceses exit MPI and terminate */
|
|---|
| 74 | MPI_Finalize();
|
|---|
| 75 | exit(0);
|
|---|
| 76 |
|
|---|
| 77 | }
|
|---|
| 78 |
|
|---|