36 : m_ClCompilerFlags(
""),
38 m_CommandQue(nullptr),
39 m_FilterID(
"mitkOclFilter"),
75 clErr = clEnqueueNDRangeKernel( this->
m_CommandQue, kernel, workSizeDim,
80 return ( clErr == CL_SUCCESS );
85 size_t offset[3] ={0, 0, 0};
90 for(offset[0] = 0; offset[0] <
m_GlobalWorkSize[0]; offset[0] += chunksDim[0])
92 for(offset[1] = 0; offset[1] < m_GlobalWorkSize[1]; offset[1] += chunksDim[1])
94 clErr |= clEnqueueNDRangeKernel(this->
m_CommandQue, kernel, workSizeDim,
99 else if(workSizeDim == 3)
101 for(offset[0] = 0; offset[0] <
m_GlobalWorkSize[0]; offset[0] += chunksDim[0])
103 for(offset[1] = 0; offset[1] < m_GlobalWorkSize[1]; offset[1] += chunksDim[1])
105 for(offset[2] = 0; offset[2] < m_GlobalWorkSize[2]; offset[2] += chunksDim[2])
107 clErr |= clEnqueueNDRangeKernel( this->
m_CommandQue, kernel, workSizeDim,
116 return ( clErr == CL_SUCCESS );
121 size_t offset[3] = { 0, 0, 0 };
124 unsigned int currentChunk = 0;
125 cl_event* waitFor =
new cl_event[batchSize];
127 if (workSizeDim == 2)
129 for (offset[0] = 0; offset[0] <
m_GlobalWorkSize[0]; offset[0] += chunksDim[0])
131 for (offset[1] = 0; offset[1] < m_GlobalWorkSize[1]; offset[1] += chunksDim[1])
133 if (currentChunk % batchSize == 0 && currentChunk != 0)
135 clWaitForEvents(batchSize, &waitFor[0]);
136 std::this_thread::sleep_for(std::chrono::milliseconds(waitTimems));
137 clErr |= clEnqueueNDRangeKernel(this->
m_CommandQue, kernel, workSizeDim,
142 clErr |= clEnqueueNDRangeKernel(this->
m_CommandQue, kernel, workSizeDim,
143 offset, chunksDim,
m_LocalWorkSize, 0,
nullptr, &waitFor[currentChunk % batchSize]);
149 else if (workSizeDim == 3)
151 for (offset[0] = 0; offset[0] <
m_GlobalWorkSize[0]; offset[0] += chunksDim[0])
153 for (offset[1] = 0; offset[1] < m_GlobalWorkSize[1]; offset[1] += chunksDim[1])
155 for (offset[2] = 0; offset[2] < m_GlobalWorkSize[2]; offset[2] += chunksDim[2])
157 if (currentChunk % batchSize == 0 && currentChunk != 0)
159 clWaitForEvents(batchSize, &waitFor[0]);
160 std::this_thread::sleep_for(std::chrono::milliseconds(waitTimems));
161 clErr |= clEnqueueNDRangeKernel(this->
m_CommandQue, kernel, workSizeDim,
166 clErr |= clEnqueueNDRangeKernel(this->
m_CommandQue, kernel, workSizeDim,
167 offset, chunksDim,
m_LocalWorkSize, 0,
nullptr, &waitFor[currentChunk % batchSize]);
176 return (clErr == CL_SUCCESS);
192 MITK_ERROR<<
"No OpenCL Source FILE specified";
204 MITK_INFO <<
"Program not stored in resource manager, compiling. " << e;
220 MITK_WARN <<
"Could not load resource: " << mdr.
GetName() <<
" is invalid!";
225 std::istreambuf_iterator<char> eos;
226 std::string source(std::istreambuf_iterator<char>(rss), eos);
234 char* tmp =
new char[src.size() + 1];
235 strcpy(tmp,src.c_str());
238 sourceCode.push_back((
const char*)tmp);
239 sourceCodeSize.push_back(src.size());
252 MITK_ERROR(
"ocl.filter") <<
"No shader source file was set";
260 cl_context gpuContext = resources->
GetContext();
264 if ( !sourceCode.empty() )
267 m_ClProgram = clCreateProgramWithSource(gpuContext, sourceCode.size(), &sourceCode[0], &sourceCodeSize[0], &clErr);
272 std::string compilerOptions =
"";
275 MITK_DEBUG(
"ocl.filter") <<
"cl compiler flags: " << compilerOptions.c_str();
277 clErr = clBuildProgram(
m_ClProgram, 0,
nullptr, compilerOptions.c_str(),
nullptr,
nullptr);
281 if (clErr != CL_SUCCESS)
283 MITK_ERROR(
"ocl.filter") <<
"Failed to build source";
293 for( CStringList::iterator it = sourceCode.begin(); it != sourceCode.end(); ++it )
300 MITK_ERROR(
"ocl.filter") <<
"Could not load from source";
#define CHECK_OCL_ERR(_er)
unsigned int iDivUp(unsigned int dividend, unsigned int divisor)
Method to estimate an integer quotient C from given dividend and divisor higher or equal to the corre...
ServiceReferenceU GetServiceReference(const std::string &clazz)
std::string m_FilterID
Unique ID of the filter, needs to be specified in the constructor of the derived class.
bool m_Initialized
status of the filter
void SetSourcePreambel(const char *preambel)
Add some source code on the beginning of the loaded source.
cl_program m_ClProgram
The compiled OpenCL program.
const char * m_Preambel
source preambel for e.g. #define commands to be inserted into the OpenCL source
bool Initialize()
Initialize all necessary parts of the filter.
vcl_size_t m_GlobalWorkSize[3]
The global work size of the filter.
void oclLogBinary(cl_program clProg, cl_device_id clDev)
Logs the GPU Program binary code.
void CompileSource()
Compile the program source.
virtual bool IsInitialized()
Returns true if the initialization was successfull.
bool ExecuteKernelChunksInBatches(cl_kernel kernel, unsigned int workSizeDim, vcl_size_t *chunksDim, vcl_size_t batchSize, int waitTimems)
Execute the given kernel on the OpenCL Index-Space defined by the local and global work sizes...
bool ExecuteKernelChunks(cl_kernel kernel, unsigned int workSizeDim, vcl_size_t *chunksDim)
Execute the given kernel on the OpenCL Index-Space defined by the local and global work sizes...
std::vector< const char * > CStringList
virtual unsigned long GetDeviceMemory()
Returns the amount of global memory of the used device in bytes.
void * GetService(const ServiceReferenceBase &reference)
virtual ~OclFilter()
Destructor.
An object of this class represents an exception of MITK. Please don't instantiate exceptions manually...
std::vector< vcl_size_t > ClSizeList
ModuleResource GetResource(const std::string &path) const
CStringList m_ClFiles
List of sourcefiles that will be compiled for this filter.
void oclLogBuildInfo(cl_program clProg, cl_device_id clDev)
Shows the OpenCL-Program build info, called if clBuildProgram != CL_SUCCES.
virtual void RemoveProgram(const std::string &name)=0
Remove given program from storage.
void SetCompilerFlags(const char *flags)
Set specific compilerflags to compile the CL source. Default is set to nullptr; example: "-cl-fast-re...
cl_command_queue m_CommandQue
Command queue for the filter.
virtual us::Module * GetModule()=0
Get the Module of the filter. Needs to be implemented by every subclass. The filter will load the Ope...
cl_ulong oclGetGlobalMemSize(cl_device_id device)
Returns the Global memory size of the current device.
vcl_size_t m_LocalWorkSize[3]
The local work size fo the filter.
virtual cl_context GetContext() const =0
Returns a valid OpenCL Context (if applicable) or nullptr if none present.
void SetWorkingSize(unsigned int locx, unsigned int dimx, unsigned int locy=1, unsigned int dimy=1, unsigned int locz=1, unsigned int dimz=1)
Set the working size for the following OpenCL kernel call.
bool ExecuteKernel(cl_kernel kernel, unsigned int workSizeDim)
Execute the given kernel on the OpenCL Index-Space defined by the local and global work sizes...
void LoadSourceFiles(CStringList &SourceCodeList, ClSizeList &SourceCodeSizeList)
Helper functions that load sourcefiles from the module context in the Initialize function.
const char * m_ClCompilerFlags
String that contains the compiler flags.
static ModuleContext * GetModuleContext()
Returns the module context of the calling module.
virtual cl_device_id GetCurrentDevice() const =0
Returns the identifier of an OpenCL device related to the current context.
void AddSourceFile(const char *filename)
Add a source file from the resource files to the OpenCL shader file list. Multiple files can be added...
std::string GetName() const
Declaration of the OpenCL Resources micro-service.