1 /* Copyright (c) 2009-2023. The SimGrid Team.
2 * All rights reserved. */
4 /* This program is free software; you can redistribute it and/or modify it
5 * under the terms of the license (GNU LGPL) which comes with this package. */
7 /* Copyright (c) 2019. Jonathan Borne. */
15 /* Where DIM is the dimension of the Grid (2D) */
16 /* and Q is the number of processes per dimension */
18 /* Local matrices size N*N */
20 int main(int argc, char** argv)
22 /* Nb of nodes in the grid:
23 initialized by MPI_Comm_size according to commandline -np value */
29 /* Current process ranks */
35 /* coords: used to get myLineRank and myColRank
36 initialized by MPI_Cart_coords
39 /* dims: Integer array of size ndims specifying the number
40 of processes in each dimension.
41 if init value is 0 it is reset by MPI_Dims_create.
44 for (int i = 0; i < DIM; i++) {
48 Logical array of size ndims specifying whether the grid is
49 periodic (true) or not (false) in each dimension. */
51 for (int i = 0; i < DIM; i++) {
54 /* reorder: do not allows rank reordering when creating the grid comm */
56 /* remainDims[]: used to set which dimension is kept in subcommunicators */
60 int* A = malloc(N * N * sizeof(int));
63 MPI_Init(&argc, &argv);
64 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
65 MPI_Comm_size(MPI_COMM_WORLD, &nbNodes);
67 printf("rank %d: Alive\n", rank);
69 MPI_Barrier(MPI_COMM_WORLD);
71 /* Set dims[] values to describe a grid of nbNodes and DIM dimensions*/
72 MPI_Cart_create(MPI_COMM_WORLD, DIM, dims, periods, reorder, &gridComm);
74 if (gridComm == MPI_COMM_NULL)
75 printf("error grid NULLCOMM\n");
77 MPI_Comm_rank(gridComm, &myGridRank);
78 MPI_Comm_size(gridComm, &gridSize);
79 MPI_Cart_coords(gridComm, myGridRank, DIM, coords);
80 myLineRank = coords[0];
81 myColRank = coords[1];
83 MPI_Barrier(MPI_COMM_WORLD);
85 /* Create a line communicator for current process */
88 MPI_Cart_sub(gridComm, remainDim, &lineComm);
90 /* Check if lineComm was initialized */
91 if (lineComm == MPI_COMM_NULL)
92 printf("(%d,%d): ERR (lineComm == NULLCOMM)\n", myLineRank, myColRank);
94 /* A Initialization */
95 for (int i = 0; i < N; i++) {
96 for (int j = 0; j < N; j++) {
97 *(A + (i * N) + j) = i == j ? rank : 0;
101 MPI_Barrier(MPI_COMM_WORLD);
105 MPI_Bcast(A, N * N, MPI_INT, root, lineComm);
108 printf("process:(%d,%d)\n", myLineRank, myColRank);
110 printf("-------------------\n");
111 for (int i = 0; i < N; i++) {
112 for (int j = 0; j < N; j++) {
113 printf(" %d", *(A + (i * N) + j));
117 printf("-------------------\n");
119 MPI_Comm_free(&lineComm);
120 MPI_Comm_free(&gridComm);
122 MPI_Barrier(MPI_COMM_WORLD);