Ginkgo Generated from branch based on master. Ginkgo version 1.7.0
A numerical linear algebra library targeting many-core architectures
Loading...
Searching...
No Matches
machine_topology.hpp
1/*******************************<GINKGO LICENSE>******************************
2Copyright (c) 2017-2023, the Ginkgo authors
3All rights reserved.
4
5Redistribution and use in source and binary forms, with or without
6modification, are permitted provided that the following conditions
7are met:
8
91. Redistributions of source code must retain the above copyright
10notice, this list of conditions and the following disclaimer.
11
122. Redistributions in binary form must reproduce the above copyright
13notice, this list of conditions and the following disclaimer in the
14documentation and/or other materials provided with the distribution.
15
163. Neither the name of the copyright holder nor the names of its
17contributors may be used to endorse or promote products derived from
18this software without specific prior written permission.
19
20THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
21IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31******************************<GINKGO LICENSE>*******************************/
32
33#ifndef GKO_PUBLIC_CORE_BASE_MACHINE_TOPOLOGY_HPP_
34#define GKO_PUBLIC_CORE_BASE_MACHINE_TOPOLOGY_HPP_
35
36
37#include <cassert>
38#include <cstddef>
39#include <functional>
40#include <iostream>
41#include <memory>
42#include <mutex>
43#include <string>
44#include <type_traits>
45#include <vector>
46
47
48#include <ginkgo/config.hpp>
49#include <ginkgo/core/base/exception.hpp>
50#include <ginkgo/core/base/exception_helpers.hpp>
51
52
53#if GKO_HAVE_HWLOC
54
55#include <hwloc.h>
56
57#else
58
60struct hwloc_obj_t {};
61
62#endif
63
64
65struct hwloc_topology;
66struct hwloc_bitmap_s;
67
68
69namespace gko {
70
71
91 template <typename T>
92 using hwloc_manager = std::unique_ptr<T, std::function<void(T*)>>;
93
97 struct normal_obj_info {
101 hwloc_obj_t obj;
102
110 size_type logical_id;
111
115 size_type os_id;
116
120 size_type gp_id;
121
125 int numa;
126
130 size_type memory_size;
131 };
132
133
150 struct io_obj_info {
154 hwloc_obj_t obj;
155
162 size_type logical_id;
163
167 size_type os_id;
168
172 size_type gp_id;
173
177 int closest_numa;
178
182 hwloc_obj_t non_io_ancestor;
183
187 int ancestor_local_id;
188
192 std::string ancestor_type;
193
197 std::vector<int> closest_pu_ids;
198
202 std::string pci_bus_id;
203 };
204
205public:
212 {
214 return &instance;
215 }
216
229 void bind_to_cores(const std::vector<int>& ids,
230 const bool singlify = true) const
231 {
232 hwloc_binding_helper(this->cores_, ids, singlify);
233 }
234
240 void bind_to_core(const int& id) const
241 {
242 machine_topology::get_instance()->bind_to_cores(std::vector<int>{id});
243 }
244
257 void bind_to_pus(const std::vector<int>& ids,
258 const bool singlify = true) const
259 {
260 hwloc_binding_helper(this->pus_, ids, singlify);
261 }
262
268 void bind_to_pu(const int& id) const
269 {
270 machine_topology::get_instance()->bind_to_pus(std::vector<int>{id});
271 }
272
279 const normal_obj_info* get_pu(size_type id) const
280 {
281 GKO_ENSURE_IN_BOUNDS(id, this->pus_.size());
282 return &this->pus_[id];
283 }
284
291 const normal_obj_info* get_core(size_type id) const
292 {
293 GKO_ENSURE_IN_BOUNDS(id, this->cores_.size());
294 return &this->cores_[id];
295 }
296
303 const io_obj_info* get_pci_device(size_type id) const
304 {
305 GKO_ENSURE_IN_BOUNDS(id, this->pci_devices_.size());
306 return &this->pci_devices_[id];
307 }
308
315 const io_obj_info* get_pci_device(const std::string& pci_bus_id) const;
316
322 size_type get_num_pus() const { return this->pus_.size(); }
323
329 size_type get_num_cores() const { return this->cores_.size(); }
330
336 size_type get_num_pci_devices() const { return this->pci_devices_.size(); }
337
343 size_type get_num_numas() const { return this->num_numas_; }
344
351 void hwloc_binding_helper(
352 const std::vector<machine_topology::normal_obj_info>& obj,
353 const std::vector<int>& ids, const bool singlify = true) const;
354
363 void load_objects(hwloc_obj_type_t type,
364 std::vector<normal_obj_info>& objects) const;
365
374 void load_objects(hwloc_obj_type_t type,
375 std::vector<io_obj_info>& vector) const;
376
383 int get_obj_id_by_os_index(const std::vector<normal_obj_info>& objects,
384 size_type os_index) const;
385
392 int get_obj_id_by_gp_index(const std::vector<normal_obj_info>& objects,
393 size_type gp_index) const;
394
395private:
403 machine_topology& operator=(machine_topology&) = delete;
404 machine_topology& operator=(machine_topology&&) = delete;
405 ~machine_topology() = default;
406
407 std::vector<normal_obj_info> pus_;
408 std::vector<normal_obj_info> cores_;
409 std::vector<normal_obj_info> packages_;
410 std::vector<normal_obj_info> numa_nodes_;
411 std::vector<io_obj_info> pci_devices_;
412 size_type num_numas_;
413
415};
416
417
418using MachineTopology GKO_DEPRECATED("please use machine_topology") =
419 machine_topology;
420
421
422} // namespace gko
423
424
425#endif // GKO_PUBLIC_CORE_BASE_MACHINE_TOPOLOGY_HPP_
The machine topology class represents the hierarchical topology of a machine, including NUMA nodes,...
Definition machine_topology.hpp:90
const io_obj_info * get_pci_device(const std::string &pci_bus_id) const
Get the object of type pci device associated with the PCI bus id.
const io_obj_info * get_pci_device(size_type id) const
Get the object of type pci device associated with the id.
Definition machine_topology.hpp:303
size_type get_num_numas() const
Get the number of NUMA objects stored in this Topology tree.
Definition machine_topology.hpp:343
size_type get_num_pus() const
Get the number of PU objects stored in this Topology tree.
Definition machine_topology.hpp:322
void bind_to_pus(const std::vector< int > &ids, const bool singlify=true) const
Bind the calling process to PUs associated with the ids.
Definition machine_topology.hpp:257
static machine_topology * get_instance()
Returns an instance of the machine_topology object.
Definition machine_topology.hpp:211
size_type get_num_cores() const
Get the number of core objects stored in this Topology tree.
Definition machine_topology.hpp:329
void bind_to_pu(const int &id) const
Bind to a Processing unit (PU)
Definition machine_topology.hpp:268
void bind_to_core(const int &id) const
Bind to a single core.
Definition machine_topology.hpp:240
void bind_to_cores(const std::vector< int > &ids, const bool singlify=true) const
Bind the calling process to the CPU cores associated with the ids.
Definition machine_topology.hpp:229
const normal_obj_info * get_core(size_type id) const
Get the object of type core associated with the id.
Definition machine_topology.hpp:291
const normal_obj_info * get_pu(size_type id) const
Get the object of type PU associated with the id.
Definition machine_topology.hpp:279
size_type get_num_pci_devices() const
Get the number of PCI device objects stored in this Topology tree.
Definition machine_topology.hpp:336
The Ginkgo namespace.
Definition abstract_factory.hpp:48
constexpr T one()
Returns the multiplicative identity for T.
Definition math.hpp:803
std::size_t size_type
Integral type used for allocation quantities.
Definition types.hpp:120
Definition machine_topology.hpp:60
Definition machine_topology.hpp:59