|
| 1 | +//---------------------------------------------------------------------------// |
| 2 | +// Copyright (c) 2013-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://kylelutz.github.com/compute for more information. |
| 9 | +//---------------------------------------------------------------------------// |
| 10 | + |
| 11 | +#include <algorithm> |
| 12 | +#include <iostream> |
| 13 | +#include <numeric> |
| 14 | +#include <vector> |
| 15 | + |
| 16 | +#include <boost/compute/system.hpp> |
| 17 | +#include <boost/compute/algorithm/reverse_copy.hpp> |
| 18 | +#include <boost/compute/container/vector.hpp> |
| 19 | + |
| 20 | +#include "perf.hpp" |
| 21 | + |
| 22 | +int rand_int() |
| 23 | +{ |
| 24 | + return static_cast<int>((rand() / double(RAND_MAX)) * 25.0); |
| 25 | +} |
| 26 | + |
| 27 | +int main(int argc, char *argv[]) |
| 28 | +{ |
| 29 | + perf_parse_args(argc, argv); |
| 30 | + std::cout << "size: " << PERF_N << std::endl; |
| 31 | + |
| 32 | + // setup context and queue for the default device |
| 33 | + boost::compute::device device = boost::compute::system::default_device(); |
| 34 | + boost::compute::context context(device); |
| 35 | + boost::compute::command_queue queue(context, device); |
| 36 | + std::cout << "device: " << device.name() << std::endl; |
| 37 | + |
| 38 | + // create vector of random numbers on the host |
| 39 | + std::vector<int> host_vector(PERF_N); |
| 40 | + std::generate(host_vector.begin(), host_vector.end(), rand_int); |
| 41 | + |
| 42 | + // create vector on the device and copy the data |
| 43 | + boost::compute::vector<int> device_vector(PERF_N, context); |
| 44 | + boost::compute::copy( |
| 45 | + host_vector.begin(), host_vector.end(), device_vector.begin(), queue |
| 46 | + ); |
| 47 | + |
| 48 | + // create vector on the device for reversed data |
| 49 | + boost::compute::vector<int> device_reversed_vector(PERF_N, context); |
| 50 | + |
| 51 | + perf_timer t; |
| 52 | + for(size_t trial = 0; trial < PERF_TRIALS; trial++){ |
| 53 | + t.start(); |
| 54 | + boost::compute::reverse_copy( |
| 55 | + device_vector.begin(), device_vector.end(), |
| 56 | + device_reversed_vector.begin(), |
| 57 | + queue |
| 58 | + ); |
| 59 | + queue.finish(); |
| 60 | + t.stop(); |
| 61 | + } |
| 62 | + std::cout << "time: " << t.min_time() / 1e6 << " ms" << std::endl; |
| 63 | + |
| 64 | + return 0; |
| 65 | +} |
0 commit comments