• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright 2012 Francisco Jerez
3 //
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:
10 //
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
13 //
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.
21 //
22 
23 #ifndef CLOVER_CORE_KERNEL_HPP
24 #define CLOVER_CORE_KERNEL_HPP
25 
26 #include <map>
27 #include <memory>
28 
29 #include "core/object.hpp"
30 #include "core/program.hpp"
31 #include "core/memory.hpp"
32 #include "core/sampler.hpp"
33 #include "pipe/p_state.h"
34 
35 namespace clover {
36    class kernel : public ref_counter, public _cl_kernel {
37    private:
38       ///
39       /// Class containing all the state required to execute a compute
40       /// kernel.
41       ///
42       struct exec_context {
43          exec_context(kernel &kern);
44          ~exec_context();
45 
46          exec_context(const exec_context &) = delete;
47          exec_context &
48          operator=(const exec_context &) = delete;
49 
50          void *bind(intrusive_ptr<command_queue> _q,
51                     const std::vector<size_t> &grid_offset);
52          void unbind();
53 
54          kernel &kern;
55          intrusive_ptr<command_queue> q;
56 
57          std::vector<uint8_t> input;
58          std::vector<void *> samplers;
59          std::vector<pipe_sampler_view *> sviews;
60          std::vector<pipe_image_view> iviews;
61          std::vector<pipe_surface *> resources;
62          std::vector<pipe_resource *> g_buffers;
63          std::vector<size_t> g_handles;
64          size_t mem_local;
65 
66       private:
67          void *st;
68          pipe_compute_state cs;
69       };
70 
71    public:
72       class argument {
73       public:
74          static std::unique_ptr<argument>
75          create(const module::argument &marg);
76 
77          argument(const argument &arg) = delete;
78          argument &
79          operator=(const argument &arg) = delete;
80 
81          /// \a true if the argument has been set.
82          bool set() const;
83 
84          /// Storage space required for the referenced object.
85          virtual size_t storage() const;
86 
87          /// Set this argument to some object.
88          virtual void set(size_t size, const void *value) = 0;
89 
90          /// Set this argument to an SVM pointer.
set_svm(const void * value)91          virtual void set_svm(const void *value) {
92             throw error(CL_INVALID_ARG_INDEX);
93          };
94 
95          /// Allocate the necessary resources to bind the specified
96          /// object to this argument, and update \a ctx accordingly.
97          virtual void bind(exec_context &ctx,
98                            const module::argument &marg) = 0;
99 
100          /// Free any resources that were allocated in bind().
101          virtual void unbind(exec_context &ctx) = 0;
102 
~argument()103          virtual ~argument() {};
104       protected:
105          argument();
106 
107          bool _set;
108       };
109 
110    private:
111       typedef adaptor_range<
112             derefs, std::vector<std::unique_ptr<argument>> &
113          > argument_range;
114 
115       typedef adaptor_range<
116             derefs, const std::vector<std::unique_ptr<argument>> &
117          > const_argument_range;
118 
119    public:
120       kernel(clover::program &prog, const std::string &name,
121              const std::vector<clover::module::argument> &margs);
122 
123       kernel(const kernel &kern) = delete;
124       kernel &
125       operator=(const kernel &kern) = delete;
126 
127       void launch(command_queue &q,
128                   const std::vector<size_t> &grid_offset,
129                   const std::vector<size_t> &grid_size,
130                   const std::vector<size_t> &block_size);
131 
132       size_t mem_local() const;
133       size_t mem_private() const;
134 
135       const std::string &name() const;
136 
137       std::vector<size_t>
138       optimal_block_size(const command_queue &q,
139                          const std::vector<size_t> &grid_size) const;
140       std::vector<size_t>
141       required_block_size() const;
142 
143       argument_range args();
144       const_argument_range args() const;
145       std::vector<clover::module::arg_info> args_infos();
146 
147       const intrusive_ref<clover::program> program;
148 
149    private:
150       const clover::module &module(const command_queue &q) const;
151 
152       class scalar_argument : public argument {
153       public:
154          scalar_argument(size_t size);
155 
156          virtual void set(size_t size, const void *value);
157          virtual void bind(exec_context &ctx,
158                            const module::argument &marg);
159          virtual void unbind(exec_context &ctx);
160 
161       private:
162          size_t size;
163          std::vector<uint8_t> v;
164       };
165 
166       class global_argument : public argument {
167       public:
168          virtual void set(size_t size, const void *value);
169          virtual void set_svm(const void *value);
170          virtual void bind(exec_context &ctx,
171                            const module::argument &marg);
172          virtual void unbind(exec_context &ctx);
173 
174       private:
175          buffer *buf;
176          const void *svm;
177       };
178 
179       class local_argument : public argument {
180       public:
181          virtual size_t storage() const;
182 
183          virtual void set(size_t size, const void *value);
184          virtual void bind(exec_context &ctx,
185                            const module::argument &marg);
186          virtual void unbind(exec_context &ctx);
187 
188       private:
189          size_t _storage = 0;
190       };
191 
192       class constant_argument : public argument {
193       public:
194          virtual void set(size_t size, const void *value);
195          virtual void bind(exec_context &ctx,
196                            const module::argument &marg);
197          virtual void unbind(exec_context &ctx);
198 
199       private:
200          buffer *buf;
201          pipe_surface *st;
202       };
203 
204       class image_argument : public argument {
205       public:
get() const206          const image *get() const {
207             return img;
208          }
209       protected:
210          image *img;
211       };
212 
213       class image_rd_argument : public image_argument {
214       public:
215          virtual void set(size_t size, const void *value);
216          virtual void bind(exec_context &ctx,
217                            const module::argument &marg);
218          virtual void unbind(exec_context &ctx);
219 
220       private:
221          pipe_sampler_view *st;
222       };
223 
224       class image_wr_argument : public image_argument {
225       public:
226          virtual void set(size_t size, const void *value);
227          virtual void bind(exec_context &ctx,
228                            const module::argument &marg);
229          virtual void unbind(exec_context &ctx);
230       };
231 
232       class sampler_argument : public argument {
233       public:
234          virtual void set(size_t size, const void *value);
235          virtual void bind(exec_context &ctx,
236                            const module::argument &marg);
237          virtual void unbind(exec_context &ctx);
238 
239       private:
240          sampler *s;
241          void *st;
242       };
243 
244       std::vector<std::unique_ptr<argument>> _args;
245       std::map<device *, std::unique_ptr<root_buffer> > _constant_buffers;
246       std::string _name;
247       exec_context exec;
248       const ref_holder program_ref;
249    };
250 }
251 
252 #endif
253