perf_cart_to_polar.cpp 4.4 KB

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