ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
init_cuda.cu
Go to the documentation of this file.
1/*
2 * Copyright (C) 2010-2022 The ESPResSo project
3 *
4 * This file is part of ESPResSo.
5 *
6 * ESPResSo is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * ESPResSo is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include "init.hpp"
21#include "utils.cuh"
22
23#include <cuda.h>
24#include <cuda_runtime.h>
25
26#include <cstring>
27#include <memory>
28#include <string>
29
30#if defined(OMPI_MPI_H) || defined(_MPI_H)
31#error CU-file includes mpi.h! This should not happen!
32#endif
33
34#ifdef ESPRESSO_CUDA
35
36/** \name minimally required compute capability. */
37/**@{*/
38static int constexpr computeCapabilityMinMajor = 3;
39static int constexpr computeCapabilityMinMinor = 0;
40/**@}*/
41
42void cuda_init() { CUDA_CHECK(cudaStreamCreate(&stream[0])) }
43
45 int deviceCount;
46 CUDA_CHECK(cudaGetDeviceCount(&deviceCount))
47 return deviceCount;
48}
49
51 cudaDeviceProp deviceProp;
52 CUDA_CHECK(cudaGetDeviceProperties(&deviceProp, dev))
53 return (deviceProp.major < computeCapabilityMinMajor or
54 (deviceProp.major == computeCapabilityMinMajor and
55 deviceProp.minor < computeCapabilityMinMinor));
56}
57
58std::string cuda_get_gpu_name(int dev) {
59 cudaDeviceProp deviceProp;
60 CUDA_CHECK(cudaGetDeviceProperties(&deviceProp, dev))
61 return {deviceProp.name};
62}
63
65 cudaDeviceProp deviceProp;
66 CUDA_CHECK(cudaGetDeviceProperties(&deviceProp, dev))
67 auto const [node, hostname] = detail::get_node_info();
68 return {dev,
69 deviceProp.name,
70 hostname,
71 node,
72 deviceProp.major,
73 deviceProp.minor,
74 deviceProp.totalGlobalMem,
75 deviceProp.multiProcessorCount};
76}
77
78void cuda_set_device(int dev) {
79 CUDA_CHECK(cudaSetDevice(dev))
80 CUDA_CHECK(cudaStreamDestroy(stream[0]))
81 CUDA_CHECK(cudaStreamCreate(&stream[0]))
82}
83
85 int dev;
86 CUDA_CHECK(cudaGetDevice(&dev))
87 return dev;
88}
89
91 auto const deleter = [](int *p) { cudaFree(reinterpret_cast<void *>(p)); };
92 int *ptr = nullptr;
93 int h = 42;
94 CUDA_CHECK(cudaMalloc(reinterpret_cast<void **>(&ptr), sizeof(int)));
95 std::unique_ptr<int, decltype(deleter)> d(ptr, deleter);
96 CUDA_CHECK(cudaMemcpy(d.get(), &h, sizeof(int), cudaMemcpyHostToDevice));
97 h = 0;
98 CUDA_CHECK(cudaMemcpy(&h, d.get(), sizeof(int), cudaMemcpyDeviceToHost));
99 return h != 42;
100}
101
103 if (cuda_get_n_gpus() == 0) {
104 throw cuda_runtime_error("No GPU was found.");
105 }
106 auto const devID = cuda_get_device();
107 auto const incompatible = cuda_check_gpu_compute_capability(devID);
108 auto const communication_failure = cuda_test_device_access();
109 if (incompatible or communication_failure) {
110 throw cuda_runtime_error("CUDA device " + std::to_string(devID) +
111 " is not capable of running ESPResSo.");
112 }
113}
114
115#endif /* defined(ESPRESSO_CUDA) */
Wrapper for CUDA runtime exceptions.
cudaStream_t stream[1]
CUDA streams for parallel computing on CPU and GPU.
EspressoGpuDevice cuda_get_device_props(const int dev)
Get properties of a CUDA device.
Definition init_cuda.cu:64
void cuda_check_device()
Check that a device is available, that its compute capability is sufficient for ESPResSo,...
Definition init_cuda.cu:102
bool cuda_check_gpu_compute_capability(int dev)
Check that a given GPU has compute capability.
Definition init_cuda.cu:50
static int constexpr computeCapabilityMinMajor
Definition init_cuda.cu:38
bool cuda_test_device_access()
Test if communication to the CUDA device works.
Definition init_cuda.cu:90
std::string cuda_get_gpu_name(int dev)
Get the name of a CUDA device.
Definition init_cuda.cu:58
int cuda_get_device()
Get the current CUDA device.
Definition init_cuda.cu:84
static int constexpr computeCapabilityMinMinor
Definition init_cuda.cu:39
void cuda_set_device(int dev)
Choose a device for future CUDA computations.
Definition init_cuda.cu:78
int cuda_get_n_gpus()
Get the number of CUDA devices on the local host.
Definition init_cuda.cu:44
void cuda_init()
Initializes the CUDA stream.
Definition init_cuda.cu:42
Struct to hold information relevant to ESPResSo about GPUs.
Definition cuda/init.hpp:39
#define CUDA_CHECK(statement)
Convert CUDA error codes into runtime errors.
Definition utils.cuh:46