ITK  4.8.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 
49  {
50  bool m_IsReady;
52  };
53 
58 
59  itkNewMacro(Self);
60  itkTypeMacro(GPUKernelManager, LightObject);
61 
62  bool LoadProgramFromFile(const char* filename, const char* cPreamble="");
63 
64  bool LoadProgramFromString(const char* cSource, const char* cPreamble="");
65 
66  int CreateKernel(const char* kernelName);
67 
68  cl_int GetKernelWorkGroupInfo(int kernelIdx,
69  cl_kernel_work_group_info paramName,void *value);
70 
71  cl_int GetDeviceInfo(cl_kernel_work_group_info paramName,
72  size_t argSize, void *argValue);
73 
74  bool SetKernelArg(int kernelIdx, cl_uint argIdx, size_t argSize, const void* argVal);
75 
76  bool SetKernelArgWithImage(int kernelIdx, cl_uint argIdx, GPUDataManager::Pointer manager);
77 
79  //template< typename TGPUImageDataManager >
80  //bool SetKernelArgWithImageAndBufferedRegion(int kernelIdx, cl_uint &argIdx,
81  // typename TGPUImageDataManager::Pointer manager);
82  template< typename TGPUImageDataManager >
84  int kernelIdx, cl_uint &argIdx,
85  TGPUImageDataManager *manager)
86  {
87  if(kernelIdx < 0 || kernelIdx >= (int)m_KernelContainer.size() ) return false;
89 
90  cl_int errid;
91 
92  errid = clSetKernelArg(m_KernelContainer[kernelIdx], argIdx, sizeof(cl_mem),
93  manager->GetGPUBufferPointer() );
94  OpenCLCheckError(errid, __FILE__, __LINE__, ITK_LOCATION);
95 
96  m_KernelArgumentReady[kernelIdx][argIdx].m_IsReady = true;
97  m_KernelArgumentReady[kernelIdx][argIdx].m_GPUDataManager = manager;
98  argIdx++;
99 
100  //this->SetKernelArg(kernelIdx, argIdx++, sizeof(int), &(TGPUImageDataManager::ImageDimension) );
101 
102  //the starting index for the buffered region
103  errid = clSetKernelArg(m_KernelContainer[kernelIdx], argIdx, sizeof(cl_mem),
104  manager->GetGPUBufferedRegionIndex()->GetGPUBufferPointer() );
105  OpenCLCheckError(errid, __FILE__, __LINE__, ITK_LOCATION);
106 
107  m_KernelArgumentReady[kernelIdx][argIdx].m_IsReady = true;
108  m_KernelArgumentReady[kernelIdx][argIdx].m_GPUDataManager = manager->GetGPUBufferedRegionIndex();
109  argIdx++;
110 
111  //the size for the buffered region
112  errid = clSetKernelArg(m_KernelContainer[kernelIdx], argIdx, sizeof(cl_mem),
113  manager->GetGPUBufferedRegionSize()->GetGPUBufferPointer() );
114  OpenCLCheckError(errid, __FILE__, __LINE__, ITK_LOCATION);
115 
116  m_KernelArgumentReady[kernelIdx][argIdx].m_IsReady = true;
117  m_KernelArgumentReady[kernelIdx][argIdx].m_GPUDataManager = manager->GetGPUBufferedRegionSize();
118  argIdx++;
119 
120  return true;
121  }
122 
123  bool LaunchKernel(int kernelIdx, int dim, size_t *globalWorkSize, size_t *localWorkSize);
124 
125  bool LaunchKernel1D(int kernelIdx, size_t globalWorkSize, size_t localWorkSize);
126 
127  bool LaunchKernel2D(int kernelIdx,
128  size_t globalWorkSizeX, size_t globalWorkSizeY,
129  size_t localWorkSizeX, size_t localWorkSizeY );
130 
131  bool LaunchKernel3D(int kernelIdx,
132  size_t globalWorkSizeX, size_t globalWorkSizeY, size_t globalWorkSizeZ,
133  size_t localWorkSizeX, size_t localWorkSizeY, size_t localWorkSizeZ );
134 
135  void SetCurrentCommandQueue( int queueid );
136 
138 
139 protected:
141  virtual ~GPUKernelManager();
142 
143  bool CheckArgumentReady(int kernelIdx);
144 
145  void ResetArguments(int kernelIdx);
146 
147 private:
148  GPUKernelManager(const Self&); //purposely not implemented
149  void operator=(const Self&);
150 
151  cl_program m_Program;
152 
155 
156  std::vector< cl_kernel > m_KernelContainer;
157  std::vector< std::vector< KernelArgumentList > > m_KernelArgumentReady;
158 };
159 }
160 
161 #endif
virtual ~GPUKernelManager()
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="")
SmartPointer< Self > Pointer
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="")
void operator=(const Self &)
cl_int GetDeviceInfo(cl_kernel_work_group_info paramName, vcl_size_t argSize, void *argValue)
GPU kernel manager implemented using OpenCL.
SmartPointer< const Self > ConstPointer
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)