1 //
2 // Copyright 2012 Francisco Jerez
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining a
5 // copy of this software and associated documentation files (the "Software"),
6 // to deal in the Software without restriction, including without limitation
7 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 // and/or sell copies of the Software, and to permit persons to whom the
9 // Software is furnished to do so, subject to the following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 // OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 // OTHER DEALINGS IN THE SOFTWARE.
21 //
22
23 #include "core/memory.hpp"
24 #include "core/resource.hpp"
25 #include "util/format/u_format.h"
26
27 using namespace clover;
28
memory_obj(clover::context & ctx,std::vector<cl_mem_properties> properties,cl_mem_flags flags,size_t size,void * host_ptr)29 memory_obj::memory_obj(clover::context &ctx,
30 std::vector<cl_mem_properties> properties,
31 cl_mem_flags flags,
32 size_t size, void *host_ptr) :
33 context(ctx), _properties(properties), _flags(flags),
34 _size(size), _host_ptr(host_ptr) {
35 if (flags & CL_MEM_COPY_HOST_PTR)
36 data.append((char *)host_ptr, size);
37 }
38
~memory_obj()39 memory_obj::~memory_obj() {
40 while (_destroy_notify.size()) {
41 _destroy_notify.top()();
42 _destroy_notify.pop();
43 }
44 }
45
46 bool
operator ==(const memory_obj & obj) const47 memory_obj::operator==(const memory_obj &obj) const {
48 return this == &obj;
49 }
50
51 void
destroy_notify(std::function<void ()> f)52 memory_obj::destroy_notify(std::function<void ()> f) {
53 _destroy_notify.push(f);
54 }
55
56 std::vector<cl_mem_properties>
properties() const57 memory_obj::properties() const {
58 return _properties;
59 }
60
61 cl_mem_flags
flags() const62 memory_obj::flags() const {
63 return _flags;
64 }
65
66 size_t
size() const67 memory_obj::size() const {
68 return _size;
69 }
70
71 void *
host_ptr() const72 memory_obj::host_ptr() const {
73 return _host_ptr;
74 }
75
buffer(clover::context & ctx,std::vector<cl_mem_properties> properties,cl_mem_flags flags,size_t size,void * host_ptr)76 buffer::buffer(clover::context &ctx,
77 std::vector<cl_mem_properties> properties,
78 cl_mem_flags flags,
79 size_t size, void *host_ptr) :
80 memory_obj(ctx, properties, flags, size, host_ptr) {
81 }
82
83 cl_mem_object_type
type() const84 buffer::type() const {
85 return CL_MEM_OBJECT_BUFFER;
86 }
87
root_buffer(clover::context & ctx,std::vector<cl_mem_properties> properties,cl_mem_flags flags,size_t size,void * host_ptr)88 root_buffer::root_buffer(clover::context &ctx,
89 std::vector<cl_mem_properties> properties,
90 cl_mem_flags flags,
91 size_t size, void *host_ptr) :
92 buffer(ctx, properties, flags, size, host_ptr) {
93 }
94
95 resource &
resource_in(command_queue & q)96 root_buffer::resource_in(command_queue &q) {
97 const void *data_ptr = NULL;
98 if (flags() & (CL_MEM_USE_HOST_PTR | CL_MEM_COPY_HOST_PTR))
99 data_ptr = !data.empty() ? data.data() : host_ptr();
100
101 return resource(q, data_ptr);
102 }
103
104 resource &
resource_undef(command_queue & q)105 root_buffer::resource_undef(command_queue &q) {
106 return resource(q, NULL);
107 }
108
109 resource &
resource(command_queue & q,const void * data_ptr)110 root_buffer::resource(command_queue &q, const void *data_ptr) {
111 std::lock_guard<std::mutex> lock(resources_mtx);
112 // Create a new resource if there's none for this device yet.
113 if (!resources.count(&q.device())) {
114 auto r = (!resources.empty() ?
115 new root_resource(q.device(), *this,
116 *resources.begin()->second) :
117 new root_resource(q.device(), *this, q, data_ptr));
118
119 resources.insert(std::make_pair(&q.device(),
120 std::unique_ptr<root_resource>(r)));
121 data.clear();
122 }
123
124 return *resources.find(&q.device())->second;
125 }
126
127 void
resource_out(command_queue & q)128 root_buffer::resource_out(command_queue &q) {
129 std::lock_guard<std::mutex> lock(resources_mtx);
130 resources.erase(&q.device());
131 }
132
sub_buffer(root_buffer & parent,cl_mem_flags flags,size_t offset,size_t size)133 sub_buffer::sub_buffer(root_buffer &parent, cl_mem_flags flags,
134 size_t offset, size_t size) :
135 buffer(parent.context(), std::vector<cl_mem_properties>(), flags, size,
136 (char *)parent.host_ptr() + offset),
137 parent(parent), _offset(offset) {
138 }
139
140 resource &
resource_in(command_queue & q)141 sub_buffer::resource_in(command_queue &q) {
142 std::lock_guard<std::mutex> lock(resources_mtx);
143 // Create a new resource if there's none for this device yet.
144 if (!resources.count(&q.device())) {
145 auto r = new sub_resource(parent().resource_in(q), {{ offset() }});
146
147 resources.insert(std::make_pair(&q.device(),
148 std::unique_ptr<sub_resource>(r)));
149 }
150
151 return *resources.find(&q.device())->second;
152 }
153
154 resource &
resource_undef(command_queue & q)155 sub_buffer::resource_undef(command_queue &q) {
156 return resource_in(q);
157 }
158
159 void
resource_out(command_queue & q)160 sub_buffer::resource_out(command_queue &q) {
161 std::lock_guard<std::mutex> lock(resources_mtx);
162 resources.erase(&q.device());
163 }
164
165 size_t
offset() const166 sub_buffer::offset() const {
167 return _offset;
168 }
169
image(clover::context & ctx,std::vector<cl_mem_properties> properties,cl_mem_flags flags,const cl_image_format * format,size_t width,size_t height,size_t depth,size_t array_size,size_t row_pitch,size_t slice_pitch,size_t size,void * host_ptr,cl_mem buffer)170 image::image(clover::context &ctx,
171 std::vector<cl_mem_properties> properties,
172 cl_mem_flags flags,
173 const cl_image_format *format,
174 size_t width, size_t height, size_t depth, size_t array_size,
175 size_t row_pitch, size_t slice_pitch, size_t size,
176 void *host_ptr, cl_mem buffer) :
177 memory_obj(ctx, properties, flags, size, host_ptr),
178 _format(*format), _width(width), _height(height), _depth(depth),
179 _row_pitch(row_pitch), _slice_pitch(slice_pitch), _array_size(array_size),
180 _buffer(buffer) {
181 }
182
183 resource &
resource_in(command_queue & q)184 image::resource_in(command_queue &q) {
185 const void *data_ptr = !data.empty() ? data.data() : NULL;
186 return resource(q, data_ptr);
187 }
188
189 resource &
resource_undef(command_queue & q)190 image::resource_undef(command_queue &q) {
191 return resource(q, NULL);
192 }
193
194 resource &
resource(command_queue & q,const void * data_ptr)195 image::resource(command_queue &q, const void *data_ptr) {
196 std::lock_guard<std::mutex> lock(resources_mtx);
197 // Create a new resource if there's none for this device yet.
198 if (!resources.count(&q.device())) {
199 auto r = (!resources.empty() ?
200 new root_resource(q.device(), *this,
201 *resources.begin()->second) :
202 new root_resource(q.device(), *this, q, data_ptr));
203
204 resources.insert(std::make_pair(&q.device(),
205 std::unique_ptr<root_resource>(r)));
206 data.clear();
207 }
208
209 return *resources.find(&q.device())->second;
210 }
211
212 void
resource_out(command_queue & q)213 image::resource_out(command_queue &q) {
214 std::lock_guard<std::mutex> lock(resources_mtx);
215 resources.erase(&q.device());
216 }
217
218 cl_image_format
format() const219 image::format() const {
220 return _format;
221 }
222
223 size_t
width() const224 image::width() const {
225 return _width;
226 }
227
228 size_t
height() const229 image::height() const {
230 return _height;
231 }
232
233 size_t
depth() const234 image::depth() const {
235 return _depth;
236 }
237
238 size_t
pixel_size() const239 image::pixel_size() const {
240 return util_format_get_blocksize(translate_format(_format));
241 }
242
243 size_t
row_pitch() const244 image::row_pitch() const {
245 return _row_pitch;
246 }
247
248 size_t
slice_pitch() const249 image::slice_pitch() const {
250 return _slice_pitch;
251 }
252
253 size_t
array_size() const254 image::array_size() const {
255 return _array_size;
256 }
257
258 cl_mem
buffer() const259 image::buffer() const {
260 return _buffer;
261 }
262
image1d(clover::context & ctx,std::vector<cl_mem_properties> properties,cl_mem_flags flags,const cl_image_format * format,size_t width,size_t row_pitch,void * host_ptr)263 image1d::image1d(clover::context &ctx,
264 std::vector<cl_mem_properties> properties,
265 cl_mem_flags flags,
266 const cl_image_format *format,
267 size_t width, size_t row_pitch,
268 void *host_ptr) :
269 basic_image(ctx, properties, flags, format, width, 1, 1, 0,
270 row_pitch, 0, row_pitch, host_ptr, nullptr) {
271 }
272
image1d_buffer(clover::context & ctx,std::vector<cl_mem_properties> properties,cl_mem_flags flags,const cl_image_format * format,size_t width,size_t row_pitch,void * host_ptr,cl_mem buffer)273 image1d_buffer::image1d_buffer(clover::context &ctx,
274 std::vector<cl_mem_properties> properties,
275 cl_mem_flags flags,
276 const cl_image_format *format,
277 size_t width, size_t row_pitch,
278 void *host_ptr, cl_mem buffer) :
279 basic_image(ctx, properties, flags, format, width, 1, 1, 0,
280 row_pitch, 0, row_pitch, host_ptr, buffer) {
281 }
282
image1d_array(clover::context & ctx,std::vector<cl_mem_properties> properties,cl_mem_flags flags,const cl_image_format * format,size_t width,size_t array_size,size_t slice_pitch,void * host_ptr)283 image1d_array::image1d_array(clover::context &ctx,
284 std::vector<cl_mem_properties> properties,
285 cl_mem_flags flags,
286 const cl_image_format *format,
287 size_t width,
288 size_t array_size, size_t slice_pitch,
289 void *host_ptr) :
290 basic_image(ctx, properties, flags, format, width, 1, 1, array_size,
291 0, slice_pitch, slice_pitch * array_size, host_ptr, nullptr) {
292 }
293
image2d(clover::context & ctx,std::vector<cl_mem_properties> properties,cl_mem_flags flags,const cl_image_format * format,size_t width,size_t height,size_t row_pitch,void * host_ptr)294 image2d::image2d(clover::context &ctx,
295 std::vector<cl_mem_properties> properties,
296 cl_mem_flags flags,
297 const cl_image_format *format, size_t width,
298 size_t height, size_t row_pitch,
299 void *host_ptr) :
300 basic_image(ctx, properties, flags, format, width, height, 1, 0,
301 row_pitch, 0, height * row_pitch, host_ptr, nullptr) {
302 }
303
image2d_array(clover::context & ctx,std::vector<cl_mem_properties> properties,cl_mem_flags flags,const cl_image_format * format,size_t width,size_t height,size_t array_size,size_t row_pitch,size_t slice_pitch,void * host_ptr)304 image2d_array::image2d_array(clover::context &ctx,
305 std::vector<cl_mem_properties> properties,
306 cl_mem_flags flags,
307 const cl_image_format *format,
308 size_t width, size_t height, size_t array_size,
309 size_t row_pitch, size_t slice_pitch,
310 void *host_ptr) :
311 basic_image(ctx, properties, flags, format, width, height, 1, array_size,
312 row_pitch, slice_pitch, slice_pitch * array_size, host_ptr, nullptr) {
313 }
314
image3d(clover::context & ctx,std::vector<cl_mem_properties> properties,cl_mem_flags flags,const cl_image_format * format,size_t width,size_t height,size_t depth,size_t row_pitch,size_t slice_pitch,void * host_ptr)315 image3d::image3d(clover::context &ctx,
316 std::vector<cl_mem_properties> properties,
317 cl_mem_flags flags,
318 const cl_image_format *format,
319 size_t width, size_t height, size_t depth,
320 size_t row_pitch, size_t slice_pitch,
321 void *host_ptr) :
322 basic_image(ctx, properties, flags, format, width, height, depth, 0,
323 row_pitch, slice_pitch, depth * slice_pitch,
324 host_ptr, nullptr) {
325 }
326