• 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 <iostream>
12 #include <vector>
13 
14 #include <boost/compute/system.hpp>
15 #include <boost/compute/algorithm/reduce.hpp>
16 #include <boost/compute/container/mapped_view.hpp>
17 
18 namespace compute = boost::compute;
19 
20 // this example demonstrates how to use the mapped_view class to map
21 // an array of numbers to device memory and use the reduce() algorithm
22 // to calculate the sum.
main()23 int main()
24 {
25     // get default device and setup context
26     compute::device gpu = compute::system::default_device();
27     compute::context context(gpu);
28     compute::command_queue queue(context, gpu);
29     std::cout << "device: " << gpu.name() << std::endl;
30 
31     // create data on host
32     int data[] = { 4, 2, 3, 7, 8, 9, 1, 6 };
33 
34     // create mapped view on device
35     compute::mapped_view<int> view(data, 8, context);
36 
37     // use reduce() to calculate sum on the device
38     int sum = 0;
39     compute::reduce(view.begin(), view.end(), &sum, queue);
40 
41     // print the sum on the host
42     std::cout << "sum: " << sum << std::endl;
43 
44     return 0;
45 }
46