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