host_sort.cpp 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. #include <iostream>
  11. #include <vector>
  12. #include <boost/spirit/include/karma.hpp>
  13. #include <boost/compute/system.hpp>
  14. #include <boost/compute/algorithm/sort.hpp>
  15. namespace compute = boost::compute;
  16. namespace karma = boost::spirit::karma;
  17. int rand_int()
  18. {
  19. return rand() % 100;
  20. }
  21. // this example demonstrates how to sort a std::vector of ints on the GPU
  22. int main()
  23. {
  24. // get default device and setup context
  25. compute::device gpu = compute::system::default_device();
  26. compute::context context(gpu);
  27. compute::command_queue queue(context, gpu);
  28. std::cout << "device: " << gpu.name() << std::endl;
  29. // create vector of random values on the host
  30. std::vector<int> vector(8);
  31. std::generate(vector.begin(), vector.end(), rand_int);
  32. // print input vector
  33. std::cout << "input: [ "
  34. << karma::format(karma::int_ % ", ", vector)
  35. << " ]"
  36. << std::endl;
  37. // sort vector
  38. compute::sort(vector.begin(), vector.end(), queue);
  39. // print sorted vector
  40. std::cout << "output: [ "
  41. << karma::format(karma::int_ % ", ", vector)
  42. << " ]"
  43. << std::endl;
  44. return 0;
  45. }