Skip to content

Commit a65de68

Browse files
committed
Making sure compiler won't cut out find() calls
Making sure that compiler won't cut out std::find() and thrust:find() calls while optimizing the code.
1 parent b1acf0a commit a65de68

File tree

3 files changed

+40
-14
lines changed

3 files changed

+40
-14
lines changed

perf/perf_find.cpp

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -54,31 +54,32 @@ int main(int argc, char *argv[])
5454
int wanted = rand_int_max + 1;
5555

5656
// device iterator
57-
boost::compute::vector<int>::iterator device_it;
57+
boost::compute::vector<int>::iterator device_result_it;
5858

5959
perf_timer t;
6060
for(size_t trial = 0; trial < PERF_TRIALS; trial++){
6161
t.start();
62-
device_it = boost::compute::find(
63-
device_vector.begin(), device_vector.end(), wanted, queue
64-
);
62+
device_result_it = boost::compute::find(device_vector.begin(),
63+
device_vector.end(),
64+
wanted,
65+
queue);
6566
queue.finish();
6667
t.stop();
6768
}
6869
std::cout << "time: " << t.min_time() / 1e6 << " ms" << std::endl;
6970

7071
// 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();
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();
7677

77-
if(device_index != host_index){
78+
if(device_result_index != host_result_index){
7879
std::cout << "ERROR: "
79-
<< "device_index (" << device_index << ") "
80+
<< "device_result_index (" << device_result_index << ") "
8081
<< "!= "
81-
<< "host_index (" << host_index << ")"
82+
<< "host_result_index (" << host_result_index << ")"
8283
<< std::endl;
8384
return -1;
8485
}

perf/perf_stl_find.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,26 @@ int main(int argc, char *argv[])
3333

3434
// trying to find element that isn't in vector (worst-case scenario)
3535
int wanted = rand_int_max + 1;
36+
37+
// result
38+
std::vector<int>::iterator host_result_it;
39+
3640
perf_timer t;
3741
for(size_t trial = 0; trial < PERF_TRIALS; trial++){
3842
t.start();
39-
std::find(host_vector.begin(), host_vector.end(), wanted);
43+
host_result_it = std::find(host_vector.begin(), host_vector.end(), wanted);
4044
t.stop();
4145
}
4246
std::cout << "time: " << t.min_time() / 1e6 << " ms" << std::endl;
4347

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+
4457
return 0;
4558
}

perf/perf_thrust_find.cu

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,26 @@ int main(int argc, char *argv[])
4040
// trying to find element that isn't in vector (worst-case scenario)
4141
int wanted = rand_int_max + 1;
4242

43+
// result
44+
thrust::device_vector<int>::iterator device_result_it;
45+
4346
perf_timer t;
4447
for(size_t trial = 0; trial < PERF_TRIALS; trial++){
4548
t.start();
46-
thrust::find(v.begin(), v.end(), wanted);
49+
device_result_it = thrust::find(v.begin(), v.end(), wanted);
4750
cudaDeviceSynchronize();
4851
t.stop();
4952
}
5053
std::cout << "time: " << t.min_time() / 1e6 << " ms" << std::endl;
5154

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+
5264
return 0;
5365
}

0 commit comments

Comments
 (0)