BioDynaMo  v1.05.159-3be850bb
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_
AgentFlatIdxMap(const AgentFlatIdxMap &other)
AgentFlatIdxMap()=default
uint64_t GetFlatIdx(const AgentHandle &ah) const
AgentHandle GetAgentHandle(uint64_t idx) const
std::vector< size_t > offset_
uint16_t NumaNode_t
Definition: agent_handle.h:31
ElementIdx_t GetElementIdx() const
Definition: agent_handle.h:45
NumaNode_t GetNumaNode() const
Definition: agent_handle.h:44
uint32_t ElementIdx_t
Definition: agent_handle.h:32
ResourceManager * GetResourceManager()
Returns the ResourceManager instance.
Definition: simulation.cc:244
static Simulation * GetActive()
This function returns the currently active Simulation simulation.
Definition: simulation.cc:68
static ThreadInfo * GetInstance()
Definition: thread_info.cc:21
int GetNumaNodes() const
Returns the number of NUMA nodes on this machine.
Definition: thread_info.h:48
Definition: agent.cc:39