Beispiel #1
0
    void saxpy(const int num, bool gen = true, int iter = 0)
    {
        static compute::device gpu;
        static compute::context context;
        static compute::command_queue queue;
        static compute::vector<T> x;
        static compute::vector<T> y;
        static compute::vector<T> res;
        static T alpha = 3.5;

        using compute::lambda::_1;
        using compute::lambda::_2;

        if (gen) {
            gpu = compute::system::default_device();
            context = compute::context(gpu);
            queue = compute::command_queue(context, gpu);

            x = compute::vector<T>(num, context);
            std::vector<T> h_x(num);
            std::generate(h_x.begin(), h_x.end(), rand);
            compute::copy(h_x.begin(), h_x.end(), x.begin(), queue);

            y = compute::vector<T>(num, context);
            std::vector<T> h_y(num);
            std::generate(h_y.begin(), h_y.end(), rand);
            compute::copy(h_y.begin(), h_y.end(), y.begin(), queue);

            res = compute::vector<T>(num, context);
            queue.finish();
        }

        for (int i = 0; i < iter; i++) {
            compute::transform(x.begin(), x.end(),
                               y.begin(), res.begin(),
                               alpha * _1 + _2,
                               queue);
        }

        queue.finish();
    }
Beispiel #2
0
double perf_accumulate(const compute::vector<T>& data,
                       const size_t trials,
                       compute::command_queue& queue)
{
    perf_timer t;
    for(size_t trial = 0; trial < trials; trial++){
        t.start();
        compute::accumulate(data.begin(), data.end(), T(0), queue);
        queue.finish();
        t.stop();
    }
    return t.min_time();
}
Beispiel #3
0
}

BOOST_AUTO_TEST_CASE(transform_custom_function)
{
    float data[] = { 9.0f, 7.0f, 5.0f, 3.0f };
    bc::vector<float> vector(data, data + 4, queue);

    BOOST_COMPUTE_FUNCTION(float, pow3add4, (float x),
    {
        return pow(x, 3.0f) + 4.0f;
    });

    bc::vector<float> result(4, context);
    bc::transform(vector.begin(),
                  vector.end(),
                  result.begin(),
                  pow3add4,
                  queue);
    queue.finish();
    BOOST_CHECK_CLOSE(float(result[0]), 733.0f, 1e-4f);
    BOOST_CHECK_CLOSE(float(result[1]), 347.0f, 1e-4f);
    BOOST_CHECK_CLOSE(float(result[2]), 129.0f, 1e-4f);
    BOOST_CHECK_CLOSE(float(result[3]), 31.0f, 1e-4f);
}

BOOST_AUTO_TEST_CASE(extract_vector_component)
{
    using bc::int2_;

    int data[] = { 1, 2,
                   3, 4,
{
    using boost::compute::int_;
    using boost::compute::float_;

    int n = 1024;
    std::vector<int_> host_keys(n);
    std::vector<float_> host_values(n);
    for(int i = 0; i < n; i++){
        host_keys[i] = n - i;
        host_values[i] = (n - i) / 2.f;
    }

    BOOST_COMPUTE_FUNCTION(bool, sort_int, (int_ a, int_ b),
    {
        return a < b;
    });

    compute::vector<int_> keys(host_keys.begin(), host_keys.end(), queue);
    compute::vector<float_> values(host_values.begin(), host_values.end(), queue);

    BOOST_CHECK(compute::is_sorted(keys.begin(), keys.end(), sort_int, queue) == false);
    BOOST_CHECK(compute::is_sorted(values.begin(), values.end(), queue) == false);

    compute::sort_by_key(keys.begin(), keys.end(), values.begin(), sort_int, queue);

    BOOST_CHECK(compute::is_sorted(keys.begin(), keys.end(), sort_int, queue) == true);
    BOOST_CHECK(compute::is_sorted(values.begin(), values.end(), queue) == true);
}

BOOST_AUTO_TEST_SUITE_END()
Beispiel #5
0
#include "context_setup.hpp"

namespace compute = boost::compute;

BOOST_AUTO_TEST_CASE(add_three)
{
    BOOST_COMPUTE_FUNCTION(int, add_three, (int),
    {
        return _1 + 3;
    });

    int data[] = { 1, 2, 3, 4 };
    compute::vector<int> vector(data, data + 4, queue);

    compute::transform(
        vector.begin(), vector.end(), vector.begin(), add_three, queue
    );
    CHECK_RANGE_EQUAL(int, 4, vector, (4, 5, 6, 7));
}

BOOST_AUTO_TEST_CASE(sum_odd_values)
{
    BOOST_COMPUTE_FUNCTION(int, add_odd_value, (int, int),
    {
        if(_2 & 1){
            return _1 + _2;
        }
        else {
            return _1 + 0;
        }
    });
Beispiel #6
0
    // returns the result of M*x
    BOOST_COMPUTE_CLOSURE(Eigen::Vector4f, transform4x4, (const Eigen::Vector4f x), (M),
    {
        float4 r;
        r.x = dot(M.s048c, x);
        r.y = dot(M.s159d, x);
        r.z = dot(M.s26ae, x);
        r.w = dot(M.s37bf, x);
        return r;
    });

    bcl::vector<Eigen::Vector4f> vectors(4, context);
    bcl::vector<Eigen::Vector4f> results(4, context);

    bcl::copy(host_vectors.begin(), host_vectors.end(), vectors.begin(), queue);

    bcl::transform(
        vectors.begin(), vectors.end(), results.begin(), transform4x4, queue
    );

    std::vector<Eigen::Vector4f> host_results(4);
    bcl::copy(results.begin(), results.end(), host_results.begin(), queue);

    BOOST_CHECK((matrix * host_vectors[0]) == host_results[0]);
    BOOST_CHECK((matrix * host_vectors[1]) == host_results[1]);
    BOOST_CHECK((matrix * host_vectors[2]) == host_results[2]);
    BOOST_CHECK((matrix * host_vectors[3]) == host_results[3]);
}

BOOST_AUTO_TEST_SUITE_END()
Beispiel #7
0
BOOST_AUTO_TEST_CASE(sort_custom_struct)
{
    // function to compare particles by their x-coordinate
    BOOST_COMPUTE_FUNCTION(bool, sort_by_x, (Particle a, Particle b),
    {
        return a.x < b.x;
    });

    std::vector<Particle> particles;
    particles.push_back(Particle(0.1f, 0.f));
    particles.push_back(Particle(-0.4f, 0.f));
    particles.push_back(Particle(10.0f, 0.f));
    particles.push_back(Particle(0.001f, 0.f));

    boost::compute::vector<Particle> vector(4, context);
    boost::compute::copy(particles.begin(), particles.end(), vector.begin(), queue);
    BOOST_CHECK_EQUAL(vector.size(), size_t(4));
    BOOST_CHECK(
        boost::compute::is_sorted(vector.begin(), vector.end(),
                                  sort_by_x, queue) == false
    );

    boost::compute::sort(vector.begin(), vector.end(), sort_by_x, queue);
    BOOST_CHECK(
        boost::compute::is_sorted(vector.begin(), vector.end(),
                                  sort_by_x, queue) == true
    );
    boost::compute::copy(vector.begin(), vector.end(), particles.begin(), queue);
    BOOST_CHECK_CLOSE(particles[0].x, -0.4f, 0.1);
    BOOST_CHECK_CLOSE(particles[1].x, 0.001f, 0.1);
    BOOST_CHECK_CLOSE(particles[2].x, 0.1f, 0.1);
Beispiel #8
0
// See http://boostorg.github.com/compute for more information.
//---------------------------------------------------------------------------//

#define BOOST_TEST_MODULE TestTabulate
#include <boost/test/unit_test.hpp>

#include <boost/compute/system.hpp>
#include <boost/compute/function.hpp>
#include <boost/compute/algorithm/copy_n.hpp>
#include <boost/compute/container/vector.hpp>
#include <boost/compute/experimental/tabulate.hpp>

#include "check_macros.hpp"
#include "context_setup.hpp"

namespace compute = boost::compute;

BOOST_AUTO_TEST_CASE(tabulate_negative_int)
{
    BOOST_COMPUTE_FUNCTION(int, negate, (int x),
    {
        return -x;
    });

    compute::vector<int> vector(10, context);
    compute::experimental::tabulate(vector.begin(), vector.end(), negate, queue);
    CHECK_RANGE_EQUAL(int, 10, vector, (0, -1, -2, -3, -4, -5, -6, -7, -8, -9));
}

BOOST_AUTO_TEST_SUITE_END()
Beispiel #9
0
BOOST_AUTO_TEST_CASE(inclusive_scan_int_custom_function)
{
    BOOST_COMPUTE_FUNCTION(int, multi, (int x, int y),
    {
        return x * y * 2;
    });

    int data[] = { 1, 2, 1, 2, 3 };
    bc::vector<int> vector(data, data + 5, queue);
    BOOST_CHECK_EQUAL(vector.size(), size_t(5));

    bc::vector<int> result(5, context);
    BOOST_CHECK_EQUAL(result.size(), size_t(5));

    // inclusive scan
    bc::inclusive_scan(vector.begin(), vector.end(), result.begin(),
                       multi, queue);
    CHECK_RANGE_EQUAL(int, 5, result, (1, 4, 8, 32, 192));

    // in-place inclusive scan
    CHECK_RANGE_EQUAL(int, 5, vector, (1, 2, 1, 2, 3));
    bc::inclusive_scan(vector.begin(), vector.end(), vector.begin(),
                       multi, queue);
    CHECK_RANGE_EQUAL(int, 5, vector, (1, 4, 8, 32, 192));
}

BOOST_AUTO_TEST_CASE(exclusive_scan_int_custom_function)
{
    BOOST_COMPUTE_FUNCTION(int, multi, (int x, int y),
    {
        return x * y * 2;
Beispiel #10
0
namespace compute = boost::compute;

BOOST_AUTO_TEST_CASE(add_two)
{
    int two = 2;
    BOOST_COMPUTE_CLOSURE(int, add_two, (int x), (two),
    {
        return x + 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 x), (two, pi),
    {
        return x + two + pi;
    });

    float data[] = { 1.9f, 2.2f, 3.4f, 4.7f };
    compute::vector<float> vector(data, data + 4, queue);