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 row_pitch,size_t slice_pitch,size_t size,void * host_ptr)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,
175 size_t row_pitch, size_t slice_pitch, size_t size,
176 void *host_ptr) :
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) {
180 }
181
182 resource &
resource_in(command_queue & q)183 image::resource_in(command_queue &q) {
184 const void *data_ptr = !data.empty() ? data.data() : NULL;
185 return resource(q, data_ptr);
186 }
187
188 resource &
resource_undef(command_queue & q)189 image::resource_undef(command_queue &q) {
190 return resource(q, NULL);
191 }
192
193 resource &
resource(command_queue & q,const void * data_ptr)194 image::resource(command_queue &q, const void *data_ptr) {
195 std::lock_guard<std::mutex> lock(resources_mtx);
196 // Create a new resource if there's none for this device yet.
197 if (!resources.count(&q.device())) {
198 auto r = (!resources.empty() ?
199 new root_resource(q.device(), *this,
200 *resources.begin()->second) :
201 new root_resource(q.device(), *this, q, data_ptr));
202
203 resources.insert(std::make_pair(&q.device(),
204 std::unique_ptr<root_resource>(r)));
205 data.clear();
206 }
207
208 return *resources.find(&q.device())->second;
209 }
210
211 void
resource_out(command_queue & q)212 image::resource_out(command_queue &q) {
213 std::lock_guard<std::mutex> lock(resources_mtx);
214 resources.erase(&q.device());
215 }
216
217 cl_image_format
format() const218 image::format() const {
219 return _format;
220 }
221
222 size_t
width() const223 image::width() const {
224 return _width;
225 }
226
227 size_t
height() const228 image::height() const {
229 return _height;
230 }
231
232 size_t
depth() const233 image::depth() const {
234 return _depth;
235 }
236
237 size_t
pixel_size() const238 image::pixel_size() const {
239 return util_format_get_blocksize(translate_format(_format));
240 }
241
242 size_t
row_pitch() const243 image::row_pitch() const {
244 return _row_pitch;
245 }
246
247 size_t
slice_pitch() const248 image::slice_pitch() const {
249 return _slice_pitch;
250 }
251
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)252 image1d::image1d(clover::context &ctx,
253 std::vector<cl_mem_properties> properties,
254 cl_mem_flags flags,
255 const cl_image_format *format,
256 size_t width, size_t row_pitch,
257 void *host_ptr) :
258 image(ctx, properties, flags, format, width, 1, 1,
259 row_pitch, 0, row_pitch, host_ptr) {
260 }
261
262 cl_mem_object_type
type() const263 image1d::type() const {
264 return CL_MEM_OBJECT_IMAGE1D;
265 }
266
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)267 image2d::image2d(clover::context &ctx,
268 std::vector<cl_mem_properties> properties,
269 cl_mem_flags flags,
270 const cl_image_format *format, size_t width,
271 size_t height, size_t row_pitch,
272 void *host_ptr) :
273 image(ctx, properties, flags, format, width, height, 1,
274 row_pitch, 0, height * row_pitch, host_ptr) {
275 }
276
277 cl_mem_object_type
type() const278 image2d::type() const {
279 return CL_MEM_OBJECT_IMAGE2D;
280 }
281
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)282 image3d::image3d(clover::context &ctx,
283 std::vector<cl_mem_properties> properties,
284 cl_mem_flags flags,
285 const cl_image_format *format,
286 size_t width, size_t height, size_t depth,
287 size_t row_pitch, size_t slice_pitch,
288 void *host_ptr) :
289 image(ctx, properties, flags, format, width, height, depth,
290 row_pitch, slice_pitch, depth * slice_pitch,
291 host_ptr) {
292 }
293
294 cl_mem_object_type
type() const295 image3d::type() const {
296 return CL_MEM_OBJECT_IMAGE3D;
297 }
298