Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
clfContext.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 OPENCLCONTEXT_HH_
26 #define OPENCLCONTEXT_HH_
27 
28 #include <vector>
29 #include <Base/Common/BIASpragmaStart.hh>
30 #include <OpenCLFramework/clfOpenCL.hh>
31 #include <OpenCLFramework/clfProgram.hh>
32 #include <OpenCLFramework/clfBuffer.hh>
33 #include <OpenCLFramework/clfException.hh>
34 #include <OpenCLFramework/clfDeviceInfo.hh>
35 #include <Base/Debug/Debug.hh>
36 
37 namespace BIAS {
38 
39  /**
40  *
41  * @brief OpenCL Context wrapper
42  *
43  * OpenCLContext bundles one context and one command queue.
44  * After creation, use OpenCLContext to instantiate buffer
45  * objects and programs valid for this context.
46  *
47  * @author fkellner 06/11
48  */
49 class BIASOpenCLFramework_EXPORT clfContext {
50  public:
52  clfContextTypeCPU = 0,
53  clfContextTypeGPU
54  };
55  public:
56  /**
57  * @brief Create clfContext for first GPU device found.
58  * @param useGLsharing true if you want to share GL objects. A valid GL context must be available!
59  */
60  clfContext(bool useGLsharing = false, clfDeviceType deviceType = clfContextTypeGPU);
61 
62  /**
63  * @brief deletes context and queue, making objects created from this context invalid!
64  */
65  virtual ~clfContext();
66 
67  /**
68  * @brief create program object
69  * @return program bound to context
70  */
72  return new clfProgram(&context_, &devices_);
73  }
74 
75  /**
76  * @brief create buffer object
77  * @return buffer bound to context
78  */
80  return new clfBuffer(&context_, &queues_[activeQueue_]);
81  }
82 
83  /**
84  * @brief create buffer object
85  * @return buffer bound to context
86  */
88  return new clfImage2D(&context_, &queues_[activeQueue_]);
89  }
90 
91  /**
92  * @brief create buffer object
93  * @return buffer bound to context
94  */
96  return new clfImage3D(&context_, &queues_[activeQueue_]);
97  }
98 
99  /// @brief print info on computing device
100  void PrintDeviceInfo(unsigned int device=0, bool verbose = false,std::ostream& out=std::cout);
101  /// @brief print info on available platforms
102  void PrintPlatformsInfo(std::ostream& out=std::cout);
103 
104  /**
105  * @brief acquire a buffer allocated from opengl, call glfinish first!
106  * @param buffer the buffer object
107  */
108  void AcquireGLObject(clfMemory &buffer);
109 
110  /**
111  * @brief release a buffer allocated from opengl, call Finish first!
112  * @param buffer the buffer object
113  */
114  void ReleaseGLObject(clfMemory &buffer);
115 
116  /**
117  * @brief run a kernel on a 1D memory range
118  * @param globalrange overall memory size in byte, should be multiple of compute units on device
119  * @param localrange memory range in byte handled by each thread, should be even divisor of compute units on device (or 0)
120  */
121  void RunOn1DRange(clfProgram &program, std::string kernelname, unsigned int globalrange, unsigned int localrange=0);
122  /**
123  * @brief run a kernel on a 2D memory range
124  */
125  void RunOn2DRange(clfProgram &program, std::string kernelname, unsigned int globalrangeX, unsigned int globalrangeY, unsigned int localrangeX=0, unsigned int localrangeY=0);
126 
127  /**
128  * @brief run a kernel on a 3D memory range
129  */
130  void RunOn3DRange(clfProgram &program, std::string kernelname, unsigned int globalrangeX, unsigned int globalrangeY, unsigned int globalrangeZ, unsigned int localrangeX=0, unsigned int localrangeY=0, unsigned int localrangeZ=0);
131 
132  /**
133  * @brief force finishing the command queue
134  */
135  void Finish();
136 
137  inline unsigned int GetNumDevices() {
138  return devices_.size();
139  }
140 
141  clfDeviceInfo GetDeviceInfo(unsigned int device = 0) {
142  return devinfo_[device];
143  }
144 
145  inline bool IsSharedGLContext() {
146  return isSharedGL_;
147  }
148 
149  void SetActiveDevice(unsigned int device);
150 
151  unsigned int GetActiveDevice();
152 
153  int DivUp(const int mod, int val);
154 
155  protected:
156 
157  /// @brief internal notification function for status from clf context
158  static void clnotify(const char * p1, const void *p2, size_t p3, clfContext *p4);
159 
160  void QueryDeviceInfo_(unsigned int device = 0);
161  private:
162 
163  cl::Context context_;
164  std::vector<cl::Device> devices_;
165 
166  std::vector<cl::Platform> platforms_;
167 
168  std::vector<cl::CommandQueue> queues_;
169  unsigned int activeQueue_;
170 
171  std::vector<clfDeviceInfo> devinfo_;
172  bool isSharedGL_;
173  };
174 
175 }
176 #include <Base/Common/BIASpragmaEnd.hh>
177 #endif /* clfCONTEXT_HH_ */
clfBuffer * CreateBuffer()
create buffer object
Definition: clfContext.hh:79
OpenCL Program wrapper.
Definition: clfProgram.hh:53
OpenCL Image2D wrapper.
Definition: clfImage2D.hh:46
OpenCL Buffer wrapper.
Definition: clfBuffer.hh:43
unsigned int GetNumDevices()
Definition: clfContext.hh:137
clfProgram * CreateProgram()
create program object
Definition: clfContext.hh:71
OpenCL Context wrapper.
Definition: clfContext.hh:49
clfDeviceInfo GetDeviceInfo(unsigned int device=0)
Definition: clfContext.hh:141
bool IsSharedGLContext()
Definition: clfContext.hh:145
OpenCL Image3D wrapper.
Definition: clfImage3D.hh:45
clfImage2D * CreateImage2D()
create buffer object
Definition: clfContext.hh:87
clfImage3D * CreateImage3D()
create buffer object
Definition: clfContext.hh:95