test_async_wait.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. //---------------------------------------------------------------------------//
  2. // Copyright (c) 2013-2014 Kyle Lutz <kyle.r.lutz@gmail.com>
  3. //
  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
  7. //
  8. // See http://boostorg.github.com/compute for more information.
  9. //---------------------------------------------------------------------------//
  10. #define BOOST_TEST_MODULE TestAsyncWait
  11. #include <boost/test/unit_test.hpp>
  12. #include <boost/compute/async/future.hpp>
  13. #include <boost/compute/async/wait.hpp>
  14. #include <boost/compute/algorithm/copy.hpp>
  15. #include <boost/compute/container/vector.hpp>
  16. #include "check_macros.hpp"
  17. #include "context_setup.hpp"
  18. namespace compute = boost::compute;
  19. BOOST_AUTO_TEST_CASE(empty)
  20. {
  21. }
  22. #ifndef BOOST_COMPUTE_NO_VARIADIC_TEMPLATES
  23. BOOST_AUTO_TEST_CASE(wait_for_copy)
  24. {
  25. // wait list
  26. compute::wait_list events;
  27. // create host data array
  28. int data[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
  29. // create vector on the device
  30. compute::vector<int> vector(8, context);
  31. // fill vector with 9's
  32. compute::future<void> fill_future =
  33. compute::fill_async(vector.begin(), vector.end(), 9, queue);
  34. // wait for fill() to complete
  35. compute::wait_for_all(fill_future);
  36. // check data on the device
  37. CHECK_RANGE_EQUAL(int, 8, vector, (9, 9, 9, 9, 9, 9, 9, 9));
  38. // copy each pair of values independently and asynchronously
  39. compute::event copy1 = queue.enqueue_write_buffer_async(
  40. vector.get_buffer(), 0 * sizeof(int), 2 * sizeof(int), data + 0
  41. );
  42. compute::event copy2 = queue.enqueue_write_buffer_async(
  43. vector.get_buffer(), 2 * sizeof(int), 2 * sizeof(int), data + 2
  44. );
  45. compute::event copy3 = queue.enqueue_write_buffer_async(
  46. vector.get_buffer(), 4 * sizeof(int), 2 * sizeof(int), data + 4
  47. );
  48. compute::event copy4 = queue.enqueue_write_buffer_async(
  49. vector.get_buffer(), 6 * sizeof(int), 2 * sizeof(int), data + 6
  50. );
  51. // wait for all copies to complete
  52. compute::wait_for_all(copy1, copy2, copy3, copy4);
  53. // check data on the device
  54. CHECK_RANGE_EQUAL(int, 8, vector, (1, 2, 3, 4, 5, 6, 7, 8));
  55. }
  56. #endif // BOOST_COMPUTE_NO_VARIADIC_TEMPLATES
  57. BOOST_AUTO_TEST_SUITE_END()