]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/compute/test/test_transform_reduce.cpp
1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2013 Kyle Lutz <kyle.r.lutz@gmail.com>
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
8 // See http://boostorg.github.com/compute for more information.
9 //---------------------------------------------------------------------------//
11 #define BOOST_TEST_MODULE TestTransformReduce
12 #include <boost/test/unit_test.hpp>
14 #include <boost/compute/lambda.hpp>
15 #include <boost/compute/system.hpp>
16 #include <boost/compute/functional.hpp>
17 #include <boost/compute/algorithm/transform_reduce.hpp>
18 #include <boost/compute/container/vector.hpp>
20 #include "context_setup.hpp"
22 namespace compute
= boost::compute
;
24 BOOST_AUTO_TEST_CASE(sum_abs_int_doctest
)
26 using boost::compute::abs
;
27 using boost::compute::plus
;
29 int data
[] = { 1, -2, -3, -4, 5 };
30 compute::vector
<int> vec(data
, data
+ 5, queue
);
34 boost::compute::transform_reduce(
35 vec
.begin(), vec
.end(), &sum
, abs
<int>(), plus
<int>(), queue
39 BOOST_CHECK_EQUAL(sum
, 15);
42 BOOST_AUTO_TEST_CASE(multiply_vector_length
)
44 float data
[] = { 2.0f
, 0.0f
, 0.0f
, 0.0f
,
45 0.0f
, 3.0f
, 0.0f
, 0.0f
,
46 0.0f
, 0.0f
, 4.0f
, 0.0f
};
47 compute::vector
<compute::float4_
> vector(
48 reinterpret_cast<compute::float4_
*>(data
),
49 reinterpret_cast<compute::float4_
*>(data
) + 3,
54 compute::transform_reduce(
58 compute::length
<compute::float4_
>(),
59 compute::multiplies
<float>(),
62 BOOST_CHECK_CLOSE(product
, 24.0f
, 1e-4f
);
65 BOOST_AUTO_TEST_CASE(mean_and_std_dev
)
67 using compute::lambda::_1
;
68 using compute::lambda::pow
;
70 float data
[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
71 compute::vector
<float> vector(data
, data
+ 10, queue
);
78 compute::plus
<float>(),
82 float mean
= sum
/ vector
.size();
83 BOOST_CHECK_CLOSE(mean
, 5.5f
, 1e-4);
85 compute::transform_reduce(
90 compute::plus
<float>(),
94 float variance
= sum
/ vector
.size();
95 BOOST_CHECK_CLOSE(variance
, 8.25f
, 1e-4);
97 float std_dev
= std::sqrt(variance
);
98 BOOST_CHECK_CLOSE(std_dev
, 2.8722813232690143, 1e-4);
101 BOOST_AUTO_TEST_SUITE_END()