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://boostorg.github.com/compute for more information. 9 //---------------------------------------------------------------------------// 10 11 #include <iostream> 12 #include <algorithm> 13 #include <vector> 14 15 #include <bolt/cl/copy.h> 16 #include <bolt/cl/device_vector.h> 17 #include <bolt/cl/max_element.h> 18 19 #include "perf.hpp" 20 rand_int()21int rand_int() 22 { 23 return static_cast<int>(rand() % 10000000); 24 } 25 main(int argc,char * argv[])26int main(int argc, char *argv[]) 27 { 28 perf_parse_args(argc, argv); 29 30 std::cout << "size: " << PERF_N << std::endl; 31 32 bolt::cl::control ctrl = bolt::cl::control::getDefault(); 33 ::cl::Device device = ctrl.getDevice(); 34 std::cout << "device: " << device.getInfo<CL_DEVICE_NAME>() << std::endl; 35 36 // create host vector 37 std::vector<int> host_vec = generate_random_vector<int>(PERF_N); 38 39 // create device vectors 40 bolt::cl::device_vector<int> device_vec(PERF_N); 41 42 // transfer data to the device 43 bolt::cl::copy(host_vec.begin(), host_vec.end(), device_vec.begin()); 44 45 bolt::cl::device_vector<int>::iterator max_iter = device_vec.begin(); 46 perf_timer t; 47 for(size_t trial = 0; trial < PERF_TRIALS; trial++){ 48 t.start(); 49 max_iter = bolt::cl::max_element(device_vec.begin(), device_vec.end()); 50 t.stop(); 51 } 52 53 int device_max = *max_iter; 54 std::cout << "time: " << t.min_time() / 1e6 << " ms" << std::endl; 55 std::cout << "max: " << device_max << std::endl; 56 57 // verify max is correct 58 int host_max = *std::max_element(host_vec.begin(), host_vec.end()); 59 if(device_max != host_max){ 60 std::cout << "ERROR: " 61 << "device_max (" << device_max << ") " 62 << "!= " 63 << "host_max (" << host_max << ")" 64 << std::endl; 65 return -1; 66 } 67 68 return 0; 69 } 70