BioDynaMo  v1.05.159-3be850bb
agent_vector.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_VECTOR_H_
16 #define CORE_CONTAINER_AGENT_VECTOR_H_
17 
18 #include <vector>
19 #include "core/resource_manager.h" // AgentHandle
20 #include "core/simulation.h"
21 
22 namespace bdm {
23 
24 namespace detail {
25 struct InitializeGPUData;
26 } // namespace detail
27 
30 template <typename T>
31 class AgentVector {
33  friend struct MechanicalForcesOpCuda;
34 
35  public:
38  data_.resize(thread_info_->GetNumaNodes());
39  size_.resize(thread_info_->GetNumaNodes());
40  reserve();
41  }
42 
49  void reserve() { // NOLINT
50  clear();
51  auto* sim = Simulation::GetActive();
52  auto* rm = sim->GetResourceManager();
53  for (int n = 0; n < thread_info_->GetNumaNodes(); n++) {
54  auto num_agents = rm->GetNumAgents(n);
55  if (data_[n].capacity() < num_agents) {
56  data_[n].reserve(num_agents * 1.5);
57  }
58  size_[n] = num_agents;
59  }
60  }
61 
62  void clear() { // NOLINT
63  for (auto& el : size_) {
64  el = 0;
65  }
66  for (auto& vec : data_) {
67  vec.clear();
68  }
69  }
70 
71  // Returns the number of elements of specified type
72  size_t size(uint16_t numa_node) { return size_[numa_node]; } // NOLINT
73 
74  const T& operator[](const AgentHandle& handle) const {
75  return data_[handle.GetNumaNode()][handle.GetElementIdx()];
76  }
77 
78  T& operator[](const AgentHandle& handle) {
79  return data_[handle.GetNumaNode()][handle.GetElementIdx()];
80  }
81 
82  bool operator==(const AgentVector<T>& other) const {
83  if (size_ != other.size_) {
84  return false;
85  }
86  // inline data
87  for (size_t i = 0; i < size_.size(); i++) {
88  auto sz = size_[i];
89  for (size_t j = 0; j < sz; j++) {
90  if (data_[i][j] != other.data_[i][j]) {
91  return false;
92  }
93  }
94  }
95  return true;
96  }
97 
98  bool operator!=(const AgentVector<T>& other) const {
99  return !this->operator==(other);
100  }
101 
102  private:
104  std::vector<std::vector<T>> data_;
105  std::vector<size_t> size_;
107 };
108 
109 } // namespace bdm
110 
111 #endif // CORE_CONTAINER_AGENT_VECTOR_H_
ElementIdx_t GetElementIdx() const
Definition: agent_handle.h:45
NumaNode_t GetNumaNode() const
Definition: agent_handle.h:44
AgentVector()
NB: Elements will not be initialized.
Definition: agent_vector.h:37
std::vector< std::vector< T > > data_
one std::vector<T> for each numa node
Definition: agent_vector.h:104
T & operator[](const AgentHandle &handle)
Definition: agent_vector.h:78
bool operator==(const AgentVector< T > &other) const
Definition: agent_vector.h:82
size_t size(uint16_t numa_node)
Definition: agent_vector.h:72
std::vector< size_t > size_
Definition: agent_vector.h:105
ThreadInfo * thread_info_
Definition: agent_vector.h:106
const T & operator[](const AgentHandle &handle) const
Definition: agent_vector.h:74
bool operator!=(const AgentVector< T > &other) const
Definition: agent_vector.h:98
static Simulation * GetActive()
This function returns the currently active Simulation simulation.
Definition: simulation.cc:68
This class stores information about each thread. (e.g. to which NUMA node it belongs to....
Definition: thread_info.h:31
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
Defines the 3D physical interactions between physical objects.