kernel.hpp revision c6db1b3396384186aab5b685fe1fd540e17b3a62
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 BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
18// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
19// OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20// SOFTWARE.
21//
22
23#ifndef __CORE_KERNEL_HPP__
24#define __CORE_KERNEL_HPP__
25
26#include <memory>
27
28#include "core/base.hpp"
29#include "core/program.hpp"
30#include "core/memory.hpp"
31#include "core/sampler.hpp"
32#include "pipe/p_state.h"
33
34namespace clover {
35   typedef struct _cl_kernel kernel;
36   class argument;
37}
38
39struct _cl_kernel : public clover::ref_counter {
40private:
41   ///
42   /// Class containing all the state required to execute a compute
43   /// kernel.
44   ///
45   struct exec_context {
46      exec_context(clover::kernel &kern);
47      ~exec_context();
48
49      void *bind(clover::command_queue *q);
50      void unbind();
51
52      clover::kernel &kern;
53      clover::command_queue *q;
54
55      std::vector<uint8_t> input;
56      std::vector<void *> samplers;
57      std::vector<pipe_sampler_view *> sviews;
58      std::vector<pipe_surface *> resources;
59      std::vector<pipe_resource *> g_buffers;
60      std::vector<size_t> g_handles;
61      size_t mem_local;
62
63   private:
64      void *st;
65      pipe_compute_state cs;
66   };
67
68public:
69   class argument {
70   public:
71      argument(size_t size);
72
73      /// \a true if the argument has been set.
74      bool set() const;
75
76      /// Argument size in the input buffer.
77      size_t size() const;
78
79      /// Storage space required for the referenced object.
80      virtual size_t storage() const;
81
82      /// Set this argument to some object.
83      virtual void set(size_t size, const void *value) = 0;
84
85      /// Allocate the necessary resources to bind the specified
86      /// object to this argument, and update \a ctx accordingly.
87      virtual void bind(exec_context &ctx) = 0;
88
89      /// Free any resources that were allocated in bind().
90      virtual void unbind(exec_context &ctx) = 0;
91
92   protected:
93      size_t __size;
94      bool __set;
95   };
96
97   _cl_kernel(clover::program &prog,
98              const std::string &name,
99              const std::vector<clover::module::argument> &args);
100
101   void launch(clover::command_queue &q,
102               const std::vector<size_t> &grid_offset,
103               const std::vector<size_t> &grid_size,
104               const std::vector<size_t> &block_size);
105
106   size_t mem_local() const;
107   size_t mem_private() const;
108   size_t max_block_size() const;
109
110   const std::string &name() const;
111   std::vector<size_t> block_size() const;
112
113   clover::program &prog;
114   std::vector<std::unique_ptr<argument>> args;
115
116private:
117   const clover::module &
118   module(const clover::command_queue &q) const;
119
120   class scalar_argument : public argument {
121   public:
122      scalar_argument(size_t size);
123
124      virtual void set(size_t size, const void *value);
125      virtual void bind(exec_context &ctx);
126      virtual void unbind(exec_context &ctx);
127
128   private:
129      std::vector<uint8_t> v;
130   };
131
132   class global_argument : public argument {
133   public:
134      global_argument(size_t size);
135
136      virtual void set(size_t size, const void *value);
137      virtual void bind(exec_context &ctx);
138      virtual void unbind(exec_context &ctx);
139
140   private:
141      clover::buffer *obj;
142   };
143
144   class local_argument : public argument {
145   public:
146      local_argument();
147
148      virtual size_t storage() const;
149
150      virtual void set(size_t size, const void *value);
151      virtual void bind(exec_context &ctx);
152      virtual void unbind(exec_context &ctx);
153
154   private:
155      size_t __storage;
156   };
157
158   class constant_argument : public argument {
159   public:
160      constant_argument();
161
162      virtual void set(size_t size, const void *value);
163      virtual void bind(exec_context &ctx);
164      virtual void unbind(exec_context &ctx);
165
166   private:
167      clover::buffer *obj;
168      pipe_surface *st;
169   };
170
171   class image_rd_argument : public argument {
172   public:
173      image_rd_argument();
174
175      virtual void set(size_t size, const void *value);
176      virtual void bind(exec_context &ctx);
177      virtual void unbind(exec_context &ctx);
178
179   private:
180      clover::image *obj;
181      pipe_sampler_view *st;
182   };
183
184   class image_wr_argument : public argument {
185   public:
186      image_wr_argument();
187
188      virtual void set(size_t size, const void *value);
189      virtual void bind(exec_context &ctx);
190      virtual void unbind(exec_context &ctx);
191
192   private:
193      clover::image *obj;
194      pipe_surface *st;
195   };
196
197   class sampler_argument : public argument {
198   public:
199      sampler_argument();
200
201      virtual void set(size_t size, const void *value);
202      virtual void bind(exec_context &ctx);
203      virtual void unbind(exec_context &ctx);
204
205   private:
206      clover::sampler *obj;
207      void *st;
208   };
209
210   std::string __name;
211   exec_context exec;
212};
213
214#endif
215