price_cross.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. //---------------------------------------------------------------------------//
  2. // Copyright (c) 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 <boost/compute/system.hpp>
  12. #include <boost/compute/algorithm/copy.hpp>
  13. #include <boost/compute/algorithm/copy_n.hpp>
  14. #include <boost/compute/algorithm/find_if.hpp>
  15. #include <boost/compute/container/vector.hpp>
  16. #include <boost/compute/iterator/zip_iterator.hpp>
  17. namespace compute = boost::compute;
  18. // this example shows how to use the find_if() algorithm to detect the
  19. // point at which two vectors of prices (such as stock prices) cross.
  20. int main()
  21. {
  22. // get default device and setup context
  23. compute::device gpu = compute::system::default_device();
  24. compute::context context(gpu);
  25. compute::command_queue queue(context, gpu);
  26. // prices #1 (from 10.0 to 11.0)
  27. std::vector<float> prices1;
  28. for(float i = 10.0; i <= 11.0; i += 0.1){
  29. prices1.push_back(i);
  30. }
  31. // prices #2 (from 11.0 to 10.0)
  32. std::vector<float> prices2;
  33. for(float i = 11.0; i >= 10.0; i -= 0.1){
  34. prices2.push_back(i);
  35. }
  36. // create gpu vectors
  37. compute::vector<float> gpu_prices1(prices1.size(), context);
  38. compute::vector<float> gpu_prices2(prices2.size(), context);
  39. // copy prices to gpu
  40. compute::copy(prices1.begin(), prices1.end(), gpu_prices1.begin(), queue);
  41. compute::copy(prices2.begin(), prices2.end(), gpu_prices2.begin(), queue);
  42. // function returning true if the second price is less than the first price
  43. BOOST_COMPUTE_FUNCTION(bool, check_price_cross, (boost::tuple<float, float> prices),
  44. {
  45. // first price
  46. const float first = boost_tuple_get(prices, 0);
  47. // second price
  48. const float second = boost_tuple_get(prices, 1);
  49. // return true if second price is less than first
  50. return second < first;
  51. });
  52. // find cross point (should be 10.5)
  53. compute::vector<float>::iterator iter = boost::get<0>(
  54. compute::find_if(
  55. compute::make_zip_iterator(
  56. boost::make_tuple(gpu_prices1.begin(), gpu_prices2.begin())
  57. ),
  58. compute::make_zip_iterator(
  59. boost::make_tuple(gpu_prices1.end(), gpu_prices2.end())
  60. ),
  61. check_price_cross,
  62. queue
  63. ).get_iterator_tuple()
  64. );
  65. // print out result
  66. int index = std::distance(gpu_prices1.begin(), iter);
  67. std::cout << "price cross at index: " << index << std::endl;
  68. float value;
  69. compute::copy_n(iter, 1, &value, queue);
  70. std::cout << "value: " << value << std::endl;
  71. return 0;
  72. }