-
Notifications
You must be signed in to change notification settings - Fork 0
/
mpi_cart_shift.cpp
65 lines (51 loc) · 1.75 KB
/
mpi_cart_shift.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*
* MPI_Cart_create
*/
# include <iostream>
# include <cstdlib>
# include "mpi.h"
using namespace std;
int main(int argc, char *argv[])
{
// MPI Init and get rank
int rank, nprocs;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &nprocs);
// Variables required by Create_cart
int ndims, dims[2], periods[2], reorder;
ndims = 2; // 2D matrix/grid
dims[0] = 3; // rows
dims[1] = 4; // columns
periods[0] = true; // row periodic (each column forms a ring)
periods[1] = true; // columns periodic (each row forms a ring)
reorder = true; // allows processes reordered for efficiency
// Create a communicator given the 2D torus topology.
MPI_Comm new_comm;
MPI_Cart_create(MPI_COMM_WORLD, ndims, dims, periods, reorder, &new_comm);
// Torus rank in the new communicator
int torus_rank;
MPI_Comm_rank(new_comm, &torus_rank);
// Get my coordinates in the new communicator
int torus_coords[2];
MPI_Cart_coords(new_comm, torus_rank, ndims, torus_coords);
// Print my location in the 2D torus
cout << "rank:" << rank
<< "> torus_rank=" << torus_rank
<< ", coords=(" << torus_coords[0] << "," << torus_coords[1] << ")" << endl;
enum DIRECTIONS {DOWN, UP, LEFT, RIGHT};
const char* neighbours_names[4] = {"DOWN", "UP", "LEFT", "RIGHT"};
int neighbours_ranks[4];
MPI_Cart_shift(new_comm, 0, 1, &neighbours_ranks[UP], &neighbours_ranks[DOWN]);
MPI_Cart_shift(new_comm, 1, 1, &neighbours_ranks[LEFT], &neighbours_ranks[RIGHT]);
int my_rank;
MPI_Comm_rank(new_comm, &my_rank);
for(int i=0; i<4; i++)
{
cout << "MPI rank=" << my_rank << " I have a " << neighbours_names[i]
<< " neighbour rank=" << neighbours_ranks[i] << endl;
}
// MPI finalize and exit
MPI_Finalize();
return 0;
}