-
Notifications
You must be signed in to change notification settings - Fork 197
Expand file tree
/
Copy pathcomm_config.cpp
More file actions
187 lines (132 loc) · 4.18 KB
/
Copy pathcomm_config.cpp
File metadata and controls
187 lines (132 loc) · 4.18 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/** @file
* Functions for querying the distributed configuration
* using the MPI interface, agnostically to the specific
* implementation (like OpenMPI vs MPICH). These functions
* are callable even when MPI has not been compiled/linked.
*
* Note that even when COMPILE_MPI=1, the user may have
* disabled distribution when creating the QuEST environment
* at runtime. Ergo we use comm_isInit() to determine whether
* functions should invoke the MPI API.
*
* @author Tyson Jones
*/
#include "quest/include/config.h"
#include "quest/include/types.h"
#include "quest/src/comm/comm_config.hpp"
#include "quest/src/core/errors.hpp"
#if COMPILE_MPI
#include <mpi.h>
#endif
/*
* WARN ABOUT CUDA-AWARENESS
*/
#if COMPILE_MPI && COMPILE_CUDA
// this check is OpenMPI specific
#ifdef OPEN_MPI
#include <mpi-ext.h>
// #warning command is always recognised (OpenMPI is not Windows compatible)
#ifndef MPIX_CUDA_AWARE_SUPPORT
#warning "Could not ascertain whether MPI is CUDA-aware, so we will assume it is not. This means inter-GPU communication will be slowly routed through the CPU/RAM."
#elif !MPIX_CUDA_AWARE_SUPPORT
#warning "MPI compiler is not CUDA-aware, so inter-GPU communication will be slowly routed through the CPU/RAM"
#endif
#endif
/// @todo check whether MPICH is CUDA-aware
/// beware MSVC cannot parse #warning, and
/// Intel MPI would crash (but not MSMPI?)
#endif
/*
* MPI ENVIRONMENT MANAGEMENT
* all of which is safely callable in non-distributed mode
*/
bool comm_isMpiCompiled() {
return (bool) COMPILE_MPI;
}
bool comm_isMpiGpuAware() {
/// @todo these checks may be OpenMPI specific, so that
/// non-OpenMPI MPI compilers are always dismissed as
/// not being CUDA-aware. Check e.g. MPICH method!
// definitely not GPU-aware if compiler declares it is not
#if defined(MPIX_CUDA_AWARE_SUPPORT) && ! MPIX_CUDA_AWARE_SUPPORT
return false;
#endif
// check CUDA-awareness at run-time if we know it's principally supported
#if defined(MPIX_CUDA_AWARE_SUPPORT)
return (bool) MPIX_Query_cuda_support();
#endif
// if we can't ascertain CUDA-awareness, just assume no to avoid seg-fault
return false;
}
bool comm_isInit() {
#if COMPILE_MPI
// safely callable before MPI initialisation, but NOT after comm_end()
int isInit;
MPI_Initialized(&isInit);
return (bool) isInit;
#else
// obviously MPI is never initialised if not even compiled
return false;
#endif
}
void comm_init() {
#if COMPILE_MPI
// error if attempting re-initialisation
if (comm_isInit())
error_commAlreadyInit();
MPI_Init(NULL, NULL);
#endif
}
void comm_end() {
#if COMPILE_MPI
// gracefully permit comm_end() before comm_init(), as input validation can trigger
if (!comm_isInit())
return;
MPI_Barrier(MPI_COMM_WORLD);
MPI_Finalize();
#endif
}
int comm_getRank() {
#if COMPILE_MPI
// if distribution was not runtime enabled (or a validation error was
// triggered), every node (if many MPI processes were launched)
// believes it is the root rank
if (!comm_isInit())
return ROOT_RANK;
int rank;
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
return rank;
#else
// if MPI isn't compiled, we're definitely non-distributed; return main rank
return ROOT_RANK;
#endif
}
bool comm_isRootNode(int rank) {
return rank == ROOT_RANK;
}
bool comm_isRootNode() {
return comm_isRootNode(comm_getRank());
}
int comm_getNumNodes() {
#if COMPILE_MPI
// if distribution was not runtime enabled (or a validation error was
// triggered), every node (if many MPI processes were launched)
// believes it is the one and only node
if (!comm_isInit())
return 1;
int numNodes;
MPI_Comm_size(MPI_COMM_WORLD, &numNodes);
return numNodes;
#else
// if MPI isn't compiled, we're definitely non-distributed; return single node
return 1;
#endif
}
void comm_sync() {
#if COMPILE_MPI
// gracefully handle when not distributed, needed by e.g. pre-MPI-setup validation
if (!comm_isInit())
return;
MPI_Barrier(MPI_COMM_WORLD);
#endif
}