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 = 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
186
187 int
189
190protected:
193
194 bool
195 CheckArgumentReady(int kernelIdx);
196
197 void
198 ResetArguments(int kernelIdx);
199
200private:
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
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="")