elementwise_operations_with_constants_test.hpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #ifndef TEST_ELEMENT_CONSTANT_OPENCL_HH
  2. #define TEST_ELEMENT_CONSTANT_OPENCL_HH
  3. #include "test_opencl.hpp"
  4. template <class T, class F, int number_of_tests, int max_dimension>
  5. class bench_elementwise_constant
  6. {
  7. public:
  8. typedef test_opencl<T, F> test;
  9. void run()
  10. {
  11. opencl::library lib;
  12. int passedOperations = 0;
  13. // get default device and setup context
  14. compute::device device = compute::system::default_device();
  15. compute::context context(device);
  16. compute::command_queue queue(context, device);
  17. std::srand(time(0));
  18. ublas::matrix<T, F> m;
  19. ublas::matrix<T, F> m_result_add_ublas;
  20. ublas::matrix<T, F> m_result_sub_ublas;
  21. ublas::matrix<T, F> m_result_add_opencl;
  22. ublas::matrix<T, F> m_result_sub_opencl;
  23. ublas::vector<T> v;
  24. ublas::vector<T> v_result_add_ublas;
  25. ublas::vector<T> v_result_sub_ublas;
  26. ublas::vector<T> v_result_add_opencl;
  27. ublas::vector<T> v_result_sub_opencl;
  28. for (int i = 0; i<number_of_tests; i++)
  29. {
  30. int rows = std::rand() % max_dimension + 1;
  31. int cols = std::rand() % max_dimension + 1;
  32. m.resize(rows, cols);
  33. v.resize(rows);
  34. test::init_matrix(m, 200);
  35. test::init_vector(v, 200);
  36. T constant = rand() % max_dimension;
  37. ublas::matrix<T, F> m_constant_holder(rows, cols, constant);
  38. ublas::vector<T> v_constant_holder(rows, constant);
  39. m_result_add_ublas = m + m_constant_holder;
  40. m_result_sub_ublas = m - m_constant_holder;
  41. m_result_add_opencl = opencl::element_add(m, constant, queue);
  42. m_result_sub_opencl = opencl::element_sub(m, constant, queue);
  43. v_result_add_ublas = v + v_constant_holder;
  44. v_result_sub_ublas = v - v_constant_holder;
  45. v_result_add_opencl = opencl::element_add(v, constant, queue);
  46. v_result_sub_opencl = opencl::element_sub(v, constant, queue);
  47. if ((!test::compare(m_result_add_ublas, m_result_add_opencl))
  48. || (!test::compare(m_result_sub_ublas, m_result_sub_opencl)) ||
  49. (!test::compare(v_result_add_ublas, v_result_add_opencl))
  50. || (!test::compare(v_result_sub_ublas, v_result_sub_opencl)))
  51. {
  52. std::cout << "Error in calculations" << std::endl;
  53. std::cout << "passed: " << passedOperations << std::endl;
  54. return;
  55. }
  56. passedOperations++;
  57. }
  58. std::cout << "All is well (matrix opencl elementwise operations with constants) of " << typeid(T).name() << std::endl;
  59. }
  60. };
  61. #endif