File: itkGPUImageDataManager.hxx

package info (click to toggle)
insighttoolkit5 5.4.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 704,588 kB
  • sloc: cpp: 784,579; ansic: 628,724; xml: 44,704; fortran: 34,250; python: 22,934; sh: 4,078; pascal: 2,636; lisp: 2,158; makefile: 461; yacc: 328; asm: 205; perl: 203; lex: 146; tcl: 132; javascript: 98; csh: 81
file content (145 lines) | stat: -rw-r--r-- 5,076 bytes parent folder | download | duplicates (2)
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
/*=========================================================================
 *
 *  Copyright NumFOCUS
 *
 *  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
 *
 *         https://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.
 *
 *=========================================================================*/
#ifndef itkGPUImageDataManager_hxx
#define itkGPUImageDataManager_hxx

#include "itkOpenCLUtil.h"

namespace itk
{
template <typename ImageType>
void
GPUImageDataManager<ImageType>::SetImagePointer(ImageType * img)
{
  m_Image = img;

  using RegionType = typename ImageType::RegionType;
  using IndexType = typename ImageType::IndexType;
  using SizeType = typename ImageType::SizeType;

  RegionType region = m_Image->GetBufferedRegion();
  IndexType  index = region.GetIndex();
  SizeType   size = region.GetSize();

  for (unsigned int d = 0; d < ImageDimension; ++d)
  {
    m_BufferedRegionIndex[d] = index[d];
    m_BufferedRegionSize[d] = size[d];
  }

  m_GPUBufferedRegionIndex = GPUDataManager::New();
  m_GPUBufferedRegionIndex->SetBufferSize(sizeof(int) * ImageDimension);
  m_GPUBufferedRegionIndex->SetCPUBufferPointer(m_BufferedRegionIndex);
  m_GPUBufferedRegionIndex->SetBufferFlag(CL_MEM_READ_ONLY);
  m_GPUBufferedRegionIndex->Allocate();
  m_GPUBufferedRegionIndex->SetGPUDirtyFlag(true);

  m_GPUBufferedRegionSize = GPUDataManager::New();
  m_GPUBufferedRegionSize->SetBufferSize(sizeof(int) * ImageDimension);
  m_GPUBufferedRegionSize->SetCPUBufferPointer(m_BufferedRegionSize);
  m_GPUBufferedRegionSize->SetBufferFlag(CL_MEM_READ_ONLY);
  m_GPUBufferedRegionSize->Allocate();
  m_GPUBufferedRegionSize->SetGPUDirtyFlag(true);
}

template <typename ImageType>
void
GPUImageDataManager<ImageType>::MakeCPUBufferUpToDate()
{
  if (m_Image.IsNotNull())
  {
    const std::lock_guard<std::mutex> lockGuard(m_Mutex);

    ModifiedTimeType gpu_time = this->GetMTime();
    TimeStamp        cpu_time_stamp = m_Image->GetTimeStamp();
    ModifiedTimeType cpu_time = cpu_time_stamp.GetMTime();

    /* Why we check dirty flag and time stamp together?
     * Because existing CPU image filters do not use pixel/buffer
     * access function in GPUImage and therefore dirty flag is not
     * correctly managed. Therefore, we check the time stamp of
     * CPU and GPU data as well
     */
    if ((m_IsCPUBufferDirty || (gpu_time > cpu_time)) && m_GPUBuffer != nullptr && m_CPUBuffer != nullptr)
    {
      cl_int errid;
      itkDebugMacro("GPU->CPU data copy");
      errid = clEnqueueReadBuffer(m_ContextManager->GetCommandQueue(m_CommandQueueId),
                                  m_GPUBuffer,
                                  CL_TRUE,
                                  0,
                                  m_BufferSize,
                                  m_CPUBuffer,
                                  0,
                                  nullptr,
                                  nullptr);
      OpenCLCheckError(errid, __FILE__, __LINE__, ITK_LOCATION);

      m_Image->Modified();
      this->SetTimeStamp(m_Image->GetTimeStamp());

      m_IsCPUBufferDirty = false;
      m_IsGPUBufferDirty = false;
    }
  }
}

template <typename ImageType>
void
GPUImageDataManager<ImageType>::MakeGPUBufferUpToDate()
{
  if (m_Image.IsNotNull())
  {
    const std::lock_guard<std::mutex> lockGuard(m_Mutex);

    ModifiedTimeType gpu_time = this->GetMTime();
    TimeStamp        cpu_time_stamp = m_Image->GetTimeStamp();
    ModifiedTimeType cpu_time = m_Image->GetMTime();

    /* Why we check dirty flag and time stamp together?
     * Because existing CPU image filters do not use pixel/buffer
     * access function in GPUImage and therefore dirty flag is not
     * correctly managed. Therefore, we check the time stamp of
     * CPU and GPU data as well
     */
    if ((m_IsGPUBufferDirty || (gpu_time < cpu_time)) && m_CPUBuffer != nullptr && m_GPUBuffer != nullptr)
    {
      cl_int errid;
      itkDebugMacro("CPU->GPU data copy");
      errid = clEnqueueWriteBuffer(m_ContextManager->GetCommandQueue(m_CommandQueueId),
                                   m_GPUBuffer,
                                   CL_TRUE,
                                   0,
                                   m_BufferSize,
                                   m_CPUBuffer,
                                   0,
                                   nullptr,
                                   nullptr);
      OpenCLCheckError(errid, __FILE__, __LINE__, ITK_LOCATION);

      this->SetTimeStamp(cpu_time_stamp);

      m_IsCPUBufferDirty = false;
      m_IsGPUBufferDirty = false;
    }
  }
}

} // namespace itk

#endif