mirror of
https://github.com/boostorg/compute.git
synced 2026-01-31 20:12:23 +00:00
This adds an output iterator result argument to the reduce() algorithm. Now, instead of returning the reduced result, the result is written to an output iterator. This allows the value to stay on the device and avoids a device-to-host copy in cases where the result is not needed on the host (e.g. it is part of a larger computation). This is an API breaking change to users of reduce(). Affected code should now declare a result variable and then pass a pointer to it as the new result argument.
66 lines
1.8 KiB
C++
66 lines
1.8 KiB
C++
//---------------------------------------------------------------------------//
|
|
// Copyright (c) 2013 Kyle Lutz <kyle.r.lutz@gmail.com>
|
|
//
|
|
// Distributed under the Boost Software License, Version 1.0
|
|
// See accompanying file LICENSE_1_0.txt or copy at
|
|
// http://www.boost.org/LICENSE_1_0.txt
|
|
//
|
|
// See http://kylelutz.github.com/compute for more information.
|
|
//---------------------------------------------------------------------------//
|
|
|
|
#define BOOST_TEST_MODULE TestTransformReduce
|
|
#include <boost/test/unit_test.hpp>
|
|
|
|
#include <boost/compute/system.hpp>
|
|
#include <boost/compute/functional.hpp>
|
|
#include <boost/compute/algorithm/transform_reduce.hpp>
|
|
#include <boost/compute/container/vector.hpp>
|
|
|
|
#include "context_setup.hpp"
|
|
|
|
namespace compute = boost::compute;
|
|
|
|
BOOST_AUTO_TEST_CASE(sum_abs_int)
|
|
{
|
|
int data[] = { 1, -2, -3, -4, 5 };
|
|
compute::vector<int> vector(data, data + 5, context);
|
|
|
|
int sum;
|
|
compute::transform_reduce(
|
|
vector.begin(),
|
|
vector.end(),
|
|
&sum,
|
|
compute::abs<int>(),
|
|
0,
|
|
compute::plus<int>(),
|
|
queue
|
|
);
|
|
BOOST_CHECK_EQUAL(sum, 15);
|
|
}
|
|
|
|
BOOST_AUTO_TEST_CASE(multiply_vector_length)
|
|
{
|
|
float data[] = { 2.0f, 0.0f, 0.0f, 0.0f,
|
|
0.0f, 3.0f, 0.0f, 0.0f,
|
|
0.0f, 0.0f, 4.0f, 0.0f };
|
|
compute::vector<compute::float4_> vector(
|
|
reinterpret_cast<compute::float4_ *>(data),
|
|
reinterpret_cast<compute::float4_ *>(data) + 3,
|
|
context
|
|
);
|
|
|
|
float product;
|
|
compute::transform_reduce(
|
|
vector.begin(),
|
|
vector.end(),
|
|
&product,
|
|
compute::length<compute::float4_>(),
|
|
1.0f,
|
|
compute::multiplies<float>(),
|
|
queue
|
|
);
|
|
BOOST_CHECK_CLOSE(product, 24.0f, 1e-4f);
|
|
}
|
|
|
|
BOOST_AUTO_TEST_SUITE_END()
|