-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy patharray2d.hpp
More file actions
405 lines (344 loc) · 10.3 KB
/
array2d.hpp
File metadata and controls
405 lines (344 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
#if !defined(DYNEARTHSOL3D_ARRAY2D_h)
#define DYNEARTHSOL3D_ARRAY2D_h
#include <vector>
#include <algorithm>
#include <cstring> // for memcpy
template <typename T, int N>
class Array2D {
T* a_;
int n_;
public:
//
// Accessor
//
struct Accessor {
T* ptr_; // pointer to the start of the array (e.g., &a_[0])
int stride_; // stride = n_ (total elements) for SoA layout
T& operator[](const int dim) const {
return ptr_[dim * stride_];
}
Accessor& operator=(const T& val) {
for (int d = 0; d < N; ++d) {
(*this)[d] = val;
}
return *this;
}
Accessor& operator=(const Accessor& other) {
if (this == &other) return *this;
for (int d = 0; d < N; ++d) {
(*this)[d] = other[d];
}
return *this;
}
void copy_to(T* dest) const {
for (int d = 0; d < N; ++d) {
dest[d] = ptr_[d * stride_];
}
}
void copy_from(const T* src) {
for (int d = 0; d < N; ++d) {
ptr_[d * stride_] = src[d];
}
}
template <typename OtherAccessor>
void copy_from(const OtherAccessor& other) {
for (int d = 0; d < N; ++d) {
(*this)[d] = other[d];
}
}
};
struct ConstAccessor {
const T* ptr_;
int stride_;
ConstAccessor(const T* p, int s) : ptr_(p), stride_(s) {}
ConstAccessor(const Accessor& other)
: ptr_(other.ptr_), stride_(other.stride_)
{}
struct ConstStridedLocalIndirect {
const T* ptr_;
int stride_;
const int* indices_;
const T& operator[](int k) const {
return ptr_[indices_[k] * stride_];
}
};
ConstStridedLocalIndirect subset(const int* indices) const {
return ConstStridedLocalIndirect{ptr_, stride_, indices};
}
const T& operator[](int dim) const {
return ptr_[dim * stride_];
}
void copy_to(T* dest) const {
for (int d = 0; d < N; ++d) {
dest[d] = ptr_[d * stride_];
}
}
};
//
// View
//
struct ConstIndirectAccessor {
const Array2D<T, N>* source_array;
const int* index_ptr;
int index_stride;
ConstAccessor operator[](int k) const {
int real_idx = index_ptr[k * index_stride];
return (*source_array)[real_idx];
}
};
struct IndirectAccessor {
Array2D<T, N>* source_array;
const int* index_ptr;
int index_stride;
Accessor operator[](int k) const {
int real_idx = index_ptr[k * index_stride];
return (*source_array)[real_idx];
}
};
IndirectAccessor view(int* indices) {
return IndirectAccessor{ this, indices, 1 };
}
IndirectAccessor view(const int* indices) {
return IndirectAccessor{ this, indices, 1 };
}
template <typename IntAccessor>
IndirectAccessor view(IntAccessor indices_acc) {
return IndirectAccessor{ this, indices_acc.ptr_, indices_acc.stride_ };
}
ConstIndirectAccessor view(const int* indices) const {
return ConstIndirectAccessor{ this, indices, 1 };
}
template <typename IntAccessor>
ConstIndirectAccessor view(IntAccessor indices_acc) const {
return ConstIndirectAccessor{ this, indices_acc.ptr_, indices_acc.stride_ };
}
ConstIndirectAccessor view_const(int* indices) const {
return ConstIndirectAccessor{ this, indices, 1 };
}
ConstIndirectAccessor view_const(const int* indices) const {
return ConstIndirectAccessor{ this, indices, 1 };
}
template <typename IntAccessor>
ConstIndirectAccessor view_const(IntAccessor indices_acc) const {
return ConstIndirectAccessor{ this, indices_acc.ptr_, indices_acc.stride_ };
}
//
// I/O and Transition Helpers
//
void load_from_buffer(const T* buffer, std::size_t count) {
if (a_ == nullptr)
a_ = new T[N * count];
else
this->resize(count, false);
n_ = count;
#ifndef ACC
#pragma omp parallel for collapse(2) if(count > 10000)
#endif
#pragma acc parallel loop gang vector collapse(2)
for (std::size_t i = 0; i < count; ++i) {
for (int d = 0; d < N; ++d) {
(*this)[i][d] = buffer[i * N + d];
}
}
}
// void copy_all_to(T* buffer) const {
// if (!a_) return;
// #ifndef ACC
// #pragma omp parallel for collapse(2) if(n_ > 10000)
// #endif
// #pragma acc parallel loop gang vector collapse(2)
// for(int i=0; i<n_; ++i) {
// for(int d=0; d<N; ++d)
// buffer[i*N + d] = (*this)[i][d];
// }
// }
void pack_to(std::vector<T>& buffer, std::size_t limit_size = 0) const {
std::size_t count = (limit_size > 0 && limit_size <= n_) ? limit_size : n_;
std::size_t total_elements = count * N;
buffer.resize(total_elements);
#ifndef ACC
#pragma omp parallel for collapse(2) if(n_ > 10000)
#endif
#pragma acc parallel loop gang vector collapse(2)
for (std::size_t i = 0; i < count; ++i) {
for (int d = 0; d < N; ++d) {
buffer[i * N + d] = (*this)[i][d];
}
}
}
#ifdef ACC
void pack_to_xyz_float(std::vector<float3>& buffer, std::size_t limit_size = 0) const {
std::size_t count = (limit_size > 0 && limit_size <= n_) ? limit_size : n_;
if (buffer.size() < count)
buffer.resize(count);
#pragma acc parallel loop gang vector
for (std::size_t i = 0; i < count; ++i) {
buffer[i].x = (float)(*this)[i][0];
buffer[i].y = (float)(*this)[i][1];
#ifdef THREED
buffer[i].z = (float)(*this)[i][2];
#else
buffer[i].z = 0.0;
#endif
}
}
#endif
//
// constructors & destructor
//
Array2D() : a_(nullptr), n_(0) {}
explicit Array2D(int size) {
n_ = size;
if (n_ > 0) a_ = new T[N * n_];
else a_ = nullptr;
}
Array2D(int size, const T& val) {
n_ = size;
if (n_ > 0) {
a_ = new T[N * n_];
std::fill_n(a_, N * n_, val);
} else {
a_ = nullptr;
}
}
// AoS to SoA constructor
Array2D(const T* aos_data, int size) {
n_ = size;
if (n_ > 0) {
a_ = new T[N * n_];
if (aos_data != nullptr) {
#ifdef SOA
#ifndef ACC
#pragma omp parallel for collapse(2) if(n_ > 10000)
#endif
#pragma acc parallel loop gang vector collapse(2)
for (int i = 0; i < n_; ++i) {
for (int d = 0; d < N; ++d) {
// AoS(i, d) -> SoA(i, d)
a_[d * n_ + i] = aos_data[i * N + d];
}
}
#else
// If data is already in AoS layout, we can copy it directly
std::memcpy(a_, aos_data, sizeof(T) * N * n_);
#endif
} else {
std::fill_n(a_, N * n_, T(0));
}
} else {
a_ = nullptr;
}
}
Array2D(const Array2D& src) {
n_ = src.size();
if (n_ > 0) {
a_ = new T[N * n_];
std::memcpy(a_, src.data(), sizeof(T) * N * n_);
} else {
a_ = nullptr;
}
}
~Array2D() { if (a_) delete [] a_; }
//
// methods
//
T* data() {return a_;}
const T* data() const {return a_;}
std::size_t size() const {return n_;}
int num_elements() const {return N*n_;}
void resize(int size, bool preserve_data = true) {
if (size == n_) return;
T* new_a = nullptr;
if (size > 0) new_a = new T[N * size];
if (preserve_data && a_ != nullptr && size > 0) {
int copy_count = (size < n_) ? size : n_;
#ifdef SOA
// SoA Resize: move each dimension separately
for (int d = 0; d < N; ++d) {
T* src = a_ + d * n_;
T* dst = new_a + d * size;
std::memcpy(dst, src, sizeof(T) * copy_count);
}
#else
// AoS Resize: move all data at once
std::memcpy(new_a, a_, sizeof(T) * N * copy_count);
#endif
}
if (a_) delete [] a_;
a_ = new_a;
n_ = size;
}
void resize(int size, const T& val) {
if (size != n_) {
T* new_a = nullptr;
if (size > 0) new_a = new T[N * size];
if (a_) delete [] a_;
a_ = new_a;
n_ = size;
}
if (a_) std::fill_n(a_, N * n_, val);
}
void steal_ref(Array2D& other) {
if (a_) delete [] a_;
a_ = other.a_;
n_ = other.n_;
other.a_ = nullptr;
other.n_ = 0;
}
void reset(T* a, int n) {
// Warning: this will take ownership of the pointer a, and delete it when destructed or resized
if (a_) delete [] a_;
a_ = a;
n_ = n;
}
void nullify() {
a_ = nullptr;
n_ = 0;
}
//
// index accessing
//
Accessor operator[](std::size_t i) {
// pass n_ as stride
#ifdef SOA
return Accessor{ a_ + i, n_ };
#else
return Accessor{ a_ + i*N, 1 };
#endif
}
ConstAccessor operator[](std::size_t i) const {
#ifdef SOA
return ConstAccessor{ a_ + i, n_ };
#else
return ConstAccessor{ a_ + i*N, 1 };
#endif
}
Accessor at(std::size_t i) {
#ifdef SOA
return Accessor{ a_ + i, n_ };
#else
return Accessor{ a_ + i*N, 1 };
#endif
}
ConstAccessor at(std::size_t i) const {
#ifdef SOA
return ConstAccessor{ a_ + i, n_ };
#else
return ConstAccessor{ a_ + i*N, 1 };
#endif
}
//
// iterators
//
typedef T* iterator;
typedef const T* const_iterator;
iterator begin() {return a_;}
const_iterator begin() const {return a_;}
iterator end() {return a_ + N*n_;}
const_iterator end() const {return a_ + N*n_;}
typedef T element;
private:
// disable assignment operator
Array2D<T,N>& operator=(const Array2D<T,N>& rhs);
};
#endif