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
);
93 q
.pipe
->memory_barrier(q
.pipe
, PIPE_BARRIER_GLOBAL_BUFFER
);
98 kernel::mem_local() const {
101 for (auto &arg
: args()) {
102 if (dynamic_cast<local_argument
*>(&arg
))
110 kernel::mem_private() const {
115 kernel::name() const {
120 kernel::optimal_block_size(const command_queue
&q
,
121 const std::vector
<size_t> &grid_size
) const {
122 return factor::find_grid_optimal_factor
<size_t>(
123 q
.device().max_threads_per_block(), q
.device().max_block_size(),
128 kernel::required_block_size() const {
132 kernel::argument_range
134 return map(derefs(), _args
);
137 kernel::const_argument_range
138 kernel::args() const {
139 return map(derefs(), _args
);
143 kernel::module(const command_queue
&q
) const {
144 return program().binary(q
.device());
147 kernel::exec_context::exec_context(kernel
&kern
) :
148 kern(kern
), q(NULL
), mem_local(0), st(NULL
), cs() {
151 kernel::exec_context::~exec_context() {
153 q
->pipe
->delete_compute_state(q
->pipe
, st
);
157 kernel::exec_context::bind(intrusive_ptr
<command_queue
> _q
,
158 const std::vector
<size_t> &grid_offset
) {
161 // Bind kernel arguments.
162 auto &m
= kern
.program().binary(q
->device());
163 auto margs
= find(name_equals(kern
.name()), m
.syms
).args
;
164 auto msec
= find(type_equals(module::section::text
), m
.secs
);
165 auto explicit_arg
= kern
._args
.begin();
167 for (auto &marg
: margs
) {
168 switch (marg
.semantic
) {
169 case module::argument::general
:
170 (*(explicit_arg
++))->bind(*this, marg
);
173 case module::argument::grid_dimension
: {
174 const cl_uint dimension
= grid_offset
.size();
175 auto arg
= argument::create(marg
);
177 arg
->set(sizeof(dimension
), &dimension
);
178 arg
->bind(*this, marg
);
181 case module::argument::grid_offset
: {
182 for (cl_uint x
: pad_vector(*q
, grid_offset
, 0)) {
183 auto arg
= argument::create(marg
);
185 arg
->set(sizeof(x
), &x
);
186 arg
->bind(*this, marg
);
190 case module::argument::image_size
: {
191 auto img
= dynamic_cast<image_argument
&>(**(explicit_arg
- 1)).get();
192 std::vector
<cl_uint
> image_size
{
193 static_cast<cl_uint
>(img
->width()),
194 static_cast<cl_uint
>(img
->height()),
195 static_cast<cl_uint
>(img
->depth())};
196 for (auto x
: image_size
) {
197 auto arg
= argument::create(marg
);
199 arg
->set(sizeof(x
), &x
);
200 arg
->bind(*this, marg
);
204 case module::argument::image_format
: {
205 auto img
= dynamic_cast<image_argument
&>(**(explicit_arg
- 1)).get();
206 cl_image_format fmt
= img
->format();
207 std::vector
<cl_uint
> image_format
{
208 static_cast<cl_uint
>(fmt
.image_channel_data_type
),
209 static_cast<cl_uint
>(fmt
.image_channel_order
)};
210 for (auto x
: image_format
) {
211 auto arg
= argument::create(marg
);
213 arg
->set(sizeof(x
), &x
);
214 arg
->bind(*this, marg
);
221 // Create a new compute state if anything changed.
222 if (!st
|| q
!= _q
||
223 cs
.req_local_mem
!= mem_local
||
224 cs
.req_input_mem
!= input
.size()) {
226 _q
->pipe
->delete_compute_state(_q
->pipe
, st
);
228 cs
.ir_type
= q
->device().ir_format();
229 cs
.prog
= &(msec
.data
[0]);
230 cs
.req_local_mem
= mem_local
;
231 cs
.req_input_mem
= input
.size();
232 st
= q
->pipe
->create_compute_state(q
->pipe
, &cs
);
239 kernel::exec_context::unbind() {
240 for (auto &arg
: kern
.args())
256 return { (uint8_t *)&x
, (uint8_t *)&x
+ sizeof(x
) };
260 /// Transform buffer \a v from the native byte order into the byte
261 /// order specified by \a e.
265 byteswap(T
&v
, pipe_endian e
) {
266 if (PIPE_ENDIAN_NATIVE
!= e
)
267 std::reverse(v
.begin(), v
.end());
271 /// Pad buffer \a v to the next multiple of \a n.
275 align(T
&v
, size_t n
) {
276 v
.resize(util_align_npot(v
.size(), n
));
280 msb(const std::vector
<uint8_t> &s
) {
281 if (PIPE_ENDIAN_NATIVE
== PIPE_ENDIAN_LITTLE
)
282 return s
.back() & 0x80;
284 return s
.front() & 0x80;
288 /// Resize buffer \a v to size \a n using sign or zero extension
289 /// according to \a ext.
293 extend(T
&v
, enum module::argument::ext_type ext
, size_t n
) {
294 const size_t m
= std::min(v
.size(), n
);
295 const bool sign_ext
= (ext
== module::argument::sign_ext
);
296 const uint8_t fill
= (sign_ext
&& msb(v
) ? ~0 : 0);
299 if (PIPE_ENDIAN_NATIVE
== PIPE_ENDIAN_LITTLE
)
300 std::copy_n(v
.begin(), m
, w
.begin());
302 std::copy_n(v
.end() - m
, m
, w
.end() - m
);
308 /// Append buffer \a w to \a v.
312 insert(T
&v
, const T
&w
) {
313 v
.insert(v
.end(), w
.begin(), w
.end());
317 /// Append \a n elements to the end of buffer \a v.
321 allocate(T
&v
, size_t n
) {
322 size_t pos
= v
.size();
328 std::unique_ptr
<kernel::argument
>
329 kernel::argument::create(const module::argument
&marg
) {
331 case module::argument::scalar
:
332 return std::unique_ptr
<kernel::argument
>(new scalar_argument(marg
.size
));
334 case module::argument::global
:
335 return std::unique_ptr
<kernel::argument
>(new global_argument
);
337 case module::argument::local
:
338 return std::unique_ptr
<kernel::argument
>(new local_argument
);
340 case module::argument::constant
:
341 return std::unique_ptr
<kernel::argument
>(new constant_argument
);
343 case module::argument::image2d_rd
:
344 case module::argument::image3d_rd
:
345 return std::unique_ptr
<kernel::argument
>(new image_rd_argument
);
347 case module::argument::image2d_wr
:
348 case module::argument::image3d_wr
:
349 return std::unique_ptr
<kernel::argument
>(new image_wr_argument
);
351 case module::argument::sampler
:
352 return std::unique_ptr
<kernel::argument
>(new sampler_argument
);
355 throw error(CL_INVALID_KERNEL_DEFINITION
);
358 kernel::argument::argument() : _set(false) {
362 kernel::argument::set() const {
367 kernel::argument::storage() const {
371 kernel::scalar_argument::scalar_argument(size_t size
) : size(size
) {
375 kernel::scalar_argument::set(size_t size
, const void *value
) {
377 throw error(CL_INVALID_ARG_VALUE
);
379 if (size
!= this->size
)
380 throw error(CL_INVALID_ARG_SIZE
);
382 v
= { (uint8_t *)value
, (uint8_t *)value
+ size
};
387 kernel::scalar_argument::bind(exec_context
&ctx
,
388 const module::argument
&marg
) {
391 extend(w
, marg
.ext_type
, marg
.target_size
);
392 byteswap(w
, ctx
.q
->device().endianness());
393 align(ctx
.input
, marg
.target_align
);
394 insert(ctx
.input
, w
);
398 kernel::scalar_argument::unbind(exec_context
&ctx
) {
402 kernel::global_argument::set(size_t size
, const void *value
) {
403 if (size
!= sizeof(cl_mem
))
404 throw error(CL_INVALID_ARG_SIZE
);
406 buf
= pobj
<buffer
>(value
? *(cl_mem
*)value
: NULL
);
411 kernel::global_argument::bind(exec_context
&ctx
,
412 const module::argument
&marg
) {
413 align(ctx
.input
, marg
.target_align
);
416 const resource
&r
= buf
->resource(*ctx
.q
);
417 ctx
.g_handles
.push_back(ctx
.input
.size());
418 ctx
.g_buffers
.push_back(r
.pipe
);
420 // How to handle multi-demensional offsets?
421 // We don't need to. Buffer offsets are always
423 auto v
= bytes(r
.offset
[0]);
424 extend(v
, marg
.ext_type
, marg
.target_size
);
425 byteswap(v
, ctx
.q
->device().endianness());
426 insert(ctx
.input
, v
);
429 allocate(ctx
.input
, marg
.target_size
);
434 kernel::global_argument::unbind(exec_context
&ctx
) {
438 kernel::local_argument::storage() const {
443 kernel::local_argument::set(size_t size
, const void *value
) {
445 throw error(CL_INVALID_ARG_VALUE
);
448 throw error(CL_INVALID_ARG_SIZE
);
455 kernel::local_argument::bind(exec_context
&ctx
,
456 const module::argument
&marg
) {
457 auto v
= bytes(ctx
.mem_local
);
459 extend(v
, module::argument::zero_ext
, marg
.target_size
);
460 byteswap(v
, ctx
.q
->device().endianness());
461 align(ctx
.input
, marg
.target_align
);
462 insert(ctx
.input
, v
);
464 ctx
.mem_local
+= _storage
;
468 kernel::local_argument::unbind(exec_context
&ctx
) {
472 kernel::constant_argument::set(size_t size
, const void *value
) {
473 if (size
!= sizeof(cl_mem
))
474 throw error(CL_INVALID_ARG_SIZE
);
476 buf
= pobj
<buffer
>(value
? *(cl_mem
*)value
: NULL
);
481 kernel::constant_argument::bind(exec_context
&ctx
,
482 const module::argument
&marg
) {
483 align(ctx
.input
, marg
.target_align
);
486 resource
&r
= buf
->resource(*ctx
.q
);
487 auto v
= bytes(ctx
.resources
.size() << 24 | r
.offset
[0]);
489 extend(v
, module::argument::zero_ext
, marg
.target_size
);
490 byteswap(v
, ctx
.q
->device().endianness());
491 insert(ctx
.input
, v
);
493 st
= r
.bind_surface(*ctx
.q
, false);
494 ctx
.resources
.push_back(st
);
497 allocate(ctx
.input
, marg
.target_size
);
502 kernel::constant_argument::unbind(exec_context
&ctx
) {
504 buf
->resource(*ctx
.q
).unbind_surface(*ctx
.q
, st
);
508 kernel::image_rd_argument::set(size_t size
, const void *value
) {
510 throw error(CL_INVALID_ARG_VALUE
);
512 if (size
!= sizeof(cl_mem
))
513 throw error(CL_INVALID_ARG_SIZE
);
515 img
= &obj
<image
>(*(cl_mem
*)value
);
520 kernel::image_rd_argument::bind(exec_context
&ctx
,
521 const module::argument
&marg
) {
522 auto v
= bytes(ctx
.sviews
.size());
524 extend(v
, module::argument::zero_ext
, marg
.target_size
);
525 byteswap(v
, ctx
.q
->device().endianness());
526 align(ctx
.input
, marg
.target_align
);
527 insert(ctx
.input
, v
);
529 st
= img
->resource(*ctx
.q
).bind_sampler_view(*ctx
.q
);
530 ctx
.sviews
.push_back(st
);
534 kernel::image_rd_argument::unbind(exec_context
&ctx
) {
535 img
->resource(*ctx
.q
).unbind_sampler_view(*ctx
.q
, st
);
539 kernel::image_wr_argument::set(size_t size
, const void *value
) {
541 throw error(CL_INVALID_ARG_VALUE
);
543 if (size
!= sizeof(cl_mem
))
544 throw error(CL_INVALID_ARG_SIZE
);
546 img
= &obj
<image
>(*(cl_mem
*)value
);
551 kernel::image_wr_argument::bind(exec_context
&ctx
,
552 const module::argument
&marg
) {
553 auto v
= bytes(ctx
.resources
.size());
555 extend(v
, module::argument::zero_ext
, marg
.target_size
);
556 byteswap(v
, ctx
.q
->device().endianness());
557 align(ctx
.input
, marg
.target_align
);
558 insert(ctx
.input
, v
);
560 st
= img
->resource(*ctx
.q
).bind_surface(*ctx
.q
, true);
561 ctx
.resources
.push_back(st
);
565 kernel::image_wr_argument::unbind(exec_context
&ctx
) {
566 img
->resource(*ctx
.q
).unbind_surface(*ctx
.q
, st
);
570 kernel::sampler_argument::set(size_t size
, const void *value
) {
572 throw error(CL_INVALID_SAMPLER
);
574 if (size
!= sizeof(cl_sampler
))
575 throw error(CL_INVALID_ARG_SIZE
);
577 s
= &obj(*(cl_sampler
*)value
);
582 kernel::sampler_argument::bind(exec_context
&ctx
,
583 const module::argument
&marg
) {
584 st
= s
->bind(*ctx
.q
);
585 ctx
.samplers
.push_back(st
);
589 kernel::sampler_argument::unbind(exec_context
&ctx
) {
590 s
->unbind(*ctx
.q
, st
);