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