Skip to content

Commit 415e7a0

Browse files
committed
Merge pull request #444 from haahh/find_benchmark
Benchmarks for find() algorithm #325
2 parents 0001385 + a65de68 commit 415e7a0

File tree

5 files changed

+216
-0
lines changed

5 files changed

+216
-0
lines changed

perf/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ set(BENCHMARKS
2525
erase_remove
2626
exclusive_scan
2727
fill
28+
find
2829
find_end
2930
includes
3031
inner_product
@@ -70,6 +71,7 @@ endforeach()
7071
set(STL_BENCHMARKS
7172
stl_accumulate
7273
stl_count
74+
stl_find
7375
stl_find_end
7476
stl_includes
7577
stl_inner_product
@@ -119,6 +121,7 @@ if(${BOOST_COMPUTE_HAVE_CUDA})
119121
thrust_accumulate
120122
thrust_count
121123
thrust_exclusive_scan
124+
thrust_find
122125
thrust_inner_product
123126
thrust_merge
124127
thrust_partial_sum

perf/perf.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ def run_benchmark(name, sizes, vs=[]):
118118
"accumulate",
119119
"count",
120120
"exclusive_scan",
121+
"find",
121122
"inner_product",
122123
"merge",
123124
"partial_sum",
@@ -137,6 +138,7 @@ def run_benchmark(name, sizes, vs=[]):
137138
"stl": [
138139
"accumulate",
139140
"count",
141+
"find",
140142
"find_end",
141143
"includes",
142144
"inner_product",

perf/perf_find.cpp

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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+
}

perf/perf_stl_find.cpp

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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 "perf.hpp"
16+
17+
// Max integer that can be generated by rand_int() function.
18+
int rand_int_max = 25;
19+
20+
int rand_int()
21+
{
22+
return static_cast<int>((rand() / double(RAND_MAX)) * rand_int_max);
23+
}
24+
25+
int main(int argc, char *argv[])
26+
{
27+
perf_parse_args(argc, argv);
28+
std::cout << "size: " << PERF_N << std::endl;
29+
30+
// create vector of random numbers on the host
31+
std::vector<int> host_vector(PERF_N);
32+
std::generate(host_vector.begin(), host_vector.end(), rand_int);
33+
34+
// trying to find element that isn't in vector (worst-case scenario)
35+
int wanted = rand_int_max + 1;
36+
37+
// result
38+
std::vector<int>::iterator host_result_it;
39+
40+
perf_timer t;
41+
for(size_t trial = 0; trial < PERF_TRIALS; trial++){
42+
t.start();
43+
host_result_it = std::find(host_vector.begin(), host_vector.end(), wanted);
44+
t.stop();
45+
}
46+
std::cout << "time: " << t.min_time() / 1e6 << " ms" << std::endl;
47+
48+
// verify
49+
if(host_result_it != host_vector.end()){
50+
std::cout << "ERROR: "
51+
<< "host_result_iterator != "
52+
<< "host_vector.end()"
53+
<< std::endl;
54+
return -1;
55+
}
56+
57+
return 0;
58+
}

perf/perf_thrust_find.cu

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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 <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+
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+
// 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+
}

0 commit comments

Comments
 (0)