OpenFPM_pdata  4.1.0
Project that contain the implementation of distributed structures
 
Loading...
Searching...
No Matches
grid_test.hpp
1#include <iostream>
2#include <boost/shared_array.hpp>
3#include <vector>
4#include "memory_cpu.hpp"
5#include "memory_gpu.hpp"
6#include <initializer_list>
7#include <array>
8
9#define HARDWARE 1
10
11template<typename T, unsigned int s1, unsigned int s2, unsigned int s3>
12class tensor
13{
14 T g_mem[s1][s2][s3];
15
16public:
17 tensor() {};
18 ~tensor() {};
19
20 size_t size()
21 {
22 return s1*s2*s3*g_mem[0][0][0].size();
23 };
24};
25
26
27template<unsigned int dim>
28class grid_key_dx
29{
30public:
31
33 {
34 }
35
36 template<typename a, typename ...T>grid_key_dx(a v,T...t)
37 {
38 k[dim-1] = v;
39 invert_assign(t...);
40 }
41
42 template<typename a, typename ...T>void set(a v, T...t)
43 {
44 k[dim-1] = v;
45 invert_assign(t...);
46 }
47
48 mem_id k[dim];
49
50private:
51
52 template<typename a, typename ...T>void invert_assign(a v,T...t)
53 {
54 k[sizeof...(T)] = v;
55 invert_assign(t...);
56 }
57
58 template<typename a, typename ...T>void invert_assign(a v)
59 {
60 k[0] = v;
61 }
62
63};
64
65template<unsigned int dim, unsigned int p>
66class grid_key_d
67{
68public:
69
70
71 template<typename a, typename ...T>grid_key_d(a v,T...t)
72 {
73 k[dim-1] = v;
74 invert_assign(t...);
75 }
76
77 template<typename a, typename ...T>void invert_assign(a v,T...t)
78 {
79 k[sizeof...(T)] = v;
80 invert_assign(t...);
81 }
82
83 template<typename a, typename ...T>void invert_assign(a v)
84 {
85 k[0] = v;
86 }
87
88 mem_id k[dim];
89};
90
91template<unsigned int p>
93{
94public:
95
96 grid_key_1(int i_)
97 {
98 k[0] = i_;
99 }
100
101 mem_id k[1];
102};
103
104template<unsigned int p>
106{
107public:
108
109 grid_key_2(int i_, int j_)
110 {
111 k[0] = j_;
112 k[1] = i_;
113 }
114
115 mem_id k[2];
116};
117
118template<unsigned int p>
120{
121public:
122
123 grid_key_c3(mem_id i_, mem_id j_, mem_id k_)
124 :k{{k_,j_,i_}}
125 {
126 }
127
128 const std::array<mem_id,3> k;
129};
130
131template<unsigned int p>
133{
134public:
135
136 grid_key_3(mem_id i_, mem_id j_, mem_id k_)
137 {
138 k[0] = k_;
139 k[1] = j_;
140 k[2] = i_;
141 }
142
143 mem_id k[3];
144};
145
146template<unsigned int p>
148{
149public:
150
151 grid_key_4(mem_id u_, mem_id i_, mem_id j_, mem_id k_)
152 {
153 k[0] = k_;
154 k[1] = j_;
155 k[2] = i_;
156 k[3] = u_;
157 }
158
159 mem_id k[4];
160};
161
162template<unsigned int p>
164{
165public:
166
167 template<unsigned int s>grid_key(grid_key<s> & key)
168 {
169 id = key.id;
170 }
171
172 grid_key(size_t sz)
173 :id(new mem_id[sz])
174 {
175 }
176
177 void set_d(size_t id_, mem_id val)
178 {
179 id[id_] = val;
180 }
181
182 void set(mem_id val1)
183 {
184 id[0] = val1;
185 }
186
187 void set(mem_id val1, mem_id val2)
188 {
189 id[0] = val2;
190 id[1] = val1;
191 }
192
193 void set(mem_id val1, mem_id val2, mem_id val3)
194 {
195 id[0] = val3;
196 id[1] = val2;
197 id[2] = val1;
198 }
199
200 void set(mem_id val1, mem_id val2, mem_id val3, mem_id val4)
201 {
202 id[0] = val4;
203 id[1] = val3;
204 id[2] = val2;
205 id[3] = val1;
206 }
207
208 mem_id * getId()
209 {
210 return id.get();
211 }
212
213 boost::shared_array<mem_id> id;
214};
215
216#pragma openfpm layout
217template<typename T>
218class grid
219{
220 size_t size_tot;
221 std::vector<size_t> sz;
222 std::vector<size_t> sz_s;
223
224public:
225
226 // Static element to calculate total size
227
228 size_t totalSize(std::vector<size_t> & sz)
229 {
230 size_t tSz = 1;
231
232 for (size_t i = 0 ; i < sz.size() ; i++)
233 {
234 tSz *= sz[i];
235 }
236
237 return tSz;
238 }
239
240 grid(std::vector<size_t> & sz)
241 : size_tot(totalSize(sz)), sz(sz)
242 {
243 sz_s.resize(sz.size());
244 sz_s[0] = sz[0];
245 for (size_t i = 1 ; i < sz.size() ; i++)
246 {
247 sz_s[i] = sz[i]*sz_s[i-1];
248 }
249 };
250
251 #pragma openfpm layout(get)
252 template<unsigned int dim> mem_id LinId(grid_key_dx<dim> & gk)
253 {
254 mem_id lid = gk.k[0];
255 for (mem_id i = 1 ; i < dim ; i++)
256 {
257 lid += gk.k[i] * sz_s[i-1];
258 }
259
260 return lid;
261 }
262
263 #pragma openfpm layout(get)
264 template<unsigned int p> mem_id LinId(grid_key_3<p> & gk)
265 {
266 mem_id lid = gk.k[0];
267 lid += gk.k[1]*sz_s[0];
268 lid += gk.k[2]*sz_s[1];
269
270 return lid;
271 }
272
273 #pragma openfpm layout(get)
274 template<unsigned int dim, unsigned int p> mem_id LinId(grid_key_d<dim,p> & gk)
275 {
276 mem_id lid = gk.k[0];
277 for (mem_id i = 1 ; i < dim ; i++)
278 {
279 lid += gk.k[i] * sz_s[i-1];
280 }
281
282 return lid;
283 }
284
285 #pragma openfpm layout(get)
286 mem_id LinId(mem_id * id)
287 {
288 mem_id lid = 0;
289 lid += id[0];
290 for (mem_id i = 1 ; i < (mem_id)sz.size() ; i++)
291 {
292 lid += id[i] * sz_s[i-1];
293 }
294
295 return lid;
296 }
297
298 #pragma openfpm layout(get)
299 mem_id LinId(mem_id i)
300 {
301 return i;
302 }
303
304 #pragma openfpm layout(get)
305 mem_id LinId(mem_id j, mem_id k)
306 {
307 mem_id lid = 0;
308 lid += k;
309 lid += j*sz_s[0];
310
311 return lid;
312 }
313
314 #pragma openfpm layout(get)
315 mem_id LinId(mem_id i, mem_id j, mem_id k)
316 {
317 mem_id lid = 0;
318 lid += k;
319 lid += j*sz_s[0];
320 lid += i*sz_s[1];
321
322 return lid;
323 }
324
325 #pragma openfpm layout(get)
326 mem_id LinId(mem_id u, mem_id i, mem_id j, mem_id k)
327 {
328 mem_id lid = 0;
329 lid += k;
330 lid += j*sz_s[0];
331 lid += i*sz_s[1];
332 lid += u*sz_s[2];
333
334 return lid;
335 }
336
337 ~grid() {};
338
339 #pragma openfpm layout(size)
340 size_t size()
341 {
342 return size_tot;
343 };
344};
345
346template<unsigned int s1, unsigned int s2, unsigned int s3>
347class tensor<int,s1,s2,s3>
348{
349
350public:
351 size_t size()
352 {
353 return s1*s2*s3;
354 }
355};
356
357
358
359
360
361
grid_key_d is the key to access any element in the grid
Definition grid_key.hpp:575
grid_key_dx is the key to access any element in the grid
Definition grid_key.hpp:19
__device__ __host__ grid_key_dx()
Constructor.
Definition grid_key.hpp:35
__device__ __host__ void invert_assign()
Constructor.
Definition grid_key.hpp:557
index_type k[dim]
structure that store all the index
Definition grid_key.hpp:530
__device__ __host__ void set(a v, T...t)
set the Key from a list of numbers
Definition grid_key.hpp:400
Definition ids.hpp:169