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
#ifndef CLOVER_CORE_KERNEL_HPP
24
#define CLOVER_CORE_KERNEL_HPP
29
#include "core/object.hpp"
30
#include "core/printf.hpp"
31
#include "core/program.hpp"
32
#include "core/memory.hpp"
33
#include "core/sampler.hpp"
34
#include "pipe/p_state.h"
37
class kernel : public ref_counter, public _cl_kernel {
40
/// Class containing all the state required to execute a compute
44
exec_context(kernel &kern);
47
exec_context(const exec_context &) = delete;
49
operator=(const exec_context &) = delete;
51
void *bind(intrusive_ptr<command_queue> _q,
52
const std::vector<size_t> &grid_offset);
56
intrusive_ptr<command_queue> q;
57
std::unique_ptr<printf_handler> print_handler;
59
std::vector<uint8_t> input;
60
std::vector<void *> samplers;
61
std::vector<pipe_sampler_view *> sviews;
62
std::vector<pipe_image_view> iviews;
63
std::vector<pipe_surface *> resources;
64
std::vector<pipe_resource *> g_buffers;
65
std::vector<size_t> g_handles;
70
pipe_compute_state cs;
76
static std::unique_ptr<argument>
77
create(const module::argument &marg);
79
argument(const argument &arg) = delete;
81
operator=(const argument &arg) = delete;
83
/// \a true if the argument has been set.
86
/// Storage space required for the referenced object.
87
virtual size_t storage() const;
89
/// Set this argument to some object.
90
virtual void set(size_t size, const void *value) = 0;
92
/// Set this argument to an SVM pointer.
93
virtual void set_svm(const void *value) {
94
throw error(CL_INVALID_ARG_INDEX);
97
/// Allocate the necessary resources to bind the specified
98
/// object to this argument, and update \a ctx accordingly.
99
virtual void bind(exec_context &ctx,
100
const module::argument &marg) = 0;
102
/// Free any resources that were allocated in bind().
103
virtual void unbind(exec_context &ctx) = 0;
105
virtual ~argument() {};
113
typedef adaptor_range<
114
derefs, std::vector<std::unique_ptr<argument>> &
117
typedef adaptor_range<
118
derefs, const std::vector<std::unique_ptr<argument>> &
119
> const_argument_range;
122
kernel(clover::program &prog, const std::string &name,
123
const std::vector<clover::module::argument> &margs);
125
kernel(const kernel &kern) = delete;
127
operator=(const kernel &kern) = delete;
129
void launch(command_queue &q,
130
const std::vector<size_t> &grid_offset,
131
const std::vector<size_t> &grid_size,
132
const std::vector<size_t> &block_size);
134
size_t mem_local() const;
135
size_t mem_private() const;
137
const std::string &name() const;
140
optimal_block_size(const command_queue &q,
141
const std::vector<size_t> &grid_size) const;
143
required_block_size() const;
145
argument_range args();
146
const_argument_range args() const;
147
std::vector<clover::module::arg_info> args_infos();
149
const intrusive_ref<clover::program> program;
152
const clover::module &module(const command_queue &q) const;
154
class scalar_argument : public argument {
156
scalar_argument(size_t size);
158
virtual void set(size_t size, const void *value);
159
virtual void bind(exec_context &ctx,
160
const module::argument &marg);
161
virtual void unbind(exec_context &ctx);
165
std::vector<uint8_t> v;
168
class global_argument : public argument {
170
virtual void set(size_t size, const void *value);
171
virtual void set_svm(const void *value);
172
virtual void bind(exec_context &ctx,
173
const module::argument &marg);
174
virtual void unbind(exec_context &ctx);
181
class local_argument : public argument {
183
virtual size_t storage() const;
185
virtual void set(size_t size, const void *value);
186
virtual void bind(exec_context &ctx,
187
const module::argument &marg);
188
virtual void unbind(exec_context &ctx);
194
class constant_argument : public argument {
196
virtual void set(size_t size, const void *value);
197
virtual void bind(exec_context &ctx,
198
const module::argument &marg);
199
virtual void unbind(exec_context &ctx);
206
class image_argument : public argument {
208
const image *get() const {
215
class image_rd_argument : public image_argument {
217
virtual void set(size_t size, const void *value);
218
virtual void bind(exec_context &ctx,
219
const module::argument &marg);
220
virtual void unbind(exec_context &ctx);
223
pipe_sampler_view *st;
226
class image_wr_argument : public image_argument {
228
virtual void set(size_t size, const void *value);
229
virtual void bind(exec_context &ctx,
230
const module::argument &marg);
231
virtual void unbind(exec_context &ctx);
234
class sampler_argument : public argument {
236
virtual void set(size_t size, const void *value);
237
virtual void bind(exec_context &ctx,
238
const module::argument &marg);
239
virtual void unbind(exec_context &ctx);
246
std::vector<std::unique_ptr<argument>> _args;
247
std::map<device *, std::unique_ptr<root_buffer> > _constant_buffers;
250
const ref_holder program_ref;