ITK  4.4.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 class ITK_EXPORT GPUKernelManager : public LightObject
46 {
47 public:
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< class TGPUImageDataManager >
81  //bool SetKernelArgWithImageAndBufferedRegion(int kernelIdx, cl_uint &argIdx,
82  // typename TGPUImageDataManager::Pointer manager);
83  template< class TGPUImageDataManager >
84  bool SetKernelArgWithImageAndBufferedRegion(
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 
138  int GetCurrentCommandQueueID();
139 
140 protected:
142  virtual ~GPUKernelManager() {
143  }
144 
145  bool CheckArgumentReady(int kernelIdx);
146 
147  void ResetArguments(int kernelIdx);
148 
149 private:
150  GPUKernelManager(const Self&); //purposely not implemented
151  void operator=(const Self&);
152 
153  cl_program m_Program;
154 
157 
158  std::vector< cl_kernel > m_KernelContainer;
159  std::vector< std::vector< KernelArgumentList > > m_KernelArgumentReady;
160 };
161 }
162 
163 #endif
164