ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
CudaInitHandle.cpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2013-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 "CudaInitHandle.hpp"
21
22#include <config/config.hpp>
23
25#include "core/cuda/init.hpp"
26#include "core/cuda/utils.hpp"
27
28#if defined(ESPRESSO_CUDA) && defined(ESPRESSO_WALBERLA)
30#endif
31
32#include <string>
33#include <unordered_map>
34#include <utility>
35#include <vector>
36
37namespace ScriptInterface {
38namespace System {
39
42#ifdef ESPRESSO_CUDA
43 {"device",
44 [this](Variant const &v) {
45 if (context()->is_head_node()) {
47 }
48 },
49 [this]() {
50 return (context()->is_head_node()) ? cuda_get_device() : 0;
51 }},
52#endif // ESPRESSO_CUDA
53 });
54}
55
57 VariantMap const &) {
58 if (name == "list_devices") {
59 std::unordered_map<int, std::string> devices{};
60#ifdef ESPRESSO_CUDA
61 if (context()->is_head_node()) {
62 // only GPUs on the head node can be displayed
63 auto n_gpus = 0;
65 for (int i = 0; i < n_gpus; ++i) {
67 [&devices, i]() { devices[i] = cuda_get_gpu_name(i); });
68 }
69 }
70#endif // ESPRESSO_CUDA
72 }
73 if (name == "list_devices_properties") {
74 std::unordered_map<std::string, std::unordered_map<int, Variant>> dict{};
75#ifdef ESPRESSO_CUDA
76 std::vector<EspressoGpuDevice> devices = cuda_gather_gpus();
77 for (auto const &dev : devices) {
78 auto const hostname = dev.proc_name;
79 if (not dict.contains(hostname)) {
80 dict[hostname] = {};
81 }
82 std::unordered_map<std::string, Variant> dev_properties = {
83 {"name", dev.name},
84 {"compute_capability",
85 Variant{std::vector<int>{
86 {dev.compute_capability_major, dev.compute_capability_minor}}}},
87 {"cores", dev.n_cores},
88 {"total_memory", dev.total_memory},
89 };
90 dict[hostname][dev.id] = std::move(dev_properties);
91 }
92#endif // ESPRESSO_CUDA
94 }
95 if (name == "get_n_gpus") {
96 auto n_gpus = 0;
97#ifdef ESPRESSO_CUDA
98 auto const devices = cuda_gather_gpus();
99 n_gpus = static_cast<int>(devices.size());
100#endif // ESPRESSO_CUDA
101 return n_gpus;
102 }
103 if (name == "is_mpi_gpu_aware") {
104 return ::communication_environment->is_mpi_gpu_aware();
105 }
106#if defined(ESPRESSO_CUDA) && defined(ESPRESSO_WALBERLA)
107 if (name == "set_device_id_per_rank") {
108 if (cuda_get_n_gpus()) {
110 }
111 return {};
112 }
113#endif
114 return {};
115}
116
117} // namespace System
118} // namespace ScriptInterface
void add_parameters(std::vector< AutoParameter > &&params)
virtual bool is_head_node() const =0
Context * context() const
Responsible context.
std::string_view name() const
Variant do_call_method(std::string const &name, VariantMap const &parameters) override
cudaStream_t stream[1]
CUDA streams for parallel computing on CPU and GPU.
This file contains the asynchronous MPI communication.
void invoke_skip_cuda_exceptions(F &&f, Args &&...args)
Invoke a function and silently ignore any thrown cuda_runtime_error error.
std::vector< EspressoGpuDevice > cuda_gather_gpus()
Gather list of CUDA devices from all nodes on the head node.
Definition cuda/init.cpp:56
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
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 set_device_id_per_rank()
std::unordered_map< std::string, Variant > VariantMap
Definition Variant.hpp:133
auto make_unordered_map_of_variants(std::unordered_map< K, V > const &v)
Definition Variant.hpp:144
Recursive variant implementation.
Definition Variant.hpp:84