Medical Imaging Interaction Toolkit  2018.4.99-389bf124
Medical Imaging Interaction Toolkit
mitkOclDataSetToDataSetFilter.cpp
Go to the documentation of this file.
1 /*============================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 
14 #include "mitkOclDataSet.h"
15 
16 #include "mitkException.h"
17 
19 {
21 }
22 
24 {
25 }
26 
28 {
29  return this->m_Output;
30 }
31 
33 {
34  void* pData = m_Output->TransferDataToCPU(m_CommandQue);
35  return pData;
36 }
37 
39 {
40  return this->m_CurrentSizeOutput;
41 }
42 
43 bool mitk::OclDataSetToDataSetFilter::InitExec(cl_kernel ckKernel, unsigned int* dimensions, size_t outputDataSize, unsigned int outputBpE)
44 {
45  cl_int clErr = 0;
46 
47  if (m_Input.IsNull())
48  mitkThrow() << "Input DataSet is null.";
49 
50  // get DataSet size once
51  const unsigned int uiDataSetWidth = dimensions[0];
52  const unsigned int uiDataSetHeight = dimensions[1];
53  const unsigned int uiDataSetDepth = dimensions[2];
54 
55  // compute work sizes
56  this->SetWorkingSize(8, uiDataSetWidth, 8, uiDataSetHeight, 8, uiDataSetDepth);
57 
58  cl_mem clBuffIn = m_Input->GetGPUBuffer();
59  cl_mem clBuffOut = m_Output->GetGPUBuffer();
60 
61  if (!clBuffIn)
62  {
63  if (m_Input->TransferDataToGPU(m_CommandQue) != CL_SUCCESS)
64  {
65  mitkThrow() << "Could not create / initialize gpu DataSet.";
66  }
67 
68  clBuffIn = m_Input->GetGPUBuffer();
69  }
70 
71  // output DataSet not initialized or output buffer size changed
72  if (!clBuffOut || (size_t)m_Output->GetBufferSize() != outputDataSize)
73  {
74  MITK_DEBUG << "Create GPU DataSet call " << uiDataSetWidth << "x" << uiDataSetHeight << "x" << uiDataSetDepth;
75  MITK_INFO << "Create GPU Buffer of size " << outputDataSize * outputBpE / 1024.f / 1024.f << "MB";
76  m_Output->SetBpE(outputBpE);
77  m_Output->SetBufferSize(outputDataSize);
78  clBuffOut = m_Output->CreateGPUBuffer();
79  m_CurrentSizeOutput = outputBpE;
80  }
81 
82  clErr = 0;
83  clErr = clSetKernelArg(ckKernel, 0, sizeof(cl_mem), &clBuffIn);
84  clErr |= clSetKernelArg(ckKernel, 1, sizeof(cl_mem), &clBuffOut);
85  CHECK_OCL_ERR(clErr);
86 
87  if (clErr != CL_SUCCESS)
88  mitkThrow() << "OpenCL Part initialization failed with " << GetOclErrorAsString(clErr);
89 
90  return(clErr == CL_SUCCESS);
91 }
92 
93 bool mitk::OclDataSetToDataSetFilter::InitExecNoInput(cl_kernel ckKernel, unsigned int* dimensions, size_t outputDataSize, unsigned int outputBpE)
94 {
95  cl_int clErr = 0;
96 
97  // get DataSet size once
98  const unsigned int uiDataSetWidth = dimensions[0];
99  const unsigned int uiDataSetHeight = dimensions[1];
100  const unsigned int uiDataSetDepth = dimensions[2];
101 
102  // compute work sizes
103  this->SetWorkingSize(8, uiDataSetWidth, 8, uiDataSetHeight, 8, uiDataSetDepth);
104 
105  cl_mem clBuffOut = m_Output->GetGPUBuffer();
106 
107  // output DataSet not initialized or output buffer size changed
108  if (!clBuffOut || (size_t)m_Output->GetBufferSize() != outputDataSize)
109  {
110  MITK_DEBUG << "Create GPU DataSet call " << uiDataSetWidth << "x" << uiDataSetHeight << "x" << uiDataSetDepth;
111  m_Output->SetBpE(outputBpE);
112  m_Output->SetBufferSize(outputDataSize);
113  clBuffOut = m_Output->CreateGPUBuffer();
114  m_CurrentSizeOutput = outputBpE;
115  }
116 
117  clErr = clSetKernelArg(ckKernel, 0, sizeof(cl_mem), &clBuffOut);
118  CHECK_OCL_ERR(clErr);
119 
120  if (clErr != CL_SUCCESS)
121  mitkThrow() << "OpenCL Part initialization failed with " << GetOclErrorAsString(clErr);
122 
123  return(clErr == CL_SUCCESS);
124 }
#define CHECK_OCL_ERR(_er)
Definition: mitkOclUtils.h:21
static Pointer New()
virtual int GetBytesPerElem()
Get the memory size needed for each element.
#define MITK_INFO
Definition: mitkLogMacros.h:18
OclDataSetToDataSetFilter()
OclDataSetToDataSetFilter Default constructor.
#define MITK_DEBUG
Definition: mitkLogMacros.h:22
std::string GetOclErrorAsString(int _clErr)
Returns the name of an OpenCL Error as a string.
mitk::OclDataSet::Pointer m_Input
mitk::OclDataSet::Pointer GetGPUOutput()
Returns a pointer to the graphics memory.
void * GetOutput()
Returns an pointer to the filtered data.
#define mitkThrow()
bool InitExecNoInput(cl_kernel ckKernel, unsigned int *dimensions, vcl_size_t outputDataSize, unsigned int outputBpE)
cl_command_queue m_CommandQue
Command queue for the filter.
Definition: mitkOclFilter.h:83
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 InitExec(cl_kernel ckKernel, unsigned int *dimensions, vcl_size_t outputDataSize, unsigned int outputBpE)
InitExec Initialize the execution.