perf_search.cpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. //---------------------------------------------------------------------------//
  2. // Copyright (c) 2014 Roshan <thisisroshansmail@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. #include <algorithm>
  11. #include <iostream>
  12. #include <numeric>
  13. #include <vector>
  14. #include <boost/compute/system.hpp>
  15. #include <boost/compute/algorithm/search.hpp>
  16. #include <boost/compute/container/vector.hpp>
  17. #include "perf.hpp"
  18. int rand_int()
  19. {
  20. return static_cast<int>((rand() / double(RAND_MAX)) * 25.0);
  21. }
  22. int main(int argc, char *argv[])
  23. {
  24. perf_parse_args(argc, argv);
  25. std::cout << "size: " << PERF_N << std::endl;
  26. // setup context and queue for the default device
  27. boost::compute::device device = boost::compute::system::default_device();
  28. boost::compute::context context(device);
  29. boost::compute::command_queue queue(context, device);
  30. std::cout << "device: " << device.name() << std::endl;
  31. // create vector of random numbers on the host
  32. std::vector<int> host_vector(PERF_N);
  33. std::generate(host_vector.begin(), host_vector.end(), rand_int);
  34. int pattern[] = {2, 6, 6, 7, 8, 4};
  35. // create vector on the device and copy the data
  36. boost::compute::vector<int> device_vector(PERF_N, context);
  37. boost::compute::copy(
  38. host_vector.begin(), host_vector.end(), device_vector.begin(), queue
  39. );
  40. boost::compute::vector<int> pattern_vector(pattern, pattern + 6, queue);
  41. perf_timer t;
  42. for(size_t trial = 0; trial < PERF_TRIALS; trial++){
  43. t.start();
  44. boost::compute::search(
  45. device_vector.begin(), device_vector.end(),
  46. pattern_vector.begin(), pattern_vector.end(), queue
  47. );
  48. queue.finish();
  49. t.stop();
  50. }
  51. std::cout << "time: " << t.min_time() / 1e6 << " ms" << std::endl;
  52. return 0;
  53. }