QMCPACK
CUDADeviceManager.cpp
Go to the documentation of this file.
1 //////////////////////////////////////////////////////////////////////////////////////
2 // This file is distributed under the University of Illinois/NCSA Open Source License.
3 // See LICENSE file in top directory for details.
4 //
5 // Copyright (c) 2023 QMCPACK developers.
6 //
7 // File developed by: Ye Luo, yeluo@anl.gov, Argonne National Laboratory
8 //
9 // File created by: Ye Luo, yeluo@anl.gov, Argonne National Laboratory
10 //
11 //////////////////////////////////////////////////////////////////////////////////////
12 
13 
14 #include "CUDADeviceManager.h"
15 #include <stdexcept>
16 #include "OutputManager.h"
17 #include "CUDAruntime.hpp" // Positioned here to avoid conflict between CUDA and GCC >= 12 headers. https://github.com/QMCPACK/qmcpack/pull/4814
19 
20 namespace qmcplusplus
21 {
22 CUDADeviceManager::CUDADeviceManager(int& default_device_num, int& num_devices, int local_rank, int local_size)
23  : cuda_default_device_num(-1), cuda_device_count(0)
24 {
25  cudaErrorCheck(cudaGetDeviceCount(&cuda_device_count), "cudaGetDeviceCount failed!");
26  if (num_devices == 0)
27  num_devices = cuda_device_count;
28  else if (num_devices != cuda_device_count)
29  throw std::runtime_error("Inconsistent number of CUDA devices with the previous record!");
30  if (cuda_device_count > local_size)
31  app_warning() << "More CUDA devices than the number of MPI ranks. "
32  << "Some devices will be left idle.\n"
33  << "There is potential performance issue with the GPU affinity. "
34  << "Use CUDA_VISIBLE_DEVICE or MPI launcher to expose desired devices.\n";
35  if (num_devices > 0)
36  {
38  if (default_device_num < 0)
39  default_device_num = cuda_default_device_num;
40  else if (default_device_num != cuda_default_device_num)
41  throw std::runtime_error("Inconsistent assigned CUDA devices with the previous record!");
42 
43 #pragma omp parallel
44  {
45  cudaErrorCheck(cudaSetDevice(cuda_default_device_num), "cudaSetDevice failed!");
46  cudaErrorCheck(cudaFree(0), "cudaFree failed!");
47  }
48  }
49 }
50 } // namespace qmcplusplus
std::ostream & app_warning()
Definition: OutputManager.h:69
helper functions for EinsplineSetBuilder
Definition: Configuration.h:43
CUDADeviceManager(int &default_device_num, int &num_devices, int local_rank, int local_size)
Declaration of OutputManager class.
handle CUDA/HIP runtime selection.
#define cudaSetDevice
Definition: cuda2hip.h:148
cudaErrorCheck(cudaMemcpyAsync(dev_lu.data(), lu.data(), sizeof(decltype(lu)::value_type) *lu.size(), cudaMemcpyHostToDevice, hstream), "cudaMemcpyAsync failed copying log_values to device")
#define cudaFree
Definition: cuda2hip.h:99
#define cudaGetDeviceCount
Definition: cuda2hip.h:102
int determineDefaultDeviceNum(int num_devices, int rank_id, int num_ranks)
distribute MPI ranks among devices