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(program
&prog
, const std::string
&name
,
32 const std::vector
<module::argument
> &margs
) :
33 prog(prog
), _name(name
), exec(*this) {
34 for (auto &marg
: margs
) {
35 if (marg
.type
== module::argument::scalar
)
36 _args
.emplace_back(new scalar_argument(marg
.size
));
37 else if (marg
.type
== module::argument::global
)
38 _args
.emplace_back(new global_argument
);
39 else if (marg
.type
== module::argument::local
)
40 _args
.emplace_back(new local_argument
);
41 else if (marg
.type
== module::argument::constant
)
42 _args
.emplace_back(new constant_argument
);
43 else if (marg
.type
== module::argument::image2d_rd
||
44 marg
.type
== module::argument::image3d_rd
)
45 _args
.emplace_back(new image_rd_argument
);
46 else if (marg
.type
== module::argument::image2d_wr
||
47 marg
.type
== module::argument::image3d_wr
)
48 _args
.emplace_back(new image_wr_argument
);
49 else if (marg
.type
== module::argument::sampler
)
50 _args
.emplace_back(new sampler_argument
);
52 throw error(CL_INVALID_KERNEL_DEFINITION
);
57 static inline std::vector
<uint
>
58 pad_vector(command_queue
&q
, const V
&v
, uint x
) {
59 std::vector
<uint
> w
{ v
.begin(), v
.end() };
60 w
.resize(q
.dev
.max_block_size().size(), x
);
65 kernel::launch(command_queue
&q
,
66 const std::vector
<size_t> &grid_offset
,
67 const std::vector
<size_t> &grid_size
,
68 const std::vector
<size_t> &block_size
) {
69 const auto m
= prog
.binary(q
.dev
);
70 const auto reduced_grid_size
=
71 map(divides(), grid_size
, block_size
);
72 void *st
= exec
.bind(&q
);
74 // The handles are created during exec_context::bind(), so we need make
75 // sure to call exec_context::bind() before retrieving them.
76 std::vector
<uint32_t *> g_handles
= map([&](size_t h
) {
77 return (uint32_t *)&exec
.input
[h
];
80 q
.pipe
->bind_compute_state(q
.pipe
, st
);
81 q
.pipe
->bind_sampler_states(q
.pipe
, PIPE_SHADER_COMPUTE
,
82 0, exec
.samplers
.size(),
83 exec
.samplers
.data());
85 q
.pipe
->set_sampler_views(q
.pipe
, PIPE_SHADER_COMPUTE
, 0,
86 exec
.sviews
.size(), exec
.sviews
.data());
87 q
.pipe
->set_compute_resources(q
.pipe
, 0, exec
.resources
.size(),
88 exec
.resources
.data());
89 q
.pipe
->set_global_binding(q
.pipe
, 0, exec
.g_buffers
.size(),
90 exec
.g_buffers
.data(), g_handles
.data());
92 q
.pipe
->launch_grid(q
.pipe
,
93 pad_vector(q
, block_size
, 1).data(),
94 pad_vector(q
, reduced_grid_size
, 1).data(),
95 find(name_equals(_name
), m
.syms
).offset
,
98 q
.pipe
->set_global_binding(q
.pipe
, 0, exec
.g_buffers
.size(), NULL
, NULL
);
99 q
.pipe
->set_compute_resources(q
.pipe
, 0, exec
.resources
.size(), NULL
);
100 q
.pipe
->set_sampler_views(q
.pipe
, PIPE_SHADER_COMPUTE
, 0,
101 exec
.sviews
.size(), NULL
);
102 q
.pipe
->bind_sampler_states(q
.pipe
, PIPE_SHADER_COMPUTE
, 0,
103 exec
.samplers
.size(), NULL
);
108 kernel::mem_local() const {
111 for (auto &arg
: args()) {
112 if (dynamic_cast<local_argument
*>(&arg
))
120 kernel::mem_private() const {
125 kernel::name() const {
130 kernel::optimal_block_size(const command_queue
&q
,
131 const std::vector
<size_t> &grid_size
) const {
132 return factor::find_grid_optimal_factor
<size_t>(
133 q
.dev
.max_threads_per_block(), q
.dev
.max_block_size(),
138 kernel::required_block_size() const {
142 kernel::argument_range
144 return map(derefs(), _args
);
147 kernel::const_argument_range
148 kernel::args() const {
149 return map(derefs(), _args
);
153 kernel::module(const command_queue
&q
) const {
154 return prog
.binary(q
.dev
);
157 kernel::exec_context::exec_context(kernel
&kern
) :
158 kern(kern
), q(NULL
), mem_local(0), st(NULL
), cs() {
161 kernel::exec_context::~exec_context() {
163 q
->pipe
->delete_compute_state(q
->pipe
, st
);
167 kernel::exec_context::bind(command_queue
*_q
) {
170 // Bind kernel arguments.
171 auto &m
= kern
.prog
.binary(q
->dev
);
172 auto margs
= find(name_equals(kern
.name()), m
.syms
).args
;
173 auto msec
= find(type_equals(module::section::text
), m
.secs
);
175 for_each([=](kernel::argument
&karg
, const module::argument
&marg
) {
176 karg
.bind(*this, marg
);
177 }, kern
.args(), margs
);
179 // Create a new compute state if anything changed.
180 if (!st
|| q
!= _q
||
181 cs
.req_local_mem
!= mem_local
||
182 cs
.req_input_mem
!= input
.size()) {
184 _q
->pipe
->delete_compute_state(_q
->pipe
, st
);
186 cs
.prog
= msec
.data
.begin();
187 cs
.req_local_mem
= mem_local
;
188 cs
.req_input_mem
= input
.size();
189 st
= q
->pipe
->create_compute_state(q
->pipe
, &cs
);
196 kernel::exec_context::unbind() {
197 for (auto &arg
: kern
.args())
213 return { (uint8_t *)&x
, (uint8_t *)&x
+ sizeof(x
) };
217 /// Transform buffer \a v from the native byte order into the byte
218 /// order specified by \a e.
222 byteswap(T
&v
, pipe_endian e
) {
223 if (PIPE_ENDIAN_NATIVE
!= e
)
224 std::reverse(v
.begin(), v
.end());
228 /// Pad buffer \a v to the next multiple of \a n.
232 align(T
&v
, size_t n
) {
233 v
.resize(util_align_npot(v
.size(), n
));
237 msb(const std::vector
<uint8_t> &s
) {
238 if (PIPE_ENDIAN_NATIVE
== PIPE_ENDIAN_LITTLE
)
239 return s
.back() & 0x80;
241 return s
.front() & 0x80;
245 /// Resize buffer \a v to size \a n using sign or zero extension
246 /// according to \a ext.
250 extend(T
&v
, enum module::argument::ext_type ext
, size_t n
) {
251 const size_t m
= std::min(v
.size(), n
);
252 const bool sign_ext
= (ext
== module::argument::sign_ext
);
253 const uint8_t fill
= (sign_ext
&& msb(v
) ? ~0 : 0);
256 if (PIPE_ENDIAN_NATIVE
== PIPE_ENDIAN_LITTLE
)
257 std::copy_n(v
.begin(), m
, w
.begin());
259 std::copy_n(v
.end() - m
, m
, w
.end() - m
);
265 /// Append buffer \a w to \a v.
269 insert(T
&v
, const T
&w
) {
270 v
.insert(v
.end(), w
.begin(), w
.end());
274 /// Append \a n elements to the end of buffer \a v.
278 allocate(T
&v
, size_t n
) {
279 size_t pos
= v
.size();
285 kernel::argument::argument() : _set(false) {
289 kernel::argument::set() const {
294 kernel::argument::storage() const {
298 kernel::scalar_argument::scalar_argument(size_t size
) : size(size
) {
302 kernel::scalar_argument::set(size_t size
, const void *value
) {
303 if (size
!= this->size
)
304 throw error(CL_INVALID_ARG_SIZE
);
306 v
= { (uint8_t *)value
, (uint8_t *)value
+ size
};
311 kernel::scalar_argument::bind(exec_context
&ctx
,
312 const module::argument
&marg
) {
315 extend(w
, marg
.ext_type
, marg
.target_size
);
316 byteswap(w
, ctx
.q
->dev
.endianness());
317 align(ctx
.input
, marg
.target_align
);
318 insert(ctx
.input
, w
);
322 kernel::scalar_argument::unbind(exec_context
&ctx
) {
326 kernel::global_argument::set(size_t size
, const void *value
) {
327 if (size
!= sizeof(cl_mem
))
328 throw error(CL_INVALID_ARG_SIZE
);
330 buf
= pobj
<buffer
>(value
? *(cl_mem
*)value
: NULL
);
335 kernel::global_argument::bind(exec_context
&ctx
,
336 const module::argument
&marg
) {
337 align(ctx
.input
, marg
.target_align
);
340 ctx
.g_handles
.push_back(allocate(ctx
.input
, marg
.target_size
));
341 ctx
.g_buffers
.push_back(buf
->resource(*ctx
.q
).pipe
);
344 allocate(ctx
.input
, marg
.target_size
);
349 kernel::global_argument::unbind(exec_context
&ctx
) {
353 kernel::local_argument::storage() const {
358 kernel::local_argument::set(size_t size
, const void *value
) {
360 throw error(CL_INVALID_ARG_VALUE
);
367 kernel::local_argument::bind(exec_context
&ctx
,
368 const module::argument
&marg
) {
369 auto v
= bytes(ctx
.mem_local
);
371 extend(v
, module::argument::zero_ext
, marg
.target_size
);
372 byteswap(v
, ctx
.q
->dev
.endianness());
373 align(ctx
.input
, marg
.target_align
);
374 insert(ctx
.input
, v
);
376 ctx
.mem_local
+= _storage
;
380 kernel::local_argument::unbind(exec_context
&ctx
) {
384 kernel::constant_argument::set(size_t size
, const void *value
) {
385 if (size
!= sizeof(cl_mem
))
386 throw error(CL_INVALID_ARG_SIZE
);
388 buf
= pobj
<buffer
>(value
? *(cl_mem
*)value
: NULL
);
393 kernel::constant_argument::bind(exec_context
&ctx
,
394 const module::argument
&marg
) {
395 align(ctx
.input
, marg
.target_align
);
398 auto v
= bytes(ctx
.resources
.size() << 24);
400 extend(v
, module::argument::zero_ext
, marg
.target_size
);
401 byteswap(v
, ctx
.q
->dev
.endianness());
402 insert(ctx
.input
, v
);
404 st
= buf
->resource(*ctx
.q
).bind_surface(*ctx
.q
, false);
405 ctx
.resources
.push_back(st
);
408 allocate(ctx
.input
, marg
.target_size
);
413 kernel::constant_argument::unbind(exec_context
&ctx
) {
414 buf
->resource(*ctx
.q
).unbind_surface(*ctx
.q
, st
);
418 kernel::image_rd_argument::set(size_t size
, const void *value
) {
419 if (size
!= sizeof(cl_mem
))
420 throw error(CL_INVALID_ARG_SIZE
);
422 img
= &obj
<image
>(*(cl_mem
*)value
);
427 kernel::image_rd_argument::bind(exec_context
&ctx
,
428 const module::argument
&marg
) {
429 auto v
= bytes(ctx
.sviews
.size());
431 extend(v
, module::argument::zero_ext
, marg
.target_size
);
432 byteswap(v
, ctx
.q
->dev
.endianness());
433 align(ctx
.input
, marg
.target_align
);
434 insert(ctx
.input
, v
);
436 st
= img
->resource(*ctx
.q
).bind_sampler_view(*ctx
.q
);
437 ctx
.sviews
.push_back(st
);
441 kernel::image_rd_argument::unbind(exec_context
&ctx
) {
442 img
->resource(*ctx
.q
).unbind_sampler_view(*ctx
.q
, st
);
446 kernel::image_wr_argument::set(size_t size
, const void *value
) {
447 if (size
!= sizeof(cl_mem
))
448 throw error(CL_INVALID_ARG_SIZE
);
450 img
= &obj
<image
>(*(cl_mem
*)value
);
455 kernel::image_wr_argument::bind(exec_context
&ctx
,
456 const module::argument
&marg
) {
457 auto v
= bytes(ctx
.resources
.size());
459 extend(v
, module::argument::zero_ext
, marg
.target_size
);
460 byteswap(v
, ctx
.q
->dev
.endianness());
461 align(ctx
.input
, marg
.target_align
);
462 insert(ctx
.input
, v
);
464 st
= img
->resource(*ctx
.q
).bind_surface(*ctx
.q
, true);
465 ctx
.resources
.push_back(st
);
469 kernel::image_wr_argument::unbind(exec_context
&ctx
) {
470 img
->resource(*ctx
.q
).unbind_surface(*ctx
.q
, st
);
474 kernel::sampler_argument::set(size_t size
, const void *value
) {
475 if (size
!= sizeof(cl_sampler
))
476 throw error(CL_INVALID_ARG_SIZE
);
478 s
= &obj(*(cl_sampler
*)value
);
483 kernel::sampler_argument::bind(exec_context
&ctx
,
484 const module::argument
&marg
) {
485 st
= s
->bind(*ctx
.q
);
486 ctx
.samplers
.push_back(st
);
490 kernel::sampler_argument::unbind(exec_context
&ctx
) {
491 s
->unbind(*ctx
.q
, st
);