]>
Commit | Line | Data |
---|---|---|
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 | ||
11 | #ifndef BOOST_COMPUTE_MEMORY_SVM_PTR_HPP | |
12 | #define BOOST_COMPUTE_MEMORY_SVM_PTR_HPP | |
13 | ||
14 | #include <boost/type_traits.hpp> | |
15 | #include <boost/static_assert.hpp> | |
16 | #include <boost/assert.hpp> | |
17 | ||
18 | #include <boost/compute/cl.hpp> | |
19 | #include <boost/compute/kernel.hpp> | |
20 | #include <boost/compute/context.hpp> | |
21 | #include <boost/compute/command_queue.hpp> | |
22 | #include <boost/compute/type_traits/is_device_iterator.hpp> | |
23 | ||
24 | namespace boost { | |
25 | namespace compute { | |
26 | ||
27 | // forward declaration for svm_ptr<T> | |
28 | template<class T> | |
29 | class svm_ptr; | |
30 | ||
31 | // svm functions require OpenCL 2.0 | |
32 | #if defined(CL_VERSION_2_0) || defined(BOOST_COMPUTE_DOXYGEN_INVOKED) | |
33 | namespace detail { | |
34 | ||
35 | template<class T, class IndexExpr> | |
36 | struct svm_ptr_index_expr | |
37 | { | |
38 | typedef T result_type; | |
39 | ||
40 | svm_ptr_index_expr(const svm_ptr<T> &svm_ptr, | |
41 | const IndexExpr &expr) | |
42 | : m_svm_ptr(svm_ptr), | |
43 | m_expr(expr) | |
44 | { | |
45 | } | |
46 | ||
47 | operator T() const | |
48 | { | |
49 | BOOST_STATIC_ASSERT_MSG(boost::is_integral<IndexExpr>::value, | |
50 | "Index expression must be integral"); | |
51 | ||
52 | BOOST_ASSERT(m_svm_ptr.get()); | |
53 | ||
54 | const context &context = m_svm_ptr.get_context(); | |
55 | const device &device = context.get_device(); | |
56 | command_queue queue(context, device); | |
57 | ||
58 | T value; | |
59 | T* ptr = | |
60 | static_cast<T*>(m_svm_ptr.get()) + static_cast<std::ptrdiff_t>(m_expr); | |
61 | queue.enqueue_svm_map(static_cast<void*>(ptr), sizeof(T), CL_MAP_READ); | |
62 | value = *(ptr); | |
63 | queue.enqueue_svm_unmap(static_cast<void*>(ptr)).wait(); | |
64 | ||
65 | return value; | |
66 | } | |
67 | ||
68 | const svm_ptr<T> &m_svm_ptr; | |
69 | IndexExpr m_expr; | |
70 | }; | |
71 | ||
72 | } // end detail namespace | |
73 | #endif | |
74 | ||
75 | template<class T> | |
76 | class svm_ptr | |
77 | { | |
78 | public: | |
79 | typedef T value_type; | |
80 | typedef std::ptrdiff_t difference_type; | |
81 | typedef T* pointer; | |
82 | typedef T& reference; | |
83 | typedef std::random_access_iterator_tag iterator_category; | |
84 | ||
85 | svm_ptr() | |
86 | : m_ptr(0) | |
87 | { | |
88 | } | |
89 | ||
90 | svm_ptr(void *ptr, const context &context) | |
91 | : m_ptr(static_cast<T*>(ptr)), | |
92 | m_context(context) | |
93 | { | |
94 | } | |
95 | ||
96 | svm_ptr(const svm_ptr<T> &other) | |
97 | : m_ptr(other.m_ptr), | |
98 | m_context(other.m_context) | |
99 | { | |
100 | } | |
101 | ||
102 | svm_ptr<T>& operator=(const svm_ptr<T> &other) | |
103 | { | |
104 | m_ptr = other.m_ptr; | |
105 | m_context = other.m_context; | |
106 | return *this; | |
107 | } | |
108 | ||
109 | ~svm_ptr() | |
110 | { | |
111 | } | |
112 | ||
113 | void* get() const | |
114 | { | |
115 | return m_ptr; | |
116 | } | |
117 | ||
118 | svm_ptr<T> operator+(difference_type n) | |
119 | { | |
120 | return svm_ptr<T>(m_ptr + n, m_context); | |
121 | } | |
122 | ||
123 | difference_type operator-(svm_ptr<T> other) | |
124 | { | |
125 | BOOST_ASSERT(other.m_context == m_context); | |
126 | return m_ptr - other.m_ptr; | |
127 | } | |
128 | ||
129 | context& get_context() const | |
130 | { | |
131 | return m_context; | |
132 | } | |
133 | ||
134 | // svm functions require OpenCL 2.0 | |
135 | #if defined(CL_VERSION_2_0) || defined(BOOST_COMPUTE_DOXYGEN_INVOKED) | |
136 | /// \internal_ | |
137 | template<class Expr> | |
138 | detail::svm_ptr_index_expr<T, Expr> | |
139 | operator[](const Expr &expr) const | |
140 | { | |
141 | BOOST_ASSERT(m_ptr); | |
142 | ||
143 | return detail::svm_ptr_index_expr<T, Expr>(*this, | |
144 | expr); | |
145 | } | |
146 | #endif | |
147 | ||
148 | private: | |
149 | T *m_ptr; | |
150 | context m_context; | |
151 | }; | |
152 | ||
153 | namespace detail { | |
154 | ||
155 | /// \internal_ | |
156 | template<class T> | |
157 | struct set_kernel_arg<svm_ptr<T> > | |
158 | { | |
159 | void operator()(kernel &kernel_, size_t index, const svm_ptr<T> &ptr) | |
160 | { | |
161 | kernel_.set_arg_svm_ptr(index, ptr.get()); | |
162 | } | |
163 | }; | |
164 | ||
165 | } // end detail namespace | |
166 | ||
167 | /// \internal_ (is_device_iterator specialization for svm_ptr) | |
168 | template<class T> | |
169 | struct is_device_iterator<svm_ptr<T> > : boost::true_type {}; | |
170 | ||
171 | } // end compute namespace | |
172 | } // end boost namespace | |
173 | ||
174 | #endif // BOOST_COMPUTE_MEMORY_SVM_PTR_HPP |