BioDynaMo  v1.05.120-25dc9790
randomized_rm.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_RANDOMIZED_RM_H_
16 #define CORE_RANDOMIZED_RM_H_
17 
18 #include <algorithm>
19 #include "core/resource_manager.h"
20 #ifdef LINUX
21 #include <parallel/algorithm>
22 #endif // LINUX
23 
24 namespace bdm {
25 
32 template <typename TBaseRm>
33 class RandomizedRm : public TBaseRm {
34  public:
35  explicit RandomizedRm(TRootIOCtor* r) {}
36  RandomizedRm(bool auto_randomize = true);
37  virtual ~RandomizedRm();
38 
39  void RandomizeAgentsOrder();
40  void EndOfIteration() override;
41 
42  protected:
43  // Automatically randomize the agent order at the end of each iteration
44  // If false, you can call RandomizeAgentsOrder() manually
45  bool auto_randomize_ = true;
47 };
48 
49 // -----------------------------------------------------------------------------
50 template <typename TBaseRm>
52  : auto_randomize_(auto_randomize){};
53 
54 // -----------------------------------------------------------------------------
55 template <typename TBaseRm>
57 
58 struct Ubrng {
59  using result_type = uint32_t;
62  static constexpr result_type min() { return 0; }
63  static constexpr result_type max() {
64  return std::numeric_limits<result_type>::max();
65  }
67  return random->Integer(std::numeric_limits<result_type>::max());
68  }
69 };
70 
71 template <typename TBaseRm>
73  // shuffle
74 #pragma omp parallel for schedule(static, 1)
75  for (uint64_t n = 0; n < this->agents_.size(); ++n) {
76 #ifdef LINUX
77  __gnu_parallel::random_shuffle(this->agents_[n].begin(),
78  this->agents_[n].end());
79 #else
80  auto* random = Simulation::GetActive()->GetRandom();
81  std::shuffle(this->agents_[n].begin(), this->agents_[n].end(),
82  Ubrng(random));
83 #endif // LINUX
84  }
85 
86  // update uid_ah_map_
87  auto update_agent_map = L2F([this](Agent* a, AgentHandle ah) {
88  this->uid_ah_map_.Insert(a->GetUid(), ah);
89  });
90  TBaseRm::ForEachAgentParallel(update_agent_map);
91 }
92 
93 // -----------------------------------------------------------------------------
94 template <typename TBaseRm>
96  TBaseRm::EndOfIteration();
97  if (auto_randomize_) {
98  RandomizeAgentsOrder();
99  }
100 }
101 
102 } // namespace bdm
103 
104 #endif // CORE_RANDOMIZED_RM_H_
bdm::RandomizedRm::auto_randomize_
bool auto_randomize_
Definition: randomized_rm.h:45
bdm::RandomizedRm::EndOfIteration
void EndOfIteration() override
Definition: randomized_rm.h:95
bdm::Ubrng::result_type
uint32_t result_type
Definition: randomized_rm.h:59
bdm
Definition: agent.cc:39
bdm::RandomizedRm::RandomizedRm
RandomizedRm(TRootIOCtor *r)
Definition: randomized_rm.h:35
bdm::RandomizedRm::~RandomizedRm
virtual ~RandomizedRm()
bdm::Ubrng::min
static constexpr result_type min()
Definition: randomized_rm.h:62
bdm::Simulation::GetRandom
Random * GetRandom()
Returns a thread local random number generator.
Definition: simulation.cc:267
bdm::Agent::GetUid
const AgentUid & GetUid() const
Definition: agent.cc:123
bdm::Random
Definition: random.h:262
bdm::RandomizedRm::RandomizeAgentsOrder
void RandomizeAgentsOrder()
Definition: randomized_rm.h:72
bdm::L2F
LambdaFunctor< decltype(&TLambda::operator())> L2F(const TLambda &l)
Definition: functor.h:110
bdm::Agent
Contains code required by all agents.
Definition: agent.h:79
bdm::Ubrng::random
Random * random
Definition: randomized_rm.h:60
bdm::Ubrng::operator()
result_type operator()()
Definition: randomized_rm.h:66
bdm::Random::Integer
unsigned Integer(int max)
Definition: random.cc:91
bdm::Ubrng::Ubrng
Ubrng(Random *random)
Definition: randomized_rm.h:61
bdm::RandomizedRm::BDM_CLASS_DEF_NV
BDM_CLASS_DEF_NV(RandomizedRm, 1)
bdm::Ubrng::max
static constexpr result_type max()
Definition: randomized_rm.h:63
bdm::Ubrng
Definition: randomized_rm.h:58
bdm::RandomizedRm
Definition: randomized_rm.h:33
resource_manager.h
bdm::Simulation::GetActive
static Simulation * GetActive()
This function returns the currently active Simulation simulation.
Definition: simulation.cc:68
bdm::AgentHandle
Definition: agent_handle.h:29