//---------------------------------------------------------------------------// // Copyright (c) 2013-2014 Kyle Lutz // // 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. //---------------------------------------------------------------------------// #include #define BOOST_TEST_MODULE TestStruct #include #include #include #include #include #include #include #include #include #include namespace compute = boost::compute; // example code defining an atom class namespace chemistry { struct Atom { Atom(float _x, float _y, float _z, int _number) : x(_x), y(_y), z(_z), number(_number) { } float x; float y; float z; int number; }; } // end chemistry namespace // adapt the chemistry::Atom class BOOST_COMPUTE_ADAPT_STRUCT(chemistry::Atom, Atom, (x, y, z, number)) #include "check_macros.hpp" #include "context_setup.hpp" BOOST_AUTO_TEST_CASE(atom_type_name) { BOOST_CHECK(std::strcmp(compute::type_name(), "Atom") == 0); } BOOST_AUTO_TEST_CASE(atom_struct) { std::vector atoms; atoms.push_back(chemistry::Atom(1.f, 0.f, 0.f, 1)); atoms.push_back(chemistry::Atom(0.f, 1.f, 0.f, 1)); atoms.push_back(chemistry::Atom(0.f, 0.f, 0.f, 8)); compute::vector vec(atoms.size(), context); compute::copy(atoms.begin(), atoms.end(), vec.begin(), queue); // find the oxygen atom BOOST_COMPUTE_FUNCTION(bool, is_oxygen, (chemistry::Atom atom), { return atom.number == 8; }); compute::vector::iterator iter = compute::find_if(vec.begin(), vec.end(), is_oxygen, queue); BOOST_CHECK(iter == vec.begin() + 2); // copy the atomic numbers to another vector compute::vector atomic_numbers(vec.size(), context); compute::transform( vec.begin(), vec.end(), atomic_numbers.begin(), compute::field("number"), queue ); CHECK_RANGE_EQUAL(int, 3, atomic_numbers, (1, 1, 8)); } BOOST_AUTO_TEST_CASE(custom_kernel) { std::vector data; data.push_back(chemistry::Atom(1.f, 0.f, 0.f, 1)); data.push_back(chemistry::Atom(0.f, 1.f, 0.f, 1)); data.push_back(chemistry::Atom(0.f, 0.f, 0.f, 8)); compute::vector atoms(data.size(), context); compute::copy(data.begin(), data.end(), atoms.begin(), queue); std::string source = BOOST_COMPUTE_STRINGIZE_SOURCE( __kernel void custom_kernel(__global const Atom *atoms, __global float *distances) { const uint i = get_global_id(0); const __global Atom *atom = &atoms[i]; const float4 center = { 0, 0, 0, 0 }; const float4 position = { atom->x, atom->y, atom->z, 0 }; distances[i] = distance(position, center); } ); // add type definition for Atom to the start of the program source source = compute::type_definition() + "\n" + source; compute::program program = compute::program::build_with_source(source, context); compute::vector distances(atoms.size(), context); compute::kernel custom_kernel = program.create_kernel("custom_kernel"); custom_kernel.set_arg(0, atoms); custom_kernel.set_arg(1, distances); queue.enqueue_1d_range_kernel(custom_kernel, 0, atoms.size(), 1); } BOOST_AUTO_TEST_SUITE_END()