3 #include <cuda_runtime_api.h>
4 #include <thrust/device_ptr.h>
7 #include "../iucutil.h"
8 #include "linearmemory.h"
17 template<
typename PixelType,
unsigned int Ndim>
23 static const unsigned int ndim = Ndim;
34 if ((!ext_data_pointer_) && (data_ != NULL))
36 IU_CUDA_SAFE_CALL(cudaFree(data_));
45 LinearMemory<Ndim>(size), data_(0), ext_data_pointer_(false)
48 cudaMalloc((
void** )&data_, this->
numel() *
sizeof(PixelType)));
50 throw std::bad_alloc();
57 LinearMemory<Ndim>(numel), data_(0), ext_data_pointer_(false)
60 cudaMalloc((
void** )&data_, this->
numel() *
sizeof(PixelType)));
62 throw std::bad_alloc();
71 bool ext_data_pointer =
false) :
72 LinearMemory<Ndim>(size), data_(0), ext_data_pointer_(ext_data_pointer)
75 throw IuException(
"input data not valid", __FILE__, __FUNCTION__,
77 if (ext_data_pointer_)
86 cudaMalloc((
void** )&data_, this->
numel() *
sizeof(PixelType)));
88 throw std::bad_alloc();
90 cudaMemcpy(data_, device_data, this->
numel() *
sizeof(PixelType),
91 cudaMemcpyHostToDevice));
101 bool ext_data_pointer =
false) :
102 LinearMemory<Ndim>(numel), data_(0), ext_data_pointer_(ext_data_pointer)
104 if (device_data == 0)
105 throw IuException(
"input data not valid", __FILE__, __FUNCTION__,
107 if (ext_data_pointer_)
116 cudaMalloc((
void** )&data_, this->
numel() *
sizeof(PixelType)));
118 throw std::bad_alloc();
120 cudaMemcpy(data_, device_data, this->
numel() *
sizeof(PixelType),
121 cudaMemcpyHostToDevice));
130 PixelType*
data(
unsigned int offset = 0)
132 if (offset >= this->
numel())
134 std::stringstream msg;
135 msg <<
"Offset (" << offset <<
") out of range (" << this->
numel() <<
").";
136 throw IuException(msg.str(), __FILE__, __FUNCTION__, __LINE__);
138 return &(data_[offset]);
146 const PixelType*
data(
unsigned int offset = 0)
const
148 if (offset >= this->
numel())
150 std::stringstream msg;
151 msg <<
"Offset (" << offset <<
") out of range (" << this->
numel() <<
").";
152 throw IuException(msg.str(), __FILE__, __FUNCTION__, __LINE__);
154 return reinterpret_cast<const PixelType*
>(&(data_[offset]));
160 thrust::device_ptr<PixelType>
begin(
void)
162 return thrust::device_ptr<PixelType>(
data());
168 thrust::device_ptr<PixelType>
end(
void)
170 return thrust::device_ptr<PixelType>(
data() + this->
numel());
176 return this->
numel() *
sizeof(PixelType);
182 return 8 *
sizeof(PixelType);
235 IU_CUDA_SAFE_CALL(cudaMalloc((
void** )&
size_, Ndim *
sizeof(
unsigned int)));
237 cudaMemcpy(
size_, mem.
size().ptr(), Ndim *
sizeof(
unsigned int),
238 cudaMemcpyHostToDevice));
239 IU_CUDA_SAFE_CALL(cudaMalloc((
void** )&
stride_, Ndim *
sizeof(
unsigned int)));
241 cudaMemcpy(
stride_, mem.
stride().ptr(), Ndim *
sizeof(
unsigned int),
242 cudaMemcpyHostToDevice));
248 IU_CUDA_SAFE_CALL(cudaFree(
size_));
250 IU_CUDA_SAFE_CALL(cudaFree(
stride_));
259 template<
typename ResultType =
void>
260 __device__
typename std::enable_if<(Ndim == 2), ResultType>::type
getPosition(
261 const unsigned int& linear_idx,
unsigned int& idx0,
unsigned int& idx1)
263 idx1 = linear_idx /
stride_[1];
264 idx0 = linear_idx %
stride_[1];
273 template<
typename ResultType =
void>
274 __device__
typename std::enable_if<(Ndim == 3), ResultType>::type
getPosition(
275 const unsigned int& linear_idx,
unsigned int& idx0,
unsigned int& idx1,
278 idx2 = linear_idx /
stride_[2];
290 template<
typename ResultType =
void>
291 __device__
typename std::enable_if<(Ndim == 4), ResultType>::type
getPosition(
292 const unsigned int& linear_idx,
unsigned int& idx0,
unsigned int& idx1,
293 unsigned int& idx2,
unsigned int& idx3)
295 idx3 = linear_idx /
stride_[3];
309 template<
typename ResultType =
void>
310 __device__
typename std::enable_if<(Ndim == 5), ResultType>::type
getPosition(
311 const unsigned int& linear_idx,
unsigned int& idx0,
unsigned int& idx1,
312 unsigned int& idx2,
unsigned int& idx3,
unsigned int& idx4)
314 idx4 = linear_idx /
stride_[4];
326 template<
typename ResultType =
unsigned int>
328 const unsigned int& idx0,
const unsigned int& idx1)
330 unsigned int linear_idx = idx0;
331 linear_idx +=
stride_[1] * idx1;
341 template<
typename ResultType =
unsigned int>
343 const unsigned int& idx0,
const unsigned int& idx1,
const unsigned int& idx2)
345 unsigned int linear_idx = idx0;
346 linear_idx +=
stride_[1] * idx1;
347 linear_idx +=
stride_[2] * idx2;
358 template<
typename ResultType =
unsigned int>
360 const unsigned int& idx0,
const unsigned int& idx1,
const unsigned int& idx2,
const unsigned int& idx3)
362 unsigned int linear_idx = idx0;
363 linear_idx +=
stride_[1] * idx1;
364 linear_idx +=
stride_[2] * idx2;
365 linear_idx +=
stride_[3] * idx3;
377 template<
typename ResultType =
unsigned int>
379 const unsigned int& idx0,
const unsigned int& idx1,
const unsigned int& idx2,
const unsigned int& idx3,
const unsigned int& idx4)
381 unsigned int linear_idx = idx0;
382 linear_idx +=
stride_[1] * idx1;
383 linear_idx +=
stride_[2] * idx2;
384 linear_idx +=
stride_[3] * idx3;
385 linear_idx +=
stride_[4] * idx4;
403 template<
typename ResultType = PixelType>
404 __device__
typename std::enable_if<(Ndim > 1), ResultType&>::type
operator()(
405 const unsigned int& idx0,
const unsigned int& idx1)
416 template<
typename ResultType = PixelType>
417 __device__
typename std::enable_if<(Ndim > 2), ResultType&>::type
operator()(
418 const unsigned int& idx0,
const unsigned int& idx1,
const unsigned int& idx2)
430 template<
typename ResultType = PixelType>
431 __device__
typename std::enable_if<(Ndim > 3), ResultType&>::type
operator()(
432 const unsigned int& idx0,
const unsigned int& idx1,
const unsigned int& idx2,
const unsigned int& idx3)
445 template<
typename ResultType = PixelType>
446 __device__
typename std::enable_if<(Ndim > 4), ResultType&>::type
operator()(
447 const unsigned int& idx0,
const unsigned int& idx1,
const unsigned int& idx2,
const unsigned int& idx3,
const unsigned int& idx4)
463 bool ext_data_pointer_;
__device__ std::enable_if<(Ndim > 3), ResultType >::type getLinearIndex(const unsigned int &idx0, const unsigned int &idx1, const unsigned int &idx2, const unsigned int &idx3)
Definition: lineardevicememory.h:359
virtual bool onDevice() const
Definition: lineardevicememory.h:186
PixelType * data(unsigned int offset=0)
Definition: lineardevicememory.h:130
virtual size_t bytes() const
Definition: lineardevicememory.h:174
__device__ std::enable_if<(Ndim > 1), ResultType >::type getLinearIndex(const unsigned int &idx0, const unsigned int &idx1)
Definition: lineardevicememory.h:327
__device__ std::enable_if<(Ndim > 2), ResultType >::type getLinearIndex(const unsigned int &idx0, const unsigned int &idx1, const unsigned int &idx2)
Definition: lineardevicememory.h:342
ndarray_ref< PixelType, Ndim > ref() const
unsigned int numel() const
Definition: linearmemory.h:105
int * size_
Definition: lineardevicememory.h:226
__device__ std::enable_if<(Ndim==3), ResultType >::type getPosition(const unsigned int &linear_idx, unsigned int &idx0, unsigned int &idx1, unsigned int &idx2)
Definition: lineardevicememory.h:274
__device__ std::enable_if<(Ndim==2), ResultType >::type getPosition(const unsigned int &linear_idx, unsigned int &idx0, unsigned int &idx1)
Definition: lineardevicememory.h:260
Exceptions with additional error information.
Definition: coredefs.h:32
__device__ std::enable_if<(Ndim > 4), ResultType >::type getLinearIndex(const unsigned int &idx0, const unsigned int &idx1, const unsigned int &idx2, const unsigned int &idx3, const unsigned int &idx4)
Definition: lineardevicememory.h:378
Definition: image_cpu.h:7
int numel_
Definition: lineardevicememory.h:223
virtual unsigned int bitDepth() const
Definition: lineardevicememory.h:180
Base class for linear memory classes.
Definition: linearmemory.h:61
__device__ std::enable_if<(Ndim==5), ResultType >::type getPosition(const unsigned int &linear_idx, unsigned int &idx0, unsigned int &idx1, unsigned int &idx2, unsigned int &idx3, unsigned int &idx4)
Definition: lineardevicememory.h:310
Size< Ndim > size() const
Definition: linearmemory.h:121
Struct pointer KernelData that can be used in CUDA kernels.
Definition: lineardevicememory.h:217
__host__ KernelData(const LinearDeviceMemory< PixelType, Ndim > &mem)
Definition: lineardevicememory.h:232
PixelType pixel_type
Definition: lineardevicememory.h:22
__host__ ~KernelData()
Definition: lineardevicememory.h:246
LinearDeviceMemory(PixelType *device_data, const unsigned int &numel, bool ext_data_pointer=false)
Definition: lineardevicememory.h:100
__device__ PixelType & operator()(const unsigned int &idx)
Definition: lineardevicememory.h:393
LinearDeviceMemory()
Definition: lineardevicememory.h:26
LinearDeviceMemory(const Size< Ndim > &size)
Definition: lineardevicememory.h:44
thrust::device_ptr< PixelType > end(void)
Definition: lineardevicememory.h:168
thrust::device_ptr< PixelType > begin(void)
Definition: lineardevicememory.h:160
int * stride_
Definition: lineardevicememory.h:229
PixelType * data_
Definition: lineardevicememory.h:220
Linear device memory class.
Definition: lineardevicememory.h:18
virtual ~LinearDeviceMemory()
Definition: lineardevicememory.h:32
LinearDeviceMemory(const unsigned int &numel)
Definition: lineardevicememory.h:56
const PixelType * data(unsigned int offset=0) const
Definition: lineardevicememory.h:146
Size< Ndim > stride() const
Definition: linearmemory.h:127
LinearDeviceMemory(PixelType *device_data, const Size< Ndim > &size, bool ext_data_pointer=false)
Definition: lineardevicememory.h:70
Main class for N-dimensional unsigned int vectors (size vectors).
Definition: vector.h:460
__device__ std::enable_if<(Ndim==4), ResultType >::type getPosition(const unsigned int &linear_idx, unsigned int &idx0, unsigned int &idx1, unsigned int &idx2, unsigned int &idx3)
Definition: lineardevicememory.h:291