BioDynaMo  v1.05.119-a4ff3934
agent_flat_idx_map.h
Go to the documentation of this file.
1 // -----------------------------------------------------------------------------
2 //
3 // Copyright (C) 2021 CERN & University of Surrey for the benefit of the
4 // BioDynaMo collaboration. All Rights Reserved.
5 //
6 // Licensed under the Apache License, Version 2.0 (the "License");
7 // you may not use this file except in compliance with the License.
8 //
9 // See the LICENSE file distributed with this work for details.
10 // See the NOTICE file distributed with this work for additional information
11 // regarding copyright ownership.
12 //
13 // -----------------------------------------------------------------------------
14 
15 #ifndef CORE_CONTAINER_AGENT_FLAT_IDX_MAP_H_
16 #define CORE_CONTAINER_AGENT_FLAT_IDX_MAP_H_
17 
18 #include <vector>
19 
21 #include "core/resource_manager.h"
22 #include "core/simulation.h"
23 #include "core/util/thread_info.h"
24 
25 namespace bdm {
26 
42  public:
43  AgentFlatIdxMap() = default;
44 
46  this->offset_ = other.offset_;
47  }
48 
49  void Update() {
51  auto num_numa_nodes = ThreadInfo::GetInstance()->GetNumaNodes();
52  offset_.resize(num_numa_nodes);
53  offset_[0] = 0;
54  for (int nn = 1; nn < num_numa_nodes; nn++) {
55  offset_[nn] = offset_[nn - 1] + rm->GetNumAgents(nn - 1);
56  }
57  }
58 
59  uint64_t GetFlatIdx(const AgentHandle& ah) const {
60  return offset_[ah.GetNumaNode()] + ah.GetElementIdx();
61  }
62 
63  AgentHandle GetAgentHandle(uint64_t idx) const {
64  size_t nn = 0;
65  for (size_t i = 1; i < offset_.size(); i++) {
66  if (idx < offset_[i]) {
67  break;
68  }
69  nn++;
70  }
71 
72  idx -= offset_[nn];
73  assert(nn <= std::numeric_limits<AgentHandle::NumaNode_t>::max());
74  assert(idx <= std::numeric_limits<AgentHandle::ElementIdx_t>::max());
75  return AgentHandle(static_cast<AgentHandle::NumaNode_t>(nn),
76  static_cast<AgentHandle::ElementIdx_t>(idx));
77  }
78 
79  private:
80  std::vector<size_t> offset_;
81 };
82 
83 } // namespace bdm
84 
85 #endif // CORE_CONTAINER_AGENT_FLAT_IDX_MAP_H_
bdm::AgentFlatIdxMap::Update
void Update()
Definition: agent_flat_idx_map.h:49
bdm::AgentHandle::NumaNode_t
uint16_t NumaNode_t
Definition: agent_handle.h:31
bdm::AgentFlatIdxMap::GetAgentHandle
AgentHandle GetAgentHandle(uint64_t idx) const
Definition: agent_flat_idx_map.h:63
bdm::ThreadInfo::GetInstance
static ThreadInfo * GetInstance()
Definition: thread_info.cc:21
agent_handle.h
bdm
Definition: agent.cc:39
thread_info.h
bdm::ThreadInfo::GetNumaNodes
int GetNumaNodes() const
Returns the number of NUMA nodes on this machine.
Definition: thread_info.h:48
bdm::AgentHandle::ElementIdx_t
uint32_t ElementIdx_t
Definition: agent_handle.h:32
bdm::AgentFlatIdxMap::AgentFlatIdxMap
AgentFlatIdxMap()=default
bdm::Simulation::GetResourceManager
ResourceManager * GetResourceManager()
Returns the ResourceManager instance.
Definition: simulation.cc:244
bdm::AgentFlatIdxMap::GetFlatIdx
uint64_t GetFlatIdx(const AgentHandle &ah) const
Definition: agent_flat_idx_map.h:59
bdm::AgentFlatIdxMap::offset_
std::vector< size_t > offset_
Definition: agent_flat_idx_map.h:80
simulation.h
bdm::AgentFlatIdxMap
Definition: agent_flat_idx_map.h:41
bdm::AgentFlatIdxMap::AgentFlatIdxMap
AgentFlatIdxMap(const AgentFlatIdxMap &other)
Definition: agent_flat_idx_map.h:45
bdm::AgentHandle::GetNumaNode
NumaNode_t GetNumaNode() const
Definition: agent_handle.h:44
resource_manager.h
bdm::Simulation::GetActive
static Simulation * GetActive()
This function returns the currently active Simulation simulation.
Definition: simulation.cc:68
bdm::AgentHandle::GetElementIdx
ElementIdx_t GetElementIdx() const
Definition: agent_handle.h:45
bdm::AgentHandle
Definition: agent_handle.h:29