BioDynaMo  v1.05.120-25dc9790
shared_data.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_SHARED_DATA_H_
16 #define CORE_CONTAINER_SHARED_DATA_H_
17 
18 #include <array>
19 #include <cstdint>
20 #include <new>
21 #include <vector>
22 
23 #include "core/util/root.h"
24 
33 #ifdef __cpp_lib_hardware_interference_size
36 #else
37 // Default values for x86-64
38 // 64 bytes on x86-64 │ L1_CACHE_BYTES │ L1_CACHE_SHIFT │ __cacheline_aligned │
39 // ...
40 constexpr std::size_t hardware_constructive_interference_size = 64u;
41 constexpr std::size_t hardware_destructive_interference_size = 64u;
42 #endif
43 
44 namespace bdm {
45 
47 template <typename T>
48 class SharedData {
49  public:
51  struct alignas(hardware_destructive_interference_size) AlignedT {
52  T data;
53  };
54 
58  using Data = std::vector<AlignedT>;
59 
60  SharedData() = default;
61  SharedData(size_t size, const T& value = T()) {
62  data_.resize(size);
63  for (auto& info : data_) {
64  info.data = value;
65  }
66  }
67  T& operator[](size_t index) { return data_[index].data; }
68  const T& operator[](size_t index) const { return data_[index].data; }
69 
71  size_t size() const { return data_.size(); } // NOLINT
72 
74  void resize(size_t new_size) { // NOTLINT
75  data_.resize(new_size);
76  }
77 
78  struct Iterator {
79  uint64_t index;
82  ++index;
83  return *this;
84  }
85  bool operator==(const Iterator& other) {
86  return index == other.index && data == other.data;
87  }
88  bool operator!=(const Iterator& other) { return !operator==(other); }
89  T& operator*() { return (*data)[index].data; }
90  const T& operator*() const { return (*data)[index].data; }
91  };
92 
93  Iterator begin() { return Iterator{0, &data_}; }
94  Iterator end() { return Iterator{data_.size(), &data_}; }
95  const Iterator begin() const {
96  return Iterator{0, const_cast<Data*>(&data_)};
97  }
98  const Iterator end() const {
99  return Iterator{data_.size(), const_cast<Data*>(&data_)};
100  }
101 
102  private:
104 
106 };
107 
108 } // namespace bdm
109 
110 #endif // CORE_CONTAINER_SHARED_DATA_H_
bdm::SharedData::Iterator::operator++
Iterator & operator++()
Definition: shared_data.h:81
bdm::SharedData::SharedData
SharedData()=default
bdm::SharedData
The SharedData class avoids false sharing between threads.
Definition: shared_data.h:48
bdm
Definition: agent.cc:39
bdm::Iterator
Definition: iterator.h:21
hardware_destructive_interference_size
constexpr std::size_t hardware_destructive_interference_size
Definition: shared_data.h:41
hardware_constructive_interference_size
constexpr std::size_t hardware_constructive_interference_size
Definition: shared_data.h:40
bdm::SharedData::data_
Data data_
Definition: shared_data.h:103
bdm::SharedData::begin
const Iterator begin() const
Definition: shared_data.h:95
bdm::SharedData::resize
void resize(size_t new_size)
Resize the SharedData.data_ vector.
Definition: shared_data.h:74
bdm::SharedData::Iterator::index
uint64_t index
Definition: shared_data.h:79
bdm::SharedData< uint64_t >::Data
std::vector< AlignedT > Data
Definition: shared_data.h:58
bdm::SharedData::AlignedT::data
T data
Definition: shared_data.h:52
bdm::SharedData::AlignedT
Wrapper for a chacheline-size aligned T.
Definition: shared_data.h:51
bdm::SharedData::Iterator::data
Data * data
Definition: shared_data.h:80
bdm::SharedData::operator[]
T & operator[](size_t index)
Definition: shared_data.h:67
bdm::SharedData::operator[]
const T & operator[](size_t index) const
Definition: shared_data.h:68
bdm::SharedData::Iterator::operator==
bool operator==(const Iterator &other)
Definition: shared_data.h:85
bdm::SharedData::Iterator::operator!=
bool operator!=(const Iterator &other)
Definition: shared_data.h:88
bdm::SharedData::size
size_t size() const
Get the size of the SharedData.data_ vector.
Definition: shared_data.h:71
bdm::SharedData::end
Iterator end()
Definition: shared_data.h:94
root.h
bdm::SharedData::Iterator::operator*
const T & operator*() const
Definition: shared_data.h:90
bdm::SharedData::Iterator::operator*
T & operator*()
Definition: shared_data.h:89
bdm::SharedData::SharedData
SharedData(size_t size, const T &value=T())
Definition: shared_data.h:61
bdm::SharedData::Iterator
Definition: shared_data.h:78
BDM_CLASS_DEF_NV
#define BDM_CLASS_DEF_NV(class_name, class_version_id)
Definition: root.h:200
bdm::SharedData::end
const Iterator end() const
Definition: shared_data.h:98
bdm::SharedData::begin
Iterator begin()
Definition: shared_data.h:93