• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2013-2014 Kyle Lutz <kyle.r.lutz@gmail.com>
3 //
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
7 //
8 // See http://boostorg.github.com/compute for more information.
9 //---------------------------------------------------------------------------//
10 
11 #include <boost/compute/config.hpp>
12 
13 #define BOOST_TEST_MODULE TestStruct
14 #include <boost/test/unit_test.hpp>
15 
16 #include <boost/compute/function.hpp>
17 #include <boost/compute/algorithm/find_if.hpp>
18 #include <boost/compute/algorithm/transform.hpp>
19 #include <boost/compute/container/vector.hpp>
20 #include <boost/compute/functional/field.hpp>
21 #include <boost/compute/types/struct.hpp>
22 #include <boost/compute/type_traits/type_name.hpp>
23 #include <boost/compute/type_traits/type_definition.hpp>
24 #include <boost/compute/utility/source.hpp>
25 
26 namespace compute = boost::compute;
27 
28 // example code defining an atom class
29 namespace chemistry {
30 
31 struct Atom
32 {
Atomchemistry::Atom33     Atom(float _x, float _y, float _z, int _number)
34         : x(_x), y(_y), z(_z), number(_number)
35     {
36     }
37 
38     float x;
39     float y;
40     float z;
41     int number;
42 };
43 
44 } // end chemistry namespace
45 
46 // adapt the chemistry::Atom class
47 BOOST_COMPUTE_ADAPT_STRUCT(chemistry::Atom, Atom, (x, y, z, number))
48 
49 struct StructWithArray {
50     int value;
51     int array[3];
52 };
53 
54 BOOST_COMPUTE_ADAPT_STRUCT(StructWithArray, StructWithArray, (value, array))
55 
56 #include "check_macros.hpp"
57 #include "context_setup.hpp"
58 
BOOST_AUTO_TEST_CASE(atom_type_name)59 BOOST_AUTO_TEST_CASE(atom_type_name)
60 {
61     BOOST_CHECK(std::strcmp(compute::type_name<chemistry::Atom>(), "Atom") == 0);
62 }
63 
BOOST_AUTO_TEST_CASE(atom_struct)64 BOOST_AUTO_TEST_CASE(atom_struct)
65 {
66     std::vector<chemistry::Atom> atoms;
67     atoms.push_back(chemistry::Atom(1.f, 0.f, 0.f, 1));
68     atoms.push_back(chemistry::Atom(0.f, 1.f, 0.f, 1));
69     atoms.push_back(chemistry::Atom(0.f, 0.f, 0.f, 8));
70 
71     compute::vector<chemistry::Atom> vec(atoms.size(), context);
72     compute::copy(atoms.begin(), atoms.end(), vec.begin(), queue);
73 
74     // find the oxygen atom
75     BOOST_COMPUTE_FUNCTION(bool, is_oxygen, (chemistry::Atom atom),
76     {
77         return atom.number == 8;
78     });
79 
80     compute::vector<chemistry::Atom>::iterator iter =
81         compute::find_if(vec.begin(), vec.end(), is_oxygen, queue);
82     BOOST_CHECK(iter == vec.begin() + 2);
83 
84     // copy the atomic numbers to another vector
85     compute::vector<int> atomic_numbers(vec.size(), context);
86     compute::transform(
87         vec.begin(), vec.end(),
88         atomic_numbers.begin(),
89         compute::field<int>("number"),
90         queue
91     );
92     CHECK_RANGE_EQUAL(int, 3, atomic_numbers, (1, 1, 8));
93 }
94 
BOOST_AUTO_TEST_CASE(custom_kernel)95 BOOST_AUTO_TEST_CASE(custom_kernel)
96 {
97     std::vector<chemistry::Atom> data;
98     data.push_back(chemistry::Atom(1.f, 0.f, 0.f, 1));
99     data.push_back(chemistry::Atom(0.f, 1.f, 0.f, 1));
100     data.push_back(chemistry::Atom(0.f, 0.f, 0.f, 8));
101 
102     compute::vector<chemistry::Atom> atoms(data.size(), context);
103     compute::copy(data.begin(), data.end(), atoms.begin(), queue);
104 
105     std::string source = BOOST_COMPUTE_STRINGIZE_SOURCE(
106         __kernel void custom_kernel(__global const Atom *atoms,
107                                     __global float *distances)
108         {
109             const uint i = get_global_id(0);
110             const __global Atom *atom = &atoms[i];
111 
112             const float4 center = { 0, 0, 0, 0 };
113             const float4 position = { atom->x, atom->y, atom->z, 0 };
114 
115             distances[i] = distance(position, center);
116         }
117     );
118 
119     // add type definition for Atom to the start of the program source
120     source = compute::type_definition<chemistry::Atom>() + "\n" + source;
121 
122     compute::program program =
123         compute::program::build_with_source(source, context);
124 
125     compute::vector<float> distances(atoms.size(), context);
126 
127     compute::kernel custom_kernel = program.create_kernel("custom_kernel");
128     custom_kernel.set_arg(0, atoms);
129     custom_kernel.set_arg(1, distances);
130 
131     queue.enqueue_1d_range_kernel(custom_kernel, 0, atoms.size(), 1);
132 }
133 
134 // Creates a StructWithArray containing 'x', 'y', 'z'.
make_struct_with_array(int x,int y,int z)135 StructWithArray make_struct_with_array(int x, int y, int z)
136 {
137     StructWithArray s;
138     s.value = 0;
139     s.array[0] = x;
140     s.array[1] = y;
141     s.array[2] = z;
142     return s;
143 }
144 
BOOST_AUTO_TEST_CASE(struct_with_array)145 BOOST_AUTO_TEST_CASE(struct_with_array)
146 {
147     compute::vector<StructWithArray> structs(context);
148 
149     structs.push_back(make_struct_with_array(1, 2, 3), queue);
150     structs.push_back(make_struct_with_array(4, 5, 6), queue);
151     structs.push_back(make_struct_with_array(7, 8, 9), queue);
152 
153     BOOST_COMPUTE_FUNCTION(int, sum_array, (StructWithArray x),
154     {
155         return x.array[0] + x.array[1] + x.array[2];
156     });
157 
158     compute::vector<int> results(structs.size(), context);
159     compute::transform(
160         structs.begin(), structs.end(), results.begin(), sum_array, queue
161     );
162     CHECK_RANGE_EQUAL(int, 3, results, (6, 15, 24));
163 }
164 
165 BOOST_AUTO_TEST_SUITE_END()
166