Medical Imaging Interaction Toolkit  2018.4.99-eed36131
Medical Imaging Interaction Toolkit
mitkOclDataSet.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 #include "mitkOclDataSet.h"
14 #include "mitkCommon.h"
15 #include "mitkLogMacros.h"
16 
17 #include "mitkOclUtils.h"
18 
19 #include <fstream>
20 
21 //#define SHOW_MEM_INFO
22 
23 mitk::OclDataSet::OclDataSet() : m_gpuBuffer(nullptr), m_context(nullptr), m_bufferSize(0), m_gpuModified(false), m_cpuModified(false),
24  m_Data(nullptr), m_BpE(1)
25 {
26 }
27 
29 {
30  MITK_DEBUG << "OclDataSet Destructor";
31 
32  //release GMEM Image buffer
33  if (m_gpuBuffer) clReleaseMemObject(m_gpuBuffer);
34 }
35 
36 
38 {
39  MITK_DEBUG << "InitializeGPUBuffer call with: BPE=" << m_BpE;
40 
43 
44  m_context = resources->GetContext();
45 
46  int clErr;
47  if (m_gpuBuffer) clReleaseMemObject(m_gpuBuffer);
48 
49  m_gpuBuffer = clCreateBuffer(m_context, CL_MEM_READ_WRITE, m_bufferSize * (size_t)m_BpE, nullptr, &clErr);
50 
51  #ifdef SHOW_MEM_INFO
52  MITK_INFO << "Created GPU Buffer Object of size: " << (size_t)m_BpE * m_bufferSize << " Bytes";
53  #endif
54 
55  CHECK_OCL_ERR(clErr);
56 
57  if (clErr != CL_SUCCESS)
58  mitkThrow() << "openCL Error when creating Buffer";
59 
60  return m_gpuBuffer;
61 }
62 
64 {
65  if (_type) return m_cpuModified;
66  else return m_gpuModified;
67 }
68 
70 {
71  // defines... GPU: 0, CPU: 1
72  m_cpuModified = _type;
73  m_gpuModified = !_type;
74 }
75 
76 int mitk::OclDataSet::TransferDataToGPU(cl_command_queue gpuComQueue)
77 {
78  cl_int clErr = 0;
79 
80  // check whether an image present
81  if (m_Data == nullptr){
82  MITK_ERROR("ocl.DataSet") << "(mitk) No data present!\n";
83  return -1;
84  }
85 
86  // there is a need for copy only if RAM-Data newer then GMEM data
87  if (m_cpuModified)
88  {
89  //check the buffer
90  if(m_gpuBuffer == nullptr)
91  {
93  }
94 
95  if (m_gpuBuffer != nullptr)
96  {
97  clErr = clEnqueueWriteBuffer(gpuComQueue, m_gpuBuffer, CL_TRUE, 0, m_bufferSize * (size_t)m_BpE, m_Data, 0, NULL, NULL);
98  MITK_DEBUG << "Wrote Data to GPU Buffer Object.";
99 
100  CHECK_OCL_ERR(clErr);
101  m_gpuModified = true;
102 
103  if (clErr != CL_SUCCESS)
104  mitkThrow() << "openCL Error when writing Buffer";
105  }
106  else
107  {
108  MITK_ERROR << "No GPU buffer present!";
109  }
110  }
111 
112  return clErr;
113 }
114 
116 {
117  // query image object info only if already initialized
118  if( this->m_gpuBuffer )
119  {
120  #ifdef SHOW_MEM_INFO
121  cl_int clErr = 0;
122  // clGetMemObjectInfo()
123  cl_mem_object_type memInfo;
124  clErr = clGetMemObjectInfo(this->m_gpuBuffer, CL_MEM_TYPE, sizeof(cl_mem_object_type), &memInfo, nullptr );
125  CHECK_OCL_ERR(clErr);
126  MITK_DEBUG << "Querying info for object, recieving: " << memInfo;
127  #endif
128  }
129 
130  return m_gpuBuffer;
131 }
132 
133 void* mitk::OclDataSet::TransferDataToCPU(cl_command_queue gpuComQueue)
134 {
135  cl_int clErr = 0;
136 
137  // if image created on GPU, needs to create mitk::Image
138  if( m_gpuBuffer == nullptr ){
139  MITK_ERROR("ocl.DataSet") << "(mitk) No buffer present!\n";
140  return nullptr;
141  }
142 
143  // check buffersize
144  char* data = new char[m_bufferSize * (size_t)m_BpE];
145 
146  // debug info
147  #ifdef SHOW_MEM_INFO
149  #endif
150 
151  clErr = clEnqueueReadBuffer( gpuComQueue, m_gpuBuffer, CL_TRUE, 0, m_bufferSize * (size_t)m_BpE, data ,0, nullptr, nullptr);
152  CHECK_OCL_ERR(clErr);
153 
154  if(clErr != CL_SUCCESS)
155  mitkThrow() << "openCL Error when reading Output Buffer";
156 
157  clFlush( gpuComQueue );
158  // the cpu data is same as gpu
159  this->m_gpuModified = false;
160 
161  return (void*) data;
162 }
163 
165 {
166  m_bufferSize = size;
167 }
168 
169 void mitk::OclDataSet::SetBpE(unsigned short BpE)
170 {
171  m_BpE = BpE;
172 }
#define CHECK_OCL_ERR(_er)
Definition: mitkOclUtils.h:21
void Modified(int _type)
Set the modified flag for one of the buffers.
ServiceReferenceU GetServiceReference(const std::string &clazz)
#define MITK_INFO
Definition: mitkLogMacros.h:18
#define MITK_ERROR
Definition: mitkLogMacros.h:20
#define MITK_DEBUG
Definition: mitkLogMacros.h:22
void * GetService(const ServiceReferenceBase &reference)
void SetBufferSize(vcl_size_t size)
Set the amount of elements in buffer.
cl_mem CreateGPUBuffer()
virtual int TransferDataToGPU(cl_command_queue)
Copies the RAM-stored data to GMEM.
#define mitkThrow()
bool IsModified(int _type)
Returns the status of the image buffer.
void SetBpE(unsigned short BpE)
Set the DataSet memory Size per Element in Bytes.
virtual void * TransferDataToCPU(cl_command_queue)
Copies the in GMEM stored data to RAM.
virtual ~OclDataSet()
Destructor.
OclDataSet()
Constructor.
static ModuleContext * GetModuleContext()
Returns the module context of the calling module.
void oclPrintMemObjectInfo(cl_mem memobj)
Prints the available memory info about the given object to std::cout.
cl_context m_context
Declaration of the OpenCL Resources micro-service.