16#include "utils/mardyn_assert.h"
17#include "AlignedAllocator.h"
19#define CACHE_LINE_SIZE 64
74template<
class T,
size_t alignment = CACHE_LINE_SIZE>
112#if defined(__SSE3__) or defined(__MIC__)
113 virtual void prefetch(
int hint = 1,
int n = -1)
const {
114 mardyn_assert(n >= -2);
117 const int stride = _round_up(1);
122 endPrefetch = _vec.capacity();
126 endPrefetch = _vec.size();
133 for (
size_t i = 0; i < endPrefetch; i+= stride) {
134 const T & val = _vec[i];
135 const T * valP = &val;
137 _mm_prefetch((
const char*)valP, 2);
139 _mm_prefetch((
const char*)valP, _MM_HINT_T1);
144 virtual void prefetch(
int ,
int )
const {}
147 virtual void increaseStorage(
size_t oldNumElements,
size_t additionalElements) {
148 mardyn_assert(oldNumElements <= _vec.capacity());
150 size_t newNumElements = oldNumElements + additionalElements;
152 if (newNumElements <= _vec.capacity()) {
158 _vec.reserve(_round_up(newNumElements));
159 _vec.resize(_vec.capacity());
162 void appendValue(T v,
size_t oldNumElements) {
163 increaseStorage(oldNumElements, 1);
165 _vec[oldNumElements] = v;
168 virtual size_t resize_zero_shrink(
size_t exact_size,
bool zero_rest_of_CL =
169 false,
bool allow_shrink =
false) {
170 size_t size_rounded_up = _round_up(exact_size);
172 bool need_resize = size_rounded_up > _vec.size()
173 or (allow_shrink and size_rounded_up < _vec.size());
176 _vec.reserve(size_rounded_up);
177 _vec.resize(_vec.capacity());
180 if (zero_rest_of_CL and size_rounded_up > 0) {
181 std::memset(_vec.data() + exact_size, 0,
182 size_rounded_up - exact_size);
185 mardyn_assert(size_rounded_up <= _vec.size());
193 _vec.reserve(_round_up(n));
194 _vec.resize(_vec.capacity());
197 virtual void zero(
size_t start_idx = 0) {
198 if (_vec.size() > 0 and start_idx < _vec.capacity()) {
199 size_t num_to_zero = _vec.capacity() - start_idx;
200 std::memset(_vec.data() + start_idx, 0, num_to_zero *
sizeof(T));
218 operator const T*()
const {
226 return _vec.capacity() *
sizeof(T);
229 static size_t _round_up(
size_t n) {
230 unsigned long j = alignment /
sizeof(T) - 1;
231 unsigned long ret = (n + j) & ~j;
237 std::vector<T, AlignedAllocator<T, alignment>> _vec;
An aligned array.
Definition: AlignedArray.h:75
AlignedArray & operator=(const AlignedArray &a)
Assign a copy of another AlignedArray.
Definition: AlignedArray.h:101
AlignedArray(const AlignedArray &a)
Construct a copy of another AlignedArray.
Definition: AlignedArray.h:94
size_t get_size() const
Return current size in terms of elements.
Definition: AlignedArray.h:207
size_t get_dynamic_memory() const
Return amount of allocated storage + .
Definition: AlignedArray.h:225
AlignedArray(size_t n)
Construct an array of n elements.
Definition: AlignedArray.h:87
virtual void resize(size_t n)
Reallocate the array. All content may be lost.
Definition: AlignedArray.h:192
AlignedArray()
Construct an empty array.
Definition: AlignedArray.h:80
virtual ~AlignedArray()
Free the array.
Definition: AlignedArray.h:109