Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/core/src/cuda_host_mem.cpp
16337 views
1
/*M///////////////////////////////////////////////////////////////////////////////////////
2
//
3
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4
//
5
// By downloading, copying, installing or using the software you agree to this license.
6
// If you do not agree to this license, do not download, install,
7
// copy or use the software.
8
//
9
//
10
// License Agreement
11
// For Open Source Computer Vision Library
12
//
13
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
14
// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
15
// Copyright (C) 2013, OpenCV Foundation, all rights reserved.
16
// Third party copyrights are property of their respective owners.
17
//
18
// Redistribution and use in source and binary forms, with or without modification,
19
// are permitted provided that the following conditions are met:
20
//
21
// * Redistribution's of source code must retain the above copyright notice,
22
// this list of conditions and the following disclaimer.
23
//
24
// * Redistribution's in binary form must reproduce the above copyright notice,
25
// this list of conditions and the following disclaimer in the documentation
26
// and/or other materials provided with the distribution.
27
//
28
// * The name of the copyright holders may not be used to endorse or promote products
29
// derived from this software without specific prior written permission.
30
//
31
// This software is provided by the copyright holders and contributors "as is" and
32
// any express or implied warranties, including, but not limited to, the implied
33
// warranties of merchantability and fitness for a particular purpose are disclaimed.
34
// In no event shall the Intel Corporation or contributors be liable for any direct,
35
// indirect, incidental, special, exemplary, or consequential damages
36
// (including, but not limited to, procurement of substitute goods or services;
37
// loss of use, data, or profits; or business interruption) however caused
38
// and on any theory of liability, whether in contract, strict liability,
39
// or tort (including negligence or otherwise) arising in any way out of
40
// the use of this software, even if advised of the possibility of such damage.
41
//
42
//M*/
43
44
#include "precomp.hpp"
45
#include <map>
46
47
using namespace cv;
48
using namespace cv::cuda;
49
50
#ifdef HAVE_CUDA
51
52
namespace {
53
54
class HostMemAllocator : public MatAllocator
55
{
56
public:
57
explicit HostMemAllocator(unsigned int flags) : flags_(flags)
58
{
59
}
60
61
UMatData* allocate(int dims, const int* sizes, int type,
62
void* data0, size_t* step,
63
AccessFlag /*flags*/, UMatUsageFlags /*usageFlags*/) const CV_OVERRIDE
64
{
65
size_t total = CV_ELEM_SIZE(type);
66
for (int i = dims-1; i >= 0; i--)
67
{
68
if (step)
69
{
70
if (data0 && step[i] != CV_AUTOSTEP)
71
{
72
CV_Assert(total <= step[i]);
73
total = step[i];
74
}
75
else
76
{
77
step[i] = total;
78
}
79
}
80
81
total *= sizes[i];
82
}
83
84
UMatData* u = new UMatData(this);
85
u->size = total;
86
87
if (data0)
88
{
89
u->data = u->origdata = static_cast<uchar*>(data0);
90
u->flags |= UMatData::USER_ALLOCATED;
91
}
92
else
93
{
94
void* ptr = 0;
95
cudaSafeCall( cudaHostAlloc(&ptr, total, flags_) );
96
97
u->data = u->origdata = static_cast<uchar*>(ptr);
98
}
99
100
return u;
101
}
102
103
bool allocate(UMatData* u, AccessFlag /*accessFlags*/, UMatUsageFlags /*usageFlags*/) const CV_OVERRIDE
104
{
105
return (u != NULL);
106
}
107
108
void deallocate(UMatData* u) const CV_OVERRIDE
109
{
110
if (!u)
111
return;
112
113
CV_Assert(u->urefcount >= 0);
114
CV_Assert(u->refcount >= 0);
115
116
if (u->refcount == 0)
117
{
118
if ( !(u->flags & UMatData::USER_ALLOCATED) )
119
{
120
cudaFreeHost(u->origdata);
121
u->origdata = 0;
122
}
123
124
delete u;
125
}
126
}
127
128
private:
129
unsigned int flags_;
130
};
131
132
} // namespace
133
134
#endif
135
136
MatAllocator* cv::cuda::HostMem::getAllocator(AllocType alloc_type)
137
{
138
#ifndef HAVE_CUDA
139
CV_UNUSED(alloc_type);
140
throw_no_cuda();
141
#else
142
static std::map<unsigned int, Ptr<MatAllocator> > allocators;
143
144
unsigned int flag = cudaHostAllocDefault;
145
146
switch (alloc_type)
147
{
148
case PAGE_LOCKED: flag = cudaHostAllocDefault; break;
149
case SHARED: flag = cudaHostAllocMapped; break;
150
case WRITE_COMBINED: flag = cudaHostAllocWriteCombined; break;
151
default: CV_Error(cv::Error::StsBadFlag, "Invalid alloc type");
152
}
153
154
Ptr<MatAllocator>& a = allocators[flag];
155
156
if (a.empty())
157
{
158
a = makePtr<HostMemAllocator>(flag);
159
}
160
161
return a.get();
162
#endif
163
}
164
165
#ifdef HAVE_CUDA
166
namespace
167
{
168
size_t alignUpStep(size_t what, size_t alignment)
169
{
170
size_t alignMask = alignment - 1;
171
size_t inverseAlignMask = ~alignMask;
172
size_t res = (what + alignMask) & inverseAlignMask;
173
return res;
174
}
175
}
176
#endif
177
178
void cv::cuda::HostMem::create(int rows_, int cols_, int type_)
179
{
180
#ifndef HAVE_CUDA
181
CV_UNUSED(rows_);
182
CV_UNUSED(cols_);
183
CV_UNUSED(type_);
184
throw_no_cuda();
185
#else
186
if (alloc_type == SHARED)
187
{
188
DeviceInfo devInfo;
189
CV_Assert( devInfo.canMapHostMemory() );
190
}
191
192
type_ &= Mat::TYPE_MASK;
193
194
if (rows == rows_ && cols == cols_ && type() == type_ && data)
195
return;
196
197
if (data)
198
release();
199
200
CV_DbgAssert( rows_ >= 0 && cols_ >= 0 );
201
202
if (rows_ > 0 && cols_ > 0)
203
{
204
flags = Mat::MAGIC_VAL + type_;
205
rows = rows_;
206
cols = cols_;
207
step = elemSize() * cols;
208
int sz[] = { rows, cols };
209
size_t steps[] = { step, CV_ELEM_SIZE(type_) };
210
flags = updateContinuityFlag(flags, 2, sz, steps);
211
212
if (alloc_type == SHARED)
213
{
214
DeviceInfo devInfo;
215
step = alignUpStep(step, devInfo.textureAlignment());
216
}
217
218
int64 _nettosize = (int64)step*rows;
219
size_t nettosize = (size_t)_nettosize;
220
221
if (_nettosize != (int64)nettosize)
222
CV_Error(cv::Error::StsNoMem, "Too big buffer is allocated");
223
224
size_t datasize = alignSize(nettosize, (int)sizeof(*refcount));
225
226
void* ptr = 0;
227
228
switch (alloc_type)
229
{
230
case PAGE_LOCKED: cudaSafeCall( cudaHostAlloc(&ptr, datasize, cudaHostAllocDefault) ); break;
231
case SHARED: cudaSafeCall( cudaHostAlloc(&ptr, datasize, cudaHostAllocMapped) ); break;
232
case WRITE_COMBINED: cudaSafeCall( cudaHostAlloc(&ptr, datasize, cudaHostAllocWriteCombined) ); break;
233
default: CV_Error(cv::Error::StsBadFlag, "Invalid alloc type");
234
}
235
236
datastart = data = (uchar*)ptr;
237
dataend = data + nettosize;
238
239
refcount = (int*)cv::fastMalloc(sizeof(*refcount));
240
*refcount = 1;
241
}
242
#endif
243
}
244
245
HostMem cv::cuda::HostMem::reshape(int new_cn, int new_rows) const
246
{
247
HostMem hdr = *this;
248
249
int cn = channels();
250
if (new_cn == 0)
251
new_cn = cn;
252
253
int total_width = cols * cn;
254
255
if ((new_cn > total_width || total_width % new_cn != 0) && new_rows == 0)
256
new_rows = rows * total_width / new_cn;
257
258
if (new_rows != 0 && new_rows != rows)
259
{
260
int total_size = total_width * rows;
261
262
if (!isContinuous())
263
CV_Error(cv::Error::BadStep, "The matrix is not continuous, thus its number of rows can not be changed");
264
265
if ((unsigned)new_rows > (unsigned)total_size)
266
CV_Error(cv::Error::StsOutOfRange, "Bad new number of rows");
267
268
total_width = total_size / new_rows;
269
270
if (total_width * new_rows != total_size)
271
CV_Error(cv::Error::StsBadArg, "The total number of matrix elements is not divisible by the new number of rows");
272
273
hdr.rows = new_rows;
274
hdr.step = total_width * elemSize1();
275
}
276
277
int new_width = total_width / new_cn;
278
279
if (new_width * new_cn != total_width)
280
CV_Error(cv::Error::BadNumChannels, "The total width is not divisible by the new number of channels");
281
282
hdr.cols = new_width;
283
hdr.flags = (hdr.flags & ~CV_MAT_CN_MASK) | ((new_cn - 1) << CV_CN_SHIFT);
284
285
return hdr;
286
}
287
288
void cv::cuda::HostMem::release()
289
{
290
#ifdef HAVE_CUDA
291
if (refcount && CV_XADD(refcount, -1) == 1)
292
{
293
cudaFreeHost(datastart);
294
fastFree(refcount);
295
}
296
297
dataend = data = datastart = 0;
298
step = rows = cols = 0;
299
refcount = 0;
300
#endif
301
}
302
303
GpuMat cv::cuda::HostMem::createGpuMatHeader() const
304
{
305
#ifndef HAVE_CUDA
306
throw_no_cuda();
307
#else
308
CV_Assert( alloc_type == SHARED );
309
310
void *pdev;
311
cudaSafeCall( cudaHostGetDevicePointer(&pdev, data, 0) );
312
313
return GpuMat(rows, cols, type(), pdev, step);
314
#endif
315
}
316
317
void cv::cuda::registerPageLocked(Mat& m)
318
{
319
#ifndef HAVE_CUDA
320
CV_UNUSED(m);
321
throw_no_cuda();
322
#else
323
CV_Assert( m.isContinuous() );
324
cudaSafeCall( cudaHostRegister(m.data, m.step * m.rows, cudaHostRegisterPortable) );
325
#endif
326
}
327
328
void cv::cuda::unregisterPageLocked(Mat& m)
329
{
330
#ifndef HAVE_CUDA
331
CV_UNUSED(m);
332
#else
333
cudaSafeCall( cudaHostUnregister(m.data) );
334
#endif
335
}
336
337