2
0
mirror of https://github.com/boostorg/compute.git synced 2026-01-28 07:02:15 +00:00
Files
compute/test/test_closure.cpp
Kyle Lutz 83d104f24f Add BOOST_COMPUTE_CLOSURE() macro
This adds a new macro which allows users to create closure functions
which can capture C++ variables and make them available in OpenCL.
2014-03-08 18:44:03 -08:00

68 lines
1.9 KiB
C++

//---------------------------------------------------------------------------//
// Copyright (c) 2013-2014 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 TestClosure
#include <boost/test/unit_test.hpp>
#include <boost/compute/system.hpp>
#include <boost/compute/closure.hpp>
#include <boost/compute/function.hpp>
#include <boost/compute/algorithm/copy.hpp>
#include <boost/compute/algorithm/transform.hpp>
#include <boost/compute/container/vector.hpp>
#include "check_macros.hpp"
#include "context_setup.hpp"
namespace compute = boost::compute;
BOOST_AUTO_TEST_CASE(add_two)
{
int two = 2;
BOOST_COMPUTE_CLOSURE(int, add_two, (int), (two),
{
return _1 + two;
});
int data[] = { 1, 2, 3, 4 };
compute::vector<int> vector(data, data + 4, queue);
compute::transform(
vector.begin(), vector.end(), vector.begin(), add_two, queue
);
CHECK_RANGE_EQUAL(int, 4, vector, (3, 4, 5, 6));
}
BOOST_AUTO_TEST_CASE(add_two_and_pi)
{
int two = 2;
float pi = 3.14f;
BOOST_COMPUTE_CLOSURE(float, add_two_and_pi, (float), (two, pi),
{
return _1 + two + pi;
});
float data[] = { 1.9f, 2.2f, 3.4f, 4.7f };
compute::vector<float> vector(data, data + 4, queue);
compute::transform(
vector.begin(), vector.end(), vector.begin(), add_two_and_pi, queue
);
std::vector<float> results(4);
compute::copy(vector.begin(), vector.end(), results.begin(), queue);
BOOST_CHECK_CLOSE(results[0], 7.04f, 1e-6);
BOOST_CHECK_CLOSE(results[1], 7.34f, 1e-6);
BOOST_CHECK_CLOSE(results[2], 8.54f, 1e-6);
BOOST_CHECK_CLOSE(results[3], 9.84f, 1e-6);
}
BOOST_AUTO_TEST_SUITE_END()