123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380 |
- //---------------------------------------------------------------------------//
- // Copyright (c) 2016 Jakub Szuppe <j.szuppe@gmail.com>
- //
- // Distributed under the Boost Software License, Version 1.0
- // See accompanying file LICENSE_1_0.txt or copy at
- // http://www.boost.org/LICENSE_1_0.txt
- //
- // See http://boostorg.github.com/compute for more information.
- //---------------------------------------------------------------------------//
- // Undefining BOOST_COMPUTE_USE_OFFLINE_CACHE macro as we want to modify cached
- // parameters for copy algorithm without any undesirable consequences (like
- // saving modified values of those parameters).
- #ifdef BOOST_COMPUTE_USE_OFFLINE_CACHE
- #undef BOOST_COMPUTE_USE_OFFLINE_CACHE
- #endif
- #define BOOST_TEST_MODULE TestCopyTypeMismatch
- #include <boost/test/unit_test.hpp>
- #include <list>
- #include <vector>
- #include <string>
- #include <sstream>
- #include <iterator>
- #include <iostream>
- #include <boost/compute/svm.hpp>
- #include <boost/compute/system.hpp>
- #include <boost/compute/functional.hpp>
- #include <boost/compute/command_queue.hpp>
- #include <boost/compute/algorithm/copy.hpp>
- #include <boost/compute/async/future.hpp>
- #include <boost/compute/container/vector.hpp>
- #include <boost/compute/detail/device_ptr.hpp>
- #include <boost/compute/memory/svm_ptr.hpp>
- #include <boost/compute/detail/parameter_cache.hpp>
- #include "quirks.hpp"
- #include "check_macros.hpp"
- #include "context_setup.hpp"
- namespace bc = boost::compute;
- namespace compute = boost::compute;
- BOOST_AUTO_TEST_CASE(is_same_ignore_const)
- {
- BOOST_STATIC_ASSERT((
- boost::compute::detail::is_same_value_type<
- std::vector<int>::iterator,
- compute::buffer_iterator<int>
- >::value
- ));
- BOOST_STATIC_ASSERT((
- boost::compute::detail::is_same_value_type<
- std::vector<int>::const_iterator,
- compute::buffer_iterator<int>
- >::value
- ));
- BOOST_STATIC_ASSERT((
- boost::compute::detail::is_same_value_type<
- std::vector<int>::iterator,
- compute::buffer_iterator<const int>
- >::value
- ));
- BOOST_STATIC_ASSERT((
- boost::compute::detail::is_same_value_type<
- std::vector<int>::const_iterator,
- compute::buffer_iterator<const int>
- >::value
- ));
- }
- BOOST_AUTO_TEST_CASE(copy_to_device_float_to_double)
- {
- if(!device.supports_extension("cl_khr_fp64")) {
- std::cout << "skipping test: device does not support double" << std::endl;
- return;
- }
- using compute::double_;
- using compute::float_;
- float_ host[] = { 6.1f, 10.2f, 19.3f, 25.4f };
- bc::vector<double_> device_vector(4, context);
- // copy host float data to double device vector
- bc::copy(host, host + 4, device_vector.begin(), queue);
- CHECK_RANGE_EQUAL(double_, 4, device_vector, (6.1f, 10.2f, 19.3f, 25.4f));
- }
- BOOST_AUTO_TEST_CASE(copy_to_device_float_to_int)
- {
- using compute::int_;
- using compute::float_;
- float_ host[] = { 6.1f, -10.2f, 19.3f, 25.4f };
- bc::vector<int_> device_vector(4, context);
- // copy host float data to int device vector
- bc::copy(host, host + 4, device_vector.begin(), queue);
- CHECK_RANGE_EQUAL(
- int_,
- 4,
- device_vector,
- (
- static_cast<int_>(6.1f),
- static_cast<int_>(-10.2f),
- static_cast<int_>(19.3f),
- static_cast<int_>(25.4f)
- )
- );
- }
- // HOST -> DEVICE
- BOOST_AUTO_TEST_CASE(copy_to_device_float_to_int_mapping_device_vector)
- {
- using compute::int_;
- using compute::uint_;
- using compute::float_;
- float_ host[] = { 6.1f, -10.2f, 19.3f, 25.4f };
- bc::vector<int_> device_vector(4, context);
- std::string cache_key =
- std::string("__boost_compute_copy_to_device_float_int");
- boost::shared_ptr<bc::detail::parameter_cache> parameters =
- bc::detail::parameter_cache::get_global_cache(device);
- // save
- uint_ map_copy_threshold =
- parameters->get(cache_key, "map_copy_threshold", 0);
- // force copy_to_device_map (mapping device vector to the host)
- parameters->set(cache_key, "map_copy_threshold", 1024);
- // copy host float data to int device vector
- bc::copy(host, host + 4, device_vector.begin(), queue);
- CHECK_RANGE_EQUAL(
- int_,
- 4,
- device_vector,
- (
- static_cast<int_>(6.1f),
- static_cast<int_>(-10.2f),
- static_cast<int_>(19.3f),
- static_cast<int_>(25.4f)
- )
- );
- // restore
- parameters->set(cache_key, "map_copy_threshold", map_copy_threshold);
- }
- BOOST_AUTO_TEST_CASE(copy_to_device_float_to_int_convert_on_host)
- {
- using compute::int_;
- using compute::uint_;
- using compute::float_;
- std::string cache_key =
- std::string("__boost_compute_copy_to_device_float_int");
- boost::shared_ptr<bc::detail::parameter_cache> parameters =
- bc::detail::parameter_cache::get_global_cache(device);
- // save
- uint_ map_copy_threshold =
- parameters->get(cache_key, "map_copy_threshold", 0);
- uint_ direct_copy_threshold =
- parameters->get(cache_key, "direct_copy_threshold", 0);
- // force copying by casting input data on host and performing
- // normal copy host->device (since types match now)
- parameters->set(cache_key, "map_copy_threshold", 0);
- parameters->set(cache_key, "direct_copy_threshold", 1024);
- float_ host[] = { 6.1f, -10.2f, 19.3f, 25.4f };
- bc::vector<int_> device_vector(4, context);
- // copy host float data to int device vector
- bc::copy(host, host + 4, device_vector.begin(), queue);
- CHECK_RANGE_EQUAL(
- int_,
- 4,
- device_vector,
- (
- static_cast<int_>(6.1f),
- static_cast<int_>(-10.2f),
- static_cast<int_>(19.3f),
- static_cast<int_>(25.4f)
- )
- );
- // restore
- parameters->set(cache_key, "map_copy_threshold", map_copy_threshold);
- parameters->set(cache_key, "direct_copy_threshold", direct_copy_threshold);
- }
- BOOST_AUTO_TEST_CASE(copy_to_device_float_to_int_with_transform)
- {
- using compute::int_;
- using compute::uint_;
- using compute::float_;
- std::string cache_key =
- std::string("__boost_compute_copy_to_device_float_int");
- boost::shared_ptr<bc::detail::parameter_cache> parameters =
- bc::detail::parameter_cache::get_global_cache(device);
- // save
- uint_ map_copy_threshold =
- parameters->get(cache_key, "map_copy_threshold", 0);
- uint_ direct_copy_threshold =
- parameters->get(cache_key, "direct_copy_threshold", 0);
- // force copying by mapping input data to the device memory
- // and using transform operation for casting & copying
- parameters->set(cache_key, "map_copy_threshold", 0);
- parameters->set(cache_key, "direct_copy_threshold", 0);
- float_ host[] = { 6.1f, -10.2f, 19.3f, 25.4f };
- bc::vector<int_> device_vector(4, context);
- // copy host float data to int device vector
- bc::copy(host, host + 4, device_vector.begin(), queue);
- CHECK_RANGE_EQUAL(
- int_,
- 4,
- device_vector,
- (
- static_cast<int_>(6.1f),
- static_cast<int_>(-10.2f),
- static_cast<int_>(19.3f),
- static_cast<int_>(25.4f)
- )
- );
- // restore
- parameters->set(cache_key, "map_copy_threshold", map_copy_threshold);
- parameters->set(cache_key, "direct_copy_threshold", direct_copy_threshold);
- }
- BOOST_AUTO_TEST_CASE(copy_async_to_device_float_to_int)
- {
- using compute::int_;
- using compute::float_;
- float_ host[] = { 6.1f, -10.2f, 19.3f, 25.4f };
- bc::vector<int_> device_vector(4, context);
- // copy host float data to int device vector
- compute::future<void> future =
- bc::copy_async(host, host + 4, device_vector.begin(), queue);
- future.wait();
- CHECK_RANGE_EQUAL(
- int_,
- 4,
- device_vector,
- (
- static_cast<int_>(6.1f),
- static_cast<int_>(-10.2f),
- static_cast<int_>(19.3f),
- static_cast<int_>(25.4f)
- )
- );
- }
- BOOST_AUTO_TEST_CASE(copy_async_to_device_float_to_int_empty)
- {
- using compute::int_;
- using compute::float_;
- float_ host[] = { 6.1f, -10.2f, 19.3f, 25.4f };
- bc::vector<int_> device_vector(size_t(4), int_(1), queue);
- // copy nothing to int device vector
- compute::future<bc::vector<int_>::iterator > future =
- bc::copy_async(host, host, device_vector.begin(), queue);
- if(future.valid()) {
- future.wait();
- }
- CHECK_RANGE_EQUAL(
- int_,
- 4,
- device_vector,
- (
- int_(1),
- int_(1),
- int_(1),
- int_(1)
- )
- );
- }
- // Test copying from a std::list to a bc::vector. This differs from
- // the test copying from std::vector because std::list has non-contiguous
- // storage for its data values.
- BOOST_AUTO_TEST_CASE(copy_to_device_float_to_int_list_device_map)
- {
- using compute::int_;
- using compute::uint_;
- using compute::float_;
- std::string cache_key =
- std::string("__boost_compute_copy_to_device_float_int");
- boost::shared_ptr<bc::detail::parameter_cache> parameters =
- bc::detail::parameter_cache::get_global_cache(device);
- // save
- uint_ map_copy_threshold =
- parameters->get(cache_key, "map_copy_threshold", 0);
- // force copy_to_device_map (mapping device vector to the host)
- parameters->set(cache_key, "map_copy_threshold", 1024);
- float_ data[] = { 6.1f, -10.2f, 19.3f, 25.4f };
- std::list<float_> host(data, data + 4);
- bc::vector<int_> device_vector(4, context);
- // copy host float data to int device vector
- bc::copy(host.begin(), host.end(), device_vector.begin(), queue);
- CHECK_RANGE_EQUAL(
- int_,
- 4,
- device_vector,
- (
- static_cast<int_>(6.1f),
- static_cast<int_>(-10.2f),
- static_cast<int_>(19.3f),
- static_cast<int_>(25.4f)
- )
- );
- // restore
- parameters->set(cache_key, "map_copy_threshold", map_copy_threshold);
- }
- // Test copying from a std::list to a bc::vector. This differs from
- // the test copying from std::vector because std::list has non-contiguous
- // storage for its data values.
- BOOST_AUTO_TEST_CASE(copy_to_device_float_to_int_list_convert_on_host)
- {
- using compute::int_;
- using compute::uint_;
- using compute::float_;
- std::string cache_key =
- std::string("__boost_compute_copy_to_device_float_int");
- boost::shared_ptr<bc::detail::parameter_cache> parameters =
- bc::detail::parameter_cache::get_global_cache(device);
- // save
- uint_ map_copy_threshold =
- parameters->get(cache_key, "map_copy_threshold", 0);
- uint_ direct_copy_threshold =
- parameters->get(cache_key, "direct_copy_threshold", 0);
- // force copying by casting input data on host and performing
- // normal copy host->device (since types match now)
- parameters->set(cache_key, "map_copy_threshold", 0);
- parameters->set(cache_key, "direct_copy_threshold", 1024);
- float_ data[] = { 6.1f, -10.2f, 19.3f, 25.4f };
- std::list<float_> host(data, data + 4);
- bc::vector<int_> device_vector(4, context);
- // copy host float data to int device vector
- bc::copy(host.begin(), host.end(), device_vector.begin(), queue);
- CHECK_RANGE_EQUAL(
- int_,
- 4,
- device_vector,
- (
- static_cast<int_>(6.1f),
- static_cast<int_>(-10.2f),
- static_cast<int_>(19.3f),
- static_cast<int_>(25.4f)
- )
- );
- // restore
- parameters->set(cache_key, "map_copy_threshold", map_copy_threshold);
- parameters->set(cache_key, "direct_copy_threshold", direct_copy_threshold);
- }
- // SVM requires OpenCL 2.0
- #if defined(BOOST_COMPUTE_CL_VERSION_2_0) || defined(BOOST_COMPUTE_DOXYGEN_INVOKED)
- BOOST_AUTO_TEST_CASE(copy_to_device_svm_float_to_int_map)
- {
- REQUIRES_OPENCL_VERSION(2, 0);
- using compute::int_;
- using compute::uint_;
- using compute::float_;
- std::string cache_key =
- std::string("__boost_compute_copy_to_device_float_int");
- boost::shared_ptr<bc::detail::parameter_cache> parameters =
- bc::detail::parameter_cache::get_global_cache(device);
- // save
- uint_ map_copy_threshold =
- parameters->get(cache_key, "map_copy_threshold", 0);
- // force copy_to_device_map (mapping device vector to the host)
- parameters->set(cache_key, "map_copy_threshold", 1024);
- float_ host[] = { 5.1f, -10.3f, 19.4f, 26.7f };
- compute::svm_ptr<int_> ptr = compute::svm_alloc<int_>(context, 4);
- // copy host float data to int device vector
- bc::copy(host, host + 4, ptr, queue);
- queue.enqueue_svm_map(ptr.get(), 4 * sizeof(cl_int), CL_MAP_READ);
- CHECK_HOST_RANGE_EQUAL(
- int_,
- 4,
- static_cast<int_*>(ptr.get()),
- (
- static_cast<int_>(5.1f),
- static_cast<int_>(-10.3f),
- static_cast<int_>(19.4f),
- static_cast<int_>(26.7f)
- )
- );
- queue.enqueue_svm_unmap(ptr.get()).wait();
- compute::svm_free(context, ptr);
- // restore
- parameters->set(cache_key, "map_copy_threshold", map_copy_threshold);
- }
- BOOST_AUTO_TEST_CASE(copy_to_device_svm_float_to_int_convert_on_host)
- {
- REQUIRES_OPENCL_VERSION(2, 0);
- if(bug_in_svmmemcpy(device)){
- std::cerr << "skipping svmmemcpy test case" << std::endl;
- return;
- }
- using compute::int_;
- using compute::uint_;
- using compute::float_;
- std::string cache_key =
- std::string("__boost_compute_copy_to_device_float_int");
- boost::shared_ptr<bc::detail::parameter_cache> parameters =
- bc::detail::parameter_cache::get_global_cache(device);
- // save
- uint_ map_copy_threshold =
- parameters->get(cache_key, "map_copy_threshold", 0);
- uint_ direct_copy_threshold =
- parameters->get(cache_key, "direct_copy_threshold", 0);
- // force copying by casting input data on host and performing
- // normal copy host->device (since types match now)
- parameters->set(cache_key, "map_copy_threshold", 0);
- parameters->set(cache_key, "direct_copy_threshold", 1024);
- float_ host[] = { 0.1f, 10.3f, 9.4f, -26.7f };
- compute::svm_ptr<int_> ptr = compute::svm_alloc<int_>(context, 4);
- // copy host float data to int device vector
- bc::copy(host, host + 4, ptr, queue);
- queue.enqueue_svm_map(ptr.get(), 4 * sizeof(cl_int), CL_MAP_READ);
- CHECK_HOST_RANGE_EQUAL(
- int_,
- 4,
- static_cast<int_*>(ptr.get()),
- (
- static_cast<int_>(0.1f),
- static_cast<int_>(10.3f),
- static_cast<int_>(9.4f),
- static_cast<int_>(-26.7f)
- )
- );
- queue.enqueue_svm_unmap(ptr.get()).wait();
- compute::svm_free(context, ptr);
- // restore
- parameters->set(cache_key, "map_copy_threshold", map_copy_threshold);
- parameters->set(cache_key, "direct_copy_threshold", direct_copy_threshold);
- }
- BOOST_AUTO_TEST_CASE(copy_to_device_svm_float_to_int_with_transform)
- {
- REQUIRES_OPENCL_VERSION(2, 0);
- using compute::int_;
- using compute::uint_;
- using compute::float_;
- std::string cache_key =
- std::string("__boost_compute_copy_to_device_float_int");
- boost::shared_ptr<bc::detail::parameter_cache> parameters =
- bc::detail::parameter_cache::get_global_cache(device);
- // save
- uint_ map_copy_threshold =
- parameters->get(cache_key, "map_copy_threshold", 0);
- uint_ direct_copy_threshold =
- parameters->get(cache_key, "direct_copy_threshold", 0);
- // force copying by mapping input data to the device memory
- // and using transform operation (copy kernel) for casting & copying
- parameters->set(cache_key, "map_copy_threshold", 0);
- parameters->set(cache_key, "direct_copy_threshold", 0);
- float_ host[] = { 4.1f, -11.3f, 219.4f, -26.7f };
- compute::svm_ptr<int_> ptr = compute::svm_alloc<int_>(context, 4);
- // copy host float data to int device vector
- bc::copy(host, host + 4, ptr, queue);
- queue.enqueue_svm_map(ptr.get(), 4 * sizeof(cl_int), CL_MAP_READ);
- CHECK_HOST_RANGE_EQUAL(
- int_,
- 4,
- static_cast<int_*>(ptr.get()),
- (
- static_cast<int_>(4.1f),
- static_cast<int_>(-11.3f),
- static_cast<int_>(219.4f),
- static_cast<int_>(-26.7f)
- )
- );
- queue.enqueue_svm_unmap(ptr.get()).wait();
- compute::svm_free(context, ptr);
- // restore
- parameters->set(cache_key, "map_copy_threshold", map_copy_threshold);
- parameters->set(cache_key, "direct_copy_threshold", direct_copy_threshold);
- }
- BOOST_AUTO_TEST_CASE(copy_async_to_device_svm_float_to_int)
- {
- REQUIRES_OPENCL_VERSION(2, 0);
- using compute::int_;
- using compute::uint_;
- using compute::float_;
- float_ host[] = { 44.1f, -14.3f, 319.4f, -26.7f };
- compute::svm_ptr<int_> ptr = compute::svm_alloc<int_>(context, 4);
- // copy host float data to int device vector
- compute::future<void> future =
- bc::copy_async(host, host + 4, ptr, queue);
- future.wait();
- queue.enqueue_svm_map(ptr.get(), 4 * sizeof(cl_int), CL_MAP_READ);
- CHECK_HOST_RANGE_EQUAL(
- int_,
- 4,
- static_cast<int_*>(ptr.get()),
- (
- static_cast<int_>(44.1f),
- static_cast<int_>(-14.3f),
- static_cast<int_>(319.4f),
- static_cast<int_>(-26.7f)
- )
- );
- queue.enqueue_svm_unmap(ptr.get()).wait();
- compute::svm_free(context, ptr);
- }
- #endif
- // DEVICE -> DEVICE
- BOOST_AUTO_TEST_CASE(copy_on_device_float_to_int)
- {
- using compute::int_;
- using compute::float_;
- float_ data[] = { 6.1f, -10.2f, 19.3f, 25.4f };
- bc::vector<float_> device_fvector(data, data + 4, queue);
- bc::vector<int_> device_ivector(4, context);
- // copy device float vector to device int vector
- bc::copy(
- device_fvector.begin(),
- device_fvector.end(),
- device_ivector.begin(),
- queue
- );
- CHECK_RANGE_EQUAL(
- int_,
- 4,
- device_ivector,
- (
- static_cast<int_>(6.1f),
- static_cast<int_>(-10.2f),
- static_cast<int_>(19.3f),
- static_cast<int_>(25.4f)
- )
- );
- }
- BOOST_AUTO_TEST_CASE(copy_async_on_device_float_to_int)
- {
- using compute::int_;
- using compute::float_;
- float_ data[] = { 6.1f, -10.2f, 19.3f, 25.4f };
- bc::vector<float_> device_fvector(data, data + 4, queue);
- bc::vector<int_> device_ivector(4, context);
- // copy device float vector to device int vector
- compute::future<void> future =
- bc::copy_async(
- device_fvector.begin(),
- device_fvector.end(),
- device_ivector.begin(),
- queue
- );
- future.wait();
- CHECK_RANGE_EQUAL(
- int_,
- 4,
- device_ivector,
- (
- static_cast<int_>(6.1f),
- static_cast<int_>(-10.2f),
- static_cast<int_>(19.3f),
- static_cast<int_>(25.4f)
- )
- );
- }
- BOOST_AUTO_TEST_CASE(copy_async_on_device_float_to_int_empty)
- {
- using compute::int_;
- using compute::float_;
- float_ data[] = { 6.1f, -10.2f, 19.3f, 25.4f };
- bc::vector<float_> device_fvector(data, data + 4, queue);
- bc::vector<int_> device_ivector(size_t(4), int_(1), queue);
- // copy device float vector to device int vector
- compute::future<void> future =
- bc::copy_async(
- device_fvector.begin(),
- device_fvector.begin(),
- device_ivector.begin(),
- queue
- );
- if(future.valid()) {
- future.wait();
- }
- CHECK_RANGE_EQUAL(
- int_,
- 4,
- device_ivector,
- (
- int_(1),
- int_(1),
- int_(1),
- int_(1)
- )
- );
- }
- // SVM requires OpenCL 2.0
- #if defined(BOOST_COMPUTE_CL_VERSION_2_0) || defined(BOOST_COMPUTE_DOXYGEN_INVOKED)
- BOOST_AUTO_TEST_CASE(copy_on_device_buffer_to_svm_float_to_int)
- {
- REQUIRES_OPENCL_VERSION(2, 0);
- using compute::int_;
- using compute::float_;
- float_ data[] = { 65.1f, -110.2f, -19.3f, 26.7f };
- bc::vector<float_> device_vector(data, data + 4, queue);
- compute::svm_ptr<int_> ptr = compute::svm_alloc<int_>(context, 4);
- // copy host float data to int svm memory
- bc::copy(device_vector.begin(), device_vector.end(), ptr, queue);
- queue.enqueue_svm_map(ptr.get(), 4 * sizeof(cl_int), CL_MAP_READ);
- CHECK_HOST_RANGE_EQUAL(
- int_,
- 4,
- static_cast<int_*>(ptr.get()),
- (
- static_cast<int_>(65.1f),
- static_cast<int_>(-110.2f),
- static_cast<int_>(-19.3f),
- static_cast<int_>(26.7f)
- )
- );
- queue.enqueue_svm_unmap(ptr.get()).wait();
- compute::svm_free(context, ptr);
- }
- BOOST_AUTO_TEST_CASE(copy_on_device_svm_to_buffer_float_to_int)
- {
- REQUIRES_OPENCL_VERSION(2, 0);
- using compute::int_;
- using compute::float_;
- float_ data[] = { 6.1f, 11.2f, 19.3f, 6.7f };
- bc::vector<int_> device_vector(4, context);
- compute::svm_ptr<float_> ptr = compute::svm_alloc<float_>(context, 4);
- queue.enqueue_svm_map(ptr.get(), 4 * sizeof(cl_int), CL_MAP_WRITE);
- for(size_t i = 0; i < 4; i++) {
- static_cast<float_*>(ptr.get())[i] = data[i];
- }
- queue.enqueue_svm_unmap(ptr.get()).wait();
- // copy host float svm data to int device vector
- bc::copy(ptr, ptr + 4, device_vector.begin(), queue);
- CHECK_RANGE_EQUAL(
- int_,
- 4,
- device_vector,
- (
- static_cast<int_>(6.1f),
- static_cast<int_>(11.2f),
- static_cast<int_>(19.3f),
- static_cast<int_>(6.7f)
- )
- );
- compute::svm_free(context, ptr);
- }
- BOOST_AUTO_TEST_CASE(copy_on_device_svm_to_svm_float_to_int)
- {
- REQUIRES_OPENCL_VERSION(2, 0);
- using compute::int_;
- using compute::float_;
- float_ data[] = { 0.1f, -10.2f, -1.3f, 2.7f };
- compute::svm_ptr<float_> ptr = compute::svm_alloc<float_>(context, 4);
- compute::svm_ptr<int_> ptr2 = compute::svm_alloc<int_>(context, 4);
- queue.enqueue_svm_map(ptr.get(), 4 * sizeof(cl_int), CL_MAP_WRITE);
- for(size_t i = 0; i < 4; i++) {
- static_cast<float_*>(ptr.get())[i] = data[i];
- }
- queue.enqueue_svm_unmap(ptr.get()).wait();
- // copy host float svm to int svm
- bc::copy(ptr, ptr + 4, ptr2, queue);
- queue.enqueue_svm_map(ptr2.get(), 4 * sizeof(cl_int), CL_MAP_READ);
- CHECK_HOST_RANGE_EQUAL(
- int_,
- 4,
- static_cast<int_*>(ptr2.get()),
- (
- static_cast<int_>(0.1f),
- static_cast<int_>(-10.2f),
- static_cast<int_>(-1.3f),
- static_cast<int_>(2.7f)
- )
- );
- queue.enqueue_svm_unmap(ptr2.get()).wait();
- compute::svm_free(context, ptr);
- compute::svm_free(context, ptr2);
- }
- BOOST_AUTO_TEST_CASE(copy_async_on_device_buffer_to_svm_float_to_int)
- {
- REQUIRES_OPENCL_VERSION(2, 0);
- using compute::int_;
- using compute::float_;
- float_ data[] = { 65.1f, -110.2f, -19.3f, 26.7f };
- bc::vector<float_> device_vector(data, data + 4, queue);
- compute::svm_ptr<int_> ptr = compute::svm_alloc<int_>(context, 4);
- // copy host float data to int svm memory
- compute::future<bc::svm_ptr<int_> > future =
- bc::copy_async(device_vector.begin(), device_vector.end(), ptr, queue);
- future.wait();
- queue.enqueue_svm_map(ptr.get(), 4 * sizeof(cl_int), CL_MAP_READ);
- CHECK_HOST_RANGE_EQUAL(
- int_,
- 4,
- static_cast<int_*>(ptr.get()),
- (
- static_cast<int_>(65.1f),
- static_cast<int_>(-110.2f),
- static_cast<int_>(-19.3f),
- static_cast<int_>(26.7f)
- )
- );
- queue.enqueue_svm_unmap(ptr.get()).wait();
- compute::svm_free(context, ptr);
- }
- BOOST_AUTO_TEST_CASE(copy_async_on_device_svm_to_buffer_float_to_int)
- {
- REQUIRES_OPENCL_VERSION(2, 0);
- using compute::int_;
- using compute::float_;
- float_ data[] = { 65.1f, -110.2f, -19.3f, 26.7f };
- bc::vector<int_> device_vector(4, context);
- compute::svm_ptr<float_> ptr = compute::svm_alloc<float_>(context, 4);
- queue.enqueue_svm_map(ptr.get(), 4 * sizeof(cl_int), CL_MAP_WRITE);
- for(size_t i = 0; i < 4; i++) {
- static_cast<float_*>(ptr.get())[i] = data[i];
- }
- queue.enqueue_svm_unmap(ptr.get()).wait();
- // copy host float svm data to int device vector
- compute::future<bc::vector<int_>::iterator > future =
- bc::copy_async(ptr, ptr + 4, device_vector.begin(), queue);
- future.wait();
- CHECK_RANGE_EQUAL(
- int_,
- 4,
- device_vector,
- (
- static_cast<int_>(65.1f),
- static_cast<int_>(-110.2f),
- static_cast<int_>(-19.3f),
- static_cast<int_>(26.7f)
- )
- );
- compute::svm_free(context, ptr);
- }
- BOOST_AUTO_TEST_CASE(copy_async_on_device_svm_to_svm_float_to_int)
- {
- REQUIRES_OPENCL_VERSION(2, 0);
- using compute::int_;
- using compute::float_;
- float_ data[] = { 0.1f, -10.2f, -1.3f, 2.7f };
- compute::svm_ptr<float_> ptr = compute::svm_alloc<float_>(context, 4);
- compute::svm_ptr<int_> ptr2 = compute::svm_alloc<int_>(context, 4);
- queue.enqueue_svm_map(ptr.get(), 4 * sizeof(cl_int), CL_MAP_WRITE);
- for(size_t i = 0; i < 4; i++) {
- static_cast<float_*>(ptr.get())[i] = data[i];
- }
- queue.enqueue_svm_unmap(ptr.get()).wait();
- // copy host float svm to int svm
- compute::future<bc::svm_ptr<int_> > future =
- bc::copy_async(ptr, ptr + 4, ptr2, queue);
- future.wait();
- queue.enqueue_svm_map(ptr2.get(), 4 * sizeof(cl_int), CL_MAP_READ);
- CHECK_HOST_RANGE_EQUAL(
- int_,
- 4,
- static_cast<int_*>(ptr2.get()),
- (
- static_cast<int_>(0.1f),
- static_cast<int_>(-10.2f),
- static_cast<int_>(-1.3f),
- static_cast<int_>(2.7f)
- )
- );
- queue.enqueue_svm_unmap(ptr2.get()).wait();
- compute::svm_free(context, ptr);
- compute::svm_free(context, ptr2);
- }
- #endif
- // DEVICE -> HOST
- BOOST_AUTO_TEST_CASE(copy_to_host_float_to_int)
- {
- using compute::int_;
- using compute::float_;
- float_ data[] = { 6.1f, -10.2f, 19.3f, 25.4f };
- bc::vector<float_> device_vector(data, data + 4, queue);
- std::vector<int_> host_vector(4);
- // copy device float vector to int host vector
- bc::copy(device_vector.begin(), device_vector.end(), host_vector.begin(), queue);
- CHECK_HOST_RANGE_EQUAL(
- int_,
- 4,
- host_vector.begin(),
- (
- static_cast<int_>(6.1f),
- static_cast<int_>(-10.2f),
- static_cast<int_>(19.3f),
- static_cast<int_>(25.4f)
- )
- );
- }
- BOOST_AUTO_TEST_CASE(copy_to_host_float_to_int_map)
- {
- using compute::int_;
- using compute::uint_;
- using compute::float_;
- std::string cache_key =
- std::string("__boost_compute_copy_to_host_float_int");
- boost::shared_ptr<bc::detail::parameter_cache> parameters =
- bc::detail::parameter_cache::get_global_cache(device);
- // save
- uint_ map_copy_threshold =
- parameters->get(cache_key, "map_copy_threshold", 0);
- // force copy_to_host_map (mapping device vector to the host)
- parameters->set(cache_key, "map_copy_threshold", 1024);
- float_ data[] = { 6.1f, -10.2f, 19.3f, 25.4f };
- bc::vector<float_> device_vector(data, data + 4, queue);
- std::vector<int_> host_vector(4);
- // copy device float vector to int host vector
- bc::copy(device_vector.begin(), device_vector.end(), host_vector.begin(), queue);
- CHECK_HOST_RANGE_EQUAL(
- int_,
- 4,
- host_vector.begin(),
- (
- static_cast<int_>(6.1f),
- static_cast<int_>(-10.2f),
- static_cast<int_>(19.3f),
- static_cast<int_>(25.4f)
- )
- );
- // restore
- parameters->set(cache_key, "map_copy_threshold", map_copy_threshold);
- }
- BOOST_AUTO_TEST_CASE(copy_to_host_float_to_int_convert_on_host)
- {
- using compute::int_;
- using compute::uint_;
- using compute::float_;
- std::string cache_key =
- std::string("__boost_compute_copy_to_host_float_int");
- boost::shared_ptr<bc::detail::parameter_cache> parameters =
- bc::detail::parameter_cache::get_global_cache(device);
- // save
- uint_ map_copy_threshold =
- parameters->get(cache_key, "map_copy_threshold", 0);
- uint_ direct_copy_threshold =
- parameters->get(cache_key, "direct_copy_threshold", 0);
- // force copying by copying input device vector to temporary
- // host vector of the same type and then copying from that temporary
- // vector to result using std::copy()
- parameters->set(cache_key, "map_copy_threshold", 0);
- parameters->set(cache_key, "direct_copy_threshold", 1024);
- float_ data[] = { 6.1f, -10.2f, 19.3f, 25.4f };
- bc::vector<float_> device_vector(data, data + 4, queue);
- std::vector<int_> host_vector(4);
- // copy device float vector to int host vector
- bc::copy(device_vector.begin(), device_vector.end(), host_vector.begin(), queue);
- CHECK_HOST_RANGE_EQUAL(
- int_,
- 4,
- host_vector.begin(),
- (
- static_cast<int_>(6.1f),
- static_cast<int_>(-10.2f),
- static_cast<int_>(19.3f),
- static_cast<int_>(25.4f)
- )
- );
- // restore
- parameters->set(cache_key, "map_copy_threshold", map_copy_threshold);
- parameters->set(cache_key, "direct_copy_threshold", direct_copy_threshold);
- }
- BOOST_AUTO_TEST_CASE(copy_to_host_float_to_int_convert_on_device)
- {
- using compute::int_;
- using compute::uint_;
- using compute::float_;
- std::string cache_key =
- std::string("__boost_compute_copy_to_host_float_int");
- boost::shared_ptr<bc::detail::parameter_cache> parameters =
- bc::detail::parameter_cache::get_global_cache(device);
- // save
- uint_ map_copy_threshold =
- parameters->get(cache_key, "map_copy_threshold", 0);
- uint_ direct_copy_threshold =
- parameters->get(cache_key, "direct_copy_threshold", 0);
- // force copying by mapping output data to the device memory
- // and using transform operation for casting & copying
- parameters->set(cache_key, "map_copy_threshold", 0);
- parameters->set(cache_key, "direct_copy_threshold", 0);
- float_ data[] = { 6.1f, -10.2f, 19.3f, 25.4f };
- bc::vector<float_> device_vector(data, data + 4, queue);
- std::vector<int_> host_vector(4);
- // copy device float vector to int host vector
- bc::copy(device_vector.begin(), device_vector.end(), host_vector.begin(), queue);
- CHECK_HOST_RANGE_EQUAL(
- int_,
- 4,
- host_vector.begin(),
- (
- static_cast<int_>(6.1f),
- static_cast<int_>(-10.2f),
- static_cast<int_>(19.3f),
- static_cast<int_>(25.4f)
- )
- );
- // restore
- parameters->set(cache_key, "map_copy_threshold", map_copy_threshold);
- parameters->set(cache_key, "direct_copy_threshold", direct_copy_threshold);
- }
- // Test copying from a bc::vector to a std::list . This differs from
- // the test copying to std::vector because std::list has non-contiguous
- // storage for its data values.
- BOOST_AUTO_TEST_CASE(copy_to_host_list_float_to_int_map)
- {
- using compute::int_;
- using compute::uint_;
- using compute::float_;
- std::string cache_key =
- std::string("__boost_compute_copy_to_host_float_int");
- boost::shared_ptr<bc::detail::parameter_cache> parameters =
- bc::detail::parameter_cache::get_global_cache(device);
- // save
- uint_ map_copy_threshold =
- parameters->get(cache_key, "map_copy_threshold", 0);
- // force copy_to_host_map (mapping device vector to the host)
- parameters->set(cache_key, "map_copy_threshold", 1024);
- float_ data[] = { 6.1f, -10.2f, 19.3f, 25.4f };
- bc::vector<float_> device_vector(data, data + 4, queue);
- std::list<int_> host_list(4);
- // copy device float vector to int host vector
- bc::copy(device_vector.begin(), device_vector.end(), host_list.begin(), queue);
- int_ expected[4] = {
- static_cast<int_>(6.1f),
- static_cast<int_>(-10.2f),
- static_cast<int_>(19.3f),
- static_cast<int_>(25.4f)
- };
- BOOST_CHECK_EQUAL_COLLECTIONS(
- host_list.begin(), host_list.end(),
- expected, expected + 4
- );
- // restore
- parameters->set(cache_key, "map_copy_threshold", map_copy_threshold);
- }
- // Test copying from a bc::vector to a std::list . This differs from
- // the test copying to std::vector because std::list has non-contiguous
- // storage for its data values.
- BOOST_AUTO_TEST_CASE(copy_to_host_list_float_to_int_covert_on_host)
- {
- using compute::int_;
- using compute::uint_;
- using compute::float_;
- std::string cache_key =
- std::string("__boost_compute_copy_to_host_float_int");
- boost::shared_ptr<bc::detail::parameter_cache> parameters =
- bc::detail::parameter_cache::get_global_cache(device);
- // save
- uint_ map_copy_threshold =
- parameters->get(cache_key, "map_copy_threshold", 0);
- uint_ direct_copy_threshold =
- parameters->get(cache_key, "direct_copy_threshold", 0);
- // force copying by copying input device vector to temporary
- // host vector of the same type and then copying from that temporary
- // vector to result using std::copy()
- parameters->set(cache_key, "map_copy_threshold", 0);
- parameters->set(cache_key, "direct_copy_threshold", 1024);
- float_ data[] = { 6.1f, -10.2f, 19.3f, 25.4f };
- bc::vector<float_> device_vector(data, data + 4, queue);
- std::list<int_> host_list(4);
- // copy device float vector to int host vector
- bc::copy(device_vector.begin(), device_vector.end(), host_list.begin(), queue);
- int_ expected[4] = {
- static_cast<int_>(6.1f),
- static_cast<int_>(-10.2f),
- static_cast<int_>(19.3f),
- static_cast<int_>(25.4f)
- };
- BOOST_CHECK_EQUAL_COLLECTIONS(
- host_list.begin(), host_list.end(),
- expected, expected + 4
- );
- // restore
- parameters->set(cache_key, "map_copy_threshold", map_copy_threshold);
- parameters->set(cache_key, "direct_copy_threshold", direct_copy_threshold);
- }
- BOOST_AUTO_TEST_CASE(copy_async_to_host_float_to_int)
- {
- using compute::int_;
- using compute::float_;
- float_ data[] = { 6.1f, -10.2f, 19.3f, 25.4f };
- bc::vector<float_> device_vector(data, data + 4, queue);
- std::vector<int_> host_vector(device_vector.size());
- // copy device float vector to host int vector
- compute::future<void> future =
- bc::copy_async(
- device_vector.begin(),
- device_vector.end(),
- host_vector.begin(),
- queue
- );
- future.wait();
- CHECK_HOST_RANGE_EQUAL(
- int_,
- 4,
- host_vector.begin(),
- (
- static_cast<int_>(6.1f),
- static_cast<int_>(-10.2f),
- static_cast<int_>(19.3f),
- static_cast<int_>(25.4f)
- )
- );
- }
- BOOST_AUTO_TEST_CASE(copy_async_to_host_float_to_int_empty)
- {
- using compute::int_;
- using compute::float_;
- float_ data[] = { 6.1f, -10.2f, 19.3f, 25.4f };
- bc::vector<float_> device_vector(data, data + 4, queue);
- std::vector<int_> host_vector(device_vector.size(), int_(1));
- // copy device float vector to host int vector
- compute::future<void> future =
- bc::copy_async(
- device_vector.begin(),
- device_vector.begin(),
- host_vector.begin(),
- queue
- );
- if(future.valid()) {
- future.wait();
- }
- CHECK_HOST_RANGE_EQUAL(
- int_,
- 4,
- host_vector.begin(),
- (
- int_(1),
- int_(1),
- int_(1),
- int_(1)
- )
- );
- }
- // SVM requires OpenCL 2.0
- #if defined(BOOST_COMPUTE_CL_VERSION_2_0) || defined(BOOST_COMPUTE_DOXYGEN_INVOKED)
- BOOST_AUTO_TEST_CASE(copy_to_host_svm_float_to_int_map)
- {
- REQUIRES_OPENCL_VERSION(2, 0);
- using compute::int_;
- using compute::uint_;
- using compute::float_;
- std::string cache_key =
- std::string("__boost_compute_copy_to_host_float_int");
- boost::shared_ptr<bc::detail::parameter_cache> parameters =
- bc::detail::parameter_cache::get_global_cache(device);
- // save
- uint_ map_copy_threshold =
- parameters->get(cache_key, "map_copy_threshold", 0);
- // force copy_to_host_map (mapping device vector to the host)
- parameters->set(cache_key, "map_copy_threshold", 1024);
- float_ data[] = { 6.1f, 1.2f, 1.3f, -66.7f };
- std::vector<int_> host_vector(4, 0);
- compute::svm_ptr<float_> ptr = compute::svm_alloc<float_>(context, 4);
- queue.enqueue_svm_map(ptr.get(), 4 * sizeof(cl_int), CL_MAP_WRITE);
- for(size_t i = 0; i < 4; i++) {
- static_cast<float_*>(ptr.get())[i] = data[i];
- }
- queue.enqueue_svm_unmap(ptr.get()).wait();
- // copy host float svm data to int host vector
- bc::copy(ptr, ptr + 4, host_vector.begin(), queue);
- CHECK_HOST_RANGE_EQUAL(
- int_,
- 4,
- host_vector.begin(),
- (
- static_cast<int_>(6.1f),
- static_cast<int_>(1.2f),
- static_cast<int_>(1.3f),
- static_cast<int_>(-66.7f)
- )
- );
- compute::svm_free(context, ptr);
- // restore
- parameters->set(cache_key, "map_copy_threshold", map_copy_threshold);
- }
- BOOST_AUTO_TEST_CASE(copy_to_host_svm_float_to_int_convert_on_host)
- {
- REQUIRES_OPENCL_VERSION(2, 0);
- if(bug_in_svmmemcpy(device)){
- std::cerr << "skipping svmmemcpy test case" << std::endl;
- return;
- }
- using compute::int_;
- using compute::uint_;
- using compute::float_;
- std::string cache_key =
- std::string("__boost_compute_copy_to_host_float_int");
- boost::shared_ptr<bc::detail::parameter_cache> parameters =
- bc::detail::parameter_cache::get_global_cache(device);
- // save
- uint_ map_copy_threshold =
- parameters->get(cache_key, "map_copy_threshold", 0);
- uint_ direct_copy_threshold =
- parameters->get(cache_key, "direct_copy_threshold", 0);
- // force copying by copying input device vector to temporary
- // host vector of the same type and then copying from that temporary
- // vector to result using std::copy()
- parameters->set(cache_key, "map_copy_threshold", 0);
- parameters->set(cache_key, "direct_copy_threshold", 1024);
- float_ data[] = { 6.1f, 1.2f, 1.3f, 766.7f };
- std::vector<int_> host_vector(4, 0);
- compute::svm_ptr<float_> ptr = compute::svm_alloc<float_>(context, 4);
- queue.enqueue_svm_map(ptr.get(), 4 * sizeof(cl_int), CL_MAP_WRITE);
- for(size_t i = 0; i < 4; i++) {
- static_cast<float_*>(ptr.get())[i] = data[i];
- }
- queue.enqueue_svm_unmap(ptr.get()).wait();
- // copy host float svm data to int host vector
- bc::copy(ptr, ptr + 4, host_vector.begin(), queue);
- CHECK_HOST_RANGE_EQUAL(
- int_,
- 4,
- host_vector.begin(),
- (
- static_cast<int_>(6.1f),
- static_cast<int_>(1.2f),
- static_cast<int_>(1.3f),
- static_cast<int_>(766.7f)
- )
- );
- compute::svm_free(context, ptr);
- // restore
- parameters->set(cache_key, "map_copy_threshold", map_copy_threshold);
- parameters->set(cache_key, "direct_copy_threshold", direct_copy_threshold);
- }
- BOOST_AUTO_TEST_CASE(copy_to_host_svm_float_to_int_transform)
- {
- REQUIRES_OPENCL_VERSION(2, 0);
- using compute::int_;
- using compute::uint_;
- using compute::float_;
- std::string cache_key =
- std::string("__boost_compute_copy_to_host_float_int");
- boost::shared_ptr<bc::detail::parameter_cache> parameters =
- bc::detail::parameter_cache::get_global_cache(device);
- // save
- uint_ map_copy_threshold =
- parameters->get(cache_key, "map_copy_threshold", 0);
- uint_ direct_copy_threshold =
- parameters->get(cache_key, "direct_copy_threshold", 0);
- // force copying by copying input device vector to temporary
- // host vector of the same type and then copying from that temporary
- // vector to result using std::copy()
- parameters->set(cache_key, "map_copy_threshold", 0);
- parameters->set(cache_key, "direct_copy_threshold", 0);
- float_ data[] = { 0.1f, 11.2f, 1.3f, -66.7f };
- std::vector<int_> host_vector(4, 0);
- compute::svm_ptr<float_> ptr = compute::svm_alloc<float_>(context, 4);
- queue.enqueue_svm_map(ptr.get(), 4 * sizeof(cl_int), CL_MAP_WRITE);
- for(size_t i = 0; i < 4; i++) {
- static_cast<float_*>(ptr.get())[i] = data[i];
- }
- queue.enqueue_svm_unmap(ptr.get()).wait();
- // copy host float svm data to int host vector
- bc::copy(ptr, ptr + 4, host_vector.begin(), queue);
- CHECK_HOST_RANGE_EQUAL(
- int_,
- 4,
- host_vector.begin(),
- (
- static_cast<int_>(0.1f),
- static_cast<int_>(11.2f),
- static_cast<int_>(1.3f),
- static_cast<int_>(-66.7f)
- )
- );
- compute::svm_free(context, ptr);
- // restore
- parameters->set(cache_key, "map_copy_threshold", map_copy_threshold);
- parameters->set(cache_key, "direct_copy_threshold", direct_copy_threshold);
- }
- #endif
- BOOST_AUTO_TEST_SUITE_END()
|