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"
32#include "itkGPUDataManager.h"
33
34namespace itk
35{
45{
46public:
47 ITK_DISALLOW_COPY_AND_MOVE(GPUKernelManager);
48
50 {
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
188
189 int
191
192protected:
195
196 bool
197 CheckArgumentReady(int kernelIdx);
198
199 void
200 ResetArguments(int kernelIdx);
201
202private:
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
Singleton class to store the GPU context.
GPU memory manager implemented using OpenCL. Required by GPUImage class.
GPU kernel manager implemented using OpenCL.
~GPUKernelManager() override
bool SetKernelArgWithLongLong(int kernelIdx, cl_uint argIdx, long long argVal)
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)
cl_int GetKernelWorkGroupInfo(int kernelIdx, cl_kernel_work_group_info paramName, void *value)
bool SetKernelArgWithULongLong(int kernelIdx, cl_uint argIdx, unsigned long long argVal)
bool SetKernelArgWithChar(int kernelIdx, cl_uint argIdx, char argVal)
bool LaunchKernel2D(int kernelIdx, vcl_size_t globalWorkSizeX, vcl_size_t globalWorkSizeY, vcl_size_t localWorkSizeX, vcl_size_t localWorkSizeY)
bool SetKernelArgWithUShort(int kernelIdx, cl_uint argIdx, unsigned short argVal)
bool SetKernelArgWithFloat(int kernelIdx, cl_uint argIdx, float argVal)
bool LoadProgramFromString(const char *cSource, const char *cPreamble="")
bool SetKernelArg(int kernelIdx, cl_uint argIdx, vcl_size_t argSize, const void *argVal)
void ResetArguments(int kernelIdx)
int GetCurrentCommandQueueID() const
void SetCurrentCommandQueue(int queueid)
bool SetKernelArgWithShort(int kernelIdx, cl_uint argIdx, short argVal)
bool SetKernelArgWithDouble(int kernelIdx, cl_uint argIdx, double argVal)
bool LaunchKernel1D(int kernelIdx, vcl_size_t globalWorkSize, vcl_size_t localWorkSize)
bool SetKernelArgWithInt(int kernelIdx, cl_uint argIdx, int argVal)
bool SetTypedKernelArg(int kernelIdx, cl_uint argIdx, TArg argVal)
std::vector< cl_kernel > m_KernelContainer
bool LaunchKernel(int kernelIdx, int dim, vcl_size_t *globalWorkSize, vcl_size_t *localWorkSize)
cl_int GetDeviceInfo(cl_kernel_work_group_info paramName, vcl_size_t argSize, void *argValue)
GPUContextManager * m_Manager
bool SetKernelArgWithImageAndBufferedRegion(int kernelIdx, cl_uint &argIdx, TGPUImageDataManager *manager)
int CreateKernel(const char *kernelName)
bool LoadProgramFromFile(const char *filename, const char *cPreamble="")
bool SetKernelArgWithUInt(int kernelIdx, cl_uint argIdx, unsigned int argVal)
bool SetKernelArgWithUChar(int kernelIdx, cl_uint argIdx, unsigned char argVal)
std::vector< std::vector< KernelArgumentList > > m_KernelArgumentReady
bool CheckArgumentReady(int kernelIdx)
bool SetKernelArgWithImage(int kernelIdx, cl_uint argIdx, GPUDataManager *manager)
Light weight base class for most itk classes.
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
void OpenCLCheckError(cl_int error, const char *filename="", int lineno=0, const char *location="")