1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2016 Jakub Szuppe <j.szuppe@gmail.com>
4 // Distributed under the Boost Software License, Version 1.0
5 // See accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt
8 // See http://boostorg.github.com/compute for more information.
9 //---------------------------------------------------------------------------//
11 // Undefining BOOST_COMPUTE_USE_OFFLINE_CACHE macro as we want to modify cached
12 // parameters for copy algorithm without any undesirable consequences (like
13 // saving modified values of those parameters).
14 #ifdef BOOST_COMPUTE_USE_OFFLINE_CACHE
15 #undef BOOST_COMPUTE_USE_OFFLINE_CACHE
18 #define BOOST_TEST_MODULE TestCopyTypeMismatch
19 #include <boost/test/unit_test.hpp>
28 #include <boost/compute/svm.hpp>
29 #include <boost/compute/system.hpp>
30 #include <boost/compute/functional.hpp>
31 #include <boost/compute/command_queue.hpp>
32 #include <boost/compute/algorithm/copy.hpp>
33 #include <boost/compute/async/future.hpp>
34 #include <boost/compute/container/vector.hpp>
35 #include <boost/compute/detail/device_ptr.hpp>
36 #include <boost/compute/memory/svm_ptr.hpp>
37 #include <boost/compute/detail/parameter_cache.hpp>
40 #include "check_macros.hpp"
41 #include "context_setup.hpp"
43 namespace bc
= boost::compute
;
44 namespace compute
= boost::compute
;
46 BOOST_AUTO_TEST_CASE(is_same_ignore_const
)
49 boost::compute::detail::is_same_value_type
<
50 std::vector
<int>::iterator
,
51 compute::buffer_iterator
<int>
55 boost::compute::detail::is_same_value_type
<
56 std::vector
<int>::const_iterator
,
57 compute::buffer_iterator
<int>
61 boost::compute::detail::is_same_value_type
<
62 std::vector
<int>::iterator
,
63 compute::buffer_iterator
<const int>
67 boost::compute::detail::is_same_value_type
<
68 std::vector
<int>::const_iterator
,
69 compute::buffer_iterator
<const int>
74 BOOST_AUTO_TEST_CASE(copy_to_device_float_to_double
)
76 if(!device
.supports_extension("cl_khr_fp64")) {
77 std::cout
<< "skipping test: device does not support double" << std::endl
;
81 using compute::double_
;
82 using compute::float_
;
84 float_ host
[] = { 6.1f
, 10.2f
, 19.3f
, 25.4f
};
85 bc::vector
<double_
> device_vector(4, context
);
87 // copy host float data to double device vector
88 bc::copy(host
, host
+ 4, device_vector
.begin(), queue
);
89 CHECK_RANGE_EQUAL(double_
, 4, device_vector
, (6.1f
, 10.2f
, 19.3f
, 25.4f
));
92 BOOST_AUTO_TEST_CASE(copy_to_device_float_to_int
)
95 using compute::float_
;
97 float_ host
[] = { 6.1f
, -10.2f
, 19.3f
, 25.4f
};
98 bc::vector
<int_
> device_vector(4, context
);
100 // copy host float data to int device vector
101 bc::copy(host
, host
+ 4, device_vector
.begin(), queue
);
107 static_cast<int_
>(6.1f
),
108 static_cast<int_
>(-10.2f
),
109 static_cast<int_
>(19.3f
),
110 static_cast<int_
>(25.4f
)
117 BOOST_AUTO_TEST_CASE(copy_to_device_float_to_int_mapping_device_vector
)
120 using compute::uint_
;
121 using compute::float_
;
123 float_ host
[] = { 6.1f
, -10.2f
, 19.3f
, 25.4f
};
124 bc::vector
<int_
> device_vector(4, context
);
126 std::string cache_key
=
127 std::string("__boost_compute_copy_to_device_float_int");
128 boost::shared_ptr
<bc::detail::parameter_cache
> parameters
=
129 bc::detail::parameter_cache::get_global_cache(device
);
132 uint_ map_copy_threshold
=
133 parameters
->get(cache_key
, "map_copy_threshold", 0);
135 // force copy_to_device_map (mapping device vector to the host)
136 parameters
->set(cache_key
, "map_copy_threshold", 1024);
138 // copy host float data to int device vector
139 bc::copy(host
, host
+ 4, device_vector
.begin(), queue
);
145 static_cast<int_
>(6.1f
),
146 static_cast<int_
>(-10.2f
),
147 static_cast<int_
>(19.3f
),
148 static_cast<int_
>(25.4f
)
153 parameters
->set(cache_key
, "map_copy_threshold", map_copy_threshold
);
156 BOOST_AUTO_TEST_CASE(copy_to_device_float_to_int_convert_on_host
)
159 using compute::uint_
;
160 using compute::float_
;
162 std::string cache_key
=
163 std::string("__boost_compute_copy_to_device_float_int");
164 boost::shared_ptr
<bc::detail::parameter_cache
> parameters
=
165 bc::detail::parameter_cache::get_global_cache(device
);
168 uint_ map_copy_threshold
=
169 parameters
->get(cache_key
, "map_copy_threshold", 0);
170 uint_ direct_copy_threshold
=
171 parameters
->get(cache_key
, "direct_copy_threshold", 0);
173 // force copying by casting input data on host and performing
174 // normal copy host->device (since types match now)
175 parameters
->set(cache_key
, "map_copy_threshold", 0);
176 parameters
->set(cache_key
, "direct_copy_threshold", 1024);
178 float_ host
[] = { 6.1f
, -10.2f
, 19.3f
, 25.4f
};
179 bc::vector
<int_
> device_vector(4, context
);
181 // copy host float data to int device vector
182 bc::copy(host
, host
+ 4, device_vector
.begin(), queue
);
188 static_cast<int_
>(6.1f
),
189 static_cast<int_
>(-10.2f
),
190 static_cast<int_
>(19.3f
),
191 static_cast<int_
>(25.4f
)
196 parameters
->set(cache_key
, "map_copy_threshold", map_copy_threshold
);
197 parameters
->set(cache_key
, "direct_copy_threshold", direct_copy_threshold
);
200 BOOST_AUTO_TEST_CASE(copy_to_device_float_to_int_with_transform
)
203 using compute::uint_
;
204 using compute::float_
;
206 std::string cache_key
=
207 std::string("__boost_compute_copy_to_device_float_int");
208 boost::shared_ptr
<bc::detail::parameter_cache
> parameters
=
209 bc::detail::parameter_cache::get_global_cache(device
);
212 uint_ map_copy_threshold
=
213 parameters
->get(cache_key
, "map_copy_threshold", 0);
214 uint_ direct_copy_threshold
=
215 parameters
->get(cache_key
, "direct_copy_threshold", 0);
217 // force copying by mapping input data to the device memory
218 // and using transform operation for casting & copying
219 parameters
->set(cache_key
, "map_copy_threshold", 0);
220 parameters
->set(cache_key
, "direct_copy_threshold", 0);
222 float_ host
[] = { 6.1f
, -10.2f
, 19.3f
, 25.4f
};
223 bc::vector
<int_
> device_vector(4, context
);
225 // copy host float data to int device vector
226 bc::copy(host
, host
+ 4, device_vector
.begin(), queue
);
232 static_cast<int_
>(6.1f
),
233 static_cast<int_
>(-10.2f
),
234 static_cast<int_
>(19.3f
),
235 static_cast<int_
>(25.4f
)
240 parameters
->set(cache_key
, "map_copy_threshold", map_copy_threshold
);
241 parameters
->set(cache_key
, "direct_copy_threshold", direct_copy_threshold
);
244 BOOST_AUTO_TEST_CASE(copy_async_to_device_float_to_int
)
247 using compute::float_
;
249 float_ host
[] = { 6.1f
, -10.2f
, 19.3f
, 25.4f
};
250 bc::vector
<int_
> device_vector(4, context
);
252 // copy host float data to int device vector
253 compute::future
<void> future
=
254 bc::copy_async(host
, host
+ 4, device_vector
.begin(), queue
);
262 static_cast<int_
>(6.1f
),
263 static_cast<int_
>(-10.2f
),
264 static_cast<int_
>(19.3f
),
265 static_cast<int_
>(25.4f
)
270 BOOST_AUTO_TEST_CASE(copy_async_to_device_float_to_int_empty
)
273 using compute::float_
;
275 float_ host
[] = { 6.1f
, -10.2f
, 19.3f
, 25.4f
};
276 bc::vector
<int_
> device_vector(size_t(4), int_(1), queue
);
278 // copy nothing to int device vector
279 compute::future
<bc::vector
<int_
>::iterator
> future
=
280 bc::copy_async(host
, host
, device_vector
.begin(), queue
);
298 // Test copying from a std::list to a bc::vector. This differs from
299 // the test copying from std::vector because std::list has non-contiguous
300 // storage for its data values.
301 BOOST_AUTO_TEST_CASE(copy_to_device_float_to_int_list_device_map
)
304 using compute::uint_
;
305 using compute::float_
;
307 std::string cache_key
=
308 std::string("__boost_compute_copy_to_device_float_int");
309 boost::shared_ptr
<bc::detail::parameter_cache
> parameters
=
310 bc::detail::parameter_cache::get_global_cache(device
);
313 uint_ map_copy_threshold
=
314 parameters
->get(cache_key
, "map_copy_threshold", 0);
316 // force copy_to_device_map (mapping device vector to the host)
317 parameters
->set(cache_key
, "map_copy_threshold", 1024);
319 float_ data
[] = { 6.1f
, -10.2f
, 19.3f
, 25.4f
};
320 std::list
<float_
> host(data
, data
+ 4);
321 bc::vector
<int_
> device_vector(4, context
);
323 // copy host float data to int device vector
324 bc::copy(host
.begin(), host
.end(), device_vector
.begin(), queue
);
330 static_cast<int_
>(6.1f
),
331 static_cast<int_
>(-10.2f
),
332 static_cast<int_
>(19.3f
),
333 static_cast<int_
>(25.4f
)
338 parameters
->set(cache_key
, "map_copy_threshold", map_copy_threshold
);
341 // Test copying from a std::list to a bc::vector. This differs from
342 // the test copying from std::vector because std::list has non-contiguous
343 // storage for its data values.
344 BOOST_AUTO_TEST_CASE(copy_to_device_float_to_int_list_convert_on_host
)
347 using compute::uint_
;
348 using compute::float_
;
350 std::string cache_key
=
351 std::string("__boost_compute_copy_to_device_float_int");
352 boost::shared_ptr
<bc::detail::parameter_cache
> parameters
=
353 bc::detail::parameter_cache::get_global_cache(device
);
356 uint_ map_copy_threshold
=
357 parameters
->get(cache_key
, "map_copy_threshold", 0);
358 uint_ direct_copy_threshold
=
359 parameters
->get(cache_key
, "direct_copy_threshold", 0);
361 // force copying by casting input data on host and performing
362 // normal copy host->device (since types match now)
363 parameters
->set(cache_key
, "map_copy_threshold", 0);
364 parameters
->set(cache_key
, "direct_copy_threshold", 1024);
366 float_ data
[] = { 6.1f
, -10.2f
, 19.3f
, 25.4f
};
367 std::list
<float_
> host(data
, data
+ 4);
368 bc::vector
<int_
> device_vector(4, context
);
370 // copy host float data to int device vector
371 bc::copy(host
.begin(), host
.end(), device_vector
.begin(), queue
);
377 static_cast<int_
>(6.1f
),
378 static_cast<int_
>(-10.2f
),
379 static_cast<int_
>(19.3f
),
380 static_cast<int_
>(25.4f
)
385 parameters
->set(cache_key
, "map_copy_threshold", map_copy_threshold
);
386 parameters
->set(cache_key
, "direct_copy_threshold", direct_copy_threshold
);
389 // SVM requires OpenCL 2.0
390 #if defined(BOOST_COMPUTE_CL_VERSION_2_0) || defined(BOOST_COMPUTE_DOXYGEN_INVOKED)
391 BOOST_AUTO_TEST_CASE(copy_to_device_svm_float_to_int_map
)
393 REQUIRES_OPENCL_VERSION(2, 0);
396 using compute::uint_
;
397 using compute::float_
;
399 std::string cache_key
=
400 std::string("__boost_compute_copy_to_device_float_int");
401 boost::shared_ptr
<bc::detail::parameter_cache
> parameters
=
402 bc::detail::parameter_cache::get_global_cache(device
);
405 uint_ map_copy_threshold
=
406 parameters
->get(cache_key
, "map_copy_threshold", 0);
408 // force copy_to_device_map (mapping device vector to the host)
409 parameters
->set(cache_key
, "map_copy_threshold", 1024);
411 float_ host
[] = { 5.1f
, -10.3f
, 19.4f
, 26.7f
};
412 compute::svm_ptr
<int_
> ptr
= compute::svm_alloc
<int_
>(context
, 4);
414 // copy host float data to int device vector
415 bc::copy(host
, host
+ 4, ptr
, queue
);
417 queue
.enqueue_svm_map(ptr
.get(), 4 * sizeof(cl_int
), CL_MAP_READ
);
418 CHECK_HOST_RANGE_EQUAL(
421 static_cast<int_
*>(ptr
.get()),
423 static_cast<int_
>(5.1f
),
424 static_cast<int_
>(-10.3f
),
425 static_cast<int_
>(19.4f
),
426 static_cast<int_
>(26.7f
)
429 queue
.enqueue_svm_unmap(ptr
.get()).wait();
431 compute::svm_free(context
, ptr
);
434 parameters
->set(cache_key
, "map_copy_threshold", map_copy_threshold
);
437 BOOST_AUTO_TEST_CASE(copy_to_device_svm_float_to_int_convert_on_host
)
439 REQUIRES_OPENCL_VERSION(2, 0);
441 if(bug_in_svmmemcpy(device
)){
442 std::cerr
<< "skipping svmmemcpy test case" << std::endl
;
447 using compute::uint_
;
448 using compute::float_
;
450 std::string cache_key
=
451 std::string("__boost_compute_copy_to_device_float_int");
452 boost::shared_ptr
<bc::detail::parameter_cache
> parameters
=
453 bc::detail::parameter_cache::get_global_cache(device
);
456 uint_ map_copy_threshold
=
457 parameters
->get(cache_key
, "map_copy_threshold", 0);
458 uint_ direct_copy_threshold
=
459 parameters
->get(cache_key
, "direct_copy_threshold", 0);
461 // force copying by casting input data on host and performing
462 // normal copy host->device (since types match now)
463 parameters
->set(cache_key
, "map_copy_threshold", 0);
464 parameters
->set(cache_key
, "direct_copy_threshold", 1024);
466 float_ host
[] = { 0.1f
, 10.3f
, 9.4f
, -26.7f
};
467 compute::svm_ptr
<int_
> ptr
= compute::svm_alloc
<int_
>(context
, 4);
469 // copy host float data to int device vector
470 bc::copy(host
, host
+ 4, ptr
, queue
);
472 queue
.enqueue_svm_map(ptr
.get(), 4 * sizeof(cl_int
), CL_MAP_READ
);
473 CHECK_HOST_RANGE_EQUAL(
476 static_cast<int_
*>(ptr
.get()),
478 static_cast<int_
>(0.1f
),
479 static_cast<int_
>(10.3f
),
480 static_cast<int_
>(9.4f
),
481 static_cast<int_
>(-26.7f
)
484 queue
.enqueue_svm_unmap(ptr
.get()).wait();
486 compute::svm_free(context
, ptr
);
489 parameters
->set(cache_key
, "map_copy_threshold", map_copy_threshold
);
490 parameters
->set(cache_key
, "direct_copy_threshold", direct_copy_threshold
);
493 BOOST_AUTO_TEST_CASE(copy_to_device_svm_float_to_int_with_transform
)
495 REQUIRES_OPENCL_VERSION(2, 0);
498 using compute::uint_
;
499 using compute::float_
;
501 std::string cache_key
=
502 std::string("__boost_compute_copy_to_device_float_int");
503 boost::shared_ptr
<bc::detail::parameter_cache
> parameters
=
504 bc::detail::parameter_cache::get_global_cache(device
);
507 uint_ map_copy_threshold
=
508 parameters
->get(cache_key
, "map_copy_threshold", 0);
509 uint_ direct_copy_threshold
=
510 parameters
->get(cache_key
, "direct_copy_threshold", 0);
512 // force copying by mapping input data to the device memory
513 // and using transform operation (copy kernel) for casting & copying
514 parameters
->set(cache_key
, "map_copy_threshold", 0);
515 parameters
->set(cache_key
, "direct_copy_threshold", 0);
517 float_ host
[] = { 4.1f
, -11.3f
, 219.4f
, -26.7f
};
518 compute::svm_ptr
<int_
> ptr
= compute::svm_alloc
<int_
>(context
, 4);
520 // copy host float data to int device vector
521 bc::copy(host
, host
+ 4, ptr
, queue
);
523 queue
.enqueue_svm_map(ptr
.get(), 4 * sizeof(cl_int
), CL_MAP_READ
);
524 CHECK_HOST_RANGE_EQUAL(
527 static_cast<int_
*>(ptr
.get()),
529 static_cast<int_
>(4.1f
),
530 static_cast<int_
>(-11.3f
),
531 static_cast<int_
>(219.4f
),
532 static_cast<int_
>(-26.7f
)
535 queue
.enqueue_svm_unmap(ptr
.get()).wait();
537 compute::svm_free(context
, ptr
);
540 parameters
->set(cache_key
, "map_copy_threshold", map_copy_threshold
);
541 parameters
->set(cache_key
, "direct_copy_threshold", direct_copy_threshold
);
544 BOOST_AUTO_TEST_CASE(copy_async_to_device_svm_float_to_int
)
546 REQUIRES_OPENCL_VERSION(2, 0);
549 using compute::uint_
;
550 using compute::float_
;
552 float_ host
[] = { 44.1f
, -14.3f
, 319.4f
, -26.7f
};
553 compute::svm_ptr
<int_
> ptr
= compute::svm_alloc
<int_
>(context
, 4);
555 // copy host float data to int device vector
556 compute::future
<void> future
=
557 bc::copy_async(host
, host
+ 4, ptr
, queue
);
560 queue
.enqueue_svm_map(ptr
.get(), 4 * sizeof(cl_int
), CL_MAP_READ
);
561 CHECK_HOST_RANGE_EQUAL(
564 static_cast<int_
*>(ptr
.get()),
566 static_cast<int_
>(44.1f
),
567 static_cast<int_
>(-14.3f
),
568 static_cast<int_
>(319.4f
),
569 static_cast<int_
>(-26.7f
)
572 queue
.enqueue_svm_unmap(ptr
.get()).wait();
574 compute::svm_free(context
, ptr
);
580 BOOST_AUTO_TEST_CASE(copy_on_device_float_to_int
)
583 using compute::float_
;
585 float_ data
[] = { 6.1f
, -10.2f
, 19.3f
, 25.4f
};
586 bc::vector
<float_
> device_fvector(data
, data
+ 4, queue
);
587 bc::vector
<int_
> device_ivector(4, context
);
589 // copy device float vector to device int vector
591 device_fvector
.begin(),
592 device_fvector
.end(),
593 device_ivector
.begin(),
602 static_cast<int_
>(6.1f
),
603 static_cast<int_
>(-10.2f
),
604 static_cast<int_
>(19.3f
),
605 static_cast<int_
>(25.4f
)
610 BOOST_AUTO_TEST_CASE(copy_async_on_device_float_to_int
)
613 using compute::float_
;
615 float_ data
[] = { 6.1f
, -10.2f
, 19.3f
, 25.4f
};
616 bc::vector
<float_
> device_fvector(data
, data
+ 4, queue
);
617 bc::vector
<int_
> device_ivector(4, context
);
619 // copy device float vector to device int vector
620 compute::future
<void> future
=
622 device_fvector
.begin(),
623 device_fvector
.end(),
624 device_ivector
.begin(),
634 static_cast<int_
>(6.1f
),
635 static_cast<int_
>(-10.2f
),
636 static_cast<int_
>(19.3f
),
637 static_cast<int_
>(25.4f
)
642 BOOST_AUTO_TEST_CASE(copy_async_on_device_float_to_int_empty
)
645 using compute::float_
;
647 float_ data
[] = { 6.1f
, -10.2f
, 19.3f
, 25.4f
};
648 bc::vector
<float_
> device_fvector(data
, data
+ 4, queue
);
649 bc::vector
<int_
> device_ivector(size_t(4), int_(1), queue
);
651 // copy device float vector to device int vector
652 compute::future
<void> future
=
654 device_fvector
.begin(),
655 device_fvector
.begin(),
656 device_ivector
.begin(),
676 // SVM requires OpenCL 2.0
677 #if defined(BOOST_COMPUTE_CL_VERSION_2_0) || defined(BOOST_COMPUTE_DOXYGEN_INVOKED)
678 BOOST_AUTO_TEST_CASE(copy_on_device_buffer_to_svm_float_to_int
)
680 REQUIRES_OPENCL_VERSION(2, 0);
683 using compute::float_
;
685 float_ data
[] = { 65.1f
, -110.2f
, -19.3f
, 26.7f
};
686 bc::vector
<float_
> device_vector(data
, data
+ 4, queue
);
687 compute::svm_ptr
<int_
> ptr
= compute::svm_alloc
<int_
>(context
, 4);
689 // copy host float data to int svm memory
690 bc::copy(device_vector
.begin(), device_vector
.end(), ptr
, queue
);
692 queue
.enqueue_svm_map(ptr
.get(), 4 * sizeof(cl_int
), CL_MAP_READ
);
693 CHECK_HOST_RANGE_EQUAL(
696 static_cast<int_
*>(ptr
.get()),
698 static_cast<int_
>(65.1f
),
699 static_cast<int_
>(-110.2f
),
700 static_cast<int_
>(-19.3f
),
701 static_cast<int_
>(26.7f
)
704 queue
.enqueue_svm_unmap(ptr
.get()).wait();
706 compute::svm_free(context
, ptr
);
709 BOOST_AUTO_TEST_CASE(copy_on_device_svm_to_buffer_float_to_int
)
711 REQUIRES_OPENCL_VERSION(2, 0);
714 using compute::float_
;
716 float_ data
[] = { 6.1f
, 11.2f
, 19.3f
, 6.7f
};
717 bc::vector
<int_
> device_vector(4, context
);
718 compute::svm_ptr
<float_
> ptr
= compute::svm_alloc
<float_
>(context
, 4);
720 queue
.enqueue_svm_map(ptr
.get(), 4 * sizeof(cl_int
), CL_MAP_WRITE
);
721 for(size_t i
= 0; i
< 4; i
++) {
722 static_cast<float_
*>(ptr
.get())[i
] = data
[i
];
724 queue
.enqueue_svm_unmap(ptr
.get()).wait();
726 // copy host float svm data to int device vector
727 bc::copy(ptr
, ptr
+ 4, device_vector
.begin(), queue
);
734 static_cast<int_
>(6.1f
),
735 static_cast<int_
>(11.2f
),
736 static_cast<int_
>(19.3f
),
737 static_cast<int_
>(6.7f
)
741 compute::svm_free(context
, ptr
);
744 BOOST_AUTO_TEST_CASE(copy_on_device_svm_to_svm_float_to_int
)
746 REQUIRES_OPENCL_VERSION(2, 0);
749 using compute::float_
;
751 float_ data
[] = { 0.1f
, -10.2f
, -1.3f
, 2.7f
};
752 compute::svm_ptr
<float_
> ptr
= compute::svm_alloc
<float_
>(context
, 4);
753 compute::svm_ptr
<int_
> ptr2
= compute::svm_alloc
<int_
>(context
, 4);
755 queue
.enqueue_svm_map(ptr
.get(), 4 * sizeof(cl_int
), CL_MAP_WRITE
);
756 for(size_t i
= 0; i
< 4; i
++) {
757 static_cast<float_
*>(ptr
.get())[i
] = data
[i
];
759 queue
.enqueue_svm_unmap(ptr
.get()).wait();
761 // copy host float svm to int svm
762 bc::copy(ptr
, ptr
+ 4, ptr2
, queue
);
764 queue
.enqueue_svm_map(ptr2
.get(), 4 * sizeof(cl_int
), CL_MAP_READ
);
765 CHECK_HOST_RANGE_EQUAL(
768 static_cast<int_
*>(ptr2
.get()),
770 static_cast<int_
>(0.1f
),
771 static_cast<int_
>(-10.2f
),
772 static_cast<int_
>(-1.3f
),
773 static_cast<int_
>(2.7f
)
776 queue
.enqueue_svm_unmap(ptr2
.get()).wait();
778 compute::svm_free(context
, ptr
);
779 compute::svm_free(context
, ptr2
);
782 BOOST_AUTO_TEST_CASE(copy_async_on_device_buffer_to_svm_float_to_int
)
784 REQUIRES_OPENCL_VERSION(2, 0);
787 using compute::float_
;
789 float_ data
[] = { 65.1f
, -110.2f
, -19.3f
, 26.7f
};
790 bc::vector
<float_
> device_vector(data
, data
+ 4, queue
);
791 compute::svm_ptr
<int_
> ptr
= compute::svm_alloc
<int_
>(context
, 4);
793 // copy host float data to int svm memory
794 compute::future
<bc::svm_ptr
<int_
> > future
=
795 bc::copy_async(device_vector
.begin(), device_vector
.end(), ptr
, queue
);
798 queue
.enqueue_svm_map(ptr
.get(), 4 * sizeof(cl_int
), CL_MAP_READ
);
799 CHECK_HOST_RANGE_EQUAL(
802 static_cast<int_
*>(ptr
.get()),
804 static_cast<int_
>(65.1f
),
805 static_cast<int_
>(-110.2f
),
806 static_cast<int_
>(-19.3f
),
807 static_cast<int_
>(26.7f
)
810 queue
.enqueue_svm_unmap(ptr
.get()).wait();
812 compute::svm_free(context
, ptr
);
815 BOOST_AUTO_TEST_CASE(copy_async_on_device_svm_to_buffer_float_to_int
)
817 REQUIRES_OPENCL_VERSION(2, 0);
820 using compute::float_
;
822 float_ data
[] = { 65.1f
, -110.2f
, -19.3f
, 26.7f
};
823 bc::vector
<int_
> device_vector(4, context
);
824 compute::svm_ptr
<float_
> ptr
= compute::svm_alloc
<float_
>(context
, 4);
826 queue
.enqueue_svm_map(ptr
.get(), 4 * sizeof(cl_int
), CL_MAP_WRITE
);
827 for(size_t i
= 0; i
< 4; i
++) {
828 static_cast<float_
*>(ptr
.get())[i
] = data
[i
];
830 queue
.enqueue_svm_unmap(ptr
.get()).wait();
832 // copy host float svm data to int device vector
833 compute::future
<bc::vector
<int_
>::iterator
> future
=
834 bc::copy_async(ptr
, ptr
+ 4, device_vector
.begin(), queue
);
842 static_cast<int_
>(65.1f
),
843 static_cast<int_
>(-110.2f
),
844 static_cast<int_
>(-19.3f
),
845 static_cast<int_
>(26.7f
)
849 compute::svm_free(context
, ptr
);
852 BOOST_AUTO_TEST_CASE(copy_async_on_device_svm_to_svm_float_to_int
)
854 REQUIRES_OPENCL_VERSION(2, 0);
857 using compute::float_
;
859 float_ data
[] = { 0.1f
, -10.2f
, -1.3f
, 2.7f
};
860 compute::svm_ptr
<float_
> ptr
= compute::svm_alloc
<float_
>(context
, 4);
861 compute::svm_ptr
<int_
> ptr2
= compute::svm_alloc
<int_
>(context
, 4);
863 queue
.enqueue_svm_map(ptr
.get(), 4 * sizeof(cl_int
), CL_MAP_WRITE
);
864 for(size_t i
= 0; i
< 4; i
++) {
865 static_cast<float_
*>(ptr
.get())[i
] = data
[i
];
867 queue
.enqueue_svm_unmap(ptr
.get()).wait();
869 // copy host float svm to int svm
870 compute::future
<bc::svm_ptr
<int_
> > future
=
871 bc::copy_async(ptr
, ptr
+ 4, ptr2
, queue
);
874 queue
.enqueue_svm_map(ptr2
.get(), 4 * sizeof(cl_int
), CL_MAP_READ
);
875 CHECK_HOST_RANGE_EQUAL(
878 static_cast<int_
*>(ptr2
.get()),
880 static_cast<int_
>(0.1f
),
881 static_cast<int_
>(-10.2f
),
882 static_cast<int_
>(-1.3f
),
883 static_cast<int_
>(2.7f
)
886 queue
.enqueue_svm_unmap(ptr2
.get()).wait();
888 compute::svm_free(context
, ptr
);
889 compute::svm_free(context
, ptr2
);
895 BOOST_AUTO_TEST_CASE(copy_to_host_float_to_int
)
898 using compute::float_
;
900 float_ data
[] = { 6.1f
, -10.2f
, 19.3f
, 25.4f
};
901 bc::vector
<float_
> device_vector(data
, data
+ 4, queue
);
903 std::vector
<int_
> host_vector(4);
904 // copy device float vector to int host vector
905 bc::copy(device_vector
.begin(), device_vector
.end(), host_vector
.begin(), queue
);
906 CHECK_HOST_RANGE_EQUAL(
911 static_cast<int_
>(6.1f
),
912 static_cast<int_
>(-10.2f
),
913 static_cast<int_
>(19.3f
),
914 static_cast<int_
>(25.4f
)
919 BOOST_AUTO_TEST_CASE(copy_to_host_float_to_int_map
)
922 using compute::uint_
;
923 using compute::float_
;
925 std::string cache_key
=
926 std::string("__boost_compute_copy_to_host_float_int");
927 boost::shared_ptr
<bc::detail::parameter_cache
> parameters
=
928 bc::detail::parameter_cache::get_global_cache(device
);
931 uint_ map_copy_threshold
=
932 parameters
->get(cache_key
, "map_copy_threshold", 0);
934 // force copy_to_host_map (mapping device vector to the host)
935 parameters
->set(cache_key
, "map_copy_threshold", 1024);
937 float_ data
[] = { 6.1f
, -10.2f
, 19.3f
, 25.4f
};
938 bc::vector
<float_
> device_vector(data
, data
+ 4, queue
);
940 std::vector
<int_
> host_vector(4);
941 // copy device float vector to int host vector
942 bc::copy(device_vector
.begin(), device_vector
.end(), host_vector
.begin(), queue
);
943 CHECK_HOST_RANGE_EQUAL(
948 static_cast<int_
>(6.1f
),
949 static_cast<int_
>(-10.2f
),
950 static_cast<int_
>(19.3f
),
951 static_cast<int_
>(25.4f
)
956 parameters
->set(cache_key
, "map_copy_threshold", map_copy_threshold
);
959 BOOST_AUTO_TEST_CASE(copy_to_host_float_to_int_convert_on_host
)
962 using compute::uint_
;
963 using compute::float_
;
965 std::string cache_key
=
966 std::string("__boost_compute_copy_to_host_float_int");
967 boost::shared_ptr
<bc::detail::parameter_cache
> parameters
=
968 bc::detail::parameter_cache::get_global_cache(device
);
971 uint_ map_copy_threshold
=
972 parameters
->get(cache_key
, "map_copy_threshold", 0);
973 uint_ direct_copy_threshold
=
974 parameters
->get(cache_key
, "direct_copy_threshold", 0);
976 // force copying by copying input device vector to temporary
977 // host vector of the same type and then copying from that temporary
978 // vector to result using std::copy()
979 parameters
->set(cache_key
, "map_copy_threshold", 0);
980 parameters
->set(cache_key
, "direct_copy_threshold", 1024);
982 float_ data
[] = { 6.1f
, -10.2f
, 19.3f
, 25.4f
};
983 bc::vector
<float_
> device_vector(data
, data
+ 4, queue
);
985 std::vector
<int_
> host_vector(4);
986 // copy device float vector to int host vector
987 bc::copy(device_vector
.begin(), device_vector
.end(), host_vector
.begin(), queue
);
988 CHECK_HOST_RANGE_EQUAL(
993 static_cast<int_
>(6.1f
),
994 static_cast<int_
>(-10.2f
),
995 static_cast<int_
>(19.3f
),
996 static_cast<int_
>(25.4f
)
1001 parameters
->set(cache_key
, "map_copy_threshold", map_copy_threshold
);
1002 parameters
->set(cache_key
, "direct_copy_threshold", direct_copy_threshold
);
1005 BOOST_AUTO_TEST_CASE(copy_to_host_float_to_int_convert_on_device
)
1007 using compute::int_
;
1008 using compute::uint_
;
1009 using compute::float_
;
1011 std::string cache_key
=
1012 std::string("__boost_compute_copy_to_host_float_int");
1013 boost::shared_ptr
<bc::detail::parameter_cache
> parameters
=
1014 bc::detail::parameter_cache::get_global_cache(device
);
1017 uint_ map_copy_threshold
=
1018 parameters
->get(cache_key
, "map_copy_threshold", 0);
1019 uint_ direct_copy_threshold
=
1020 parameters
->get(cache_key
, "direct_copy_threshold", 0);
1022 // force copying by mapping output data to the device memory
1023 // and using transform operation for casting & copying
1024 parameters
->set(cache_key
, "map_copy_threshold", 0);
1025 parameters
->set(cache_key
, "direct_copy_threshold", 0);
1027 float_ data
[] = { 6.1f
, -10.2f
, 19.3f
, 25.4f
};
1028 bc::vector
<float_
> device_vector(data
, data
+ 4, queue
);
1030 std::vector
<int_
> host_vector(4);
1031 // copy device float vector to int host vector
1032 bc::copy(device_vector
.begin(), device_vector
.end(), host_vector
.begin(), queue
);
1033 CHECK_HOST_RANGE_EQUAL(
1036 host_vector
.begin(),
1038 static_cast<int_
>(6.1f
),
1039 static_cast<int_
>(-10.2f
),
1040 static_cast<int_
>(19.3f
),
1041 static_cast<int_
>(25.4f
)
1046 parameters
->set(cache_key
, "map_copy_threshold", map_copy_threshold
);
1047 parameters
->set(cache_key
, "direct_copy_threshold", direct_copy_threshold
);
1050 // Test copying from a bc::vector to a std::list . This differs from
1051 // the test copying to std::vector because std::list has non-contiguous
1052 // storage for its data values.
1053 BOOST_AUTO_TEST_CASE(copy_to_host_list_float_to_int_map
)
1055 using compute::int_
;
1056 using compute::uint_
;
1057 using compute::float_
;
1059 std::string cache_key
=
1060 std::string("__boost_compute_copy_to_host_float_int");
1061 boost::shared_ptr
<bc::detail::parameter_cache
> parameters
=
1062 bc::detail::parameter_cache::get_global_cache(device
);
1065 uint_ map_copy_threshold
=
1066 parameters
->get(cache_key
, "map_copy_threshold", 0);
1068 // force copy_to_host_map (mapping device vector to the host)
1069 parameters
->set(cache_key
, "map_copy_threshold", 1024);
1071 float_ data
[] = { 6.1f
, -10.2f
, 19.3f
, 25.4f
};
1072 bc::vector
<float_
> device_vector(data
, data
+ 4, queue
);
1074 std::list
<int_
> host_list(4);
1075 // copy device float vector to int host vector
1076 bc::copy(device_vector
.begin(), device_vector
.end(), host_list
.begin(), queue
);
1078 int_ expected
[4] = {
1079 static_cast<int_
>(6.1f
),
1080 static_cast<int_
>(-10.2f
),
1081 static_cast<int_
>(19.3f
),
1082 static_cast<int_
>(25.4f
)
1084 BOOST_CHECK_EQUAL_COLLECTIONS(
1085 host_list
.begin(), host_list
.end(),
1086 expected
, expected
+ 4
1090 parameters
->set(cache_key
, "map_copy_threshold", map_copy_threshold
);
1093 // Test copying from a bc::vector to a std::list . This differs from
1094 // the test copying to std::vector because std::list has non-contiguous
1095 // storage for its data values.
1096 BOOST_AUTO_TEST_CASE(copy_to_host_list_float_to_int_covert_on_host
)
1098 using compute::int_
;
1099 using compute::uint_
;
1100 using compute::float_
;
1102 std::string cache_key
=
1103 std::string("__boost_compute_copy_to_host_float_int");
1104 boost::shared_ptr
<bc::detail::parameter_cache
> parameters
=
1105 bc::detail::parameter_cache::get_global_cache(device
);
1108 uint_ map_copy_threshold
=
1109 parameters
->get(cache_key
, "map_copy_threshold", 0);
1110 uint_ direct_copy_threshold
=
1111 parameters
->get(cache_key
, "direct_copy_threshold", 0);
1113 // force copying by copying input device vector to temporary
1114 // host vector of the same type and then copying from that temporary
1115 // vector to result using std::copy()
1116 parameters
->set(cache_key
, "map_copy_threshold", 0);
1117 parameters
->set(cache_key
, "direct_copy_threshold", 1024);
1119 float_ data
[] = { 6.1f
, -10.2f
, 19.3f
, 25.4f
};
1120 bc::vector
<float_
> device_vector(data
, data
+ 4, queue
);
1122 std::list
<int_
> host_list(4);
1123 // copy device float vector to int host vector
1124 bc::copy(device_vector
.begin(), device_vector
.end(), host_list
.begin(), queue
);
1125 int_ expected
[4] = {
1126 static_cast<int_
>(6.1f
),
1127 static_cast<int_
>(-10.2f
),
1128 static_cast<int_
>(19.3f
),
1129 static_cast<int_
>(25.4f
)
1131 BOOST_CHECK_EQUAL_COLLECTIONS(
1132 host_list
.begin(), host_list
.end(),
1133 expected
, expected
+ 4
1137 parameters
->set(cache_key
, "map_copy_threshold", map_copy_threshold
);
1138 parameters
->set(cache_key
, "direct_copy_threshold", direct_copy_threshold
);
1141 BOOST_AUTO_TEST_CASE(copy_async_to_host_float_to_int
)
1143 using compute::int_
;
1144 using compute::float_
;
1146 float_ data
[] = { 6.1f
, -10.2f
, 19.3f
, 25.4f
};
1147 bc::vector
<float_
> device_vector(data
, data
+ 4, queue
);
1148 std::vector
<int_
> host_vector(device_vector
.size());
1150 // copy device float vector to host int vector
1151 compute::future
<void> future
=
1153 device_vector
.begin(),
1154 device_vector
.end(),
1155 host_vector
.begin(),
1160 CHECK_HOST_RANGE_EQUAL(
1163 host_vector
.begin(),
1165 static_cast<int_
>(6.1f
),
1166 static_cast<int_
>(-10.2f
),
1167 static_cast<int_
>(19.3f
),
1168 static_cast<int_
>(25.4f
)
1173 BOOST_AUTO_TEST_CASE(copy_async_to_host_float_to_int_empty
)
1175 using compute::int_
;
1176 using compute::float_
;
1178 float_ data
[] = { 6.1f
, -10.2f
, 19.3f
, 25.4f
};
1179 bc::vector
<float_
> device_vector(data
, data
+ 4, queue
);
1180 std::vector
<int_
> host_vector(device_vector
.size(), int_(1));
1182 // copy device float vector to host int vector
1183 compute::future
<void> future
=
1185 device_vector
.begin(),
1186 device_vector
.begin(),
1187 host_vector
.begin(),
1190 if(future
.valid()) {
1194 CHECK_HOST_RANGE_EQUAL(
1197 host_vector
.begin(),
1207 // SVM requires OpenCL 2.0
1208 #if defined(BOOST_COMPUTE_CL_VERSION_2_0) || defined(BOOST_COMPUTE_DOXYGEN_INVOKED)
1209 BOOST_AUTO_TEST_CASE(copy_to_host_svm_float_to_int_map
)
1211 REQUIRES_OPENCL_VERSION(2, 0);
1213 using compute::int_
;
1214 using compute::uint_
;
1215 using compute::float_
;
1217 std::string cache_key
=
1218 std::string("__boost_compute_copy_to_host_float_int");
1219 boost::shared_ptr
<bc::detail::parameter_cache
> parameters
=
1220 bc::detail::parameter_cache::get_global_cache(device
);
1223 uint_ map_copy_threshold
=
1224 parameters
->get(cache_key
, "map_copy_threshold", 0);
1226 // force copy_to_host_map (mapping device vector to the host)
1227 parameters
->set(cache_key
, "map_copy_threshold", 1024);
1229 float_ data
[] = { 6.1f
, 1.2f
, 1.3f
, -66.7f
};
1230 std::vector
<int_
> host_vector(4, 0);
1231 compute::svm_ptr
<float_
> ptr
= compute::svm_alloc
<float_
>(context
, 4);
1233 queue
.enqueue_svm_map(ptr
.get(), 4 * sizeof(cl_int
), CL_MAP_WRITE
);
1234 for(size_t i
= 0; i
< 4; i
++) {
1235 static_cast<float_
*>(ptr
.get())[i
] = data
[i
];
1237 queue
.enqueue_svm_unmap(ptr
.get()).wait();
1239 // copy host float svm data to int host vector
1240 bc::copy(ptr
, ptr
+ 4, host_vector
.begin(), queue
);
1242 CHECK_HOST_RANGE_EQUAL(
1245 host_vector
.begin(),
1247 static_cast<int_
>(6.1f
),
1248 static_cast<int_
>(1.2f
),
1249 static_cast<int_
>(1.3f
),
1250 static_cast<int_
>(-66.7f
)
1254 compute::svm_free(context
, ptr
);
1257 parameters
->set(cache_key
, "map_copy_threshold", map_copy_threshold
);
1260 BOOST_AUTO_TEST_CASE(copy_to_host_svm_float_to_int_convert_on_host
)
1262 REQUIRES_OPENCL_VERSION(2, 0);
1264 if(bug_in_svmmemcpy(device
)){
1265 std::cerr
<< "skipping svmmemcpy test case" << std::endl
;
1269 using compute::int_
;
1270 using compute::uint_
;
1271 using compute::float_
;
1273 std::string cache_key
=
1274 std::string("__boost_compute_copy_to_host_float_int");
1275 boost::shared_ptr
<bc::detail::parameter_cache
> parameters
=
1276 bc::detail::parameter_cache::get_global_cache(device
);
1279 uint_ map_copy_threshold
=
1280 parameters
->get(cache_key
, "map_copy_threshold", 0);
1281 uint_ direct_copy_threshold
=
1282 parameters
->get(cache_key
, "direct_copy_threshold", 0);
1284 // force copying by copying input device vector to temporary
1285 // host vector of the same type and then copying from that temporary
1286 // vector to result using std::copy()
1287 parameters
->set(cache_key
, "map_copy_threshold", 0);
1288 parameters
->set(cache_key
, "direct_copy_threshold", 1024);
1290 float_ data
[] = { 6.1f
, 1.2f
, 1.3f
, 766.7f
};
1291 std::vector
<int_
> host_vector(4, 0);
1292 compute::svm_ptr
<float_
> ptr
= compute::svm_alloc
<float_
>(context
, 4);
1294 queue
.enqueue_svm_map(ptr
.get(), 4 * sizeof(cl_int
), CL_MAP_WRITE
);
1295 for(size_t i
= 0; i
< 4; i
++) {
1296 static_cast<float_
*>(ptr
.get())[i
] = data
[i
];
1298 queue
.enqueue_svm_unmap(ptr
.get()).wait();
1300 // copy host float svm data to int host vector
1301 bc::copy(ptr
, ptr
+ 4, host_vector
.begin(), queue
);
1303 CHECK_HOST_RANGE_EQUAL(
1306 host_vector
.begin(),
1308 static_cast<int_
>(6.1f
),
1309 static_cast<int_
>(1.2f
),
1310 static_cast<int_
>(1.3f
),
1311 static_cast<int_
>(766.7f
)
1315 compute::svm_free(context
, ptr
);
1318 parameters
->set(cache_key
, "map_copy_threshold", map_copy_threshold
);
1319 parameters
->set(cache_key
, "direct_copy_threshold", direct_copy_threshold
);
1322 BOOST_AUTO_TEST_CASE(copy_to_host_svm_float_to_int_transform
)
1324 REQUIRES_OPENCL_VERSION(2, 0);
1326 using compute::int_
;
1327 using compute::uint_
;
1328 using compute::float_
;
1330 std::string cache_key
=
1331 std::string("__boost_compute_copy_to_host_float_int");
1332 boost::shared_ptr
<bc::detail::parameter_cache
> parameters
=
1333 bc::detail::parameter_cache::get_global_cache(device
);
1336 uint_ map_copy_threshold
=
1337 parameters
->get(cache_key
, "map_copy_threshold", 0);
1338 uint_ direct_copy_threshold
=
1339 parameters
->get(cache_key
, "direct_copy_threshold", 0);
1341 // force copying by copying input device vector to temporary
1342 // host vector of the same type and then copying from that temporary
1343 // vector to result using std::copy()
1344 parameters
->set(cache_key
, "map_copy_threshold", 0);
1345 parameters
->set(cache_key
, "direct_copy_threshold", 0);
1347 float_ data
[] = { 0.1f
, 11.2f
, 1.3f
, -66.7f
};
1348 std::vector
<int_
> host_vector(4, 0);
1349 compute::svm_ptr
<float_
> ptr
= compute::svm_alloc
<float_
>(context
, 4);
1351 queue
.enqueue_svm_map(ptr
.get(), 4 * sizeof(cl_int
), CL_MAP_WRITE
);
1352 for(size_t i
= 0; i
< 4; i
++) {
1353 static_cast<float_
*>(ptr
.get())[i
] = data
[i
];
1355 queue
.enqueue_svm_unmap(ptr
.get()).wait();
1357 // copy host float svm data to int host vector
1358 bc::copy(ptr
, ptr
+ 4, host_vector
.begin(), queue
);
1360 CHECK_HOST_RANGE_EQUAL(
1363 host_vector
.begin(),
1365 static_cast<int_
>(0.1f
),
1366 static_cast<int_
>(11.2f
),
1367 static_cast<int_
>(1.3f
),
1368 static_cast<int_
>(-66.7f
)
1372 compute::svm_free(context
, ptr
);
1375 parameters
->set(cache_key
, "map_copy_threshold", map_copy_threshold
);
1376 parameters
->set(cache_key
, "direct_copy_threshold", direct_copy_threshold
);
1380 BOOST_AUTO_TEST_SUITE_END()