• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 //[transform_sqrt_example
12 
13 #include <vector>
14 #include <algorithm>
15 
16 #include <boost/compute/algorithm/transform.hpp>
17 #include <boost/compute/container/vector.hpp>
18 #include <boost/compute/functional/math.hpp>
19 
20 namespace compute = boost::compute;
21 
main()22 int main()
23 {
24     // get default device and setup context
25     compute::device device = compute::system::default_device();
26     compute::context context(device);
27     compute::command_queue queue(context, device);
28 
29     // generate random data on the host
30     std::vector<float> host_vector(10000);
31     std::generate(host_vector.begin(), host_vector.end(), rand);
32 
33     // create a vector on the device
34     compute::vector<float> device_vector(host_vector.size(), context);
35 
36     // transfer data from the host to the device
37     compute::copy(
38         host_vector.begin(), host_vector.end(), device_vector.begin(), queue
39     );
40 
41     // calculate the square-root of each element in-place
42     compute::transform(
43         device_vector.begin(),
44         device_vector.end(),
45         device_vector.begin(),
46         compute::sqrt<float>(),
47         queue
48     );
49 
50     // copy values back to the host
51     compute::copy(
52         device_vector.begin(), device_vector.end(), host_vector.begin(), queue
53     );
54 
55     return 0;
56 }
57 
58 //]
59