File: itkOpenCLVectorBase.cxx

package info (click to toggle)
elastix 5.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 42,480 kB
  • sloc: cpp: 68,403; lisp: 4,118; python: 1,013; xml: 182; sh: 177; makefile: 33
file content (380 lines) | stat: -rw-r--r-- 10,618 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/*=========================================================================
 *
 *  Copyright UMC Utrecht and contributors
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0.txt
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 *=========================================================================*/
#include "itkOpenCLVectorBase.h"
#include "itkOpenCLContext.h"

// thread/SMP safe reference counter
#include <vcl_atomic_count.h>

namespace itk
{
#if defined(__APPLE__) || defined(__MACOSX)
#  define ITK_CL_COPY_VECTOR 1
#endif

enum OpenCLVectorState
{
  Uninitialized, // Buffer contains uninitialized contents.
  InHost,        // Data is currently in the host.
  InKernel       // Data is currently in a kernel.
};

class OpenCLVectorBasePimpl
{
public:
  OpenCLVectorBasePimpl()
    : referenceCount(1)
    , // reference count to 1
    state(Uninitialized)
    , context(0)
    , id(0)
    , hostCopy(0)
  {}

  void *
  GetHostPointer(const std::size_t size)
  {
    if (!hostCopy)
    {
      hostCopy = ::malloc(size);
    }
    return hostCopy;
  }


public:
  vcl_atomic_count              referenceCount;
  OpenCLVectorState             state;
  OpenCLContext *               context;
  cl_mem                        id;
  void *                        hostCopy;
  std::list<OpenCLVectorBase *> owners;
};

//------------------------------------------------------------------------------
OpenCLVectorBase::OpenCLVectorBase(const std::size_t elemSize)
  : d_ptr(0)
  , m_ElementSize(elemSize)
  , m_Size(0)
  , m_Mapped(0)
{}

//------------------------------------------------------------------------------
OpenCLVectorBase::OpenCLVectorBase(const std::size_t elemSize, const OpenCLVectorBase & other)
  : d_ptr(other.d_ptr)
  , m_ElementSize(elemSize)
  , m_Size(other.m_Size)
  , m_Mapped(other.m_Mapped)
{
  if (this->d_ptr)
  {
    ++this->d_ptr->referenceCount;
    this->d_ptr->owners.push_back(this);
  }
}


//------------------------------------------------------------------------------
OpenCLVectorBase::~OpenCLVectorBase()
{
  this->Release();
}


//------------------------------------------------------------------------------
void
OpenCLVectorBase::Assign(const OpenCLVectorBase & other)
{
  if (this->d_ptr == other.d_ptr)
  {
    return;
  }
  this->Release();
  this->d_ptr = other.d_ptr;
  this->m_Size = other.m_Size;
  this->m_Mapped = other.m_Mapped;
  if (this->d_ptr)
  {
    ++this->d_ptr->referenceCount;
    this->d_ptr->owners.push_back(this);
  }
}


//------------------------------------------------------------------------------
void
OpenCLVectorBase::Create(OpenCLContext * context, const OpenCLMemoryObject::Access access, const std::size_t size)
{
  this->Release();

  // Create base pimpl class
  this->d_ptr = new OpenCLVectorBasePimpl();
  itkAssertOrThrowMacro((this->d_ptr != 0), "OpenCLVectorBase::Create() unable to create base pimpl class.");

  this->d_ptr->owners.push_back(this);
  cl_int error;
  cl_mem id = clCreateBuffer(context->GetContextId(),
#ifndef ITK_CL_COPY_VECTOR
                             cl_mem_flags(access) | CL_MEM_ALLOC_HOST_PTR,
#else
                             cl_mem_flags(access),
#endif
                             size * this->m_ElementSize,
                             0,
                             &error);
  context->ReportError(error, __FILE__, __LINE__, ITK_LOCATION);
  if (id)
  {
    this->d_ptr->context = context;
    this->d_ptr->id = id;
    this->d_ptr->state = Uninitialized;
    this->d_ptr->hostCopy = 0;
    this->m_Size = size;
  }
}


//------------------------------------------------------------------------------
void
OpenCLVectorBase::Release()
{
  if (!this->d_ptr)
  {
    return;
  }

  if (--this->d_ptr->referenceCount <= 0)
  {
    this->d_ptr = 0;
    this->m_Size = 0;
    this->m_Mapped = 0;
    return;
  }
#ifndef ITK_CL_COPY_VECTOR
  this->Unmap();
#else
  // No need to write back if we will discard the contents anyway.
  this->m_Mapped = 0;
#endif
  if (this->d_ptr->id)
  {
    clReleaseMemObject(this->d_ptr->id);
    this->d_ptr->id = 0;
  }
  this->d_ptr->context = 0;
  this->d_ptr->state = Uninitialized;
  this->m_Size = 0;
  if (this->d_ptr->hostCopy)
  {
    ::free(this->d_ptr->hostCopy);
    this->d_ptr->hostCopy = 0;
  }
  delete this->d_ptr;
  this->d_ptr = 0;
}


//------------------------------------------------------------------------------
void
OpenCLVectorBase::Map()
{
  // Bail out if no buffer, or already mapped.
  if (!this->d_ptr || !this->d_ptr->id || this->m_Mapped)
  {
    return;
  }

#ifndef ITK_CL_COPY_VECTOR
  cl_int error;
  this->m_Mapped = clEnqueueMapBuffer(this->d_ptr->context->GetActiveQueue(),
                                      this->d_ptr->id,
                                      CL_TRUE,
                                      CL_MAP_READ | CL_MAP_WRITE,
                                      0,
                                      this->m_Size * this->m_ElementSize,
                                      0,
                                      0,
                                      0,
                                      &error);
  this->d_ptr->context->ReportError(error, __FILE__, __LINE__, ITK_LOCATION);
#else
  // We cannot map the buffer directly, so do an explicit read-back.
  // We skip the read-back if the buffer was not recently in a kernel.
  void * hostPtr = this->d_ptr->GetHostPointer(this->m_Size * this->m_ElementSize);
  if (this->d_ptr->state == InKernel)
  {
    const cl_int error = clEnqueueReadBuffer(this->d_ptr->context->GetActiveQueue(),
                                             this->d_ptr->id,
                                             CL_TRUE,
                                             0,
                                             this->m_Size * this->m_ElementSize,
                                             hostPtr,
                                             0,
                                             0,
                                             0);
    this->d_ptr->context->ReportError(error, __FILE__, __LINE__, ITK_LOCATION);
    if (error == CL_SUCCESS)
    {
      this->m_Mapped = hostPtr;
    }
  }
  else
  {
    this->m_Mapped = hostPtr;
  }
  this->d_ptr->state = InHost;
#endif

  // Update all of the other owners with the map state.
  if (this->d_ptr->owners.size() > 1)
  {
    std::list<OpenCLVectorBase *>::iterator it;
    for (it = this->d_ptr->owners.begin(); it != this->d_ptr->owners.end(); ++it)
    {
      if (*it != this)
      {
        (*it)->m_Mapped = this->m_Mapped;
      }
    }
  }
}


//------------------------------------------------------------------------------
void
OpenCLVectorBase::Unmap() const
{
  if (this->m_Mapped)
  {
#ifndef ITK_CL_COPY_VECTOR
    const cl_int error =
      clEnqueueUnmapMemObject(this->d_ptr->context->GetActiveQueue(), this->d_ptr->id, this->m_Mapped, 0, 0, 0);
    this->d_ptr->context->ReportError(error, __FILE__, __LINE__, ITK_LOCATION);
#else
    // Write the local copy back to the OpenCL device.
    if (this->d_ptr->hostCopy && this->d_ptr->state == InHost)
    {
      const cl_int error = clEnqueueWriteBuffer(this->d_ptr->context->GetActiveQueue(),
                                                this->d_ptr->id,
                                                CL_FALSE,
                                                0,
                                                this->m_Size * this->m_ElementSize,
                                                this->d_ptr->hostCopy,
                                                0,
                                                0,
                                                0);
      this->d_ptr->context->ReportError(error, __FILE__, __LINE__, ITK_LOCATION);
    }
    this->d_ptr->state = InKernel;
#endif
    this->m_Mapped = 0;

    // Update all of the other owners with the unmap state.
    if (this->d_ptr->owners.size() > 1)
    {
      std::list<OpenCLVectorBase *>::iterator it;
      for (it = this->d_ptr->owners.begin(); it != this->d_ptr->owners.end(); ++it)
      {
        if (*it != this)
        {
          (*it)->m_Mapped = 0;
        }
      }
    }
  }
}


//------------------------------------------------------------------------------
void
OpenCLVectorBase::Read(void * data, const std::size_t size, const std::size_t offset /*= 0 */)
{
  if (size == 0)
  {
    return;
  }
  if (this->m_Mapped)
  {
    memcpy(data, reinterpret_cast<unsigned char *>(this->m_Mapped) + offset, size);
  }
  else if (this->d_ptr && this->d_ptr->id)
  {
    const cl_int error = clEnqueueReadBuffer(
      this->d_ptr->context->GetActiveQueue(), this->d_ptr->id, CL_TRUE, offset, size, data, 0, 0, 0);
    this->d_ptr->context->ReportError(error, __FILE__, __LINE__, ITK_LOCATION);
    this->d_ptr->state = InKernel;
  }
}


//------------------------------------------------------------------------------
void
OpenCLVectorBase::Write(const void * data, const std::size_t size, const std::size_t offset /*= 0 */)
{
  if (size == 0)
  {
    return;
  }
  if (this->m_Mapped)
  {
    memcpy(reinterpret_cast<unsigned char *>(this->m_Mapped) + offset, data, size);
  }
  else if (this->d_ptr && this->d_ptr->id)
  {
    const cl_int error = clEnqueueWriteBuffer(
      this->d_ptr->context->GetActiveQueue(), this->d_ptr->id, CL_TRUE, offset, size, data, 0, 0, 0);
    this->d_ptr->context->ReportError(error, __FILE__, __LINE__, ITK_LOCATION);
    this->d_ptr->state = InKernel;
  }
}


//------------------------------------------------------------------------------
cl_mem
OpenCLVectorBase::GetMemoryId() const
{
  return this->d_ptr ? this->d_ptr->id : 0;
}


//------------------------------------------------------------------------------
OpenCLContext *
OpenCLVectorBase::GetContext() const
{
  return this->d_ptr ? this->d_ptr->context : 0;
}


//------------------------------------------------------------------------------
cl_mem
OpenCLVectorBase::GetKernelArgument() const
{
  if (this->d_ptr)
  {
    this->Unmap();
    this->d_ptr->state = InKernel;
    return this->d_ptr->id;
  }
  else
  {
    return 0;
  }
}


} // end namespace itk