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 "util/factor.hpp"
26 #include "util/u_math.h"
27 #include "pipe/p_context.h"
29 using namespace clover
;
31 kernel::kernel(clover::program
&prog
, const std::string
&name
,
32 const std::vector
<module::argument
> &margs
) :
33 program(prog
), _name(name
), exec(*this),
34 program_ref(prog
._kernel_ref_counter
) {
35 for (auto &marg
: margs
) {
36 if (marg
.semantic
== module::argument::general
)
37 _args
.emplace_back(argument::create(marg
));
42 static inline std::vector
<uint
>
43 pad_vector(command_queue
&q
, const V
&v
, uint x
) {
44 std::vector
<uint
> w
{ v
.begin(), v
.end() };
45 w
.resize(q
.device().max_block_size().size(), x
);
50 kernel::launch(command_queue
&q
,
51 const std::vector
<size_t> &grid_offset
,
52 const std::vector
<size_t> &grid_size
,
53 const std::vector
<size_t> &block_size
) {
54 const auto m
= program().binary(q
.device());
55 const auto reduced_grid_size
=
56 map(divides(), grid_size
, block_size
);
57 void *st
= exec
.bind(&q
, grid_offset
);
58 struct pipe_grid_info info
;
60 // The handles are created during exec_context::bind(), so we need make
61 // sure to call exec_context::bind() before retrieving them.
62 std::vector
<uint32_t *> g_handles
= map([&](size_t h
) {
63 return (uint32_t *)&exec
.input
[h
];
66 q
.pipe
->bind_compute_state(q
.pipe
, st
);
67 q
.pipe
->bind_sampler_states(q
.pipe
, PIPE_SHADER_COMPUTE
,
68 0, exec
.samplers
.size(),
69 exec
.samplers
.data());
71 q
.pipe
->set_sampler_views(q
.pipe
, PIPE_SHADER_COMPUTE
, 0,
72 exec
.sviews
.size(), exec
.sviews
.data());
73 q
.pipe
->set_compute_resources(q
.pipe
, 0, exec
.resources
.size(),
74 exec
.resources
.data());
75 q
.pipe
->set_global_binding(q
.pipe
, 0, exec
.g_buffers
.size(),
76 exec
.g_buffers
.data(), g_handles
.data());
78 // Fill information for the launch_grid() call.
79 copy(pad_vector(q
, block_size
, 1), info
.block
);
80 copy(pad_vector(q
, reduced_grid_size
, 1), info
.grid
);
81 info
.pc
= find(name_equals(_name
), m
.syms
).offset
;
82 info
.input
= exec
.input
.data();
84 q
.pipe
->launch_grid(q
.pipe
, &info
);
86 q
.pipe
->set_global_binding(q
.pipe
, 0, exec
.g_buffers
.size(), NULL
, NULL
);
87 q
.pipe
->set_compute_resources(q
.pipe
, 0, exec
.resources
.size(), NULL
);
88 q
.pipe
->set_sampler_views(q
.pipe
, PIPE_SHADER_COMPUTE
, 0,
89 exec
.sviews
.size(), NULL
);
90 q
.pipe
->bind_sampler_states(q
.pipe
, PIPE_SHADER_COMPUTE
, 0,
91 exec
.samplers
.size(), NULL
);
96 kernel::mem_local() const {
99 for (auto &arg
: args()) {
100 if (dynamic_cast<local_argument
*>(&arg
))
108 kernel::mem_private() const {
113 kernel::name() const {
118 kernel::optimal_block_size(const command_queue
&q
,
119 const std::vector
<size_t> &grid_size
) const {
120 return factor::find_grid_optimal_factor
<size_t>(
121 q
.device().max_threads_per_block(), q
.device().max_block_size(),
126 kernel::required_block_size() const {
130 kernel::argument_range
132 return map(derefs(), _args
);
135 kernel::const_argument_range
136 kernel::args() const {
137 return map(derefs(), _args
);
141 kernel::module(const command_queue
&q
) const {
142 return program().binary(q
.device());
145 kernel::exec_context::exec_context(kernel
&kern
) :
146 kern(kern
), q(NULL
), mem_local(0), st(NULL
), cs() {
149 kernel::exec_context::~exec_context() {
151 q
->pipe
->delete_compute_state(q
->pipe
, st
);
155 kernel::exec_context::bind(intrusive_ptr
<command_queue
> _q
,
156 const std::vector
<size_t> &grid_offset
) {
159 // Bind kernel arguments.
160 auto &m
= kern
.program().binary(q
->device());
161 auto margs
= find(name_equals(kern
.name()), m
.syms
).args
;
162 auto msec
= find(type_equals(module::section::text
), m
.secs
);
163 auto explicit_arg
= kern
._args
.begin();
165 for (auto &marg
: margs
) {
166 switch (marg
.semantic
) {
167 case module::argument::general
:
168 (*(explicit_arg
++))->bind(*this, marg
);
171 case module::argument::grid_dimension
: {
172 const cl_uint dimension
= grid_offset
.size();
173 auto arg
= argument::create(marg
);
175 arg
->set(sizeof(dimension
), &dimension
);
176 arg
->bind(*this, marg
);
179 case module::argument::grid_offset
: {
180 for (cl_uint x
: pad_vector(*q
, grid_offset
, 1)) {
181 auto arg
= argument::create(marg
);
183 arg
->set(sizeof(x
), &x
);
184 arg
->bind(*this, marg
);
188 case module::argument::image_size
: {
189 auto img
= dynamic_cast<image_argument
&>(**(explicit_arg
- 1)).get();
190 std::vector
<cl_uint
> image_size
{
191 static_cast<cl_uint
>(img
->width()),
192 static_cast<cl_uint
>(img
->height()),
193 static_cast<cl_uint
>(img
->depth())};
194 for (auto x
: image_size
) {
195 auto arg
= argument::create(marg
);
197 arg
->set(sizeof(x
), &x
);
198 arg
->bind(*this, marg
);
202 case module::argument::image_format
: {
203 auto img
= dynamic_cast<image_argument
&>(**(explicit_arg
- 1)).get();
204 cl_image_format fmt
= img
->format();
205 std::vector
<cl_uint
> image_format
{
206 static_cast<cl_uint
>(fmt
.image_channel_data_type
),
207 static_cast<cl_uint
>(fmt
.image_channel_order
)};
208 for (auto x
: image_format
) {
209 auto arg
= argument::create(marg
);
211 arg
->set(sizeof(x
), &x
);
212 arg
->bind(*this, marg
);
219 // Create a new compute state if anything changed.
220 if (!st
|| q
!= _q
||
221 cs
.req_local_mem
!= mem_local
||
222 cs
.req_input_mem
!= input
.size()) {
224 _q
->pipe
->delete_compute_state(_q
->pipe
, st
);
226 cs
.prog
= &(msec
.data
[0]);
227 cs
.req_local_mem
= mem_local
;
228 cs
.req_input_mem
= input
.size();
229 st
= q
->pipe
->create_compute_state(q
->pipe
, &cs
);
236 kernel::exec_context::unbind() {
237 for (auto &arg
: kern
.args())
253 return { (uint8_t *)&x
, (uint8_t *)&x
+ sizeof(x
) };
257 /// Transform buffer \a v from the native byte order into the byte
258 /// order specified by \a e.
262 byteswap(T
&v
, pipe_endian e
) {
263 if (PIPE_ENDIAN_NATIVE
!= e
)
264 std::reverse(v
.begin(), v
.end());
268 /// Pad buffer \a v to the next multiple of \a n.
272 align(T
&v
, size_t n
) {
273 v
.resize(util_align_npot(v
.size(), n
));
277 msb(const std::vector
<uint8_t> &s
) {
278 if (PIPE_ENDIAN_NATIVE
== PIPE_ENDIAN_LITTLE
)
279 return s
.back() & 0x80;
281 return s
.front() & 0x80;
285 /// Resize buffer \a v to size \a n using sign or zero extension
286 /// according to \a ext.
290 extend(T
&v
, enum module::argument::ext_type ext
, size_t n
) {
291 const size_t m
= std::min(v
.size(), n
);
292 const bool sign_ext
= (ext
== module::argument::sign_ext
);
293 const uint8_t fill
= (sign_ext
&& msb(v
) ? ~0 : 0);
296 if (PIPE_ENDIAN_NATIVE
== PIPE_ENDIAN_LITTLE
)
297 std::copy_n(v
.begin(), m
, w
.begin());
299 std::copy_n(v
.end() - m
, m
, w
.end() - m
);
305 /// Append buffer \a w to \a v.
309 insert(T
&v
, const T
&w
) {
310 v
.insert(v
.end(), w
.begin(), w
.end());
314 /// Append \a n elements to the end of buffer \a v.
318 allocate(T
&v
, size_t n
) {
319 size_t pos
= v
.size();
325 std::unique_ptr
<kernel::argument
>
326 kernel::argument::create(const module::argument
&marg
) {
328 case module::argument::scalar
:
329 return std::unique_ptr
<kernel::argument
>(new scalar_argument(marg
.size
));
331 case module::argument::global
:
332 return std::unique_ptr
<kernel::argument
>(new global_argument
);
334 case module::argument::local
:
335 return std::unique_ptr
<kernel::argument
>(new local_argument
);
337 case module::argument::constant
:
338 return std::unique_ptr
<kernel::argument
>(new constant_argument
);
340 case module::argument::image2d_rd
:
341 case module::argument::image3d_rd
:
342 return std::unique_ptr
<kernel::argument
>(new image_rd_argument
);
344 case module::argument::image2d_wr
:
345 case module::argument::image3d_wr
:
346 return std::unique_ptr
<kernel::argument
>(new image_wr_argument
);
348 case module::argument::sampler
:
349 return std::unique_ptr
<kernel::argument
>(new sampler_argument
);
352 throw error(CL_INVALID_KERNEL_DEFINITION
);
355 kernel::argument::argument() : _set(false) {
359 kernel::argument::set() const {
364 kernel::argument::storage() const {
368 kernel::scalar_argument::scalar_argument(size_t size
) : size(size
) {
372 kernel::scalar_argument::set(size_t size
, const void *value
) {
374 throw error(CL_INVALID_ARG_VALUE
);
376 if (size
!= this->size
)
377 throw error(CL_INVALID_ARG_SIZE
);
379 v
= { (uint8_t *)value
, (uint8_t *)value
+ size
};
384 kernel::scalar_argument::bind(exec_context
&ctx
,
385 const module::argument
&marg
) {
388 extend(w
, marg
.ext_type
, marg
.target_size
);
389 byteswap(w
, ctx
.q
->device().endianness());
390 align(ctx
.input
, marg
.target_align
);
391 insert(ctx
.input
, w
);
395 kernel::scalar_argument::unbind(exec_context
&ctx
) {
399 kernel::global_argument::set(size_t size
, const void *value
) {
400 if (size
!= sizeof(cl_mem
))
401 throw error(CL_INVALID_ARG_SIZE
);
403 buf
= pobj
<buffer
>(value
? *(cl_mem
*)value
: NULL
);
408 kernel::global_argument::bind(exec_context
&ctx
,
409 const module::argument
&marg
) {
410 align(ctx
.input
, marg
.target_align
);
413 const resource
&r
= buf
->resource(*ctx
.q
);
414 ctx
.g_handles
.push_back(ctx
.input
.size());
415 ctx
.g_buffers
.push_back(r
.pipe
);
417 // How to handle multi-demensional offsets?
418 // We don't need to. Buffer offsets are always
420 auto v
= bytes(r
.offset
[0]);
421 extend(v
, marg
.ext_type
, marg
.target_size
);
422 byteswap(v
, ctx
.q
->device().endianness());
423 insert(ctx
.input
, v
);
426 allocate(ctx
.input
, marg
.target_size
);
431 kernel::global_argument::unbind(exec_context
&ctx
) {
435 kernel::local_argument::storage() const {
440 kernel::local_argument::set(size_t size
, const void *value
) {
442 throw error(CL_INVALID_ARG_VALUE
);
445 throw error(CL_INVALID_ARG_SIZE
);
452 kernel::local_argument::bind(exec_context
&ctx
,
453 const module::argument
&marg
) {
454 auto v
= bytes(ctx
.mem_local
);
456 extend(v
, module::argument::zero_ext
, marg
.target_size
);
457 byteswap(v
, ctx
.q
->device().endianness());
458 align(ctx
.input
, marg
.target_align
);
459 insert(ctx
.input
, v
);
461 ctx
.mem_local
+= _storage
;
465 kernel::local_argument::unbind(exec_context
&ctx
) {
469 kernel::constant_argument::set(size_t size
, const void *value
) {
470 if (size
!= sizeof(cl_mem
))
471 throw error(CL_INVALID_ARG_SIZE
);
473 buf
= pobj
<buffer
>(value
? *(cl_mem
*)value
: NULL
);
478 kernel::constant_argument::bind(exec_context
&ctx
,
479 const module::argument
&marg
) {
480 align(ctx
.input
, marg
.target_align
);
483 resource
&r
= buf
->resource(*ctx
.q
);
484 auto v
= bytes(ctx
.resources
.size() << 24 | r
.offset
[0]);
486 extend(v
, module::argument::zero_ext
, marg
.target_size
);
487 byteswap(v
, ctx
.q
->device().endianness());
488 insert(ctx
.input
, v
);
490 st
= r
.bind_surface(*ctx
.q
, false);
491 ctx
.resources
.push_back(st
);
494 allocate(ctx
.input
, marg
.target_size
);
499 kernel::constant_argument::unbind(exec_context
&ctx
) {
501 buf
->resource(*ctx
.q
).unbind_surface(*ctx
.q
, st
);
505 kernel::image_rd_argument::set(size_t size
, const void *value
) {
507 throw error(CL_INVALID_ARG_VALUE
);
509 if (size
!= sizeof(cl_mem
))
510 throw error(CL_INVALID_ARG_SIZE
);
512 img
= &obj
<image
>(*(cl_mem
*)value
);
517 kernel::image_rd_argument::bind(exec_context
&ctx
,
518 const module::argument
&marg
) {
519 auto v
= bytes(ctx
.sviews
.size());
521 extend(v
, module::argument::zero_ext
, marg
.target_size
);
522 byteswap(v
, ctx
.q
->device().endianness());
523 align(ctx
.input
, marg
.target_align
);
524 insert(ctx
.input
, v
);
526 st
= img
->resource(*ctx
.q
).bind_sampler_view(*ctx
.q
);
527 ctx
.sviews
.push_back(st
);
531 kernel::image_rd_argument::unbind(exec_context
&ctx
) {
532 img
->resource(*ctx
.q
).unbind_sampler_view(*ctx
.q
, st
);
536 kernel::image_wr_argument::set(size_t size
, const void *value
) {
538 throw error(CL_INVALID_ARG_VALUE
);
540 if (size
!= sizeof(cl_mem
))
541 throw error(CL_INVALID_ARG_SIZE
);
543 img
= &obj
<image
>(*(cl_mem
*)value
);
548 kernel::image_wr_argument::bind(exec_context
&ctx
,
549 const module::argument
&marg
) {
550 auto v
= bytes(ctx
.resources
.size());
552 extend(v
, module::argument::zero_ext
, marg
.target_size
);
553 byteswap(v
, ctx
.q
->device().endianness());
554 align(ctx
.input
, marg
.target_align
);
555 insert(ctx
.input
, v
);
557 st
= img
->resource(*ctx
.q
).bind_surface(*ctx
.q
, true);
558 ctx
.resources
.push_back(st
);
562 kernel::image_wr_argument::unbind(exec_context
&ctx
) {
563 img
->resource(*ctx
.q
).unbind_surface(*ctx
.q
, st
);
567 kernel::sampler_argument::set(size_t size
, const void *value
) {
569 throw error(CL_INVALID_SAMPLER
);
571 if (size
!= sizeof(cl_sampler
))
572 throw error(CL_INVALID_ARG_SIZE
);
574 s
= &obj(*(cl_sampler
*)value
);
579 kernel::sampler_argument::bind(exec_context
&ctx
,
580 const module::argument
&marg
) {
581 st
= s
->bind(*ctx
.q
);
582 ctx
.samplers
.push_back(st
);
586 kernel::sampler_argument::unbind(exec_context
&ctx
) {
587 s
->unbind(*ctx
.q
, st
);