Medical Imaging Interaction Toolkit  2018.4.99-1640525a
Medical Imaging Interaction Toolkit
mitkPhotoacousticOCLDelayCalculation.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 
13 #define _USE_MATH_DEFINES
14 
15 #include <cmath>
17 #include "usServiceReference.h"
18 #include "mitkImageReadAccessor.h"
19 
20 mitk::OCLDelayCalculation::OCLDelayCalculation(mitk::BeamformingSettings::Pointer settings)
21  : m_PixelCalculation(NULL),
22  m_Conf(settings)
23 {
24  this->AddSourceFile("DelayCalculation.cl");
25  this->m_FilterID = "DelayCalculation";
26 
27  m_ChunkSize[0] = 128;
28  m_ChunkSize[1] = 128;
29  m_ChunkSize[2] = 8;
30 
31  this->Initialize();
32 }
33 
34 mitk::OCLDelayCalculation::~OCLDelayCalculation()
35 {
36  if (this->m_PixelCalculation)
37  {
38  clReleaseKernel(m_PixelCalculation);
39  }
40 }
41 
43 {
44  //Check if context & program available
45  if (!this->Initialize())
46  {
49 
50  // clean-up also the resources
51  resources->InvalidateStorage();
52  mitkThrow() << "Filter is not initialized. Cannot update.";
53  }
54  else {
55  // Execute
56  this->Execute();
57  }
58 }
59 
60 void mitk::OCLDelayCalculation::Execute()
61 {
62  cl_int clErr = 0;
63 
64  unsigned int gridDim[3] = { m_Conf->GetReconstructionLines() / 2, m_Conf->GetSamplesPerLine(), 1 };
65  m_BufferSize = gridDim[0] * gridDim[1] * 1;
66 
67  try
68  {
69  this->InitExecNoInput(this->m_PixelCalculation, gridDim, m_BufferSize, sizeof(unsigned short));
70  }
71  catch (const mitk::Exception& e)
72  {
73  MITK_ERROR << "Caught exception while initializing Delay Calculation filter: " << e.what();
74  return;
75  }
76 
77  // This calculation is the same for all kernels, so for performance reasons simply perform it here instead of within the kernels
78  m_DelayMultiplicatorRaw = 1 / (m_Conf->GetTimeSpacing()*m_Conf->GetSpeedOfSound()) *
79  (m_Conf->GetPitchInMeters()*(float)m_Conf->GetTransducerElements());
80 
81  // as openCL does not support bool as a kernel argument, we need to buffer this value in a char...
82  m_IsPAImage = m_Conf->GetIsPhotoacousticImage();
83 
84  unsigned int reconstructionLines = this->m_Conf->GetReconstructionLines();
85  unsigned int samplesperLine = this->m_Conf->GetSamplesPerLine();
86 
87  float totalSamples_i = (float)(m_Conf->GetReconstructionDepth()) / (float)(m_Conf->GetSpeedOfSound() * m_Conf->GetTimeSpacing());
88  totalSamples_i = totalSamples_i <= m_Conf->GetInputDim()[1] ? totalSamples_i : m_Conf->GetInputDim()[1];
89 
90  clErr = clSetKernelArg(this->m_PixelCalculation, 1, sizeof(cl_mem), &(this->m_UsedLines));
91  clErr |= clSetKernelArg(this->m_PixelCalculation, 2, sizeof(cl_uint), &(this->m_Conf->GetInputDim()[0]));
92  clErr |= clSetKernelArg(this->m_PixelCalculation, 3, sizeof(cl_uint), &(this->m_Conf->GetInputDim()[1]));
93  clErr |= clSetKernelArg(this->m_PixelCalculation, 4, sizeof(cl_uint), &(reconstructionLines));
94  clErr |= clSetKernelArg(this->m_PixelCalculation, 5, sizeof(cl_uint), &(samplesperLine));
95  clErr |= clSetKernelArg(this->m_PixelCalculation, 6, sizeof(cl_char), &(this->m_IsPAImage));
96  clErr |= clSetKernelArg(this->m_PixelCalculation, 7, sizeof(cl_float), &(this->m_DelayMultiplicatorRaw));
97  clErr |= clSetKernelArg(this->m_PixelCalculation, 8, sizeof(cl_float), &(totalSamples_i));
98 
99  CHECK_OCL_ERR(clErr);
100 
101  // execute the filter on a 3D NDRange
102  if (!this->ExecuteKernelChunksInBatches(m_PixelCalculation, 2, m_ChunkSize, 16, 50))
103  mitkThrow() << "openCL Error when executing Kernel";
104  // signalize the GPU-side data changed
105  m_Output->Modified(GPU_DATA);
106 }
107 
108 us::Module *mitk::OCLDelayCalculation::GetModule()
109 {
110  return us::GetModuleContext()->GetModule();
111 }
112 
113 bool mitk::OCLDelayCalculation::Initialize()
114 {
115  bool buildErr = true;
116  cl_int clErr = 0;
117 
118  if (OclFilter::Initialize())
119  {
120  this->m_PixelCalculation = clCreateKernel(this->m_ClProgram, "ckDelayCalculationSphe", &clErr);
121  buildErr |= CHECK_OCL_ERR(clErr);
122  }
123  return (OclFilter::IsInitialized() && buildErr);
124 }
#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.