This repository was archived by the owner on Jul 4, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmpi_cart.cpp
More file actions
62 lines (51 loc) · 2.2 KB
/
mpi_cart.cpp
File metadata and controls
62 lines (51 loc) · 2.2 KB
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
#include <mpi.h>
#include <stdio.h>
#include <stdlib.h>
#define SIZE 16
#define UP 0
#define DOWN 1
#define LEFT 2
#define RIGHT 3
int main(int argc, char *argv[]) {
int numtasks, rank, source, dest, outbuf, i, tag=1,
inbuf[4]={MPI_PROC_NULL,MPI_PROC_NULL,MPI_PROC_NULL,MPI_PROC_NULL},
nbrs[4], dims[2]={4,4},
periods[2]={0,0}, reorder=0, coords[2];
MPI_Request reqs[8];
MPI_Status stats[8];
MPI_Comm cartcomm;
MPI_Init(&argc,&argv);
MPI_Comm_size(MPI_COMM_WORLD, &numtasks);
//TODO: make this work for any number of processes, not just 16:
// look up and use MPI_Dims_create
if (numtasks == SIZE) {
//Create cartesian communicator for 2D, dims[0]*dims[1] processes,
//without periodicity and reordering
MPI_Cart_create(MPI_COMM_WORLD, 2, dims, periods, reorder, &cartcomm);
//Get my rank in the new communicator
MPI_Comm_rank(cartcomm, &rank);
//Get my coordinates coords[0] and coords[1]
MPI_Cart_coords(cartcomm, rank, 2, coords);
//Get my neighbours in dimension 0
MPI_Cart_shift(cartcomm, 0, 1, &nbrs[LEFT], &nbrs[RIGHT]);
//Get my neighbours in dirmension 1
MPI_Cart_shift(cartcomm, 1, 1, &nbrs[UP], &nbrs[DOWN]);
printf("rank= %d coords= %d %d neighbors(u,d,l,r)= %d %d %d %d\n",
rank,coords[0],coords[1],nbrs[UP],nbrs[DOWN],nbrs[LEFT],
nbrs[RIGHT]);
outbuf = rank;
//Send my rank to all four neighbours, and receive message from them
MPI_Sendrecv(&rank, 1, MPI_INT, nbrs[LEFT], 421,
&inbuf[RIGHT], 1, MPI_INT, nbrs[RIGHT], 421, cartcomm, MPI_STATUS_IGNORE);
MPI_Sendrecv(&rank, 1, MPI_INT, nbrs[RIGHT], 422,
&inbuf[LEFT], 1, MPI_INT, nbrs[LEFT], 422, cartcomm, MPI_STATUS_IGNORE);
MPI_Sendrecv(&rank, 1, MPI_INT, nbrs[UP], 423,
&inbuf[DOWN], 1, MPI_INT, nbrs[DOWN], 423, cartcomm, MPI_STATUS_IGNORE);
MPI_Sendrecv(&rank, 1, MPI_INT, nbrs[DOWN], 424,
&inbuf[UP], 1, MPI_INT, nbrs[UP], 424, cartcomm, MPI_STATUS_IGNORE);
printf("rank= %d inbuf(u,d,l,r)= %d %d %d %d\n",
rank,inbuf[UP],inbuf[DOWN],inbuf[LEFT],inbuf[RIGHT]); }
else
printf("Must specify %d processors. Terminating.\n",SIZE);
MPI_Finalize();
}