Skip to content

Commit b1acf0a

Browse files
committed
Benchmarks for find() algorithm
Boost.Compute, STL and Thrust benchmarks for find() algorithm.
1 parent aa15cd6 commit b1acf0a

File tree

5 files changed

+190
-0
lines changed

5 files changed

+190
-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
@@ -118,6 +120,7 @@ if(${BOOST_COMPUTE_HAVE_CUDA})
118120
thrust_accumulate
119121
thrust_count
120122
thrust_exclusive_scan
123+
thrust_find
121124
thrust_inner_product
122125
thrust_merge
123126
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",
@@ -136,6 +137,7 @@ def run_benchmark(name, sizes, vs=[]):
136137
"stl": [
137138
"accumulate",
138139
"count",
140+
"find",
139141
"find_end",
140142
"includes",
141143
"inner_product",

perf/perf_find.cpp

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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_it;
58+
59+
perf_timer t;
60+
for(size_t trial = 0; trial < PERF_TRIALS; trial++){
61+
t.start();
62+
device_it = boost::compute::find(
63+
device_vector.begin(), device_vector.end(), wanted, queue
64+
);
65+
queue.finish();
66+
t.stop();
67+
}
68+
std::cout << "time: " << t.min_time() / 1e6 << " ms" << std::endl;
69+
70+
// verify if found index is correct by comparing it with std::find() result
71+
size_t host_index = std::distance(host_vector.begin(),
72+
std::find(host_vector.begin(),
73+
host_vector.end(),
74+
wanted));
75+
size_t device_index = device_it.get_index();
76+
77+
if(device_index != host_index){
78+
std::cout << "ERROR: "
79+
<< "device_index (" << device_index << ") "
80+
<< "!= "
81+
<< "host_index (" << host_index << ")"
82+
<< std::endl;
83+
return -1;
84+
}
85+
86+
return 0;
87+
}

perf/perf_stl_find.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
perf_timer t;
37+
for(size_t trial = 0; trial < PERF_TRIALS; trial++){
38+
t.start();
39+
std::find(host_vector.begin(), host_vector.end(), wanted);
40+
t.stop();
41+
}
42+
std::cout << "time: " << t.min_time() / 1e6 << " ms" << std::endl;
43+
44+
return 0;
45+
}

perf/perf_thrust_find.cu

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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+
perf_timer t;
44+
for(size_t trial = 0; trial < PERF_TRIALS; trial++){
45+
t.start();
46+
thrust::find(v.begin(), v.end(), wanted);
47+
cudaDeviceSynchronize();
48+
t.stop();
49+
}
50+
std::cout << "time: " << t.min_time() / 1e6 << " ms" << std::endl;
51+
52+
return 0;
53+
}

0 commit comments

Comments
 (0)