OpenFPM_pdata  4.1.0
Project that contain the implementation of distributed structures
 
Loading...
Searching...
No Matches
copy_grid_unit_test.cpp
1/*
2 * copy_grid_unit_test.hpp
3 *
4 * Created on: Dec 4, 2017
5 * Author: i-bird
6 */
7
8#define BOOST_TEST_DYN_LINK
9#include <boost/test/unit_test.hpp>
10#include "Grid/map_grid.hpp"
11#include "data_type/aggregate.hpp"
12#include "Vector/map_vector.hpp"
13#include "Point_test.hpp"
14
15BOOST_AUTO_TEST_SUITE( copy_grid_test )
16
17template<unsigned int dim, typename grid>
18void Test_copy_grid(grid & g_src, grid & g_dst,
19 Box<dim,size_t> & bsrc_1, Box<dim,size_t> & bdst_1)
20{
21 auto gs1 = g_src.getGrid();
22 auto gd1 = g_dst.getGrid();
23 auto it = g_src.getIterator();
24
25 grid_key_dx<dim> zero[1];
26 zero[0].zero();
27
28 while (it.isNext())
29 {
30 auto key = it.get();
31
32 g_src.template get<0>(key) = gs1.LinId(key);
33
34 ++it;
35 }
36
37 copy_grid_fast<false,
38 dim,
40 grid_sm<dim,void >>::copy(gs1,gd1,
41 bsrc_1,bdst_1,
42 g_src,g_dst,
43 zero);
44
45 // Check
46
47 bool match = true;
48
51
52 while (its.isNext())
53 {
54 auto key_s = its.get();
55 auto key_d = itd.get();
56
57 match &= g_src.template get<0>(key_s) == g_dst.template get<0>(key_d);
58
59 ++its;
60 ++itd;
61 }
62
63 BOOST_REQUIRE_EQUAL(match,true);
64}
65
66template<unsigned int dim, typename grid>
67void Test_copy_grid_cmp(grid & g_src, grid & g_dst,
68 Box<dim,size_t> & bsrc_1, Box<dim,size_t> & bdst_1)
69{
70 auto gs1 = g_src.getGrid();
71 auto gd1 = g_dst.getGrid();
72 auto it = g_src.getIterator();
73
74 grid_key_dx<dim> zero[1];
75 zero[0].zero();
76
77 while (it.isNext())
78 {
79 auto key = it.get();
80
81 for (size_t k = 0 ; k < (size_t)gs1.LinId(key) % 4 ; k++)
82 {
83 g_src.template get<0>(key).add(gs1.LinId(key) + 1);
84 }
85
86 ++it;
87 }
88
89 copy_grid_fast<true,
90 dim,
92 grid_sm<dim,void>>::copy(gs1,gd1,
93 bsrc_1,bdst_1,
94 g_src,g_dst,
95 zero);
96
97 // Check
98
99 bool match = true;
100
101 grid_key_dx_iterator_sub<dim, no_stencil> its(gs1,bsrc_1.getKP1(), bsrc_1.getKP2());
102 grid_key_dx_iterator_sub<dim, no_stencil> itd(gd1,bdst_1.getKP1(), bdst_1.getKP2());
103
104 while (its.isNext())
105 {
106 auto key_s = its.get();
107 auto key_d = itd.get();
108
109 match &= g_src.template get<0>(key_s).size() == g_dst.template get<0>(key_d).size();
110
111 for (size_t i = 0 ; i < g_dst.template get<0>(key_d).size() ; i++)
112 {
113 match &= g_src.template get<0>(key_s).get(i) == g_dst.template get<0>(key_d).get(i);
114 }
115
116 ++its;
117 ++itd;
118 }
119
120 BOOST_REQUIRE_EQUAL(match,true);
121}
122
123BOOST_AUTO_TEST_CASE( copy_grid_test_use)
124{
125 {
126 size_t sz1[1] = {37};
127 size_t sz2[2] = {37,37};
128 size_t sz3[3] = {37,37,37};
129 size_t sz4[4] = {37,37,37,37};
130
131 grid_cpu<1,aggregate<double>> g1_src(sz1);
132 grid_cpu<1,aggregate<double>> g1_dst(sz1);
133 grid_cpu<2,aggregate<double>> g2_src(sz2);
134 grid_cpu<2,aggregate<double>> g2_dst(sz2);
135 grid_cpu<3,aggregate<double>> g3_src(sz3);
136 grid_cpu<3,aggregate<double>> g3_dst(sz3);
137 grid_cpu<4,aggregate<double>> g4_src(sz4);
138 grid_cpu<4,aggregate<double>> g4_dst(sz4);
139 g1_src.setMemory();
140 g1_dst.setMemory();
141 g2_src.setMemory();
142 g2_dst.setMemory();
143 g3_src.setMemory();
144 g3_dst.setMemory();
145 g4_src.setMemory();
146 g4_dst.setMemory();
147
148 // fill all grids
149
150 Box<1,size_t> bsrc_1({4},{11});
151 Box<1,size_t> bdst_1{{20},{27}};
152
153 Test_copy_grid(g1_src,g1_dst,bsrc_1,bdst_1);
154
155 Box<2,size_t> bsrc_2({4,7},{11,20});
156 Box<2,size_t> bdst_2({20,5},{27,18});
157
158 Test_copy_grid(g2_src,g2_dst,bsrc_2,bdst_2);
159
160 Box<3,size_t> bsrc_3({4,7,1},{11,20,6});
161 Box<3,size_t> bdst_3({20,5,10},{27,18,15});
162
163 Test_copy_grid(g3_src,g3_dst,bsrc_3,bdst_3);
164
165#ifdef SE_CLASS1
166 Box<4,size_t> bsrc_4({4,7,1,3},{11,20,6,6});
167 Box<4,size_t> bdst_4({20,5,10,13},{27,18,15,16});
168
169 Test_copy_grid(g4_src,g4_dst,bsrc_4,bdst_4);
170#else
171 Box<4,size_t> bsrc_4({4,7,1,3},{11,20,6,6});
172 Box<4,size_t> bdst_4({20,5,10,13},{27,18,15,16});
173
174 Test_copy_grid(g4_src,g4_dst,bsrc_4,bdst_4);
175#endif
176 }
178
179 {
180 size_t sz1[1] = {37};
181 size_t sz2[2] = {37,37};
182 size_t sz3[3] = {37,37,37};
183 size_t sz4[4] = {37,37,37,37};
184
193 g1_src.setMemory();
194 g1_dst.setMemory();
195 g2_src.setMemory();
196 g2_dst.setMemory();
197 g3_src.setMemory();
198 g3_dst.setMemory();
199 g4_src.setMemory();
200 g4_dst.setMemory();
201
202 // fill all grids
203
204 Box<1,size_t> bsrc_1({4},{11});
205 Box<1,size_t> bdst_1{{20},{27}};
206
207 Test_copy_grid_cmp(g1_src,g1_dst,bsrc_1,bdst_1);
208
209 Box<2,size_t> bsrc_2({4,7},{11,20});
210 Box<2,size_t> bdst_2({20,5},{27,18});
211
212 Test_copy_grid_cmp(g2_src,g2_dst,bsrc_2,bdst_2);
213
214 Box<3,size_t> bsrc_3({4,7,1},{11,20,6});
215 Box<3,size_t> bdst_3({20,5,10},{27,18,15});
216
217 Test_copy_grid_cmp(g3_src,g3_dst,bsrc_3,bdst_3);
218
219#ifdef SE_CLASS1
220 Box<4,size_t> bsrc_4({4,7,1,3},{7,14,6,6});
221 Box<4,size_t> bdst_4({20,5,10,13},{23,12,15,16});
222
223 Test_copy_grid_cmp(g4_src,g4_dst,bsrc_4,bdst_4);
224#else
225 Box<4,size_t> bsrc_4({4,7,1,3},{11,20,6,6});
226 Box<4,size_t> bdst_4({20,5,10,13},{27,18,15,16});
227
228 Test_copy_grid_cmp(g4_src,g4_dst,bsrc_4,bdst_4);
229#endif
230 }
231}
232
233BOOST_AUTO_TEST_CASE( copy_grid_test_invalid)
234{
235 {
236 size_t sz2_dst[2] = {3,7};
237 size_t sz2_src[2] = {3,4};
238
239 grid_cpu<2,Point_test<double>> g2_src(sz2_src);
240 grid_cpu<2,Point_test<double>> g2_dst(sz2_dst);
241 g2_src.setMemory();
242 g2_dst.setMemory();
243
244 Box<2,long int> bsrc_2({2,1},{1,1});
245 Box<2,long int> bdst_2({0,1},{-1,1});
246
247 auto it = g2_src.getIterator();
248
249 while (it.isNext())
250 {
251 auto key = it.get();
252
253 g2_src.template get<0>(key) = 0.0;
254
255 ++it;
256 }
257
258 auto it3 = g2_dst.getIterator();
259
260 while (it3.isNext())
261 {
262 auto key = it3.get();
263
264 g2_dst.template get<0>(key) = 1.0;
265
266 ++it3;
267 }
268
269 g2_dst.copy_to(g2_src,bsrc_2,bdst_2);
270
271 auto it2 = g2_dst.getIterator();
272
273 while (it2.isNext())
274 {
275 auto key = it2.get();
276
277 BOOST_REQUIRE_EQUAL(g2_dst.template get<0>(key),1.0);
278
279 ++it2;
280 }
281
282 }
283}
284
285BOOST_AUTO_TEST_SUITE_END()
286
287
This class represent an N-dimensional box.
Definition Box.hpp:61
grid_key_dx< dim > getKP2() const
Get the point p12 as grid_key_dx.
Definition Box.hpp:669
grid_key_dx< dim > getKP1() const
Get the point p1 as grid_key_dx.
Definition Box.hpp:656
Declaration grid_key_dx_iterator_sub.
grid_key_dx is the key to access any element in the grid
Definition grid_key.hpp:19
void zero()
Set to zero the key.
Definition grid_key.hpp:170
Declaration grid_sm.
Definition grid_sm.hpp:167
This is a way to quickly copy a grid into another grid.