ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
cuda/init.cpp
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 "config/config.hpp"
21
22#ifdef ESPRESSO_CUDA
23
24#include "init.hpp"
25#include "utils.hpp"
26
27#include "communication.hpp"
28
30
31#include <mpi.h>
32
33#include <algorithm>
34#include <iterator>
35#include <set>
36#include <string>
37#include <utility>
38#include <vector>
39
40/** Helper class for device sets.
41 */
44 const EspressoGpuDevice &b) const {
45 auto const name_comp = a.proc_name.compare(b.proc_name);
46 /* if both devices are from the same node, order by id */
47 return (name_comp == 0) ? a.id < b.id : name_comp < 0;
48 }
49};
50
51/** Gather list of CUDA devices from all nodes on the head node.
52 * It relies on <tt>MPI_Get_processor_name()</tt> to get a unique identifier
53 * of the physical node, as opposed to the logical rank of which there can
54 * be more than one per node.
55 */
56std::vector<EspressoGpuDevice> cuda_gather_gpus() {
57 std::vector<EspressoGpuDevice> devices;
58
59 int n_devices = 0;
61 [&n_devices]() { n_devices = cuda_get_n_gpus(); });
62
63 invoke_skip_cuda_exceptions([&devices, n_devices]() {
64 for (int i = 0; i < n_devices; ++i) {
65 devices.emplace_back(cuda_get_device_props(i));
66 }
67 });
68
70
71 if (this_node == 0) {
72 std::set<EspressoGpuDevice, CompareDevices> device_set;
73 std::ranges::copy(devices, std::inserter(device_set, device_set.begin()));
74 devices.clear();
75 std::ranges::copy(device_set, std::back_inserter(devices));
76 } else {
77 devices.clear();
78 }
79 return devices;
80}
81
82namespace detail {
83std::pair<int, std::string> get_node_info() {
84 int proc_name_len;
85 char proc_name[MPI_MAX_PROCESSOR_NAME];
86 MPI_Get_processor_name(proc_name, &proc_name_len);
87 return {this_node, proc_name};
88}
89} // namespace detail
90
96
97#endif // ESPRESSO_CUDA
Communicator communicator
boost::mpi::communicator comm_cart
The communicator.
int this_node
The number of this node.
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
void cuda_on_program_start()
Called on program start.
Definition cuda/init.cpp:91
EspressoGpuDevice cuda_get_device_props(int dev)
Get properties of a CUDA device.
Definition init_cuda.cu:64
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
void gather_buffer(std::vector< T, Allocator > &buffer, boost::mpi::communicator const &comm, int root=0)
Gather buffer with different size on each node.
Helper class for device sets.
Definition cuda/init.cpp:42
bool operator()(const EspressoGpuDevice &a, const EspressoGpuDevice &b) const
Definition cuda/init.cpp:43
Struct to hold information relevant to ESPResSo about GPUs.
Definition cuda/init.hpp:39
std::string proc_name
Node identification.
Definition cuda/init.hpp:45
int id
Local CUDA device id.
Definition cuda/init.hpp:41