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 = clSetKernelArg(m_KernelContainer[kernelIdx], argIdx, sizeof(cl_mem), manager->GetGPUBufferPointer());
129  OpenCLCheckError(errid, __FILE__, __LINE__, ITK_LOCATION);
130 
131  m_KernelArgumentReady[kernelIdx][argIdx].m_IsReady = true;
132  m_KernelArgumentReady[kernelIdx][argIdx].m_GPUDataManager = manager;
133  ++argIdx;
134 
135  // this->SetKernelArg(kernelIdx, argIdx++, sizeof(int), &(TGPUImageDataManager::ImageDimension) );
136 
137  // the starting index for the buffered region
138  errid = clSetKernelArg(m_KernelContainer[kernelIdx],
139  argIdx,
140  sizeof(cl_mem),
141  manager->GetModifiableGPUBufferedRegionIndex()->GetGPUBufferPointer());
142  OpenCLCheckError(errid, __FILE__, __LINE__, ITK_LOCATION);
143 
144  m_KernelArgumentReady[kernelIdx][argIdx].m_IsReady = true;
145  m_KernelArgumentReady[kernelIdx][argIdx].m_GPUDataManager = manager->GetModifiableGPUBufferedRegionIndex();
146  ++argIdx;
147 
148  // the size for the buffered region
149  errid = clSetKernelArg(m_KernelContainer[kernelIdx],
150  argIdx,
151  sizeof(cl_mem),
152  manager->GetModifiableGPUBufferedRegionSize()->GetGPUBufferPointer());
153  OpenCLCheckError(errid, __FILE__, __LINE__, ITK_LOCATION);
154 
155  m_KernelArgumentReady[kernelIdx][argIdx].m_IsReady = true;
156  m_KernelArgumentReady[kernelIdx][argIdx].m_GPUDataManager = manager->GetModifiableGPUBufferedRegionSize();
157  ++argIdx;
158 
159  return true;
160  }
161 
162  bool
163  LaunchKernel(int kernelIdx, int dim, size_t * globalWorkSize, size_t * localWorkSize);
164 
165  bool
166  LaunchKernel1D(int kernelIdx, size_t globalWorkSize, size_t localWorkSize);
167 
168  bool
169  LaunchKernel2D(int kernelIdx,
170  size_t globalWorkSizeX,
171  size_t globalWorkSizeY,
172  size_t localWorkSizeX,
173  size_t localWorkSizeY);
174 
175  bool
176  LaunchKernel3D(int kernelIdx,
177  size_t globalWorkSizeX,
178  size_t globalWorkSizeY,
179  size_t globalWorkSizeZ,
180  size_t localWorkSizeX,
181  size_t localWorkSizeY,
182  size_t localWorkSizeZ);
183 
184  void
185  SetCurrentCommandQueue(int queueid);
186 
187  int
188  GetCurrentCommandQueueID() const;
189 
190 protected:
192  ~GPUKernelManager() override;
193 
194  bool
195  CheckArgumentReady(int kernelIdx);
196 
197  void
198  ResetArguments(int kernelIdx);
199 
200 private:
201  cl_program m_Program{};
202 
205 
206  std::vector<cl_kernel> m_KernelContainer{};
207  std::vector<std::vector<KernelArgumentList>> m_KernelArgumentReady{};
208 
209  template <typename TArg>
210  bool
211  SetTypedKernelArg(int kernelIdx, cl_uint argIdx, TArg argVal);
212 };
213 } // namespace itk
214 
215 #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:201
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:207
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:204
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:206
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:203
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