• 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 #include <algorithm>
12 #include <iostream>
13 #include <vector>
14 
15 #include <boost/compute/system.hpp>
16 #include <boost/compute/algorithm/copy.hpp>
17 #include <boost/compute/algorithm/sort.hpp>
18 #include <boost/compute/container/vector.hpp>
19 
20 namespace compute = boost::compute;
21 
rand_int()22 int rand_int()
23 {
24     return rand() % 100;
25 }
26 
27 // this example demonstrates how to sort a vector of ints on the GPU
main()28 int main()
29 {
30     // create vector of random values on the host
31     std::vector<int> host_vector(10);
32     std::generate(host_vector.begin(), host_vector.end(), rand_int);
33 
34     // print out input vector
35     std::cout << "input:  [ ";
36     for(size_t i = 0; i < host_vector.size(); i++){
37         std::cout << host_vector[i];
38 
39         if(i != host_vector.size() - 1){
40             std::cout << ", ";
41         }
42     }
43     std::cout << " ]" << std::endl;
44 
45     // transfer the values to the device
46     compute::vector<int> device_vector = host_vector;
47 
48     // sort the values on the device
49     compute::sort(device_vector.begin(), device_vector.end());
50 
51     // transfer the values back to the host
52     compute::copy(device_vector.begin(),
53                   device_vector.end(),
54                   host_vector.begin());
55 
56     // print out the sorted vector
57     std::cout << "output: [ ";
58     for(size_t i = 0; i < host_vector.size(); i++){
59         std::cout << host_vector[i];
60 
61         if(i != host_vector.size() - 1){
62             std::cout << ", ";
63         }
64     }
65     std::cout << " ]" << std::endl;
66 
67     return 0;
68 }
69