BEM++  2.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Groups Pages
opencl_handler.hpp
1 // Copyright (C) 2011-2012 by the BEM++ Authors
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining a copy
4 // of this software and associated documentation files (the "Software"), to deal
5 // in the Software without restriction, including without limitation the rights
6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 // copies of the Software, and to permit persons to whom the Software is
8 // furnished to do so, subject to the following conditions:
9 //
10 // The above copyright notice and this permission notice shall be included in
11 // all copies or substantial portions of the Software.
12 //
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19 // THE SOFTWARE.
20 
21 #include "../common/common.hpp"
22 
23 
24 #include "bempp/common/config_opencl.hpp"
25 
26 #ifndef fiber_opencl_handler_hpp
27 #define fiber_opencl_handler_hpp
28 
29 #define __CL_ENABLE_EXCEPTIONS
30 
31 #include <string>
32 #include <vector>
33 #include "../common/armadillo_fwd.hpp"
34 #include "opencl_options.hpp"
35 
36 #ifdef WITH_OPENCL
37 #include "CL/cl.hpp"
38 #endif
39 
40 
41 // TODO: rewrite the constructor of OpenClHandler.
42 // It should take a bool useOpenCl and *in addition to that* openClOptions.
43 // The role of the latter should be to e.g. select the device to use
44 // and other configurable execution parameters.
45 // If there are no such parameters, OpenClOptions should just be removed.
46 
47 // Justification: right now there can be a conflict: the user can invoke
48 // AssemblyOptions::switchToOpenCl() and pass to it an instance of OpenClOptions
49 // with useOpenCl set to false. This makes no sense.
50 
51 namespace Fiber
52 {
53 
54 #ifdef WITH_OPENCL
55 
56 class OpenClHandler
57 {
58 public:
63  OpenClHandler(const OpenClOptions& options);
64 
69  ~OpenClHandler();
70 
71  bool UseOpenCl () const { return useOpenCl; }
72 
77  const std::pair<const char*,int> initStr () const;
78 
84  void loadProgramFromString (std::string strSource);
85 
90  void loadProgramFromStringArray (cl::Program::Sources sources) const;
91 
92  cl::Kernel &setKernel (const char *kernelName) const;
93 
97  void enqueueKernel (const cl::NDRange &global) const;
98 
104  template<typename CoordinateType, typename IndexType>
105  void pushGeometry (const arma::Mat<CoordinateType>& vtx,
106  const arma::Mat<IndexType>& idx) const;
107 
114  int SetGeometryArgs (cl::Kernel &kernel, int argid) const;
115 
121  template<typename BufferType>
122  cl::Buffer *createBuffer (int size, cl_mem_flags usage) const;
123 
127  template<typename BufferType>
128  cl::Buffer *pushVector (const std::vector<BufferType> &vec) const;
129 
133  template<typename BufferType>
134  cl::Buffer *pushBuffer (const BufferType *buf, int size) const;
135 
139  //cl::Buffer *pushValueVector (const std::vector<ValueType> &vec) const;
140 
144  template<typename BufferType>
145  cl::Buffer *pushRow (const arma::Row<BufferType> &row) const;
146 
150  //cl::Buffer *pushIndexList (const arma::Row<IndexType>& idx);
151 
155  template<typename BufferType>
156  cl::Buffer *pushMatrix (const arma::Mat<BufferType> &mat) const;
157 
161  template<typename BufferType>
162  cl::Buffer *pushCube (const arma::Cube<BufferType> &cube) const;
163 
164  template<typename BufferType>
165  void pullVector (const cl::Buffer &clbuf,
166  std::vector<BufferType>& vec,
167  int size) const;
168 
169  template<typename BufferType>
170  void pullCube (const cl::Buffer &clbuf,
171  arma::Cube<BufferType>& cube) const;
172 
173  struct MeshGeom {
174  struct MeshDims {
175  int dim;
176  int nvtx;
177  int nels;
178  int nidx;
179  } size;
180  cl::Buffer cl_vtxbuf;
181  cl::Buffer cl_elbuf;
182  };
183 
184  const MeshGeom &meshGeom() const { return meshgeom; }
185 
186 private:
187  const std::pair<const char*,int> typedefStr () const;
188 
189  bool useOpenCl;
190  unsigned int deviceUsed;
191  std::vector<cl::Device> devices;
192  cl::CommandQueue queue;
193  cl::Context context;
194  mutable cl::Program program;
195  mutable cl::Kernel kernel;
196  mutable cl::Event event;
197 
198  mutable MeshGeom meshgeom;
199 
200  mutable const char **progBuf;
201  mutable int nProgBuf;
202 };
203 
204 #else
205 
206 // Dummy implementation for the OpenCL handler
207 
209 {
210 public:
211  OpenClHandler(const OpenClOptions& options) {}
212 
213  bool UseOpenCl () const { return false; }
214 
215  template<typename CoordinateType, typename IndexType>
216  void pushGeometry (const arma::Mat<CoordinateType>& vtx,
217  const arma::Mat<IndexType>& idx) const {}
218 };
219 
220 #endif // WITH_OPENCL
221 
222 
223 } // namespace Fiber
224 
225 #endif // fiber_opencl_handler_hpp
Definition: opencl_options.hpp:29
Definition: opencl_handler.hpp:208