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
24#include "core/cuda/init.hpp"
25#include "core/cuda/utils.hpp"
26
27#if defined(CUDA) && defined(WALBERLA)
29#endif
30
31#include <string>
32#include <unordered_map>
33#include <utility>
34#include <vector>
35
36namespace ScriptInterface {
37namespace System {
38
41#ifdef CUDA
42 {"device",
43 [this](Variant const &v) {
44 if (context()->is_head_node()) {
45 cuda_set_device(get_value<int>(v));
46 }
47 },
48 [this]() {
49 return (context()->is_head_node()) ? cuda_get_device() : 0;
50 }},
51#endif // CUDA
52 });
53}
54
56 VariantMap const &parameters) {
57 if (name == "list_devices") {
58 std::unordered_map<int, std::string> devices{};
59#ifdef CUDA
60 if (context()->is_head_node()) {
61 // only GPUs on the head node can be used
62 auto n_gpus = 0;
63 invoke_skip_cuda_exceptions([&n_gpus]() { n_gpus = cuda_get_n_gpus(); });
64 for (int i = 0; i < n_gpus; ++i) {
65 invoke_skip_cuda_exceptions([&devices, i]() {
66 char gpu_name_buffer[256] = {'\0'};
67 cuda_get_gpu_name(i, gpu_name_buffer);
68 devices[i] = std::string{gpu_name_buffer};
69 });
70 }
71 }
72#endif // CUDA
73 return make_unordered_map_of_variants(devices);
74 }
75 if (name == "list_devices_properties") {
76 std::unordered_map<std::string, std::unordered_map<int, Variant>> dict{};
77#ifdef CUDA
78 std::vector<EspressoGpuDevice> devices = cuda_gather_gpus();
79 for (auto const &dev : devices) {
80 auto const hostname = std::string{dev.proc_name};
81 if (dict.count(hostname) == 0) {
82 dict[hostname] = {};
83 }
84 std::unordered_map<std::string, Variant> dev_properties = {
85 {"name", std::string{dev.name}},
86 {"compute_capability",
87 Variant{std::vector<int>{
88 {dev.compute_capability_major, dev.compute_capability_minor}}}},
89 {"cores", dev.n_cores},
90 {"total_memory", dev.total_memory},
91 };
92 dict[hostname][dev.id] = std::move(dev_properties);
93 }
94#endif // CUDA
96 }
97 if (name == "get_n_gpus") {
98 auto n_gpus = 0;
99#ifdef CUDA
100 if (context()->is_head_node()) {
101 // only GPUs on the head node can be used
102 invoke_skip_cuda_exceptions([&n_gpus]() { n_gpus = cuda_get_n_gpus(); });
103 }
104#endif // CUDA
105 return n_gpus;
106 }
107#if defined(CUDA) && defined(WALBERLA)
108 if (name == "set_device_id_per_rank") {
109 if (cuda_get_n_gpus()) {
111 }
112 return {};
113 }
114#endif
115 return {};
116}
117
118} // namespace System
119} // namespace ScriptInterface
void add_parameters(std::vector< AutoParameter > &&params)
virtual bool is_head_node() const =0
boost::string_ref name() const
Context * context() const
Responsible context.
Variant do_call_method(std::string const &name, VariantMap const &parameters) override
This file contains the defaults for ESPResSo.
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 on all nodes on the head node.
Definition init.cpp:52
int cuda_get_device()
Get the current CUDA device.
Definition init_cuda.cu:94
void cuda_set_device(int dev)
Choose a device for future CUDA computations.
Definition init_cuda.cu:88
void cuda_get_gpu_name(int dev, char *name)
Get the name of a CUDA device.
Definition init_cuda.cu:67
int cuda_get_n_gpus()
Get the number of CUDA devices.
Definition init_cuda.cu:43
void set_device_id_per_rank()
std::unordered_map< std::string, Variant > VariantMap
Definition Variant.hpp:69
auto make_unordered_map_of_variants(std::unordered_map< K, V > const &v)
Definition Variant.hpp:80
boost::make_recursive_variant< None, bool, int, std::size_t, double, std::string, ObjectRef, Utils::Vector3b, Utils::Vector3i, Utils::Vector2d, Utils::Vector3d, Utils::Vector4d, std::vector< int >, std::vector< double >, std::vector< boost::recursive_variant_ >, std::unordered_map< int, boost::recursive_variant_ >, std::unordered_map< std::string, boost::recursive_variant_ > >::type Variant
Possible types for parameters.
Definition Variant.hpp:67