16 #include <morton/morton.h>
31 if (grid_->total_num_boxes_ == 0) {
35 mo_.Update(grid_->num_boxes_axis_);
44 if (sorted_boxes_.capacity() < grid_->boxes_.size()) {
45 sorted_boxes_.reserve(grid_->boxes_.capacity());
47 if (cummulated_agents_.capacity() < grid_->boxes_.size()) {
48 cummulated_agents_.reserve(grid_->boxes_.capacity());
57 auto num_threads = ti->GetMaxThreads();
58 auto tid = ti->GetMyThreadId();
60 auto correction = grid_->total_num_boxes_ % num_threads == 0 ? 0 : 1;
61 auto chunk = grid_->total_num_boxes_ / num_threads + correction;
62 auto start = tid * chunk;
63 auto end = std::min(grid_->total_num_boxes_, start + chunk);
66 mo_.CallMortonIteratorConsumer(start, end - 1, f);
73 uint64_t
start, end, box_index, discard;
79 uint64_t end, uint64_t box_index, uint64_t discard,
80 decltype(sorted_boxes) sorted_boxes)
86 sorted_boxes(sorted_boxes),
87 box_it(sorted_boxes[box_index]->begin(grid)) {
90 for (uint64_t i = 0; i < discard; ++i) {
96 bool HasNext()
const override {
return start < end; }
101 box_it = sorted_boxes[box_index]->
begin(grid_);
115 if (rm->GetNumAgents() != 0) {
122 std::array<real_t, 6> tmp_dim = {{inf, -inf, inf, -inf, inf, -inf}};
139 "The largest object size was found to be 0. Please check if your "
140 "cells are correctly initialized.");
144 "No box length specified although determine_sim_size_ is "
145 "set to false. Call the member function "
146 "SetBoxLength(box_length), or SetDetermineSimSize(false).");
155 for (
int i = 0; i < 3; i++) {
156 int dimension_length =
172 for (
int i = 0; i < 3; i++) {
178 for (
int i = 0; i < 3; i++) {
179 int dimension_length =
182 "The grid dimensions are not a multiple of its box length");
203 rm->ForEachAgentParallel(param->scheduling_batch_size, functor);
204 if (param->bound_space) {
205 int min = param->min_bound;
206 int max = param->max_bound;
210 if (param->thread_safety_mechanism ==
211 Param::ThreadSafetyMechanism::kAutomatic) {
218 bool uninitialized =
boxes_.size() == 0;
219 if (uninitialized && param->bound_space) {
224 int min = param->min_bound;
225 int max = param->max_bound;
229 }
else if (!uninitialized) {
236 "UniformGridEnvironment",
237 "You tried to initialize an empty simulation without bound space. "
238 "Therefore we cannot determine the size of the simulation space. "
239 "Please add agents, or set Param::bound_space, "
240 "Param::min_bound, and Param::max_bound.");
247 uint64_t start, uint64_t end,
256 start - cummulated_agents_[index - 1], sorted_boxes_);
263 decltype(sorted_boxes) sorted_boxes,
264 decltype(cummulated_agents) cummulated_agents)
267 sorted_boxes(sorted_boxes),
268 cummulated_agents(cummulated_agents) {}
278 auto morton_code = it->
Next();
279 uint_fast32_t x, y, z;
280 libmorton::morton3D_64_decode(morton_code, x, y, z);
282 static_cast<uint64_t>(x), static_cast<uint64_t>(y),
283 static_cast<uint64_t>(z)}));
284 sorted_boxes[start] = box;
302 mutex->SetMutexIndices(box_indices);
318 const unsigned batch_size = 64;
320 Agent* agents[batch_size] __attribute__((aligned(64)));
322 auto process_batch = [&]() {
323 for (uint64_t i = 0; i < size; ++i) {
333 auto* agent = rm->GetAgent(ah);
334 if (agent != &query) {
335 agents[size] = agent;
337 if (size == batch_size) {