Medical Imaging Interaction Toolkit  2018.4.99-1640525a
Medical Imaging Interaction Toolkit
mitkPhotoacousticOCLUsedLinesCalculation.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 #if defined(PHOTOACOUSTICS_USE_GPU) || DOXYGEN
13 
15 #include "usServiceReference.h"
16 #include "mitkImageReadAccessor.h"
17 
18 mitk::OCLUsedLinesCalculation::OCLUsedLinesCalculation(mitk::BeamformingSettings::Pointer settings)
19  : m_PixelCalculation(NULL),
20  m_Conf(settings)
21 {
22  this->AddSourceFile("UsedLinesCalculation.cl");
23  this->m_FilterID = "UsedLinesCalculation";
24 
25  m_ChunkSize[0] = 128;
26  m_ChunkSize[1] = 128;
27  m_ChunkSize[2] = 8;
28 
29  this->Initialize();
30 }
31 
32 mitk::OCLUsedLinesCalculation::~OCLUsedLinesCalculation()
33 {
34  if (this->m_PixelCalculation)
35  {
36  clReleaseKernel(m_PixelCalculation);
37  }
38 }
39 
40 void mitk::OCLUsedLinesCalculation::SetElementHeightsBuffer(cl_mem elementHeightsBuffer)
41 {
42  m_ElementHeightsBuffer = elementHeightsBuffer;
43 }
44 
45 void mitk::OCLUsedLinesCalculation::SetElementPositionsBuffer(cl_mem elementPositionsBuffer)
46 {
47  m_ElementPositionsBuffer = elementPositionsBuffer;
48 }
49 
51 {
52  //Check if context & program available
53  if (!this->Initialize())
54  {
57 
58  // clean-up also the resources
59  resources->InvalidateStorage();
60  mitkThrow() << "Filter is not initialized. Cannot update.";
61  }
62  else {
63  // Execute
64  this->Execute();
65  }
66 }
67 
68 void mitk::OCLUsedLinesCalculation::Execute()
69 {
70  cl_int clErr = 0;
71 
72  unsigned int gridDim[3] = { m_Conf->GetReconstructionLines(), m_Conf->GetSamplesPerLine(), 1 };
73  size_t outputSize = gridDim[0] * gridDim[1] * 3;
74 
75  try
76  {
77  this->InitExecNoInput(this->m_PixelCalculation, gridDim, outputSize, sizeof(unsigned short));
78  }
79  catch (const mitk::Exception& e)
80  {
81  MITK_ERROR << "Caught exception while initializing UsedLines filter: " << e.what();
82  return;
83  }
84 
85  if (m_Conf->GetGeometry() == mitk::BeamformingSettings::ProbeGeometry::Linear)
86  {
87  // This calculation is the same for all kernels, so for performance reasons simply perform it here instead of within the kernels
88  m_part = (tan(m_Conf->GetAngle() / 360 * 2 * itk::Math::pi) *
89  ((m_Conf->GetSpeedOfSound() * m_Conf->GetTimeSpacing())) /
90  (m_Conf->GetPitchInMeters() * m_Conf->GetTransducerElements())) * m_Conf->GetInputDim()[0];
91  unsigned int reconLines = this->m_Conf->GetReconstructionLines();
92  unsigned int samplesPerLine = this->m_Conf->GetSamplesPerLine();
93 
94  float totalSamples_i = (float)(m_Conf->GetReconstructionDepth()) / (float)(m_Conf->GetSpeedOfSound() * m_Conf->GetTimeSpacing());
95  totalSamples_i = totalSamples_i <= m_Conf->GetInputDim()[1] ? totalSamples_i : m_Conf->GetInputDim()[1];
96  clErr = clSetKernelArg(this->m_PixelCalculation, 1, sizeof(cl_float), &(this->m_part));
97  clErr |= clSetKernelArg(this->m_PixelCalculation, 2, sizeof(cl_uint), &(this->m_Conf->GetInputDim()[0]));
98  clErr |= clSetKernelArg(this->m_PixelCalculation, 3, sizeof(cl_uint), &(this->m_Conf->GetInputDim()[1]));
99  clErr |= clSetKernelArg(this->m_PixelCalculation, 4, sizeof(cl_uint), &(reconLines));
100  clErr |= clSetKernelArg(this->m_PixelCalculation, 5, sizeof(cl_uint), &(samplesPerLine));
101  clErr |= clSetKernelArg(this->m_PixelCalculation, 6, sizeof(cl_float), &(totalSamples_i));
102  }
103  else
104  {
105  unsigned int reconLines = this->m_Conf->GetReconstructionLines();
106  unsigned int samplesPerLine = this->m_Conf->GetSamplesPerLine();
107 
108  float probeRadius = m_Conf->GetProbeRadius();
109  float sin_deg = std::sin(m_Conf->GetAngle() / 360 * 2 * itk::Math::pi);
110 
111  float horizontalExtent = m_Conf->GetHorizontalExtent();
112  float verticalExtent = m_Conf->GetReconstructionDepth();
113 
114  clErr = clSetKernelArg(this->m_PixelCalculation, 1, sizeof(cl_mem), &(this->m_ElementHeightsBuffer));
115  clErr |= clSetKernelArg(this->m_PixelCalculation, 2, sizeof(cl_mem), &(this->m_ElementPositionsBuffer));
116  clErr |= clSetKernelArg(this->m_PixelCalculation, 3, sizeof(cl_float), &(sin_deg));
117  clErr |= clSetKernelArg(this->m_PixelCalculation, 4, sizeof(cl_float), &(probeRadius));
118  clErr |= clSetKernelArg(this->m_PixelCalculation, 5, sizeof(cl_uint), &(this->m_Conf->GetInputDim()[0]));
119  clErr |= clSetKernelArg(this->m_PixelCalculation, 6, sizeof(cl_uint), &(this->m_Conf->GetInputDim()[1]));
120  clErr |= clSetKernelArg(this->m_PixelCalculation, 7, sizeof(cl_uint), &(reconLines));
121  clErr |= clSetKernelArg(this->m_PixelCalculation, 8, sizeof(cl_uint), &(samplesPerLine));
122  clErr |= clSetKernelArg(this->m_PixelCalculation, 9, sizeof(cl_float), &(horizontalExtent));
123  clErr |= clSetKernelArg(this->m_PixelCalculation, 10, sizeof(cl_float), &(verticalExtent));
124  }
125 
126  CHECK_OCL_ERR(clErr);
127 
128  // execute the filter on a 2D NDRange
129  if (!this->ExecuteKernelChunksInBatches(m_PixelCalculation, 2, m_ChunkSize, 16, 50))
130  mitkThrow() << "openCL Error when executing Kernel";
131 
132  // signalize the GPU-side data changed
133  m_Output->Modified(GPU_DATA);
134 }
135 
136 us::Module *mitk::OCLUsedLinesCalculation::GetModule()
137 {
138  return us::GetModuleContext()->GetModule();
139 }
140 
141 bool mitk::OCLUsedLinesCalculation::Initialize()
142 {
143  bool buildErr = true;
144  cl_int clErr = 0;
145 
146  if (OclFilter::Initialize())
147  {
148  if (m_Conf->GetGeometry() == mitk::BeamformingSettings::ProbeGeometry::Linear)
149  {
150  this->m_PixelCalculation = clCreateKernel(this->m_ClProgram, "ckUsedLines", &clErr);
151  buildErr |= CHECK_OCL_ERR(clErr);
152  }
153  else
154  {
155  this->m_PixelCalculation = clCreateKernel(this->m_ClProgram, "ckUsedLines_g", &clErr);
156  buildErr |= CHECK_OCL_ERR(clErr);
157  }
158  }
159  return (OclFilter::IsInitialized() && buildErr);
160 }
161 #endif
#define CHECK_OCL_ERR(_er)
Definition: mitkOclUtils.h:21
virtual void InvalidateStorage()=0
Remove all invalid (=do not compile) programs from the internal storage.
ServiceReferenceU GetServiceReference(const std::string &clazz)
#define GPU_DATA
#define MITK_ERROR
Definition: mitkLogMacros.h:20
static void Update(vtkPolyData *)
Definition: mitkSurface.cpp:31
void * GetService(const ServiceReferenceBase &reference)
An object of this class represents an exception of MITK. Please don&#39;t instantiate exceptions manually...
Definition: mitkException.h:45
#define mitkThrow()
Module * GetModule() const
static ModuleContext * GetModuleContext()
Returns the module context of the calling module.
Declaration of the OpenCL Resources micro-service.