read_write_single_value.hpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. //---------------------------------------------------------------------------//
  2. // Copyright (c) 2013 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. #ifndef BOOST_COMPUTE_DETAIL_READ_WRITE_SINGLE_VALUE_HPP
  11. #define BOOST_COMPUTE_DETAIL_READ_WRITE_SINGLE_VALUE_HPP
  12. #include <boost/throw_exception.hpp>
  13. #include <boost/compute/buffer.hpp>
  14. #include <boost/compute/event.hpp>
  15. #include <boost/compute/exception.hpp>
  16. #include <boost/compute/command_queue.hpp>
  17. namespace boost {
  18. namespace compute {
  19. namespace detail {
  20. // reads and returns a single value at index in the buffer
  21. template<class T>
  22. inline T read_single_value(const buffer &buffer,
  23. size_t index,
  24. command_queue &queue)
  25. {
  26. BOOST_ASSERT(index < buffer.size() / sizeof(T));
  27. BOOST_ASSERT(buffer.get_context() == queue.get_context());
  28. T value;
  29. queue.enqueue_read_buffer(buffer,
  30. sizeof(T) * index,
  31. sizeof(T),
  32. &value);
  33. return value;
  34. }
  35. // reads and returns a the first value in the buffer
  36. template<class T>
  37. inline T read_single_value(const buffer &buffer, command_queue &queue)
  38. {
  39. return read_single_value<T>(buffer, 0, queue);
  40. }
  41. // writes a single value at index to the buffer
  42. template<class T>
  43. inline event write_single_value(const T &value,
  44. const buffer &buffer,
  45. size_t index,
  46. command_queue &queue)
  47. {
  48. BOOST_ASSERT(index < buffer.size() / sizeof(T));
  49. BOOST_ASSERT(buffer.get_context() == queue.get_context());
  50. return queue.enqueue_write_buffer(buffer,
  51. index * sizeof(T),
  52. sizeof(T),
  53. &value);
  54. }
  55. // writes value to the first location in buffer
  56. template<class T>
  57. inline void write_single_value(const T &value,
  58. const buffer &buffer,
  59. command_queue &queue)
  60. {
  61. write_single_value<T>(value, buffer, 0, queue);
  62. }
  63. } // end detail namespace
  64. } // end compute namespace
  65. } // end boost namespace
  66. #endif // BOOST_COMPUTE_DETAIL_READ_WRITE_SINGLE_VALUE_HPP