1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2013-2014 Kyle Lutz <kyle.r.lutz@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 #define _USE_MATH_DEFINES
12 #include <algorithm>
13 #include <iostream>
14 #include <vector>
15
16 #include <boost/compute/system.hpp>
17 #include <boost/compute/algorithm/copy.hpp>
18 #include <boost/compute/algorithm/copy_n.hpp>
19 #include <boost/compute/algorithm/transform.hpp>
20 #include <boost/compute/container/vector.hpp>
21
22 #include "perf.hpp"
23
24 namespace compute = boost::compute;
25
26 using compute::float2_;
27
rand_float()28 float rand_float()
29 {
30 return (float(rand()) / float(RAND_MAX)) * 1000.f;
31 }
32
serial_cartesian_to_polar(const float * input,size_t n,float * output)33 void serial_cartesian_to_polar(const float *input, size_t n, float *output)
34 {
35 for(size_t i = 0; i < n; i++){
36 float x = input[i*2+0];
37 float y = input[i*2+1];
38
39 float magnitude = std::sqrt(x*x + y*y);
40 float angle = std::atan2(y, x) * 180.f / M_PI;
41
42 output[i*2+0] = magnitude;
43 output[i*2+1] = angle;
44 }
45 }
46
serial_polar_to_cartesian(const float * input,size_t n,float * output)47 void serial_polar_to_cartesian(const float *input, size_t n, float *output)
48 {
49 for(size_t i = 0; i < n; i++){
50 float magnitude = input[i*2+0];
51 float angle = input[i*2+1];
52
53 float x = magnitude * cos(angle);
54 float y = magnitude * sin(angle);
55
56 output[i*2+0] = x;
57 output[i*2+1] = y;
58 }
59 }
60
61 // converts from cartesian coordinates (x, y) to polar coordinates (magnitude, angle)
62 BOOST_COMPUTE_FUNCTION(float2_, cartesian_to_polar, (float2_ p),
63 {
64 float x = p.x;
65 float y = p.y;
66
67 float magnitude = sqrt(x*x + y*y);
68 float angle = atan2(y, x) * 180.f / M_PI;
69
70 return (float2)(magnitude, angle);
71 });
72
73 // converts from polar coordinates (magnitude, angle) to cartesian coordinates (x, y)
74 BOOST_COMPUTE_FUNCTION(float2_, polar_to_cartesian, (float2_ p),
75 {
76 float magnitude = p.x;
77 float angle = p.y;
78
79 float x = magnitude * cos(angle);
80 float y = magnitude * sin(angle);
81
82 return (float2)(x, y)
83 });
84
main(int argc,char * argv[])85 int main(int argc, char *argv[])
86 {
87 perf_parse_args(argc, argv);
88
89 std::cout << "size: " << PERF_N << std::endl;
90
91 // setup context and queue for the default device
92 compute::device device = compute::system::default_device();
93 compute::context context(device);
94 compute::command_queue queue(context, device);
95 std::cout << "device: " << device.name() << std::endl;
96
97 // create vector of random numbers on the host
98 std::vector<float> host_vector(PERF_N*2);
99 std::generate(host_vector.begin(), host_vector.end(), rand_float);
100
101 // create vector on the device and copy the data
102 compute::vector<float2_> device_vector(PERF_N, context);
103 compute::copy_n(
104 reinterpret_cast<float2_ *>(&host_vector[0]),
105 PERF_N,
106 device_vector.begin(),
107 queue
108 );
109
110 perf_timer t;
111 for(size_t trial = 0; trial < PERF_TRIALS; trial++){
112 t.start();
113 compute::transform(
114 device_vector.begin(),
115 device_vector.end(),
116 device_vector.begin(),
117 cartesian_to_polar,
118 queue
119 );
120 queue.finish();
121 t.stop();
122 }
123 std::cout << "time: " << t.min_time() / 1e6 << " ms" << std::endl;
124
125 // perform saxpy on host
126 t.clear();
127 for(size_t trial = 0; trial < PERF_TRIALS; trial++){
128 t.start();
129 serial_cartesian_to_polar(&host_vector[0], PERF_N, &host_vector[0]);
130 t.stop();
131 }
132 std::cout << "host time: " << t.min_time() / 1e6 << " ms" << std::endl;
133
134 std::vector<float> device_data(PERF_N*2);
135 compute::copy(
136 device_vector.begin(),
137 device_vector.end(),
138 reinterpret_cast<float2_ *>(&device_data[0]),
139 queue
140 );
141
142 for(size_t i = 0; i < PERF_N; i++){
143 float host_value = host_vector[i];
144 float device_value = device_data[i];
145
146 if(std::abs(device_value - host_value) > 1e-3){
147 std::cout << "ERROR: "
148 << "value at " << i << " "
149 << "device_value (" << device_value << ") "
150 << "!= "
151 << "host_value (" << host_value << ")"
152 << std::endl;
153 return -1;
154 }
155 }
156
157 return 0;
158 }
159