JayBeams  0.1
Another project to have fun coding.
ut_reduce_argmax_real.cpp
Go to the documentation of this file.
3 
4 #include <boost/compute/algorithm/max_element.hpp>
5 #include <boost/compute/container/vector.hpp>
6 #include <boost/compute/context.hpp>
7 #include <boost/compute/types/complex.hpp>
8 #include <boost/test/unit_test.hpp>
9 #include <random>
10 #include <sstream>
11 
12 /**
13  * @test Test max_element for std::complex<float>
14  */
15 BOOST_AUTO_TEST_CASE(conjugate_and_multiply_float) {
16  constexpr std::size_t size = 32768;
17  using precision_t = float;
18 
19  boost::compute::device device = jb::opencl::device_selector();
20  boost::compute::context context(device);
21  boost::compute::command_queue queue(context, device);
22 
23  unsigned int seed = std::random_device()();
24  std::mt19937 gen(seed);
25  std::uniform_real_distribution<precision_t> dis(-1000, 1000);
26  auto generator = [&gen, &dis]() { return dis(gen); };
27  BOOST_TEST_MESSAGE("SEED = " << seed);
28 
29  std::vector<std::complex<precision_t>> src;
30  jb::testing::create_random_timeseries(generator, size, src);
31 
32  boost::compute::vector<std::complex<precision_t>> a(size, context);
33 
34  boost::compute::copy(src.begin(), src.end(), a.begin(), queue);
35 
36  using value_type = std::complex<precision_t>;
37  BOOST_COMPUTE_FUNCTION(
38  bool, less_real, (value_type const& a, value_type const& b),
39  { return a.x < b.x; });
40 
41  auto actual =
42  boost::compute::max_element(a.begin(), a.end(), less_real, queue);
43 
44  auto pos = std::max_element(
45  src.begin(), src.end(), [](value_type const& a, value_type const& b) {
46  return real(a) < real(b);
47  });
48 
49  auto expected = std::distance(src.begin(), pos);
50  BOOST_CHECK_EQUAL(expected, std::distance(a.begin(), actual));
51  BOOST_TEST_MESSAGE(
52  "Maximum found at " << expected << " value = " << src[expected]);
53 }
void create_random_timeseries(generator &gen, int nsamples, timeseries &ts)
Create a simple timeseries where the values look like a random.
boost::compute::device device_selector(config const &cfg)
Select an OpenCL device matching the current configuration.
BOOST_AUTO_TEST_CASE(conjugate_and_multiply_float)