1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2013 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 <iostream>
12
13 #include <boost/compute/algorithm/copy.hpp>
14 #include <boost/compute/algorithm/transform.hpp>
15 #include <boost/compute/container/vector.hpp>
16 #include <boost/compute/functional/operator.hpp>
17
18 namespace compute = boost::compute;
19
20 // this example demonstrates how to use Boost.Compute's STL
21 // implementation to add two vectors on the GPU
main()22 int main()
23 {
24 // setup input arrays
25 float a[] = { 1, 2, 3, 4 };
26 float b[] = { 5, 6, 7, 8 };
27
28 // make space for the output
29 float c[] = { 0, 0, 0, 0 };
30
31 // create vectors and transfer data for the input arrays 'a' and 'b'
32 compute::vector<float> vector_a(a, a + 4);
33 compute::vector<float> vector_b(b, b + 4);
34
35 // create vector for the output array
36 compute::vector<float> vector_c(4);
37
38 // add the vectors together
39 compute::transform(
40 vector_a.begin(),
41 vector_a.end(),
42 vector_b.begin(),
43 vector_c.begin(),
44 compute::plus<float>()
45 );
46
47 // transfer results back to the host array 'c'
48 compute::copy(vector_c.begin(), vector_c.end(), c);
49
50 // print out results in 'c'
51 std::cout << "c: [" << c[0] << ", "
52 << c[1] << ", "
53 << c[2] << ", "
54 << c[3] << "]" << std::endl;
55
56 return 0;
57 }
58