BioDynaMo  v1.05.120-25dc9790
dynamic_loop.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_MULTI_SIMULATION_DYNAMIC_LOOP_H_
16 #define CORE_MULTI_SIMULATION_DYNAMIC_LOOP_H_
17 
18 #include <algorithm>
19 #include <functional>
20 #include <vector>
21 
23 
24 namespace bdm {
25 namespace experimental {
26 
27 // Emulates dynamic nested loops. The `action` gets back a vector of integers
28 // that represent the iteration of each respective `OptimizationParamType`
29 template <typename Lambda>
30 inline void DynamicNestedLoop(
31  const std::vector<OptimizationParamType*>& containers,
32  const Lambda& action) {
33  // Initialize the slots to hold the iterator value for each depth
34  auto depth = containers.size();
35  if (depth == 0) {
36  return;
37  }
38  std::vector<uint32_t> slots(depth, 0);
39 
40  // The depth index
41  size_t index = 0;
42  while (true) {
43  action(slots);
44 
45  // Increment iterator over outer-most loop
46  slots[0]++;
47 
48  // Carry
49  while (slots[index] == containers[index]->GetNumElements()) {
50  // Overflow, we're done
51  if (index == depth - 1) {
52  return;
53  }
54 
55  slots[index++] = 0;
56  slots[index]++;
57  }
58 
59  index = 0;
60  }
61 }
62 
63 } // namespace experimental
64 } // namespace bdm
65 
66 #endif // CORE_MULTI_SIMULATION_DYNAMIC_LOOP_H_
optimization_param_type.h
bdm
Definition: agent.cc:39
bdm::experimental::DynamicNestedLoop
void DynamicNestedLoop(const std::vector< OptimizationParamType * > &containers, const Lambda &action)
Definition: dynamic_loop.h:30