8#ifndef MAP_VECTOR_SPARSE_CUDA_KER_UNIT_TESTS_CUH_
9#define MAP_VECTOR_SPARSE_CUDA_KER_UNIT_TESTS_CUH_
11#define BOOST_TEST_DYN_LINK
12#include <boost/test/unit_test.hpp>
13#include "Vector/map_vector_sparse.hpp"
14#include "Vector/map_vector.hpp"
17template<
typename vd_type>
18__global__
void test_insert_sparse(vd_type vd_insert)
22 int p = blockIdx.x*blockDim.x + threadIdx.x;
26 auto ie = vd_insert.insert(10000 - p);
27 ie.template get<0>() = p + 100;
28 ie.template get<1>() = p + 10100;
29 ie.template get<2>() = p + 20100;
30 vd_insert.flush_block_insert();
52template<
typename vd_type>
53__global__
void test_remove_sparse(vd_type vd_insert)
57 int p = blockIdx.x*blockDim.x + threadIdx.x;
61 vd_insert.remove(10000 - p);
62 vd_insert.flush_block_remove();
65template<
typename vd_type>
66__global__
void test_insert_sparse2(vd_type vd_insert)
70 int p = blockIdx.x*blockDim.x + threadIdx.x;
74 auto ie = vd_insert.insert(9000 - p);
75 ie.template get<0>() = p + 3000;
76 ie.template get<1>() = p + 13000;
77 ie.template get<2>() = p + 23000;
79 vd_insert.flush_block_insert();
82template<
typename vd_type>
83__global__
void test_remove_sparse2(vd_type vd_insert)
87 int p = blockIdx.x*blockDim.x + threadIdx.x;
91 vd_insert.remove(9000 - p);
93 vd_insert.flush_block_remove();
96template<
typename vd_type>
97__global__
void test_insert_sparse2_inc(vd_type vd_insert)
99 vd_insert.init_ins_inc();
101 int p = blockIdx.x*blockDim.x + threadIdx.x;
105 auto ie = vd_insert.insert(9000 - p);
106 ie.template get<0>() = p + 3000;
107 ie.template get<1>() = p + 13000;
108 ie.template get<2>() = p + 23000;
110 vd_insert.flush_block_insert();
113template<
typename vd_type>
114__global__
void test_remove_sparse2_inc(vd_type vd_insert)
116 vd_insert.init_rem_inc();
118 int p = blockIdx.x*blockDim.x + threadIdx.x;
122 vd_insert.remove(9000 - p);
124 vd_insert.flush_block_remove();
127template<
typename vd_type>
128__global__
void test_insert_sparse3(vd_type vd_insert)
132 int p = blockIdx.x*blockDim.x + threadIdx.x;
136 auto ie = vd_insert.insert(p);
137 ie.template get<0>() = 5;
138 ie.template get<1>() = 1;
139 ie.template get<2>() = 1;
141 vd_insert.flush_block_insert();
144template<
typename vd_type>
145__global__
void test_remove_sparse3(vd_type vd_insert)
149 int p = blockIdx.x*blockDim.x + threadIdx.x;
155 vd_insert.flush_block_remove();
158template<
typename vd_sparse_type,
typename vector_out_type>
159__global__
void test_sparse_get_test(vd_sparse_type vd_test, vector_out_type output)
161 int p = blockIdx.x*blockDim.x + threadIdx.x;
162 int i = blockIdx.x*blockDim.x + threadIdx.x;
168 output.template get<0>(i) = vd_test.template get<0>(10000 - p,v);
169 output.template get<1>(i) = vd_test.template get_ele<1>(v);
170 output.template get<2>(i) = vd_test.template get_ele<2>(v);
190BOOST_AUTO_TEST_SUITE( vector_cuda_sparse )
192BOOST_AUTO_TEST_CASE( vector_sparse_cuda_gpu )
196 vs.template setBackground<0>(17);
198 vs.template setBackground<1>(18);
200 vs.template setBackground<2>(19);
205 CUDA_LAUNCH_DIM3(test_insert_sparse,10,100,vs.
toKernel());
211 CUDA_LAUNCH_DIM3(test_insert_sparse2,10,100,vs.
toKernel());
216 CUDA_LAUNCH_DIM3(test_insert_sparse3,4000,256,vs.
toKernel());
224 CUDA_LAUNCH_DIM3(test_sparse_get_test,10,150,vs.
toKernel(),output.toKernel());
226 output.template deviceToHost<0,1,2>();
227 vs.template deviceToHost<0,1,2>();
230 for (
size_t i = 0 ; i < output.size() ; i++)
232 match &= output.template get<0>(i) == vs.template get<0>(10000 - 2*i);
233 match &= output.template get<1>(i) == vs.template get<1>(10000 - 2*i);
234 match &= output.template get<2>(i) == vs.template get<2>(10000 - 2*i);
237 BOOST_REQUIRE_EQUAL(match,
true);
241 CUDA_LAUNCH_DIM3(test_sparse_get_test,10,150,vs.
toKernel(),output.toKernel());
243 output.template deviceToHost<0,1,2>();
244 vs.template deviceToHost<0,1,2>();
247 for (
size_t i = 0 ; i < output.size() ; i++)
249 match &= output.template get<0>(i) == 17;
250 match &= output.template get<1>(i) == 18;
251 match &= output.template get<2>(i) == 19;
254 if (match ==
false){
break;}
257 BOOST_REQUIRE_EQUAL(match,
true);
261BOOST_AUTO_TEST_CASE( vector_sparse_cuda_gpu_incremental_add )
265 vs.template setBackground<0>(17);
267 vs.template setBackground<1>(18);
269 vs.template setBackground<2>(19);
277 CUDA_LAUNCH_DIM3(test_insert_sparse,10,100,aaa);
284 CUDA_LAUNCH_DIM3(test_insert_sparse2_inc,10,100,vs.
toKernel());
285 CUDA_LAUNCH_DIM3(test_insert_sparse2_inc,10,100,vs.
toKernel());
286 CUDA_LAUNCH_DIM3(test_insert_sparse2_inc,10,100,vs.
toKernel());
292 vs.template deviceToHost<0,1,2>();
294 BOOST_REQUIRE_EQUAL(vs.
size(),1500);
297 for (
size_t i = 500 ; i < 1000 ; i++)
299 match &= vs.template get<0>(9000 - 2*i) == 3*(2*i + 3000);
300 match &= vs.template get<1>(9000 - 2*i) == 2*i + 13000;
301 match &= vs.template get<2>(9000 - 2*i) == 2*i + 23000;
305 std::cout << i <<
" " << vs.template get<0>(9000 - 2*i) <<
"!=" << 3*(2*i + 3000) <<
" " << vs.template get<1>(9000 - 2*i) <<
"!=" << 2*i + 13000 <<
" " << vs.template get<2>(9000 - 2*i) <<
"!=" << 2*i + 23000 << std::endl;
310 for (
size_t i = 0 ; i < 500 ; i++)
312 match &= vs.template get<0>(9000 - 2*i) == 3*(2*i + 3000) + 2*i + 1100;
313 match &= vs.template get<1>(9000 - 2*i) == 2*i + 11100;
314 match &= vs.template get<2>(9000 - 2*i) == 2*i + 23000;
318 std::cout << i <<
" " << vs.template get<0>(9000 - 2*i) <<
"!=" << 3*(2*i + 3000) <<
" " << vs.template get<1>(9000 - 2*i) <<
"!=" << 2*i + 13000 <<
" " << vs.template get<2>(9000 - 2*i) <<
"!=" << 2*i + 23000 << std::endl;
323 for (
size_t i = 0 ; i < 500 ; i++)
325 match &= vs.template get<0>(10000 - 2*i) == 2*i + 100;
326 match &= vs.template get<1>(10000 - 2*i) == 2*i + 10100;
327 match &= vs.template get<2>(10000 - 2*i) == 2*i + 20100;
331 std::cout << i <<
" " << vs.template get<0>(9000 - 2*i) <<
"!=" << 3*(2*i + 3000) <<
" " << vs.template get<1>(9000 - 2*i) <<
"!=" << 2*i + 13000 <<
" " << vs.template get<2>(9000 - 2*i) <<
"!=" << 2*i + 23000 << std::endl;
336 BOOST_REQUIRE_EQUAL(match,
true);
339BOOST_AUTO_TEST_CASE( vector_sparse_cuda_gpu_get )
343 vs.template setBackground<0>(0);
345 vs.template setBackground<1>(0);
347 vs.template setBackground<2>(0);
352 CUDA_LAUNCH_DIM3(test_insert_sparse,10,100,vs.
toKernel());
358 vs.template deviceToHost<0,1,2>();
361 for (
size_t i = 0 ; i < 1000 ; i++)
363 match &= vs.template get<0>(10000 - 2*i) == 2*i + 100;
364 match &= vs.template get<1>(10000 - 2*i) == 2*i + 10100;
365 match &= vs.template get<2>(10000 - 2*i) == 2*i + 20100;
368 BOOST_REQUIRE_EQUAL(match,
true);
371 CUDA_LAUNCH_DIM3(test_insert_sparse2,10,100,vs.
toKernel());
375 vs.template deviceToHost<0,1,2>();
377 BOOST_REQUIRE_EQUAL(vs.
size(),1500);
380 for (
size_t i = 500 ; i < 1000 ; i++)
382 match &= vs.template get<0>(9000 - 2*i) == 2*i + 3000;
383 match &= vs.template get<1>(9000 - 2*i) == 2*i + 13000;
384 match &= vs.template get<2>(9000 - 2*i) == 2*i + 23000;
387 for (
size_t i = 0 ; i < 500 ; i++)
389 match &= vs.template get<0>(9000 - 2*i) == 2*i + 3000 + 2*i + 1100;
390 match &= vs.template get<1>(9000 - 2*i) == 2*i + 11100;
391 match &= vs.template get<2>(9000 - 2*i) == 2*i + 23000;
395 std::cout << 0 <<
" " << vs.template get<0>(9000 - 2*i) <<
" " << 2*i + 3000 + 2*i + 1100 << std::endl;
396 std::cout << 1 <<
" " << vs.template get<1>(9000 - 2*i) <<
" " << 2*i + 11100 << std::endl;
397 std::cout << 2 <<
" " << vs.template get<2>(9000 - 2*i) <<
" " << 2*i + 23000 << std::endl;
401 for (
size_t i = 0 ; i < 500 ; i++)
403 match &= vs.template get<0>(10000 - 2*i) == 2*i + 100;
404 match &= vs.template get<1>(10000 - 2*i) == 2*i + 10100;
405 match &= vs.template get<2>(10000 - 2*i) == 2*i + 20100;
408 BOOST_REQUIRE_EQUAL(match,
true);
411 CUDA_LAUNCH_DIM3(test_insert_sparse3,4000,256,vs.
toKernel());
414 vs.template deviceToHost<0,1,2>();
416 for (
size_t i = 0 ; i <= 3500 ; i++)
418 match &= vs.template get<0>(2*i) == 5;
419 match &= vs.template get<1>(2*i) == 1;
420 match &= vs.template get<2>(2*i) == 1;
423 BOOST_REQUIRE_EQUAL(match,
true);
425 for (
size_t i = 3501 ; i <= 4000 ; i++)
427 match &= vs.template get<0>(2*i) == 5 - 2*i + 3000 + 9000;
428 match &= vs.template get<1>(2*i) == 1;
429 match &= vs.template get<2>(2*i) == 23000 + 9000 - 2*i;
433 std::cout << i <<
" " << vs.template get<0>(2*i) <<
" " << 5 - 2*i + 3000 + 9000 << std::endl;
434 std::cout << i <<
" " << vs.template get<1>(2*i) <<
" " << 1 << std::endl;
435 std::cout << i <<
" " << vs.template get<2>(2*i) <<
" " << 23000 + 9000 - 2*i << std::endl;
439 BOOST_REQUIRE_EQUAL(match,
true);
441 for (
size_t i = 4001 ; i <= 4500 ; i++)
443 match &= vs.template get<0>(2*i) == 5 - 2*i + 1100 - 2*i + 3000 + 18000;
444 match &= vs.template get<1>(2*i) == 1;
445 match &= vs.template get<2>(2*i) == 23000 + 9000 - 2*i;
448 BOOST_REQUIRE_EQUAL(match,
true);
450 for (
size_t i = 4501 ; i <= 5000 ; i++)
452 match &= vs.template get<0>(2*i) == 5 - 2*i + 1100 + 9000;
453 match &= vs.template get<1>(2*i) == 1;
454 match &= vs.template get<2>(2*i) == 21100 + 9000 - 2*i;
457 BOOST_REQUIRE_EQUAL(match,
true);
460BOOST_AUTO_TEST_CASE( vector_sparse_cuda_gpu_special_function )
464 vs.template setBackground<0>(17);
466 vs.template setBackground<1>(18);
468 vs.template setBackground<2>(19);
473 CUDA_LAUNCH_DIM3(test_insert_sparse,10,100,vs.
toKernel());
474 CUDA_LAUNCH_DIM3(test_insert_sparse2_inc,10,100,vs.
toKernel());
475 CUDA_LAUNCH_DIM3(test_insert_sparse2_inc,10,100,vs.
toKernel());
476 CUDA_LAUNCH_DIM3(test_insert_sparse2_inc,10,100,vs.
toKernel());
480 vs.
flush<sstart_<0>>(ctx,flush_type::FLUSH_ON_DEVICE);
482 vs.template deviceToHost<0>();
484 BOOST_REQUIRE_EQUAL(vs.
size(),1500);
489 for (
size_t i = 999 ; i >= 500 ; i--)
491 match &= vs.template get<0>(9000 - 2*i) == count;
495 for (
long int i = 499 ; i >= 0 ; i--)
497 match &= vs.template get<0>(9000 - 2*i) == count;
501 for (
long int i = 499 ; i >= 0 ; i--)
503 match &= vs.template get<0>(10000 - 2*i) == count;
507 BOOST_REQUIRE_EQUAL(match,
true);
520 for (
size_t i = 0 ; i <= 3500 ; i++)
524 match &= vs.template get<0>(2*i) == 5;
525 match &= vs.template get<1>(2*i) == 1;
526 match &= vs.template get<2>(2*i) == 1;
530 match &= vs.template get<0>(2*i) == 17;
531 match &= vs.template get<1>(2*i) == 18;
532 match &= vs.template get<2>(2*i) == 19;
536 BOOST_REQUIRE_EQUAL(match,
true);
538 for (
size_t i = 3501 ; i <= 4000 ; i++)
542 match &= vs.template get<0>(2*i) == 5 - 2*i + 3000 + 9000;
543 match &= vs.template get<1>(2*i) == 1;
544 match &= vs.template get<2>(2*i) == 23000 + 9000 - 2*i;
548 match &= vs.template get<0>(2*i) == 17;
549 match &= vs.template get<1>(2*i) == 18;
550 match &= vs.template get<2>(2*i) == 19;
554 BOOST_REQUIRE_EQUAL(match,
true);
556 for (
size_t i = 4001 ; i <= 4500 ; i++)
560 match &= vs.template get<0>(2*i) == 5 - 2*i + 1100 - 2*i + 3000 + 18000;
561 match &= vs.template get<1>(2*i) == 1;
562 match &= vs.template get<2>(2*i) == 23000 + 9000 - 2*i;
566 match &= vs.template get<0>(2*i) == 17;
567 match &= vs.template get<1>(2*i) == 18;
568 match &= vs.template get<2>(2*i) == 19;
572 BOOST_REQUIRE_EQUAL(match,
true);
574 for (
size_t i = 4501 ; i <= 5000 ; i++)
578 match &= vs.template get<0>(2*i) == 5 - 2*i + 1100 + 9000;
579 match &= vs.template get<1>(2*i) == 1;
580 match &= vs.template get<2>(2*i) == 21100 + 9000 - 2*i;
584 match &= vs.template get<0>(2*i) == 17;
585 match &= vs.template get<1>(2*i) == 18;
586 match &= vs.template get<2>(2*i) == 19;
592BOOST_AUTO_TEST_CASE( vector_sparse_cuda_gpu_remove )
596 vs.template setBackground<0>(17);
598 vs.template setBackground<1>(18);
600 vs.template setBackground<2>(19);
605 CUDA_LAUNCH_DIM3(test_insert_sparse,10,100,vs.
toKernel());
611 CUDA_LAUNCH_DIM3(test_insert_sparse2,10,100,vs.
toKernel());
616 CUDA_LAUNCH_DIM3(test_insert_sparse3,4000,256,vs.
toKernel());
622 CUDA_LAUNCH_DIM3(test_remove_sparse,10,100,vs.
toKernel());
624 size_t sz = vs.
size();
627 vs.template deviceToHost<0,1,2>();
629 BOOST_REQUIRE_EQUAL(vs.
size(),sz - 1000);
639 CUDA_LAUNCH_DIM3(test_remove_sparse2,10,100,vs.
toKernel());
643 BOOST_REQUIRE_EQUAL(vs.
size(),sz - 1500);
645 vs.template deviceToHost<0,1,2>();
654 CUDA_LAUNCH_DIM3(test_remove_sparse3,4000,256,vs.
toKernel());
658 BOOST_REQUIRE_EQUAL(vs.
size(),0);
660 vs.template deviceToHost<0,1,2>();
662 BOOST_REQUIRE_EQUAL(vs.
size(),0);
671BOOST_AUTO_TEST_CASE( vector_sparse_cuda_gpu_remove_incremental )
675 vs.template setBackground<0>(17);
677 vs.template setBackground<1>(18);
679 vs.template setBackground<2>(19);
684 CUDA_LAUNCH_DIM3(test_insert_sparse,10,100,vs.
toKernel());
685 CUDA_LAUNCH_DIM3(test_insert_sparse2_inc,10,100,vs.
toKernel());
686 CUDA_LAUNCH_DIM3(test_insert_sparse2_inc,10,100,vs.
toKernel());
687 CUDA_LAUNCH_DIM3(test_insert_sparse2_inc,10,100,vs.
toKernel());
695 CUDA_LAUNCH_DIM3(test_remove_sparse,10,100,vs.
toKernel());
696 CUDA_LAUNCH_DIM3(test_remove_sparse2_inc,10,99,vs.
toKernel());
697 CUDA_LAUNCH_DIM3(test_remove_sparse2_inc,10,99,vs.
toKernel());
698 CUDA_LAUNCH_DIM3(test_remove_sparse2_inc,10,99,vs.
toKernel());
702 BOOST_REQUIRE_EQUAL(vs.
size(),10);
704 vs.template deviceToHost<0,1,2>();
706 BOOST_REQUIRE_EQUAL(vs.template get<0>(7020),14940);
707 BOOST_REQUIRE_EQUAL(vs.template get<0>(7018),14946);
708 BOOST_REQUIRE_EQUAL(vs.template get<0>(7016),14952);
709 BOOST_REQUIRE_EQUAL(vs.template get<0>(7014),14958);
710 BOOST_REQUIRE_EQUAL(vs.template get<0>(7012),14964);
711 BOOST_REQUIRE_EQUAL(vs.template get<0>(7010),14970);
712 BOOST_REQUIRE_EQUAL(vs.template get<0>(7008),14976);
713 BOOST_REQUIRE_EQUAL(vs.template get<0>(7006),14982);
714 BOOST_REQUIRE_EQUAL(vs.template get<0>(7004),14988);
715 BOOST_REQUIRE_EQUAL(vs.template get<0>(7002),14994);
717 BOOST_REQUIRE_EQUAL(vs.template get<1>(7020),44940);
718 BOOST_REQUIRE_EQUAL(vs.template get<1>(7018),44946);
719 BOOST_REQUIRE_EQUAL(vs.template get<1>(7016),44952);
720 BOOST_REQUIRE_EQUAL(vs.template get<1>(7014),44958);
721 BOOST_REQUIRE_EQUAL(vs.template get<1>(7012),44964);
722 BOOST_REQUIRE_EQUAL(vs.template get<1>(7010),44970);
723 BOOST_REQUIRE_EQUAL(vs.template get<1>(7008),44976);
724 BOOST_REQUIRE_EQUAL(vs.template get<1>(7006),44982);
725 BOOST_REQUIRE_EQUAL(vs.template get<1>(7004),44988);
726 BOOST_REQUIRE_EQUAL(vs.template get<1>(7002),44994);
728 BOOST_REQUIRE_EQUAL(vs.template get<2>(7020),74940);
729 BOOST_REQUIRE_EQUAL(vs.template get<2>(7018),74946);
730 BOOST_REQUIRE_EQUAL(vs.template get<2>(7016),74952);
731 BOOST_REQUIRE_EQUAL(vs.template get<2>(7014),74958);
732 BOOST_REQUIRE_EQUAL(vs.template get<2>(7012),74964);
733 BOOST_REQUIRE_EQUAL(vs.template get<2>(7010),74970);
734 BOOST_REQUIRE_EQUAL(vs.template get<2>(7008),74976);
735 BOOST_REQUIRE_EQUAL(vs.template get<2>(7006),74982);
736 BOOST_REQUIRE_EQUAL(vs.template get<2>(7004),74988);
737 BOOST_REQUIRE_EQUAL(vs.template get<2>(7002),74994);
741BOOST_AUTO_TEST_SUITE_END()
void flush_remove(gpu::ofp_context_t &context, flush_type opt=FLUSH_ON_HOST)
merge the added element to the main data array
void setGPURemoveBuffer(int nblock, int nslot)
set the gpu remove buffer for every block
void clear()
Clear all from all the elements.
vector_sparse_gpu_ker< T, Ti, layout_base > toKernel()
toKernel function transform this structure into one that can be used on GPU
size_t size()
Return how many element you have in this map.
void flush(gpu::ofp_context_t &context, flush_type opt=FLUSH_ON_HOST)
merge the added element to the main data array
void setGPUInsertBuffer(int nblock, int nslot)
set the gpu insert buffer for every block
Implementation of 1-D std::vector like structure.
aggregate of properties, from a list of object if create a struct that follow the OPENFPM native stru...