test_amd_cpp_kernel_language.cpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 TestAmdCppKernel
  11. #include <boost/test/unit_test.hpp>
  12. #include <iostream>
  13. #include <boost/compute/kernel.hpp>
  14. #include <boost/compute/program.hpp>
  15. #include <boost/compute/system.hpp>
  16. #include <boost/compute/container/vector.hpp>
  17. #include <boost/compute/detail/vendor.hpp>
  18. #include <boost/compute/utility/source.hpp>
  19. #include "check_macros.hpp"
  20. #include "context_setup.hpp"
  21. namespace compute = boost::compute;
  22. BOOST_AUTO_TEST_CASE(amd_template_function)
  23. {
  24. if(!compute::detail::is_amd_device(device)){
  25. std::cerr << "skipping amd_template_function test: c++ static kernel "
  26. "language is only supported on AMD devices." << std::endl;
  27. return;
  28. }
  29. const char source[] = BOOST_COMPUTE_STRINGIZE_SOURCE(
  30. template<typename T>
  31. inline T square(const T x)
  32. {
  33. return x * x;
  34. }
  35. template<typename T>
  36. __kernel void square_kernel(__global T *data)
  37. {
  38. const uint i = get_global_id(0);
  39. data[i] = square(data[i]);
  40. }
  41. template __attribute__((mangled_name(square_kernel_int)))
  42. __kernel void square_kernel(__global int *data);
  43. );
  44. int data[] = { 1, 2, 3, 4 };
  45. compute::vector<int> vec(data, data + 4, queue);
  46. compute::program square_program =
  47. compute::program::build_with_source(source, context, "-x clc++");
  48. compute::kernel square_kernel(square_program, "square_kernel_int");
  49. square_kernel.set_arg(0, vec);
  50. queue.enqueue_1d_range_kernel(square_kernel, 0, vec.size(), 4);
  51. CHECK_RANGE_EQUAL(int, 4, vec, (1, 4, 9, 16));
  52. }
  53. BOOST_AUTO_TEST_SUITE_END()