test_svm_ptr.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 TestSvmPtr
  11. #include <boost/test/unit_test.hpp>
  12. #include <iostream>
  13. #include <boost/compute/core.hpp>
  14. #include <boost/compute/svm.hpp>
  15. #include <boost/compute/container/vector.hpp>
  16. #include <boost/compute/utility/source.hpp>
  17. #include "quirks.hpp"
  18. #include "check_macros.hpp"
  19. #include "context_setup.hpp"
  20. namespace compute = boost::compute;
  21. BOOST_AUTO_TEST_CASE(empty)
  22. {
  23. }
  24. #ifdef BOOST_COMPUTE_CL_VERSION_2_0
  25. BOOST_AUTO_TEST_CASE(alloc)
  26. {
  27. REQUIRES_OPENCL_VERSION(2, 0);
  28. compute::svm_ptr<cl_int> ptr = compute::svm_alloc<cl_int>(context, 8);
  29. compute::svm_free(context, ptr);
  30. }
  31. BOOST_AUTO_TEST_CASE(svmmemcpy)
  32. {
  33. REQUIRES_OPENCL_VERSION(2, 0);
  34. if(bug_in_svmmemcpy(device)){
  35. std::cerr << "skipping svmmemcpy test case" << std::endl;
  36. return;
  37. }
  38. cl_int input[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
  39. cl_int output[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
  40. compute::svm_ptr<cl_int> ptr = compute::svm_alloc<cl_int>(context, 8);
  41. compute::svm_ptr<cl_int> ptr2 = compute::svm_alloc<cl_int>(context, 8);
  42. // copying from and to host mem
  43. queue.enqueue_svm_memcpy(ptr.get(), input, 8 * sizeof(cl_int));
  44. queue.enqueue_svm_memcpy(output, ptr.get(), 8 * sizeof(cl_int));
  45. queue.finish();
  46. CHECK_HOST_RANGE_EQUAL(cl_int, 8, output, (1, 2, 3, 4, 5, 6, 7, 8));
  47. // copying between svm mem
  48. queue.enqueue_svm_memcpy(ptr2.get(), ptr.get(), 8 * sizeof(cl_int));
  49. queue.enqueue_svm_memcpy(output, ptr2.get(), 8 * sizeof(cl_int));
  50. queue.finish();
  51. CHECK_HOST_RANGE_EQUAL(cl_int, 8, output, (1, 2, 3, 4, 5, 6, 7, 8));
  52. compute::svm_free(context, ptr);
  53. compute::svm_free(context, ptr2);
  54. }
  55. BOOST_AUTO_TEST_CASE(sum_svm_kernel)
  56. {
  57. REQUIRES_OPENCL_VERSION(2, 0);
  58. const char source[] = BOOST_COMPUTE_STRINGIZE_SOURCE(
  59. __kernel void sum_svm_mem(__global const int *ptr, __global int *result)
  60. {
  61. int sum = 0;
  62. for(uint i = 0; i < 8; i++){
  63. sum += ptr[i];
  64. }
  65. *result = sum;
  66. }
  67. );
  68. compute::program program =
  69. compute::program::build_with_source(source, context, "-cl-std=CL2.0");
  70. compute::kernel sum_svm_mem_kernel = program.create_kernel("sum_svm_mem");
  71. cl_int data[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
  72. compute::svm_ptr<cl_int> ptr = compute::svm_alloc<cl_int>(context, 8);
  73. queue.enqueue_svm_map(ptr.get(), 8 * sizeof(cl_int), CL_MAP_WRITE);
  74. for(size_t i = 0; i < 8; i ++) {
  75. static_cast<cl_int*>(ptr.get())[i] = data[i];
  76. }
  77. queue.enqueue_svm_unmap(ptr.get());
  78. compute::vector<cl_int> result(1, context);
  79. sum_svm_mem_kernel.set_arg(0, ptr);
  80. sum_svm_mem_kernel.set_arg(1, result);
  81. queue.enqueue_task(sum_svm_mem_kernel);
  82. queue.finish();
  83. BOOST_CHECK_EQUAL(result[0], (36));
  84. compute::svm_free(context, ptr);
  85. }
  86. #endif // BOOST_COMPUTE_CL_VERSION_2_0
  87. #ifdef BOOST_COMPUTE_CL_VERSION_2_1
  88. BOOST_AUTO_TEST_CASE(migrate)
  89. {
  90. REQUIRES_OPENCL_VERSION(2, 1);
  91. compute::svm_ptr<cl_int> ptr =
  92. compute::svm_alloc<cl_int>(context, 8);
  93. // Migrate to device
  94. std::vector<const void*> ptrs(1, ptr.get());
  95. std::vector<size_t> sizes(1, 8 * sizeof(cl_int));
  96. queue.enqueue_svm_migrate_memory(ptrs, sizes).wait();
  97. // Set on device
  98. const char source[] = BOOST_COMPUTE_STRINGIZE_SOURCE(
  99. __kernel void foo(__global int *ptr)
  100. {
  101. for(int i = 0; i < 8; i++){
  102. ptr[i] = i;
  103. }
  104. }
  105. );
  106. compute::program program =
  107. compute::program::build_with_source(source, context, "-cl-std=CL2.0");
  108. compute::kernel foo_kernel = program.create_kernel("foo");
  109. foo_kernel.set_arg(0, ptr);
  110. queue.enqueue_task(foo_kernel).wait();
  111. // Migrate to host
  112. queue.enqueue_svm_migrate_memory(
  113. ptr.get(), 0, boost::compute::command_queue::migrate_to_host
  114. ).wait();
  115. // Check
  116. CHECK_HOST_RANGE_EQUAL(
  117. cl_int, 8,
  118. static_cast<cl_int*>(ptr.get()),
  119. (0, 1, 2, 3, 4, 5, 6, 7)
  120. );
  121. compute::svm_free(context, ptr);
  122. }
  123. #endif // BOOST_COMPUTE_CL_VERSION_2_1
  124. BOOST_AUTO_TEST_SUITE_END()