|
| 1 | +//---------------------------------------------------------------------------// |
| 2 | +// Copyright (c) 2015 Jakub Szuppe <j.szuppe@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://kylelutz.github.com/compute for more information. |
| 9 | +//---------------------------------------------------------------------------// |
| 10 | + |
| 11 | +#include <algorithm> |
| 12 | +#include <iostream> |
| 13 | +#include <vector> |
| 14 | + |
| 15 | +#include <boost/compute/system.hpp> |
| 16 | +#include <boost/compute/algorithm/find.hpp> |
| 17 | +#include <boost/compute/container/vector.hpp> |
| 18 | + |
| 19 | +#include "perf.hpp" |
| 20 | + |
| 21 | +// Max integer that can be generated by rand_int() function. |
| 22 | +int rand_int_max = 25; |
| 23 | + |
| 24 | +int rand_int() |
| 25 | +{ |
| 26 | + return static_cast<int>((rand() / double(RAND_MAX)) * rand_int_max); |
| 27 | +} |
| 28 | + |
| 29 | +int main(int argc, char *argv[]) |
| 30 | +{ |
| 31 | + perf_parse_args(argc, argv); |
| 32 | + std::cout << "size: " << PERF_N << std::endl; |
| 33 | + |
| 34 | + // setup context and queue for the default device |
| 35 | + boost::compute::device device = boost::compute::system::default_device(); |
| 36 | + boost::compute::context context(device); |
| 37 | + boost::compute::command_queue queue(context, device); |
| 38 | + std::cout << "device: " << device.name() << std::endl; |
| 39 | + |
| 40 | + // create vector of random numbers on the host |
| 41 | + std::vector<int> host_vector(PERF_N); |
| 42 | + std::generate(host_vector.begin(), host_vector.end(), rand_int); |
| 43 | + |
| 44 | + // create vector on the device and copy the data |
| 45 | + boost::compute::vector<int> device_vector(PERF_N, context); |
| 46 | + boost::compute::copy( |
| 47 | + host_vector.begin(), |
| 48 | + host_vector.end(), |
| 49 | + device_vector.begin(), |
| 50 | + queue |
| 51 | + ); |
| 52 | + |
| 53 | + // trying to find element that isn't in vector (worst-case scenario) |
| 54 | + int wanted = rand_int_max + 1; |
| 55 | + |
| 56 | + // device iterator |
| 57 | + boost::compute::vector<int>::iterator device_result_it; |
| 58 | + |
| 59 | + perf_timer t; |
| 60 | + for(size_t trial = 0; trial < PERF_TRIALS; trial++){ |
| 61 | + t.start(); |
| 62 | + device_result_it = boost::compute::find(device_vector.begin(), |
| 63 | + device_vector.end(), |
| 64 | + wanted, |
| 65 | + queue); |
| 66 | + queue.finish(); |
| 67 | + t.stop(); |
| 68 | + } |
| 69 | + std::cout << "time: " << t.min_time() / 1e6 << " ms" << std::endl; |
| 70 | + |
| 71 | + // verify if found index is correct by comparing it with std::find() result |
| 72 | + size_t host_result_index = std::distance(host_vector.begin(), |
| 73 | + std::find(host_vector.begin(), |
| 74 | + host_vector.end(), |
| 75 | + wanted)); |
| 76 | + size_t device_result_index = device_result_it.get_index(); |
| 77 | + |
| 78 | + if(device_result_index != host_result_index){ |
| 79 | + std::cout << "ERROR: " |
| 80 | + << "device_result_index (" << device_result_index << ") " |
| 81 | + << "!= " |
| 82 | + << "host_result_index (" << host_result_index << ")" |
| 83 | + << std::endl; |
| 84 | + return -1; |
| 85 | + } |
| 86 | + |
| 87 | + return 0; |
| 88 | +} |
0 commit comments