#include <iostream>
#include <boost/compute/system.hpp>
#include <boost/compute/algorithm/copy.hpp>
#include <boost/compute/algorithm/copy_n.hpp>
#include <boost/compute/algorithm/find_if.hpp>
#include <boost/compute/container/vector.hpp>
#include <boost/compute/iterator/zip_iterator.hpp>
namespace compute = boost::compute;
int main()
{
compute::device gpu = compute::system::default_device();
compute::context context(gpu);
compute::command_queue queue(context, gpu);
std::vector<float> prices1;
for(float i = 10.0; i <= 11.0; i += 0.1){
prices1.push_back(i);
}
std::vector<float> prices2;
for(float i = 11.0; i >= 10.0; i -= 0.1){
prices2.push_back(i);
}
compute::vector<float> gpu_prices1(prices1.size(), context);
compute::vector<float> gpu_prices2(prices2.size(), context);
compute::copy(prices1.begin(), prices1.end(), gpu_prices1.begin(), queue);
compute::copy(prices2.begin(), prices2.end(), gpu_prices2.begin(), queue);
BOOST_COMPUTE_FUNCTION(bool, check_price_cross, (boost::tuple<float, float> prices),
{
const float first = boost_tuple_get(prices, 0);
const float second = boost_tuple_get(prices, 1);
return second < first;
});
compute::vector<float>::iterator iter = boost::get<0>(
compute::find_if(
compute::make_zip_iterator(
boost::make_tuple(gpu_prices1.begin(), gpu_prices2.begin())
),
compute::make_zip_iterator(
boost::make_tuple(gpu_prices1.end(), gpu_prices2.end())
),
check_price_cross,
queue
).get_iterator_tuple()
);
int index = std::distance(gpu_prices1.begin(), iter);
std::cout << "price cross at index: " << index << std::endl;
float value;
compute::copy_n(iter, 1, &value, queue);
std::cout << "value: " << value << std::endl;
return 0;
}