-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmpi_shared_window.cpp
More file actions
94 lines (76 loc) · 2.3 KB
/
mpi_shared_window.cpp
File metadata and controls
94 lines (76 loc) · 2.3 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*
compile using :
mpic++ mpi_shared_window
and run using:
mpirun a.out
*/
#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>
void print(int t[],int s) {
for (int i = 0; i < s; i++) {
printf("%d ",t[i]);
}
printf("\n");
}
int main(int argc, char **argv) {
int size, rank, nodesize, noderank;
int *table;
MPI_Comm nodecomm;
MPI_Aint winsize;
int windisp;
int *winprintr;
MPI_Win wintable;
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &size);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
/* Create node-local communicator */
MPI_Comm_split_type(MPI_COMM_WORLD, MPI_COMM_TYPE_SHARED, rank, MPI_INFO_NULL, &nodecomm);
MPI_Comm_size(nodecomm, &nodesize);
MPI_Comm_rank(nodecomm, &noderank);
/*
--------------------------------------------------
important part
--------------------------------------------------
*/
/*
We first allocate a shared window using MPI_Win_allocate_shared
Only rank 0 on a node actually allocates memory
We then get the actual shared table pointer using MPI_Win_shared_query
*/
MPI_Win_allocate_shared(noderank == 0 ? nodesize*sizeof(int) : 0,
sizeof(int), MPI_INFO_NULL, nodecomm, &table, &wintable);
if (noderank != 0) MPI_Win_shared_query(wintable, 0, &winsize, &windisp, &table);
/*
--------------------------------------------------
important part
--------------------------------------------------
*/
printf("Rank %d of %d, in-node: rank %d of %d\n", rank, size, noderank, nodesize);
// Initialise table on rank 0 with appropriate synchronisation
if (noderank == 0)
for (int i = 0; i < nodesize; i++)
table[i] = -1;
MPI_Win_fence(0, wintable);
// print it
printf("(first) rank %d, noderank %d, ", rank, noderank); print(table, nodesize);
// modify the table from each node
MPI_Win_fence(0, wintable);
table[noderank] = noderank;
MPI_Win_fence(0, wintable);
// print it
printf("(second) rank %d, noderank %d, ", rank, noderank); print(table, nodesize);
/*
--------------------------------------------------
important part
--------------------------------------------------
*/
/* free the memory */
MPI_Win_free(&wintable);
/*
--------------------------------------------------
important part
--------------------------------------------------
*/
MPI_Finalize();
}