Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
clfProgram.hh
1 /*
2  This file is part of the BIAS library (Basic ImageAlgorithmS).
3 
4  Copyright (C) 2003, 2004 (see file CONTACTS for details)
5  Multimediale Systeme der Informationsverarbeitung
6  Institut fuer Informatik
7  Christian-Albrechts-Universitaet Kiel
8 
9 
10  BIAS is free software; you can redistribute it and/or modify
11  it under the terms of the GNU Lesser General Public License as published by
12  the Free Software Foundation; either version 2.1 of the License, or
13  (at your option) any later version.
14 
15  BIAS is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  GNU Lesser General Public License for more details.
19 
20  You should have received a copy of the GNU Lesser General Public License
21  along with BIAS; if not, write to the Free Software
22  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23  */
24 
25 #ifndef OPENCLPROGRAM_HH_
26 #define OPENCLPROGRAM_HH_
27 
28 #include <bias_config.h>
29 #include <Base/Common/BIASpragmaStart.hh>
30 #include <OpenCLFramework/clfOpenCL.hh>
31 #include <OpenCLFramework/clfBuffer.hh>
32 #include <OpenCLFramework/clfImage2D.hh>
33 #include <OpenCLFramework/clfImage3D.hh>
34 #include <map>
35 #include <OpenCLFramework/clfKernelSources.hh>
36 
37 namespace BIAS {
38 
39  typedef std::map<std::string, cl::Kernel> cl_kernel_map;
40 
41  /**
42  *
43  * @brief OpenCL Program wrapper
44  *
45  * A program can have multiple kernels and can be set up from different source
46  * files.
47  *
48  * Programs can only be created from a valid OpenCLContext Object, hence the
49  * constructor is protected.
50  *
51  * @author fkellner 06/11
52  */
53  class BIASOpenCLFramework_EXPORT clfProgram {
54  public:
55 
56  virtual ~clfProgram();
57 
58  /**
59  * @brief adds source code from a file
60  * @throw clfException
61  * @return source code for anatol
62  */
63  std::string AddSource(std::string filename);
64 
65  /**
66  * @brief adds source code from a string
67  * @throw clfException
68  */
69  void AddSourceFromString(std::string sourceCode);
70 
71  /**
72  * @brief builds the sources added by AddSource and AddSourceFromString
73  * @throw clfException
74  */
75  void Build(int deviceNr = 0, std::string options = "");
76 
77  std::string GetBuildLog(int deviceNr = 0);
78 
79  /**
80  * @brief adds a kernel to the program. program needs to be built first!
81  * @throw clfException
82  */
83  void AddKernel(std::string kernelname);
84 
85  /**
86  * @brief adds a vector of kernels to the program. program needs to be built first!
87  * @throw clfException
88  */
89  void AddKernels(std::vector<std::string> kernelnames);
90 
91  /**
92  * @brief set kernel argument
93  * @param kernelname name of kernel function
94  * @param argnumber number of kernel argument
95  * @param buffer the argument
96  * @throw clfException
97  */
98  void KernelSetArgument(std::string kernelname, unsigned int argnumber, clfBuffer &buffer);
99  void KernelSetArgument(std::string kernelname, unsigned int argnumber, clfImage2D &buffer);
100  void KernelSetArgument(std::string kernelname, unsigned int argnumber, clfImage3D &buffer);
101 
102  // todo implement more if needed!
103  void KernelSetArgument(std::string kernelname, unsigned int argnumber, cl_float2 arg);
104  void KernelSetArgument(std::string kernelname, unsigned int argnumber, cl_float4 arg);
105  void KernelSetArgument(std::string kernelname, unsigned int argnumber, cl_float8 arg);
106  void KernelSetArgument(std::string kernelname, unsigned int argnumber, cl_float16 arg);
107 
108  void KernelSetArgument(std::string kernelname, unsigned int argnumber, cl_int2 arg);
109  void KernelSetArgument(std::string kernelname, unsigned int argnumber, cl_int4 arg);
110  void KernelSetArgument(std::string kernelname, unsigned int argnumber, cl_int8 arg);
111  void KernelSetArgument(std::string kernelname, unsigned int argnumber, cl_int16 arg);
112 
113  void KernelSetArgument(std::string kernelname, unsigned int argnumber, cl_uint2 arg);
114  void KernelSetArgument(std::string kernelname, unsigned int argnumber, cl_uint4 arg);
115  void KernelSetArgument(std::string kernelname, unsigned int argnumber, cl_uint8 arg);
116  void KernelSetArgument(std::string kernelname, unsigned int argnumber, cl_uint16 arg);
117 
118  void KernelSetArgument(std::string kernelname, unsigned int argnumber, int arg);
119  void KernelSetArgument(std::string kernelname, unsigned int argnumber, float arg);
120  void KernelSetArgument(std::string kernelname, unsigned int argnumber, float* arg);
121 
122  void KernelSetLocalArgument(std::string kernelname, unsigned int argnumber, int size);
123 
124  static void AddSourceToDB(const std::string fname, const char *source, unsigned int &blocks) {
125  kernelsourcesdb_.AddSource(fname, source, blocks);
126  }
127  static void AddAdditionalSourceDir(const std::string dir) {
128  additionalsourcedirs_.push_back(dir);
129  }
130 
131  protected:
132  clfProgram(cl::Context *context, std::vector<cl::Device> *devices);
133  cl::Kernel& operator()(std::string kernelname);
134 
135  private:
136  friend class clfContext;
137 
138  cl::Context *context_;
139  std::vector<cl::Device> *devices_;
140 
141  cl::Program::Sources sources_;
142  cl::Program program_;
143  cl_kernel_map kernels_;
144 
145  bool addWriteMultiplier_;
146 
147  static clfKernelSources kernelsourcesdb_;
148  static std::vector<std::string> additionalsourcedirs_;
149  };
150 }
151 #include <Base/Common/BIASpragmaEnd.hh>
152 #endif /* clfPROGRAM_HH_ */
OpenCL Program wrapper.
Definition: clfProgram.hh:53
static void AddSourceToDB(const std::string fname, const char *source, unsigned int &blocks)
Definition: clfProgram.hh:124
OpenCL Image2D wrapper.
Definition: clfImage2D.hh:46
OpenCL Buffer wrapper.
Definition: clfBuffer.hh:43
OpenCL Context wrapper.
Definition: clfContext.hh:49
std::map< std::string, cl::Kernel > cl_kernel_map
Definition: clfProgram.hh:39
OpenCL Image3D wrapper.
Definition: clfImage3D.hh:45
static void AddAdditionalSourceDir(const std::string dir)
Definition: clfProgram.hh:127