Grid 0.7.0
Accelerator.cc
Go to the documentation of this file.
1#include <Grid/GridCore.h>
2
4int world_rank; // Use to control world rank for print guarding
7uint32_t acceleratorThreads(void) {return accelerator_threads;};
8void acceleratorThreads(uint32_t t) {accelerator_threads = t;};
9
10#define ENV_LOCAL_RANK_PALS "PALS_LOCAL_RANKID"
11#define ENV_RANK_PALS "PALS_RANKID"
12#define ENV_LOCAL_RANK_OMPI "OMPI_COMM_WORLD_LOCAL_RANK"
13#define ENV_RANK_OMPI "OMPI_COMM_WORLD_RANK"
14#define ENV_LOCAL_RANK_SLURM "SLURM_LOCALID"
15#define ENV_RANK_SLURM "SLURM_PROCID"
16#define ENV_LOCAL_RANK_MVAPICH "MV2_COMM_WORLD_LOCAL_RANK"
17#define ENV_RANK_MVAPICH "MV2_COMM_WORLD_RANK"
18
19#ifdef GRID_CUDA
20cudaDeviceProp *gpu_props;
21cudaStream_t copyStream;
22cudaStream_t computeStream;
23void acceleratorInit(void)
24{
25 int nDevices = 1;
26 cudaGetDeviceCount(&nDevices);
27 gpu_props = new cudaDeviceProp[nDevices];
28
29 char * localRankStr = NULL;
30 int rank = 0;
31 world_rank=0;
32 if ((localRankStr = getenv(ENV_RANK_OMPI )) != NULL) { world_rank = atoi(localRankStr);}
33 if ((localRankStr = getenv(ENV_RANK_MVAPICH)) != NULL) { world_rank = atoi(localRankStr);}
34 if ((localRankStr = getenv(ENV_RANK_SLURM )) != NULL) {
35 int slurm_rank = atoi(localRankStr);
36 if (world_rank != 0 && world_rank != slurm_rank) {
37 std::cout << "Both SLURM and OMPI ranks detected and differ - " << slurm_rank << " != " << world_rank << "! Preferring the OMPI rank." << std::endl;
38 } else {
39 world_rank = slurm_rank;
40 }
41 }
42 // We extract the local rank initialization using an environment variable
43 if ((localRankStr = getenv(ENV_LOCAL_RANK_OMPI)) != NULL) {
44 if (!world_rank)
45 std::cout << "OPENMPI detected" << std::endl;
46 rank = atoi(localRankStr);
47 } else if ((localRankStr = getenv(ENV_LOCAL_RANK_MVAPICH)) != NULL) {
48 if (!world_rank)
49 std::cout << "MVAPICH detected" << std::endl;
50 rank = atoi(localRankStr);
51 } else if ((localRankStr = getenv(ENV_LOCAL_RANK_SLURM)) != NULL) {
52 if (!world_rank)
53 std::cout << "SLURM detected" << std::endl;
54 rank = atoi(localRankStr);
55 } else {
56 if (!world_rank)
57 std::cout << "MPI version is unknown - bad things may happen" << std::endl;
58 }
59
60 size_t totalDeviceMem=0;
61 for (int i = 0; i < nDevices; i++) {
62
63#define GPU_PROP_FMT(__name__,__value__) std::cout << "AcceleratorCudaInit[" << rank << "]: " #__name__ ": " << __value__ << std::endl;
64#define GPU_PROP(__name__) GPU_PROP_FMT(__name__, prop.__name__);
65 cudaGetDeviceProperties(&gpu_props[i], i);
66 cudaDeviceProp prop;
67 prop = gpu_props[i];
68 totalDeviceMem = prop.totalGlobalMem;
69 if ( world_rank == 0) {
70 if ( i==rank ) {
71 std::cout << "AcceleratorCudaInit[" << rank << "]: ========================" << std::endl;
72 GPU_PROP_FMT(Device Number, i);
73 std::cout << "AcceleratorCudaInit[" << rank << "]: ========================" << std::endl;
74 GPU_PROP_FMT(Device identifier, prop.name);
75
76 GPU_PROP(totalGlobalMem);
77 GPU_PROP(managedMemory);
78 GPU_PROP(isMultiGpuBoard);
79 GPU_PROP(warpSize);
80 GPU_PROP(pciBusID);
81 GPU_PROP(pciDeviceID);
82 std::cout << "AcceleratorCudaInit[" << rank << "]: maxGridSize (" << prop.maxGridSize[0] << "," << prop.maxGridSize[1] << "," << prop.maxGridSize[2] << ")" << std::endl;
83 }
84 // GPU_PROP(unifiedAddressing);
85 // GPU_PROP(l2CacheSize);
86 // GPU_PROP(singleToDoublePrecisionPerfRatio);
87 }
88 }
89
90 MemoryManager::DeviceMaxBytes = (8*totalDeviceMem)/10; // Assume 80% ours
91#undef GPU_PROP_FMT
92#undef GPU_PROP
93
94#ifdef GRID_DEFAULT_GPU
95 int device = 0;
96 // IBM Jsrun makes cuda Device numbering screwy and not match rank
97 if ( world_rank == 0 ) {
98 std::cout << "AcceleratorCudaInit: using default device" << std::endl;
99 std::cout << "AcceleratorCudaInit: assume user either uses" << std::endl;
100 std::cout << "AcceleratorCudaInit: a) IBM jsrun, or " << std::endl;
101 std::cout << "AcceleratorCudaInit: b) invokes through a wrapping script to set CUDA_VISIBLE_DEVICES, UCX_NET_DEVICES, and numa binding " << std::endl;
102 std::cout << "AcceleratorCudaInit: Configure options --enable-setdevice=no " << std::endl;
103 }
104#else
105 int device = rank;
106 std::cout << "AcceleratorCudaInit: rank " << world_rank << " setting device to node rank " << rank << std::endl;
107 std::cout << "AcceleratorCudaInit: Configure options --enable-setdevice=yes " << std::endl;
108#endif
109
110 cudaSetDevice(device);
111 cudaStreamCreate(&copyStream);
112 cudaStreamCreate(&computeStream);
113 const int len=64;
114 char busid[len];
115 if( rank == world_rank ) {
116 cudaDeviceGetPCIBusId(busid, len, device);
117 std::cout << "local rank " << rank << " device " << device << " bus id: " << busid << std::endl;
118 }
119
120 if ( world_rank == 0 ) std::cout << "AcceleratorCudaInit: ================================================" << std::endl;
121}
122#endif
123
124#ifdef GRID_HIP
125hipDeviceProp_t *gpu_props;
126hipStream_t copyStream;
127hipStream_t computeStream;
128void acceleratorInit(void)
129{
130 int nDevices = 1;
131 auto discard = hipGetDeviceCount(&nDevices);
132 gpu_props = new hipDeviceProp_t[nDevices];
133
134 char * localRankStr = NULL;
135 int rank = 0;
136 world_rank=0;
137 // We extract the local rank initialization using an environment variable
138 if ((localRankStr = getenv(ENV_LOCAL_RANK_OMPI)) != NULL)
139 {
140 rank = atoi(localRankStr);
141 }
142 if ((localRankStr = getenv(ENV_LOCAL_RANK_MVAPICH)) != NULL)
143 {
144 rank = atoi(localRankStr);
145 }
146 if ((localRankStr = getenv(ENV_RANK_OMPI )) != NULL) { world_rank = atoi(localRankStr);}
147 if ((localRankStr = getenv(ENV_RANK_MVAPICH)) != NULL) { world_rank = atoi(localRankStr);}
148 if ((localRankStr = getenv(ENV_RANK_SLURM )) != NULL) { world_rank = atoi(localRankStr);}
149
150 if ( world_rank == 0 )
151 printf("world_rank %d has %d devices\n",world_rank,nDevices);
152 size_t totalDeviceMem=0;
153 for (int i = 0; i < nDevices; i++) {
154
155#define GPU_PROP_FMT(__name__, __value__) std::cout << "AcceleratorHipInit: " #__name__ ": " << __value__ << std::endl;
156#define GPU_PROP(__name__) GPU_PROP_FMT(__name__, prop.__name__);
157
158 discard = hipGetDeviceProperties(&gpu_props[i], i);
159 hipDeviceProp_t prop;
160 prop = gpu_props[i];
161 totalDeviceMem = prop.totalGlobalMem;
162 if ( world_rank == 0) {
163 std::cout << "AcceleratorHipInit[" << rank << ": ========================" << std::endl;
164 GPU_PROP_FMT(Device Number, i);
165 std::cout << "AcceleratorHipInit[" << rank << ": ========================" << std::endl;
166 GPU_PROP_FMT(Device identifier, prop.name);
167
168 GPU_PROP(totalGlobalMem);
169 // GPU_PROP(managedMemory);
170 GPU_PROP(isMultiGpuBoard);
171 GPU_PROP(warpSize);
172 // GPU_PROP(unifiedAddressing);
173 // GPU_PROP(l2CacheSize);
174 // GPU_PROP(singleToDoublePrecisionPerfRatio);
175 }
176 }
177 MemoryManager::DeviceMaxBytes = (8*totalDeviceMem)/10; // Assume 80% ours
178#undef GPU_PROP_FMT
179#undef GPU_PROP
180
181#ifdef GRID_DEFAULT_GPU
182 if ( world_rank == 0 ) {
183 std::cout << "AcceleratorHipInit: using default device " << std::endl;
184 std::cout << "AcceleratorHipInit: assume user or srun sets ROCR_VISIBLE_DEVICES and numa binding " << std::endl;
185 std::cout << "AcceleratorHipInit: Configure options --enable-setdevice=no " << std::endl;
186 }
187 int device = 0;
188#else
189 if ( world_rank == 0 ) {
190 std::cout << "AcceleratorHipInit: rank " << world_rank << " setting device to node rank " << rank << std::endl;
191 std::cout << "AcceleratorHipInit: Configure options --enable-setdevice=yes " << std::endl;
192 }
193 int device = rank;
194#endif
195 discard = hipSetDevice(device);
196 discard = hipStreamCreate(&copyStream);
197 discard = hipStreamCreate(&computeStream);
198 const int len=64;
199 char busid[len];
200 if( rank == world_rank ) {
201 discard = hipDeviceGetPCIBusId(busid, len, device);
202 std::cout << "local rank " << rank << " device " << device << " bus id: " << busid << std::endl;
203 }
204 if ( world_rank == 0 ) std::cout << "AcceleratorHipInit: ================================================" << std::endl;
205}
206#endif
207
208
209#ifdef GRID_SYCL
210
211sycl::queue *theGridAccelerator;
212sycl::queue *theCopyAccelerator;
213void acceleratorInit(void)
214{
215 int nDevices = 1;
216 // sycl::gpu_selector selector;
217 // sycl::device selectedDevice { selector };
218 theGridAccelerator = new sycl::queue (sycl::gpu_selector_v);
219 theCopyAccelerator = new sycl::queue (sycl::gpu_selector_v);
220 // theCopyAccelerator = theGridAccelerator; // Should proceed concurrenlty anyway.
221
222#ifdef GRID_SYCL_LEVEL_ZERO_IPC
223 zeInit(0);
224#endif
225
226 char * localRankStr = NULL;
227 int rank = 0;
228 world_rank=0;
229
230 // We extract the local rank initialization using an environment variable
231 if ((localRankStr = getenv(ENV_LOCAL_RANK_OMPI)) != NULL)
232 {
233 rank = atoi(localRankStr);
234 }
235 if ((localRankStr = getenv(ENV_LOCAL_RANK_MVAPICH)) != NULL)
236 {
237 rank = atoi(localRankStr);
238 }
239 if ((localRankStr = getenv(ENV_LOCAL_RANK_PALS)) != NULL)
240 {
241 rank = atoi(localRankStr);
242 }
243 if ((localRankStr = getenv(ENV_RANK_OMPI )) != NULL) { world_rank = atoi(localRankStr);}
244 if ((localRankStr = getenv(ENV_RANK_MVAPICH)) != NULL) { world_rank = atoi(localRankStr);}
245 if ((localRankStr = getenv(ENV_RANK_PALS )) != NULL) { world_rank = atoi(localRankStr);}
246
247 char hostname[HOST_NAME_MAX+1];
248 gethostname(hostname, HOST_NAME_MAX+1);
249 if ( rank==0 ) std::cout << "AcceleratorSyclInit world_rank " << world_rank << " is host " << hostname << std::endl;
250
251 auto devices = sycl::device::get_devices();
252 for(int d = 0;d<devices.size();d++){
253
254#define GPU_PROP(__prop__) \
255 std::cout << "AcceleratorSyclInit: " #__prop__ ": " << devices[d].get_info<sycl::info::device::__prop__>() << std::endl;
256
257 if ( world_rank == 0) {
258
259 GPU_PROP(vendor);
260 GPU_PROP(version);
261 // GPU_PROP_STR(device_type);
262 /*
263 GPU_PROP(max_compute_units);
264 GPU_PROP(native_vector_width_char);
265 GPU_PROP(native_vector_width_short);
266 GPU_PROP(native_vector_width_int);
267 GPU_PROP(native_vector_width_long);
268 GPU_PROP(native_vector_width_float);
269 GPU_PROP(native_vector_width_double);
270 GPU_PROP(native_vector_width_half);
271 GPU_PROP(address_bits);
272 GPU_PROP(half_fp_config);
273 GPU_PROP(single_fp_config);
274 */
275 // GPU_PROP(double_fp_config);
276 GPU_PROP(global_mem_size);
277 }
278
279 }
280 if ( world_rank == 0 ) {
281 auto name = theGridAccelerator->get_device().get_info<sycl::info::device::name>();
282 std::cout << "AcceleratorSyclInit: Selected device is " << name << std::endl;
283 std::cout << "AcceleratorSyclInit: ================================================" << std::endl;
284 }
285}
286#endif
287
288#if (!defined(GRID_CUDA)) && (!defined(GRID_SYCL))&& (!defined(GRID_HIP))
289void acceleratorInit(void){}
290#endif
291
int world_rank
Definition Accelerator.cc:4
void acceleratorInit(void)
#define ENV_LOCAL_RANK_MVAPICH
uint32_t accelerator_threads
Definition Accelerator.cc:6
#define ENV_RANK_PALS
#define ENV_RANK_OMPI
uint32_t acceleratorThreads(void)
Definition Accelerator.cc:7
#define ENV_RANK_SLURM
#define ENV_LOCAL_RANK_PALS
#define ENV_RANK_MVAPICH
#define ENV_LOCAL_RANK_OMPI
int acceleratorAbortOnGpuError
Definition Accelerator.cc:5
#define ENV_LOCAL_RANK_SLURM
char hostname[HOST_NAME_MAX+1]
Definition Init.cc:101
#define HOST_NAME_MAX
Definition Init.cc:85
#define NAMESPACE_BEGIN(A)
Definition Namespace.h:35
#define NAMESPACE_END(A)
Definition Namespace.h:36
static uint64_t DeviceMaxBytes