OpenFPM_pdata  4.1.0
Project that contain the implementation of distributed structures
Memory_unit_tests.hpp
1 /*
2  * HeapMemory_unit_tests.hpp
3  *
4  * Created on: Jul 9, 2015
5  * Author: i-bird
6  */
7 
8 #ifndef HEAPMEMORY_UNIT_TESTS_HPP_
9 #define HEAPMEMORY_UNIT_TESTS_HPP_
10 
11 #include "config.h"
12 
13 #include "memory/HeapMemory.hpp"
14 #include "memory/BHeapMemory.hpp"
15 #ifdef CUDA_GPU
16 #include "memory/CudaMemory.cuh"
17 #endif
18 
19 BOOST_AUTO_TEST_SUITE( HeapMemory_test )
20 
21 #define FIRST_ALLOCATION 1024ul
23 #define SECOND_ALLOCATION 4096ul
24 
26 template<typename T> void test()
27 {
29  T mem;
30 
31  BOOST_REQUIRE_EQUAL(mem.size(),0ul);
32 
33  mem.allocate(FIRST_ALLOCATION);
34 
35  BOOST_REQUIRE_EQUAL(mem.size(),FIRST_ALLOCATION);
36 
37  // get the pointer of the allocated memory and fill
38 
39  unsigned char * ptr = (unsigned char *)mem.getPointer();
40  for (size_t i = 0 ; i < mem.size() ; i++)
41  {ptr[i] = i;}
42 
43  mem.hostToDevice();
44 
46 
48  mem.resize(SECOND_ALLOCATION);
49 
50  unsigned char * ptr2 = (unsigned char *)mem.getPointer();
51 
52  BOOST_REQUIRE_EQUAL(mem.size(),SECOND_ALLOCATION);
53  BOOST_REQUIRE_EQUAL(mem.isInitialized(),false);
54 
56 
57  // check that the data are retained
58  for (size_t i = 0 ; i < FIRST_ALLOCATION ; i++)
59  {
60  unsigned char c = i;
61  BOOST_REQUIRE_EQUAL(ptr2[i],c);
62  }
63 
65 
66  mem.resize(1);
67  BOOST_REQUIRE_EQUAL(mem.size(),SECOND_ALLOCATION);
68 
70 
71 
72  {
74  T src;
75  T dst;
76 
77  src.allocate(FIRST_ALLOCATION);
78  dst.allocate(SECOND_ALLOCATION);
79 
80  unsigned char * ptr = (unsigned char *)src.getPointer();
81  for (size_t i = 0 ; i < src.size() ; i++)
82  {ptr[i] = i;}
83 
84  src.hostToDevice();
85  dst.copy(src);
86  dst.deviceToHost();
87 
88  for (size_t i = 0 ; i < FIRST_ALLOCATION ; i++)
89  {
90  unsigned char c=i;
91  BOOST_REQUIRE_EQUAL(ptr2[i],c);
92  }
93 
95  }
96 
97  {
98  T src;
99  src.allocate(FIRST_ALLOCATION);
100 
101  unsigned char * ptr = (unsigned char *)src.getPointer();
102  for (size_t i = 0 ; i < src.size() ; i++)
103  {ptr[i] = i;}
104 
105  src.hostToDevice();
106  T dst = src;
107  dst.deviceToHost();
108 
109  unsigned char * ptr2 = (unsigned char *)dst.getPointer();
110 
111  BOOST_REQUIRE(src.getPointer() != dst.getPointer());
112  for (size_t i = 0 ; i < FIRST_ALLOCATION ; i++)
113  {
114  unsigned char c=i;
115  BOOST_REQUIRE_EQUAL(ptr2[i],c);
116  }
117 
118  mem.destroy();
119 
120  BOOST_REQUIRE_EQUAL(mem.size(),0ul);
121 
122  mem.allocate(FIRST_ALLOCATION);
123 
124  BOOST_REQUIRE_EQUAL(mem.size(),FIRST_ALLOCATION);
125 
126  }
127 }
128 
129 template<typename T> void Btest()
130 {
132  T mem;
133 
134  mem.allocate(FIRST_ALLOCATION);
135 
136  BOOST_REQUIRE_EQUAL(mem.size(),FIRST_ALLOCATION);
137 
138  // get the pointer of the allocated memory and fill
139 
140  unsigned char * ptr = (unsigned char *)mem.getPointer();
141  for (size_t i = 0 ; i < mem.size() ; i++)
142  {ptr[i] = i;}
143  mem.hostToDevice();
144 
145  mem.resize(0);
146  BOOST_REQUIRE_EQUAL(mem.size(),0);
147  BOOST_REQUIRE_EQUAL(mem.msize(),FIRST_ALLOCATION);
148 
150 
152  mem.resize(SECOND_ALLOCATION);
153 
154  unsigned char * ptr2 = (unsigned char *)mem.getPointer();
155 
156  BOOST_REQUIRE_EQUAL(mem.size(),SECOND_ALLOCATION);
157  BOOST_REQUIRE_EQUAL(mem.isInitialized(),false);
158 
160 
161  // check that the data are retained
162  for (size_t i = 0 ; i < FIRST_ALLOCATION ; i++)
163  {
164  unsigned char c = i;
165  BOOST_REQUIRE_EQUAL(ptr2[i],c);
166  }
167 
168  // check that the data are retained on device
169 
170  mem.deviceToHost();
171  for (size_t i = 0 ; i < FIRST_ALLOCATION ; i++)
172  {
173  unsigned char c = i;
174  BOOST_REQUIRE_EQUAL(ptr2[i],c);
175  }
176 
178 
179  mem.resize(1);
180  BOOST_REQUIRE_EQUAL(mem.size(),1ul);
181 
183 
184  mem.destroy();
185 
186  BOOST_REQUIRE_EQUAL(mem.size(),0ul);
187 
188  mem.allocate(FIRST_ALLOCATION);
189 
190  BOOST_REQUIRE_EQUAL(mem.size(),FIRST_ALLOCATION);
191 }
192 
193 
194 template<typename T> void Stest()
195 {
196  T mem1;
197  T mem2;
198 
199  mem1.allocate(5*sizeof(size_t));
200  mem2.allocate(6*sizeof(size_t));
201 
202  BOOST_REQUIRE_EQUAL(mem1.size(),5*sizeof(size_t));
203  BOOST_REQUIRE_EQUAL(mem2.size(),6*sizeof(size_t));
204 
205  // get the pointer of the allocated memory and fill
206 
207  size_t * ptr1 = (size_t *)mem1.getPointer();
208  size_t * ptr2 = (size_t *)mem2.getPointer();
209  for (size_t i = 0 ; i < 5 ; i++)
210  ptr1[i] = i;
211 
212  for (size_t i = 0 ; i < 6 ; i++)
213  ptr2[i] = i+100;
214 
215  mem1.swap(mem2);
216 
217  bool ret = true;
218  ptr1 = (size_t *)mem2.getPointer();
219  ptr2 = (size_t *)mem1.getPointer();
220  for (size_t i = 0 ; i < 5 ; i++)
221  ret &= ptr1[i] == i;
222 
223  for (size_t i = 0 ; i < 6 ; i++)
224  ret &= ptr2[i] == i+100;
225 
226  BOOST_REQUIRE_EQUAL(ret,true);
227 
228  BOOST_REQUIRE_EQUAL(mem1.size(),6*sizeof(size_t));
229  BOOST_REQUIRE_EQUAL(mem2.size(),5*sizeof(size_t));
230 }
231 
232 template<typename Mem>
233 void copy_device_host_test()
234 {
235  Mem mem1;
236  Mem mem2;
237 
238  mem1.allocate(300);
239  mem2.allocate(500);
240 
241  if (mem1.isDeviceHostSame() == false)
242  {
243  BOOST_REQUIRE(mem1.getPointer() != mem2.getDevicePointer());
244  }
245 
246  unsigned char * ptr1 = (unsigned char *)mem1.getPointer();
247  unsigned char * ptr2 = (unsigned char *)mem2.getPointer();
248 
249  for (size_t i = 0 ; i < mem1.size() ; i++)
250  {ptr1[i] = i;}
251 
252  // force to move the memory from host to device
253  mem1.hostToDevice();
254  mem2.copyDeviceToDevice(mem1);
255  mem2.deviceToHost();
256 
257  for (size_t i = 0 ; i < mem1.size() ; i++)
258  {BOOST_REQUIRE_EQUAL(ptr1[i],ptr2[i]);}
259 }
260 
261 BOOST_AUTO_TEST_CASE( use_heap_memory )
262 {
263  test<HeapMemory>();
264 #ifdef CUDA_GPU
265  test<CudaMemory>();
266 #endif
267 }
268 
269 BOOST_AUTO_TEST_CASE( use_memory )
270 {
271  test<HeapMemory>();
272 #ifdef CUDA_GPU
273  test<CudaMemory>();
274 #endif
275 }
276 
277 BOOST_AUTO_TEST_CASE( use_bheap_memory )
278 {
279  Btest<BMemory<HeapMemory>>();
280 }
281 
282 BOOST_AUTO_TEST_CASE( use_bcuda_memory )
283 {
284 #ifdef CUDA_GPU
285  Btest<BMemory<CudaMemory>>();
286 #endif
287 }
288 
289 BOOST_AUTO_TEST_CASE( swap_heap_memory )
290 {
291  Stest<HeapMemory>();
292 }
293 
294 BOOST_AUTO_TEST_CASE( copy_device_host_test_use )
295 {
296  copy_device_host_test<HeapMemory>();
297 #ifdef CUDA_GPU
298  copy_device_host_test<CudaMemory>();
299 #endif
300 }
301 
302 BOOST_AUTO_TEST_SUITE_END()
303 
304 
305 #endif /* HEAPMEMORY_UNIT_TESTS_HPP_ */