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#include <string>
28#include <unordered_map>
29#include <utility>
30#include <vector>
31
32namespace ScriptInterface {
33namespace System {
34
37#ifdef CUDA
38 {"device",
39 [this](Variant const &v) {
40 if (context()->is_head_node()) {
41 cuda_set_device(get_value<int>(v));
42 }
43 },
44 [this]() {
45 return (context()->is_head_node()) ? cuda_get_device() : 0;
46 }},
47#endif // CUDA
48 });
49}
50
52 VariantMap const &parameters) {
53 if (name == "list_devices") {
54 std::unordered_map<int, std::string> devices{};
55#ifdef CUDA
56 if (context()->is_head_node()) {
57 // only GPUs on the head node can be used
58 auto n_gpus = 0;
59 invoke_skip_cuda_exceptions([&n_gpus]() { n_gpus = cuda_get_n_gpus(); });
60 for (int i = 0; i < n_gpus; ++i) {
61 invoke_skip_cuda_exceptions([&devices, i]() {
62 char gpu_name_buffer[256] = {'\0'};
63 cuda_get_gpu_name(i, gpu_name_buffer);
64 devices[i] = std::string{gpu_name_buffer};
65 });
66 }
67 }
68#endif // CUDA
69 return make_unordered_map_of_variants(devices);
70 }
71 if (name == "list_devices_properties") {
72 std::unordered_map<std::string, std::unordered_map<int, Variant>> dict{};
73#ifdef CUDA
74 std::vector<EspressoGpuDevice> devices = cuda_gather_gpus();
75 for (auto const &dev : devices) {
76 auto const hostname = std::string{dev.proc_name};
77 if (dict.count(hostname) == 0) {
78 dict[hostname] = {};
79 }
80 std::unordered_map<std::string, Variant> dev_properties = {
81 {"name", std::string{dev.name}},
82 {"compute_capability",
83 Variant{std::vector<int>{
84 {dev.compute_capability_major, dev.compute_capability_minor}}}},
85 {"cores", dev.n_cores},
86 {"total_memory", dev.total_memory},
87 };
88 dict[hostname][dev.id] = std::move(dev_properties);
89 }
90#endif // CUDA
92 }
93 if (name == "get_n_gpus") {
94 auto n_gpus = 0;
95#ifdef CUDA
96 if (context()->is_head_node()) {
97 // only GPUs on the head node can be used
98 invoke_skip_cuda_exceptions([&n_gpus]() { n_gpus = cuda_get_n_gpus(); });
99 }
100#endif // CUDA
101 return n_gpus;
102 }
103 return {};
104}
105
106} // namespace System
107} // 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:98
void cuda_set_device(int dev)
Choose a device for future CUDA computations.
Definition init_cuda.cu:92
void cuda_get_gpu_name(int dev, char *name)
Get the name of a CUDA device.
Definition init_cuda.cu:71
int cuda_get_n_gpus()
Get the number of CUDA devices.
Definition init_cuda.cu:44
std::unordered_map< std::string, Variant > VariantMap
Definition Variant.hpp:82
auto make_unordered_map_of_variants(std::unordered_map< K, V > const &v)
Definition Variant.hpp:93
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:80