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 <algorithm> 12 #include <iostream> 13 #include <vector> 14 15 #include <thrust/find.h> 16 #include <thrust/host_vector.h> 17 #include <thrust/device_vector.h> 18 19 #include "perf.hpp" 20 21 // Max integer that can be generated by rand_int() function. 22 int rand_int_max = 25; 23 rand_int()24int rand_int() 25 { 26 return static_cast<int>((rand() / double(RAND_MAX)) * rand_int_max); 27 } 28 main(int argc,char * argv[])29int main(int argc, char *argv[]) 30 { 31 perf_parse_args(argc, argv); 32 std::cout << "size: " << PERF_N << std::endl; 33 34 // create vector of random numbers on the host 35 thrust::host_vector<int> host_vector(PERF_N); 36 thrust::generate(host_vector.begin(), host_vector.end(), rand_int); 37 38 thrust::device_vector<int> v = host_vector; 39 40 // trying to find element that isn't in vector (worst-case scenario) 41 int wanted = rand_int_max + 1; 42 43 // result 44 thrust::device_vector<int>::iterator device_result_it; 45 46 perf_timer t; 47 for(size_t trial = 0; trial < PERF_TRIALS; trial++){ 48 t.start(); 49 device_result_it = thrust::find(v.begin(), v.end(), wanted); 50 cudaDeviceSynchronize(); 51 t.stop(); 52 } 53 std::cout << "time: " << t.min_time() / 1e6 << " ms" << std::endl; 54 55 // verify 56 if(device_result_it != v.end()){ 57 std::cout << "ERROR: " 58 << "device_result_iterator != " 59 << "v.end()" 60 << std::endl; 61 return -1; 62 } 63 64 return 0; 65 } 66