test_user_defined_types.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. //---------------------------------------------------------------------------//
  2. // Copyright (c) 2013 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 TestUserDefinedTypes
  11. #include <boost/test/unit_test.hpp>
  12. #include <iostream>
  13. #include <boost/compute/function.hpp>
  14. #include <boost/compute/system.hpp>
  15. #include <boost/compute/algorithm/sort.hpp>
  16. #include <boost/compute/container/vector.hpp>
  17. #include <boost/compute/types/struct.hpp>
  18. namespace compute = boost::compute;
  19. // user-defined data type containing two int's and a float
  20. struct UDD
  21. {
  22. int a;
  23. int b;
  24. float c;
  25. };
  26. // make UDD available to OpenCL
  27. BOOST_COMPUTE_ADAPT_STRUCT(UDD, UDD, (a, b, c))
  28. // comparison operator for UDD
  29. bool operator==(const UDD &lhs, const UDD &rhs)
  30. {
  31. return lhs.a == rhs.a && lhs.b == rhs.b && lhs.c == rhs.c;
  32. }
  33. // output stream operator for UDD
  34. std::ostream& operator<<(std::ostream &stream, const UDD &x)
  35. {
  36. return stream << "(" << x.a << ", " << x.b << ", " << x.c << ")";
  37. }
  38. // function to generate a random UDD on the host
  39. UDD rand_UDD()
  40. {
  41. UDD udd;
  42. udd.a = rand() % 100;
  43. udd.b = rand() % 100;
  44. udd.c = (float)(rand() % 100) / 1.3f;
  45. return udd;
  46. }
  47. // function to compare two UDD's on the host by their first component
  48. bool compare_UDD_host(const UDD &lhs, const UDD &rhs)
  49. {
  50. return lhs.a < rhs.a;
  51. }
  52. // function to compate two UDD's on the device by their first component
  53. BOOST_COMPUTE_FUNCTION(bool, compare_UDD_device, (UDD lhs, UDD rhs),
  54. {
  55. return lhs.a < rhs.a;
  56. });
  57. #include "check_macros.hpp"
  58. #include "context_setup.hpp"
  59. // see: issue #11 (https://github.com/boostorg/compute/issues/11)
  60. BOOST_AUTO_TEST_CASE(issue_11)
  61. {
  62. if(device.vendor() == "NVIDIA" && device.platform().name() == "Apple"){
  63. // FIXME: this test currently segfaults on NVIDIA GPUs on Apple
  64. std::cerr << "skipping issue test on NVIDIA GPU on Apple platform" << std::endl;
  65. return;
  66. }
  67. // create vector of random values on the host
  68. std::vector<UDD> host_vector(10);
  69. std::generate(host_vector.begin(), host_vector.end(), rand_UDD);
  70. // transfer the values to the device
  71. compute::vector<UDD> device_vector(host_vector.size(), context);
  72. compute::copy(
  73. host_vector.begin(), host_vector.end(), device_vector.begin(), queue
  74. );
  75. // sort values on the device
  76. compute::sort(
  77. device_vector.begin(),
  78. device_vector.end(),
  79. compare_UDD_device,
  80. queue
  81. );
  82. // sort values on the host
  83. std::sort(
  84. host_vector.begin(),
  85. host_vector.end(),
  86. compare_UDD_host
  87. );
  88. // copy sorted device values back to the host
  89. std::vector<UDD> tmp(10);
  90. compute::copy(
  91. device_vector.begin(),
  92. device_vector.end(),
  93. tmp.begin(),
  94. queue
  95. );
  96. // verify sorted values
  97. for(size_t i = 0; i < host_vector.size(); i++){
  98. BOOST_CHECK_EQUAL(tmp[i], host_vector[i]);
  99. }
  100. }
  101. BOOST_AUTO_TEST_SUITE_END()