File size: 6,492 Bytes
dffe123 | 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 | // This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.
#include "opencv2/opencv_modules.hpp"
#ifndef HAVE_OPENCV_CUDEV
#error "opencv_cudev is required"
#else
#include "opencv2/core/cuda.hpp"
#include "opencv2/cudev.hpp"
using namespace cv;
using namespace cv::cuda;
GpuData::GpuData(const size_t _size)
: data(nullptr), size(_size)
{
CV_CUDEV_SAFE_CALL(cudaMalloc(&data, _size));
}
GpuData::~GpuData()
{
CV_CUDEV_SAFE_CALL(cudaFree(data));
}
/////////////////////////////////////////////////////
/// create
void GpuMatND::create(SizeArray _size, int _type)
{
{
auto elements_nonzero = [](SizeArray& v)
{
return std::all_of(v.begin(), v.end(),
[](unsigned u){ return u > 0; });
};
CV_Assert(!_size.empty());
CV_Assert(elements_nonzero(_size));
}
_type &= Mat::TYPE_MASK;
if (size == _size && type() == _type && !empty() && !external() && isContinuous() && !isSubmatrix())
return;
release();
setFields(std::move(_size), _type);
data_ = std::make_shared<GpuData>(totalMemSize());
data = data_->data;
offset = 0;
}
/////////////////////////////////////////////////////
/// release
void GpuMatND::release()
{
data = nullptr;
data_.reset();
flags = dims = offset = 0;
size.clear();
step.clear();
}
/////////////////////////////////////////////////////
/// clone
static bool next(uchar*& d, const uchar*& s, std::vector<int>& idx, const int dims, const GpuMatND& dst, const GpuMatND& src)
{
int inc = dims-3;
while (true)
{
if (idx[inc] == src.size[inc] - 1)
{
if (inc == 0)
{
return false;
}
idx[inc] = 0;
d -= (dst.size[inc] - 1) * dst.step[inc];
s -= (src.size[inc] - 1) * src.step[inc];
inc--;
}
else
{
idx[inc]++;
d += dst.step[inc];
s += src.step[inc];
break;
}
}
return true;
}
GpuMatND GpuMatND::clone() const
{
CV_DbgAssert(!empty());
GpuMatND ret(size, type());
if (isContinuous())
{
CV_CUDEV_SAFE_CALL(cudaMemcpy(ret.getDevicePtr(), getDevicePtr(), ret.totalMemSize(), cudaMemcpyDeviceToDevice));
}
else
{
// 1D arrays are always continuous
if (dims == 2)
{
CV_CUDEV_SAFE_CALL(
cudaMemcpy2D(ret.getDevicePtr(), ret.step[0], getDevicePtr(), step[0],
size[1]*step[1], size[0], cudaMemcpyDeviceToDevice)
);
}
else
{
std::vector<int> idx(dims-2, 0);
uchar* d = ret.getDevicePtr();
const uchar* s = getDevicePtr();
// iterate each 2D plane
do
{
CV_CUDEV_SAFE_CALL(
cudaMemcpy2DAsync(
d, ret.step[dims-2], s, step[dims-2],
size[dims-1]*step[dims-1], size[dims-2], cudaMemcpyDeviceToDevice)
);
}
while (next(d, s, idx, dims, ret, *this));
CV_CUDEV_SAFE_CALL(cudaStreamSynchronize(0));
}
}
return ret;
}
GpuMatND GpuMatND::clone(Stream& stream) const
{
CV_DbgAssert(!empty());
GpuMatND ret(size, type());
cudaStream_t _stream = StreamAccessor::getStream(stream);
if (isContinuous())
{
CV_CUDEV_SAFE_CALL(cudaMemcpyAsync(ret.getDevicePtr(), getDevicePtr(), ret.totalMemSize(), cudaMemcpyDeviceToDevice, _stream));
}
else
{
// 1D arrays are always continuous
if (dims == 2)
{
CV_CUDEV_SAFE_CALL(
cudaMemcpy2DAsync(ret.getDevicePtr(), ret.step[0], getDevicePtr(), step[0],
size[1]*step[1], size[0], cudaMemcpyDeviceToDevice, _stream)
);
}
else
{
std::vector<int> idx(dims-2, 0);
uchar* d = ret.getDevicePtr();
const uchar* s = getDevicePtr();
// iterate each 2D plane
do
{
CV_CUDEV_SAFE_CALL(
cudaMemcpy2DAsync(
d, ret.step[dims-2], s, step[dims-2],
size[dims-1]*step[dims-1], size[dims-2], cudaMemcpyDeviceToDevice, _stream)
);
}
while (next(d, s, idx, dims, ret, *this));
}
}
return ret;
}
/////////////////////////////////////////////////////
/// upload
void GpuMatND::upload(InputArray src)
{
Mat mat = src.getMat();
CV_DbgAssert(!mat.empty());
if (!mat.isContinuous())
mat = mat.clone();
SizeArray _size(mat.dims);
std::copy_n(mat.size.p, mat.dims, _size.data());
create(std::move(_size), mat.type());
CV_CUDEV_SAFE_CALL(cudaMemcpy(getDevicePtr(), mat.data, totalMemSize(), cudaMemcpyHostToDevice));
}
void GpuMatND::upload(InputArray src, Stream& stream)
{
Mat mat = src.getMat();
CV_DbgAssert(!mat.empty());
if (!mat.isContinuous())
mat = mat.clone();
SizeArray _size(mat.dims);
std::copy_n(mat.size.p, mat.dims, _size.data());
create(std::move(_size), mat.type());
cudaStream_t _stream = StreamAccessor::getStream(stream);
CV_CUDEV_SAFE_CALL(cudaMemcpyAsync(getDevicePtr(), mat.data, totalMemSize(), cudaMemcpyHostToDevice, _stream));
}
/////////////////////////////////////////////////////
/// download
void GpuMatND::download(OutputArray dst) const
{
CV_DbgAssert(!empty());
dst.create(dims, size.data(), type());
Mat mat = dst.getMat();
GpuMatND gmat = *this;
if (!gmat.isContinuous())
gmat = gmat.clone();
CV_CUDEV_SAFE_CALL(cudaMemcpy(mat.data, gmat.getDevicePtr(), mat.total() * mat.elemSize(), cudaMemcpyDeviceToHost));
}
void GpuMatND::download(OutputArray dst, Stream& stream) const
{
CV_DbgAssert(!empty());
dst.create(dims, size.data(), type());
Mat mat = dst.getMat();
GpuMatND gmat = *this;
if (!gmat.isContinuous())
gmat = gmat.clone(stream);
cudaStream_t _stream = StreamAccessor::getStream(stream);
CV_CUDEV_SAFE_CALL(cudaMemcpyAsync(mat.data, gmat.getDevicePtr(), mat.total() * mat.elemSize(), cudaMemcpyDeviceToHost, _stream));
}
#endif
|