MCPcopy Create free account
hub / github.com/boostorg/compute / main

Function main

example/price_cross.cpp:24–87  ·  view source on GitHub ↗

this example shows how to use the find_if() algorithm to detect the point at which two vectors of prices (such as stock prices) cross.

Source from the content-addressed store, hash-verified

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.
24int 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(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(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(prices2.begin(), prices2.end(), 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

Callers

nothing calls this directly

Calls 8

copyFunction · 0.85
find_ifFunction · 0.85
make_zip_iteratorFunction · 0.85
copy_nFunction · 0.85
push_backMethod · 0.80
sizeMethod · 0.45
beginMethod · 0.45
endMethod · 0.45

Tested by

no test coverage detected