2 // Copyright 2012 Francisco Jerez
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:
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
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.
23 #include "core/kernel.hpp"
24 #include "core/resource.hpp"
25 #include "pipe/p_context.h"
27 using namespace clover
;
29 _cl_kernel::_cl_kernel(clover::program
&prog
,
30 const std::string
&name
,
31 const std::vector
<clover::module::argument
> &args
) :
32 prog(prog
), __name(name
), exec(*this) {
33 for (auto arg
: args
) {
34 if (arg
.type
== module::argument::scalar
)
35 this->args
.emplace_back(new scalar_argument(arg
.size
));
36 else if (arg
.type
== module::argument::global
)
37 this->args
.emplace_back(new global_argument(arg
.size
));
38 else if (arg
.type
== module::argument::local
)
39 this->args
.emplace_back(new local_argument());
40 else if (arg
.type
== module::argument::constant
)
41 this->args
.emplace_back(new constant_argument());
42 else if (arg
.type
== module::argument::image2d_rd
||
43 arg
.type
== module::argument::image3d_rd
)
44 this->args
.emplace_back(new image_rd_argument());
45 else if (arg
.type
== module::argument::image2d_wr
||
46 arg
.type
== module::argument::image3d_wr
)
47 this->args
.emplace_back(new image_wr_argument());
48 else if (arg
.type
== module::argument::sampler
)
49 this->args
.emplace_back(new sampler_argument());
51 throw error(CL_INVALID_KERNEL_DEFINITION
);
55 template<typename T
, typename V
>
56 static inline std::vector
<T
>
57 pad_vector(clover::command_queue
&q
, const V
&v
, T x
) {
58 std::vector
<T
> w
{ v
.begin(), v
.end() };
59 w
.resize(q
.dev
.max_block_size().size(), x
);
64 _cl_kernel::launch(clover::command_queue
&q
,
65 const std::vector
<size_t> &grid_offset
,
66 const std::vector
<size_t> &grid_size
,
67 const std::vector
<size_t> &block_size
) {
68 void *st
= exec
.bind(&q
);
69 auto g_handles
= map([&](size_t h
) { return (uint32_t *)&exec
.input
[h
]; },
70 exec
.g_handles
.begin(), exec
.g_handles
.end());
72 q
.pipe
->bind_compute_state(q
.pipe
, st
);
73 q
.pipe
->bind_compute_sampler_states(q
.pipe
, 0, exec
.samplers
.size(),
74 exec
.samplers
.data());
75 q
.pipe
->set_compute_sampler_views(q
.pipe
, 0, exec
.sviews
.size(),
77 q
.pipe
->set_compute_resources(q
.pipe
, 0, exec
.resources
.size(),
78 exec
.resources
.data());
79 q
.pipe
->set_global_binding(q
.pipe
, 0, exec
.g_buffers
.size(),
80 exec
.g_buffers
.data(), g_handles
.data());
82 q
.pipe
->launch_grid(q
.pipe
,
83 pad_vector
<uint
>(q
, block_size
, 1).data(),
84 pad_vector
<uint
>(q
, grid_size
, 1).data(),
85 module(q
).sym(__name
).offset
,
88 q
.pipe
->set_global_binding(q
.pipe
, 0, exec
.g_buffers
.size(), NULL
, NULL
);
89 q
.pipe
->set_compute_resources(q
.pipe
, 0, exec
.resources
.size(), NULL
);
90 q
.pipe
->set_compute_sampler_views(q
.pipe
, 0, exec
.sviews
.size(), NULL
);
91 q
.pipe
->bind_compute_sampler_states(q
.pipe
, 0, exec
.samplers
.size(), NULL
);
96 _cl_kernel::mem_local() const {
99 for (auto &arg
: args
) {
100 if (dynamic_cast<local_argument
*>(arg
.get()))
101 sz
+= arg
->storage();
108 _cl_kernel::mem_private() const {
113 _cl_kernel::max_block_size() const {
118 _cl_kernel::name() const {
123 _cl_kernel::block_size() const {
127 const clover::module
&
128 _cl_kernel::module(const clover::command_queue
&q
) const {
129 return prog
.binaries().find(&q
.dev
)->second
;
133 _cl_kernel::exec_context::exec_context(clover::kernel
&kern
) :
134 kern(kern
), q(NULL
), mem_local(0), st(NULL
) {
137 _cl_kernel::exec_context::~exec_context() {
139 q
->pipe
->delete_compute_state(q
->pipe
, st
);
143 _cl_kernel::exec_context::bind(clover::command_queue
*__q
) {
146 for (auto &arg
: kern
.args
)
149 // Create a new compute state if anything changed.
150 if (!st
|| q
!= __q
||
151 cs
.req_local_mem
!= mem_local
||
152 cs
.req_input_mem
!= input
.size()) {
154 __q
->pipe
->delete_compute_state(__q
->pipe
, st
);
156 cs
.prog
= kern
.module(*q
).sec(module::section::text
).data
.begin();
157 cs
.req_local_mem
= mem_local
;
158 cs
.req_input_mem
= input
.size();
159 st
= q
->pipe
->create_compute_state(q
->pipe
, &cs
);
166 _cl_kernel::exec_context::unbind() {
167 for (auto &arg
: kern
.args
)
179 _cl_kernel::argument::argument(size_t size
) :
180 __size(size
), __set(false) {
184 _cl_kernel::argument::set() const {
189 _cl_kernel::argument::storage() const {
193 _cl_kernel::scalar_argument::scalar_argument(size_t size
) :
198 _cl_kernel::scalar_argument::set(size_t size
, const void *value
) {
200 throw error(CL_INVALID_ARG_SIZE
);
202 v
= { (uint8_t *)value
, (uint8_t *)value
+ size
};
207 _cl_kernel::scalar_argument::bind(exec_context
&ctx
) {
208 ctx
.input
.insert(ctx
.input
.end(), v
.begin(), v
.end());
212 _cl_kernel::scalar_argument::unbind(exec_context
&ctx
) {
215 _cl_kernel::global_argument::global_argument(size_t size
) :
220 _cl_kernel::global_argument::set(size_t size
, const void *value
) {
221 if (size
!= sizeof(cl_mem
))
222 throw error(CL_INVALID_ARG_SIZE
);
224 obj
= dynamic_cast<clover::buffer
*>(*(cl_mem
*)value
);
226 throw error(CL_INVALID_MEM_OBJECT
);
232 _cl_kernel::global_argument::bind(exec_context
&ctx
) {
233 size_t offset
= ctx
.input
.size();
234 size_t idx
= ctx
.g_buffers
.size();
236 ctx
.input
.resize(offset
+ __size
);
238 ctx
.g_buffers
.resize(idx
+ 1);
239 ctx
.g_buffers
[idx
] = obj
->resource(ctx
.q
).pipe
;
241 ctx
.g_handles
.resize(idx
+ 1);
242 ctx
.g_handles
[idx
] = offset
;
246 _cl_kernel::global_argument::unbind(exec_context
&ctx
) {
249 _cl_kernel::local_argument::local_argument() :
250 argument(sizeof(uint32_t)) {
254 _cl_kernel::local_argument::storage() const {
259 _cl_kernel::local_argument::set(size_t size
, const void *value
) {
261 throw error(CL_INVALID_ARG_VALUE
);
268 _cl_kernel::local_argument::bind(exec_context
&ctx
) {
269 size_t offset
= ctx
.input
.size();
270 size_t ptr
= ctx
.mem_local
;
272 ctx
.input
.resize(offset
+ sizeof(uint32_t));
273 *(uint32_t *)&ctx
.input
[offset
] = ptr
;
275 ctx
.mem_local
+= __storage
;
279 _cl_kernel::local_argument::unbind(exec_context
&ctx
) {
282 _cl_kernel::constant_argument::constant_argument() :
283 argument(sizeof(uint32_t)) {
287 _cl_kernel::constant_argument::set(size_t size
, const void *value
) {
288 if (size
!= sizeof(cl_mem
))
289 throw error(CL_INVALID_ARG_SIZE
);
291 obj
= dynamic_cast<clover::buffer
*>(*(cl_mem
*)value
);
293 throw error(CL_INVALID_MEM_OBJECT
);
299 _cl_kernel::constant_argument::bind(exec_context
&ctx
) {
300 size_t offset
= ctx
.input
.size();
301 size_t idx
= ctx
.resources
.size();
303 ctx
.input
.resize(offset
+ sizeof(uint32_t));
304 *(uint32_t *)&ctx
.input
[offset
] = idx
<< 24;
306 ctx
.resources
.resize(idx
+ 1);
307 ctx
.resources
[idx
] = st
= obj
->resource(ctx
.q
).bind_surface(*ctx
.q
, false);
311 _cl_kernel::constant_argument::unbind(exec_context
&ctx
) {
312 obj
->resource(ctx
.q
).unbind_surface(*ctx
.q
, st
);
315 _cl_kernel::image_rd_argument::image_rd_argument() :
316 argument(sizeof(uint32_t)) {
320 _cl_kernel::image_rd_argument::set(size_t size
, const void *value
) {
321 if (size
!= sizeof(cl_mem
))
322 throw error(CL_INVALID_ARG_SIZE
);
324 obj
= dynamic_cast<clover::image
*>(*(cl_mem
*)value
);
326 throw error(CL_INVALID_MEM_OBJECT
);
332 _cl_kernel::image_rd_argument::bind(exec_context
&ctx
) {
333 size_t offset
= ctx
.input
.size();
334 size_t idx
= ctx
.sviews
.size();
336 ctx
.input
.resize(offset
+ sizeof(uint32_t));
337 *(uint32_t *)&ctx
.input
[offset
] = idx
;
339 ctx
.sviews
.resize(idx
+ 1);
340 ctx
.sviews
[idx
] = st
= obj
->resource(ctx
.q
).bind_sampler_view(*ctx
.q
);
344 _cl_kernel::image_rd_argument::unbind(exec_context
&ctx
) {
345 obj
->resource(ctx
.q
).unbind_sampler_view(*ctx
.q
, st
);
348 _cl_kernel::image_wr_argument::image_wr_argument() :
349 argument(sizeof(uint32_t)) {
353 _cl_kernel::image_wr_argument::set(size_t size
, const void *value
) {
354 if (size
!= sizeof(cl_mem
))
355 throw error(CL_INVALID_ARG_SIZE
);
357 obj
= dynamic_cast<clover::image
*>(*(cl_mem
*)value
);
359 throw error(CL_INVALID_MEM_OBJECT
);
365 _cl_kernel::image_wr_argument::bind(exec_context
&ctx
) {
366 size_t offset
= ctx
.input
.size();
367 size_t idx
= ctx
.resources
.size();
369 ctx
.input
.resize(offset
+ sizeof(uint32_t));
370 *(uint32_t *)&ctx
.input
[offset
] = idx
;
372 ctx
.resources
.resize(idx
+ 1);
373 ctx
.resources
[idx
] = st
= obj
->resource(ctx
.q
).bind_surface(*ctx
.q
, true);
377 _cl_kernel::image_wr_argument::unbind(exec_context
&ctx
) {
378 obj
->resource(ctx
.q
).unbind_surface(*ctx
.q
, st
);
381 _cl_kernel::sampler_argument::sampler_argument() :
386 _cl_kernel::sampler_argument::set(size_t size
, const void *value
) {
387 if (size
!= sizeof(cl_sampler
))
388 throw error(CL_INVALID_ARG_SIZE
);
390 obj
= *(cl_sampler
*)value
;
395 _cl_kernel::sampler_argument::bind(exec_context
&ctx
) {
396 size_t idx
= ctx
.samplers
.size();
398 ctx
.samplers
.resize(idx
+ 1);
399 ctx
.samplers
[idx
] = st
= obj
->bind(*ctx
.q
);
403 _cl_kernel::sampler_argument::unbind(exec_context
&ctx
) {
404 obj
->unbind(*ctx
.q
, st
);