DiSMEC++
numa.h
Go to the documentation of this file.
1 // Copyright (c) 2021, Aalto University, developed by Erik Schultheis
2 // All rights reserved.
3 //
4 // SPDX-License-Identifier: MIT
5 
6 #ifndef DISMEC_NUMA_H
7 #define DISMEC_NUMA_H
8 
9 #include <memory>
10 #include <vector>
11 #include <functional>
12 #include <any>
13 #include "spdlog/spdlog.h"
14 #include "thread_id.h"
15 
16 namespace dismec::parallel {
17 
19  void pin_to_data(const void* data);
20 
33  public:
35  [[nodiscard]] bool has_numa() const { return m_HasNUMA; }
37  [[nodiscard]] int num_numa() const { return m_Copies.size(); }
38 
39  protected:
41  virtual ~NUMAReplicatorBase() = default;
42 
44  void make_copies();
45 
48  [[nodiscard]] const std::any& access_local() const;
49 
50  private:
51  bool m_HasNUMA = false;
52 
54  std::vector<std::any> m_Copies;
55 
57  [[nodiscard]] virtual std::any get_clone() const = 0;
58  };
59 
71  template<class T>
73  using ptr_t = std::shared_ptr<const T>;
74  public:
76  explicit NUMAReplicator(std::shared_ptr<const T> data) : m_Data(std::move(data)) {
77  make_copies();
78  }
79 
82  std::shared_ptr<const T> get_local() const {
83  const std::any& data = access_local();
84  if(data.has_value()) {
85  return std::any_cast<ptr_t>(data);
86  }
87  return m_Data;
88  }
89  private:
91 
92  [[nodiscard]] std::any get_clone() const override {
93  return std::make_shared<const T>(*m_Data);
94  }
95  };
96 
97 
119  public:
120  ThreadDistributor(long num_threads, std::shared_ptr<spdlog::logger> = {});
121  void pin_this_thread(thread_id_t thread_id);
122  private:
123  std::vector<cpu_id_t> m_TargetCPUs;
124 
125  std::shared_ptr<spdlog::logger> m_Logger;
126  };
127 }
128 
129 #endif //DISMEC_NUMA_H
Base class for NUMAReplicator.
Definition: numa.h:32
bool m_HasNUMA
Whether NUMA functions are available.
Definition: numa.h:51
void make_copies()
Uses the get_clone() function of the implementing class to generate NUMA-local copies for each NUMA n...
Definition: numa.cpp:91
virtual std::any get_clone() const =0
This function needs to be implemented by a derived class.
std::vector< std::any > m_Copies
This vector contains one copy of data for each NUMA node.
Definition: numa.h:54
int num_numa() const
This returns the number of NUMA nodes.
Definition: numa.h:37
bool has_numa() const
this returns true if NUMA is available. Otherwise, there will be no replication.
Definition: numa.h:35
const std::any & access_local() const
Definition: numa.cpp:110
Helper class to ensure that each NUMA node has its own copy of some immutable data.
Definition: numa.h:72
std::shared_ptr< const T > get_local() const
Definition: numa.h:82
std::any get_clone() const override
This function needs to be implemented by a derived class.
Definition: numa.h:92
std::shared_ptr< const T > ptr_t
Definition: numa.h:73
NUMAReplicator(std::shared_ptr< const T > data)
Initializes the NUMAReplicator and distributes the data to all NUMA nodes.
Definition: numa.h:76
This class helps with distributing threads to the different CPU cores.
Definition: numa.h:118
void pin_this_thread(thread_id_t thread_id)
Definition: numa.cpp:265
std::vector< cpu_id_t > m_TargetCPUs
List of CPUs to which the threads will be assigned.
Definition: numa.h:123
ThreadDistributor(long num_threads, std::shared_ptr< spdlog::logger >={})
Definition: numa.cpp:240
std::shared_ptr< spdlog::logger > m_Logger
Definition: numa.h:125
Strong typedef for an int to signify a thread id.
Definition: thread_id.h:20
void pin_to_data(const void *data)
Pint the calling thread to the NUMA node on which data resides.
Definition: numa.cpp:78