BioDynaMo  v1.05.119-a4ff3934
agent_uid_generator.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_AGENT_AGENT_UID_GENERATOR_H_
16 #define CORE_AGENT_AGENT_UID_GENERATOR_H_
17 
18 #include <atomic>
19 #include <limits>
20 #include <mutex>
22 #include "core/agent/agent_uid.h"
25 #include "core/scheduler.h"
26 #include "core/simulation.h"
27 #include "core/util/root.h"
28 #include "core/util/spinlock.h"
29 
30 namespace bdm {
31 
34  public:
35  AgentUidGenerator(const AgentUidGenerator&) = delete;
36  AgentUidGenerator() : counter_(0), tinfo_(ThreadInfo::GetInstance()) {
37  Update();
38  }
39 
45  auto& old_uids = tl_uids_[tinfo_->GetMyThreadId()];
46  if (old_uids.size()) {
47  auto uid = old_uids.back();
48  old_uids.pop_back();
49  return AgentUid(uid.GetIndex(), uid.GetReused() + 1);
50  }
51  return AgentUid(counter_++);
52  }
53 
54  // Returns the highest index that was used for an AgentUid
57 
60  void ReuseAgentUid(const AgentUid& uid) {
61  tl_uids_[tinfo_->GetMyThreadId()].push_back(uid);
62  }
63 
67  void Update() { tl_uids_.resize(tinfo_->GetMaxThreads()); }
68 
69  private:
70  std::atomic<typename AgentUid::Index_t> counter_;
74 
77  ThreadInfo* tinfo_ = nullptr;
78 
80 };
81 
82 // The following custom streamer should be visible to rootcling for dictionary
83 // generation, but not to the interpreter!
84 #if (!defined(__CLING__) || defined(__ROOTCLING__)) && defined(USE_DICT)
85 
86 inline void AgentUidGenerator::Streamer(TBuffer& R__b) {
87  if (R__b.IsReading()) {
88  R__b.ReadClassBuffer(AgentUidGenerator::Class(), this);
89  this->counter_ = this->root_counter_;
90  } else {
91  this->root_counter_ = this->counter_.load();
92  R__b.WriteClassBuffer(AgentUidGenerator::Class(), this);
93  }
94 }
95 
96 #endif // !defined(__CLING__) || defined(__ROOTCLING__)
97 
98 } // namespace bdm
99 
100 #endif // CORE_AGENT_AGENT_UID_GENERATOR_H_
bdm::AgentUidGenerator::tl_uids_
SharedData< std::vector< AgentUid > > tl_uids_
Thread local vector of AgentUids that can be reused.
Definition: agent_uid_generator.h:76
spinlock.h
agent_uid_map.h
agent_uid.h
agent_handle.h
bdm::SharedData
The SharedData class avoids false sharing between threads.
Definition: shared_data.h:48
bdm
Definition: agent.cc:39
bdm::AgentUidGenerator::AgentUidGenerator
AgentUidGenerator()
Definition: agent_uid_generator.h:36
bdm::AgentUid::Index_t
uint32_t Index_t
Definition: agent_uid.h:27
bdm::AgentUidGenerator::Update
void Update()
Definition: agent_uid_generator.h:67
scheduler.h
bdm::AgentUidGenerator::tinfo_
ThreadInfo * tinfo_
Definition: agent_uid_generator.h:77
bdm::AgentUidGenerator::GenerateUid
AgentUid GenerateUid()
Definition: agent_uid_generator.h:44
bdm::AgentUidGenerator::ReuseAgentUid
void ReuseAgentUid(const AgentUid &uid)
Definition: agent_uid_generator.h:60
bdm::AgentUidGenerator::GetHighestIndex
AgentUid::Index_t GetHighestIndex() const
Thread-safe.
Definition: agent_uid_generator.h:56
bdm::AgentUidGenerator::BDM_CLASS_DEF_NV
BDM_CLASS_DEF_NV(AgentUidGenerator, 1)
root.h
bdm::AgentUidGenerator::counter_
std::atomic< typename AgentUid::Index_t > counter_
Definition: agent_uid_generator.h:70
bdm::AgentUidGenerator::root_counter_
AgentUid::Index_t root_counter_
Definition: agent_uid_generator.h:73
bdm::AgentUidGenerator
This class generates unique ids for agents.
Definition: agent_uid_generator.h:33
bdm::AgentUid
Definition: agent_uid.h:25
shared_data.h
simulation.h
bdm::ThreadInfo::GetMaxThreads
int GetMaxThreads() const
Return the maximum number of threads.
Definition: thread_info.h:66
bdm::ThreadInfo::GetMyThreadId
int GetMyThreadId() const
Definition: thread_info.h:39
bdm::ThreadInfo
This class stores information about each thread. (e.g. to which NUMA node it belongs to....
Definition: thread_info.h:31