ITK  6.0.0
Insight Toolkit
itkGPUKernelManager.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright NumFOCUS
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * https://www.apache.org/licenses/LICENSE-2.0.txt
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *=========================================================================*/
18 
19 //
20 // GPU Kernel Manager Class
21 //
22 
23 #ifndef itkGPUKernelManager_h
24 #define itkGPUKernelManager_h
25 
26 #include <vector>
27 #include "itkLightObject.h"
28 #include "itkObjectFactory.h"
29 #include "itkOpenCLUtil.h"
30 #include "itkGPUImage.h"
31 #include "itkGPUContextManager.h"
32 #include "itkGPUDataManager.h"
33 
34 namespace itk
35 {
45 {
46 public:
47  ITK_DISALLOW_COPY_AND_MOVE(GPUKernelManager);
48 
50  {
51  bool m_IsReady;
53  };
54 
59 
60  itkNewMacro(Self);
61  itkOverrideGetNameOfClassMacro(GPUKernelManager);
62 
63  bool
64  LoadProgramFromFile(const char * filename, const char * cPreamble = "");
65 
66  bool
67  LoadProgramFromString(const char * cSource, const char * cPreamble = "");
68 
69  int
70  CreateKernel(const char * kernelName);
71 
72  cl_int
73  GetKernelWorkGroupInfo(int kernelIdx, cl_kernel_work_group_info paramName, void * value);
74 
75  cl_int
76  GetDeviceInfo(cl_kernel_work_group_info paramName, size_t argSize, void * argValue);
77 
78  bool
79  SetKernelArg(int kernelIdx, cl_uint argIdx, size_t argSize, const void * argVal);
80 
81  bool
82  SetKernelArgWithChar(int kernelIdx, cl_uint argIdx, char argVal);
83 
84  bool
85  SetKernelArgWithUChar(int kernelIdx, cl_uint argIdx, unsigned char argVal);
86 
87  bool
88  SetKernelArgWithShort(int kernelIdx, cl_uint argIdx, short argVal);
89 
90  bool
91  SetKernelArgWithUShort(int kernelIdx, cl_uint argIdx, unsigned short argVal);
92 
93  bool
94  SetKernelArgWithInt(int kernelIdx, cl_uint argIdx, int argVal);
95 
96  bool
97  SetKernelArgWithUInt(int kernelIdx, cl_uint argIdx, unsigned int argVal);
98 
99  bool
100  SetKernelArgWithLongLong(int kernelIdx, cl_uint argIdx, long long argVal);
101 
102  bool
103  SetKernelArgWithULongLong(int kernelIdx, cl_uint argIdx, unsigned long long argVal);
104 
105  bool
106  SetKernelArgWithFloat(int kernelIdx, cl_uint argIdx, float argVal);
107 
108  bool
109  SetKernelArgWithDouble(int kernelIdx, cl_uint argIdx, double argVal);
110 
111  bool
112  SetKernelArgWithImage(int kernelIdx, cl_uint argIdx, GPUDataManager * manager);
113 
115  // template< typename TGPUImageDataManager >
116  // bool SetKernelArgWithImageAndBufferedRegion(int kernelIdx, cl_uint &argIdx,
117  // typename TGPUImageDataManager::Pointer manager);
118  template <typename TGPUImageDataManager>
119  bool
120  SetKernelArgWithImageAndBufferedRegion(int kernelIdx, cl_uint & argIdx, TGPUImageDataManager * manager)
121  {
122  if (kernelIdx < 0 || kernelIdx >= static_cast<int>(m_KernelContainer.size()))
123  {
124  return false;
125  }
128  cl_int errid;
129 
130  errid = clSetKernelArg(m_KernelContainer[kernelIdx], argIdx, sizeof(cl_mem), manager->GetGPUBufferPointer());
131  OpenCLCheckError(errid, __FILE__, __LINE__, ITK_LOCATION);
132 
133  m_KernelArgumentReady[kernelIdx][argIdx].m_IsReady = true;
134  m_KernelArgumentReady[kernelIdx][argIdx].m_GPUDataManager = manager;
135  ++argIdx;
136 
137  // this->SetKernelArg(kernelIdx, argIdx++, sizeof(int), &(TGPUImageDataManager::ImageDimension) );
138 
139  // the starting index for the buffered region
140  errid = clSetKernelArg(m_KernelContainer[kernelIdx],
141  argIdx,
142  sizeof(cl_mem),
143  manager->GetModifiableGPUBufferedRegionIndex()->GetGPUBufferPointer());
144  OpenCLCheckError(errid, __FILE__, __LINE__, ITK_LOCATION);
145 
146  m_KernelArgumentReady[kernelIdx][argIdx].m_IsReady = true;
147  m_KernelArgumentReady[kernelIdx][argIdx].m_GPUDataManager = manager->GetModifiableGPUBufferedRegionIndex();
148  ++argIdx;
149 
150  // the size for the buffered region
151  errid = clSetKernelArg(m_KernelContainer[kernelIdx],
152  argIdx,
153  sizeof(cl_mem),
154  manager->GetModifiableGPUBufferedRegionSize()->GetGPUBufferPointer());
155  OpenCLCheckError(errid, __FILE__, __LINE__, ITK_LOCATION);
156 
157  m_KernelArgumentReady[kernelIdx][argIdx].m_IsReady = true;
158  m_KernelArgumentReady[kernelIdx][argIdx].m_GPUDataManager = manager->GetModifiableGPUBufferedRegionSize();
159  ++argIdx;
160 
161  return true;
162  }
163 
164  bool
165  LaunchKernel(int kernelIdx, int dim, size_t * globalWorkSize, size_t * localWorkSize);
166 
167  bool
168  LaunchKernel1D(int kernelIdx, size_t globalWorkSize, size_t localWorkSize);
169 
170  bool
171  LaunchKernel2D(int kernelIdx,
172  size_t globalWorkSizeX,
173  size_t globalWorkSizeY,
174  size_t localWorkSizeX,
175  size_t localWorkSizeY);
176 
177  bool
178  LaunchKernel3D(int kernelIdx,
179  size_t globalWorkSizeX,
180  size_t globalWorkSizeY,
181  size_t globalWorkSizeZ,
182  size_t localWorkSizeX,
183  size_t localWorkSizeY,
184  size_t localWorkSizeZ);
185 
186  void
187  SetCurrentCommandQueue(int queueid);
188 
189  int
190  GetCurrentCommandQueueID() const;
191 
192 protected:
194  ~GPUKernelManager() override;
195 
196  bool
197  CheckArgumentReady(int kernelIdx);
198 
199  void
200  ResetArguments(int kernelIdx);
201 
202 private:
203  cl_program m_Program{};
204 
207 
208  std::vector<cl_kernel> m_KernelContainer{};
209  std::vector<std::vector<KernelArgumentList>> m_KernelArgumentReady{};
210 
211  template <typename TArg>
212  bool
213  SetTypedKernelArg(int kernelIdx, cl_uint argIdx, TArg argVal);
214 };
215 } // namespace itk
216 
217 #endif
itk::GPUKernelManager::LaunchKernel2D
bool LaunchKernel2D(int kernelIdx, vcl_size_t globalWorkSizeX, vcl_size_t globalWorkSizeY, vcl_size_t localWorkSizeX, vcl_size_t localWorkSizeY)
itk::GPUKernelManager::~GPUKernelManager
~GPUKernelManager() override
itk::GPUKernelManager::LaunchKernel3D
bool LaunchKernel3D(int kernelIdx, vcl_size_t globalWorkSizeX, vcl_size_t globalWorkSizeY, vcl_size_t globalWorkSizeZ, vcl_size_t localWorkSizeX, vcl_size_t localWorkSizeY, vcl_size_t localWorkSizeZ)
itkObjectFactory.h
itk::GPUKernelManager::SetKernelArgWithFloat
bool SetKernelArgWithFloat(int kernelIdx, cl_uint argIdx, float argVal)
itk::GPUKernelManager::m_Program
cl_program m_Program
Definition: itkGPUKernelManager.h:203
itk::GPUKernelManager::GetCurrentCommandQueueID
int GetCurrentCommandQueueID() const
itk::GPUKernelManager::SetKernelArgWithDouble
bool SetKernelArgWithDouble(int kernelIdx, cl_uint argIdx, double argVal)
itk::GPUKernelManager::GPUKernelManager
GPUKernelManager()
itk::GPUKernelManager::m_KernelArgumentReady
std::vector< std::vector< KernelArgumentList > > m_KernelArgumentReady
Definition: itkGPUKernelManager.h:209
itk::GPUKernelManager::LoadProgramFromString
bool LoadProgramFromString(const char *cSource, const char *cPreamble="")
itk::GPUKernelManager::SetCurrentCommandQueue
void SetCurrentCommandQueue(int queueid)
itk::OpenCLCheckError
void OpenCLCheckError(cl_int error, const char *filename="", int lineno=0, const char *location="")
itk::GPUKernelManager::SetKernelArgWithUShort
bool SetKernelArgWithUShort(int kernelIdx, cl_uint argIdx, unsigned short argVal)
itk::LightObject::LightObject
LightObject()
itk::SmartPointer< Self >
itkOpenCLUtil.h
itk::GPUKernelManager::KernelArgumentList::m_IsReady
bool m_IsReady
Definition: itkGPUKernelManager.h:51
itk::GPUKernelManager::SetKernelArgWithShort
bool SetKernelArgWithShort(int kernelIdx, cl_uint argIdx, short argVal)
itk::GPUKernelManager::LaunchKernel1D
bool LaunchKernel1D(int kernelIdx, vcl_size_t globalWorkSize, vcl_size_t localWorkSize)
itk::GPUKernelManager::SetKernelArg
bool SetKernelArg(int kernelIdx, cl_uint argIdx, vcl_size_t argSize, const void *argVal)
itk::GPUDataManager
GPU memory manager implemented using OpenCL. Required by GPUImage class.
Definition: itkGPUDataManager.h:42
itk::GPUKernelManager::m_CommandQueueId
int m_CommandQueueId
Definition: itkGPUKernelManager.h:206
itk::LightObject
Light weight base class for most itk classes.
Definition: itkLightObject.h:55
itk::GPUKernelManager::SetKernelArgWithInt
bool SetKernelArgWithInt(int kernelIdx, cl_uint argIdx, int argVal)
itk::GPUKernelManager::KernelArgumentList::m_GPUDataManager
GPUDataManager::Pointer m_GPUDataManager
Definition: itkGPUKernelManager.h:52
itkGPUImage.h
itk::GPUKernelManager::LoadProgramFromFile
bool LoadProgramFromFile(const char *filename, const char *cPreamble="")
itk::GPUKernelManager::SetKernelArgWithULongLong
bool SetKernelArgWithULongLong(int kernelIdx, cl_uint argIdx, unsigned long long argVal)
itk::GPUKernelManager::m_KernelContainer
std::vector< cl_kernel > m_KernelContainer
Definition: itkGPUKernelManager.h:208
itk::GPUKernelManager::SetKernelArgWithUInt
bool SetKernelArgWithUInt(int kernelIdx, cl_uint argIdx, unsigned int argVal)
itk::GPUKernelManager::SetTypedKernelArg
bool SetTypedKernelArg(int kernelIdx, cl_uint argIdx, TArg argVal)
itk::GPUKernelManager::SetKernelArgWithImage
bool SetKernelArgWithImage(int kernelIdx, cl_uint argIdx, GPUDataManager *manager)
itk::GPUKernelManager::KernelArgumentList
Definition: itkGPUKernelManager.h:49
itk::GPUKernelManager::GetKernelWorkGroupInfo
cl_int GetKernelWorkGroupInfo(int kernelIdx, cl_kernel_work_group_info paramName, void *value)
itk::GPUContextManager
Singleton class to store the GPU context.
Definition: itkGPUContextManager.h:34
itk::GPUKernelManager::SetKernelArgWithLongLong
bool SetKernelArgWithLongLong(int kernelIdx, cl_uint argIdx, long long argVal)
itk::GPUKernelManager::m_Manager
GPUContextManager * m_Manager
Definition: itkGPUKernelManager.h:205
itk::GPUKernelManager
GPU kernel manager implemented using OpenCL.
Definition: itkGPUKernelManager.h:44
itk::GPUKernelManager::ResetArguments
void ResetArguments(int kernelIdx)
itk::GPUKernelManager::GetDeviceInfo
cl_int GetDeviceInfo(cl_kernel_work_group_info paramName, vcl_size_t argSize, void *argValue)
itk::GPUKernelManager::CreateKernel
int CreateKernel(const char *kernelName)
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkAnatomicalOrientation.h:29
itk::GPUKernelManager::LaunchKernel
bool LaunchKernel(int kernelIdx, int dim, vcl_size_t *globalWorkSize, vcl_size_t *localWorkSize)
itk::GPUKernelManager::SetKernelArgWithChar
bool SetKernelArgWithChar(int kernelIdx, cl_uint argIdx, char argVal)
itk::GPUKernelManager::SetKernelArgWithUChar
bool SetKernelArgWithUChar(int kernelIdx, cl_uint argIdx, unsigned char argVal)
itk::GPUKernelManager::CheckArgumentReady
bool CheckArgumentReady(int kernelIdx)
itkGPUContextManager.h
itkLightObject.h
itk::GPUKernelManager::SetKernelArgWithImageAndBufferedRegion
bool SetKernelArgWithImageAndBufferedRegion(int kernelIdx, cl_uint &argIdx, TGPUImageDataManager *manager)
Definition: itkGPUKernelManager.h:120
itkGPUDataManager.h