ITK  5.0.0
Insight Segmentation and Registration Toolkit
itkGPUKernelManager.h
Go to the documentation of this file.
1 /*=========================================================================
2 *
3 * Copyright Insight Software Consortium
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 * http://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_ASSIGN(GPUKernelManager);
48 
50  {
51  bool m_IsReady;
53  };
54 
59 
60  itkNewMacro(Self);
61  itkTypeMacro(GPUKernelManager, LightObject);
62 
63  bool LoadProgramFromFile(const char* filename, const char* cPreamble="");
64 
65  bool LoadProgramFromString(const char* cSource, const char* cPreamble="");
66 
67  int CreateKernel(const char* kernelName);
68 
69  cl_int GetKernelWorkGroupInfo(int kernelIdx,
70  cl_kernel_work_group_info paramName,void *value);
71 
72  cl_int GetDeviceInfo(cl_kernel_work_group_info paramName,
73  size_t argSize, void *argValue);
74 
75  bool SetKernelArg(int kernelIdx, cl_uint argIdx, size_t argSize, const void* argVal);
76 
77  bool SetKernelArgWithImage(int kernelIdx, cl_uint argIdx, GPUDataManager::Pointer manager);
78 
80  //template< typename TGPUImageDataManager >
81  //bool SetKernelArgWithImageAndBufferedRegion(int kernelIdx, cl_uint &argIdx,
82  // typename TGPUImageDataManager::Pointer manager);
83  template< typename TGPUImageDataManager >
85  int kernelIdx, cl_uint &argIdx,
86  TGPUImageDataManager *manager)
87  {
88  if(kernelIdx < 0 || kernelIdx >= (int)m_KernelContainer.size() ) return false;
90 
91  cl_int errid;
92 
93  errid = clSetKernelArg(m_KernelContainer[kernelIdx], argIdx, sizeof(cl_mem),
94  manager->GetGPUBufferPointer() );
95  OpenCLCheckError(errid, __FILE__, __LINE__, ITK_LOCATION);
96 
97  m_KernelArgumentReady[kernelIdx][argIdx].m_IsReady = true;
98  m_KernelArgumentReady[kernelIdx][argIdx].m_GPUDataManager = manager;
99  argIdx++;
100 
101  //this->SetKernelArg(kernelIdx, argIdx++, sizeof(int), &(TGPUImageDataManager::ImageDimension) );
102 
103  //the starting index for the buffered region
104  errid = clSetKernelArg(m_KernelContainer[kernelIdx], argIdx, sizeof(cl_mem),
105  manager->GetGPUBufferedRegionIndex()->GetGPUBufferPointer() );
106  OpenCLCheckError(errid, __FILE__, __LINE__, ITK_LOCATION);
107 
108  m_KernelArgumentReady[kernelIdx][argIdx].m_IsReady = true;
109  m_KernelArgumentReady[kernelIdx][argIdx].m_GPUDataManager = manager->GetGPUBufferedRegionIndex();
110  argIdx++;
111 
112  //the size for the buffered region
113  errid = clSetKernelArg(m_KernelContainer[kernelIdx], argIdx, sizeof(cl_mem),
114  manager->GetGPUBufferedRegionSize()->GetGPUBufferPointer() );
115  OpenCLCheckError(errid, __FILE__, __LINE__, ITK_LOCATION);
116 
117  m_KernelArgumentReady[kernelIdx][argIdx].m_IsReady = true;
118  m_KernelArgumentReady[kernelIdx][argIdx].m_GPUDataManager = manager->GetGPUBufferedRegionSize();
119  argIdx++;
120 
121  return true;
122  }
123 
124  bool LaunchKernel(int kernelIdx, int dim, size_t *globalWorkSize, size_t *localWorkSize);
125 
126  bool LaunchKernel1D(int kernelIdx, size_t globalWorkSize, size_t localWorkSize);
127 
128  bool LaunchKernel2D(int kernelIdx,
129  size_t globalWorkSizeX, size_t globalWorkSizeY,
130  size_t localWorkSizeX, size_t localWorkSizeY );
131 
132  bool LaunchKernel3D(int kernelIdx,
133  size_t globalWorkSizeX, size_t globalWorkSizeY, size_t globalWorkSizeZ,
134  size_t localWorkSizeX, size_t localWorkSizeY, size_t localWorkSizeZ );
135 
136  void SetCurrentCommandQueue( int queueid );
137 
139 
140 protected:
142  ~GPUKernelManager() override;
143 
144  bool CheckArgumentReady(int kernelIdx);
145 
146  void ResetArguments(int kernelIdx);
147 
148 private:
149  cl_program m_Program;
150 
153 
154  std::vector< cl_kernel > m_KernelContainer;
155  std::vector< std::vector< KernelArgumentList > > m_KernelArgumentReady;
156 };
157 }
158 
159 #endif
bool SetKernelArgWithImage(int kernelIdx, cl_uint argIdx, GPUDataManager::Pointer manager)
Light weight base class for most itk classes.
bool CheckArgumentReady(int kernelIdx)
std::vector< std::vector< KernelArgumentList > > m_KernelArgumentReady
bool LoadProgramFromFile(const char *filename, const char *cPreamble="")
int CreateKernel(const char *kernelName)
bool SetKernelArg(int kernelIdx, cl_uint argIdx, vcl_size_t argSize, const void *argVal)
std::vector< cl_kernel > m_KernelContainer
bool LoadProgramFromString(const char *cSource, const char *cPreamble="")
cl_int GetDeviceInfo(cl_kernel_work_group_info paramName, vcl_size_t argSize, void *argValue)
GPU kernel manager implemented using OpenCL.
~GPUKernelManager() override
cl_int GetKernelWorkGroupInfo(int kernelIdx, cl_kernel_work_group_info paramName, void *value)
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)
bool LaunchKernel1D(int kernelIdx, vcl_size_t globalWorkSize, vcl_size_t localWorkSize)
void OpenCLCheckError(cl_int error, const char *filename="", int lineno=0, const char *location="")
GPUContextManager * m_Manager
Singleton class to store the GPU context.
void SetCurrentCommandQueue(int queueid)
void ResetArguments(int kernelIdx)
bool LaunchKernel(int kernelIdx, int dim, vcl_size_t *globalWorkSize, vcl_size_t *localWorkSize)
bool LaunchKernel2D(int kernelIdx, vcl_size_t globalWorkSizeX, vcl_size_t globalWorkSizeY, vcl_size_t localWorkSizeX, vcl_size_t localWorkSizeY)
bool SetKernelArgWithImageAndBufferedRegion(int kernelIdx, cl_uint &argIdx, TGPUImageDataManager *manager)