Skip to content

Commit

Permalink
Use std::array instead of std::vector to reduce memory allocations.
Browse files Browse the repository at this point in the history
  • Loading branch information
asmorkalov committed May 30, 2024
1 parent 7a41af5 commit 56a6cdf
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 35 deletions.
6 changes: 3 additions & 3 deletions modules/cudaarithm/src/cuda/split_merge.cu
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ namespace
{
static void call(const GpuMat* src, GpuMat& dst, Stream& stream)
{
const std::vector<GlobPtrSz<T>> d_src = {globPtr<T>(src[0]), globPtr<T>(src[1])};
const std::array<GlobPtrSz<T>, 2> d_src = {globPtr<T>(src[0]), globPtr<T>(src[1])};
gridMerge(d_src,
globPtr<typename MakeVec<T, 2>::type>(dst),
stream);
Expand All @@ -78,7 +78,7 @@ namespace
{
static void call(const GpuMat* src, GpuMat& dst, Stream& stream)
{
const std::vector<GlobPtrSz<T>> d_src = {globPtr<T>(src[0]), globPtr<T>(src[1]), globPtr<T>(src[2])};
const std::array<GlobPtrSz<T>, 3> d_src = {globPtr<T>(src[0]), globPtr<T>(src[1]), globPtr<T>(src[2])};
gridMerge(d_src,
globPtr<typename MakeVec<T, 3>::type>(dst),
stream);
Expand All @@ -89,7 +89,7 @@ namespace
{
static void call(const GpuMat* src, GpuMat& dst, Stream& stream)
{
const std::vector<GlobPtrSz<T> > d_src = {globPtr<T>(src[0]), globPtr<T>(src[1]), globPtr<T>(src[2]), globPtr<T>(src[3])};
const std::array<GlobPtrSz<T>, 4 > d_src = {globPtr<T>(src[0]), globPtr<T>(src[1]), globPtr<T>(src[2]), globPtr<T>(src[3])};
gridMerge(d_src,
globPtr<typename MakeVec<T, 4>::type>(dst),
stream);
Expand Down
12 changes: 6 additions & 6 deletions modules/cudev/include/opencv2/cudev/grid/detail/split_merge.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,8 @@ namespace grid_split_merge_detail
mergeC2<Policy>(get<0>(src), get<1>(src), dst, mask, rows, cols, stream);
}

template <typename VecType, typename DstType, class MaskPtr>
__host__ static void mergeVector(const std::vector<VecType>& src, const GlobPtr<DstType>& dst, const MaskPtr& mask, int rows, int cols, cudaStream_t stream)
template <class SrcPtrArray, typename DstType, class MaskPtr>
__host__ static void mergeArray(const SrcPtrArray& src, const GlobPtr<DstType>& dst, const MaskPtr& mask, int rows, int cols, cudaStream_t stream)
{
mergeC2<Policy>(src[0], src[1], dst, mask, rows, cols, stream);
}
Expand All @@ -178,8 +178,8 @@ namespace grid_split_merge_detail
mergeC3<Policy>(get<0>(src), get<1>(src), get<2>(src), dst, mask, rows, cols, stream);
}

template <typename VecType, typename DstType, class MaskPtr>
__host__ static void mergeVector(const std::vector<VecType>& src, const GlobPtr<DstType>& dst, const MaskPtr& mask, int rows, int cols, cudaStream_t stream)
template <class SrcPtrArray, typename DstType, class MaskPtr>
__host__ static void mergeArray(const SrcPtrArray& src, const GlobPtr<DstType>& dst, const MaskPtr& mask, int rows, int cols, cudaStream_t stream)
{
mergeC3<Policy>(src[0], src[1], src[2], dst, mask, rows, cols, stream);
}
Expand All @@ -193,8 +193,8 @@ namespace grid_split_merge_detail
mergeC4<Policy>(get<0>(src), get<1>(src), get<2>(src), get<3>(src), dst, mask, rows, cols, stream);
}

template <typename VecType, typename DstType, class MaskPtr>
__host__ static void mergeVector(const std::vector<VecType>& src, const GlobPtr<DstType>& dst, const MaskPtr& mask, int rows, int cols, cudaStream_t stream)
template <class SrcPtrArray, typename DstType, class MaskPtr>
__host__ static void mergeArray(const SrcPtrArray& src, const GlobPtr<DstType>& dst, const MaskPtr& mask, int rows, int cols, cudaStream_t stream)
{
mergeC4<Policy>(src[0], src[1], src[2], src[3], dst, mask, rows, cols, stream);
}
Expand Down
48 changes: 24 additions & 24 deletions modules/cudev/include/opencv2/cudev/grid/split_merge.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,8 @@ __host__ void gridMerge_(const SrcPtrTuple& src, const GlobPtrSz<DstType>& dst,
StreamAccessor::getStream(stream));
}

template <class Policy, typename VecType, typename DstType, class MaskPtr>
__host__ void gridMergeVector_(const std::vector<VecType>& src, GpuMat_<DstType>& dst, const MaskPtr& mask, Stream& stream = Stream::Null())
template <class Policy, class ArrayType, size_t ArraySize, typename DstType, class MaskPtr>
__host__ void gridMergeArray_(const std::array<ArrayType, ArraySize>& src, GpuMat_<DstType>& dst, const MaskPtr& mask, Stream& stream = Stream::Null())
{
CV_Assert( VecTraits<DstType>::cn == src.size() );

Expand All @@ -143,15 +143,15 @@ __host__ void gridMergeVector_(const std::vector<VecType>& src, GpuMat_<DstType>

dst.create(rows, cols);

grid_split_merge_detail::MergeImpl<VecTraits<DstType>::cn, Policy>::mergeVector(src,
grid_split_merge_detail::MergeImpl<VecTraits<DstType>::cn, Policy>::mergeArray(src,
shrinkPtr(dst),
shrinkPtr(mask),
rows, cols,
StreamAccessor::getStream(stream));
}

template <class Policy, typename VecType, typename DstType, class MaskPtr>
__host__ void gridMergeVector_(const std::vector<VecType>& src, const GlobPtrSz<DstType>& dst, const MaskPtr& mask, Stream& stream = Stream::Null())
template <class Policy, class ArrayType, size_t ArraySize, typename DstType, class MaskPtr>
__host__ void gridMergeArray_(const std::array<ArrayType, ArraySize>& src, const GlobPtrSz<DstType>& dst, const MaskPtr& mask, Stream& stream = Stream::Null())
{
CV_Assert( VecTraits<DstType>::cn == src.size() );

Expand All @@ -161,15 +161,15 @@ __host__ void gridMergeVector_(const std::vector<VecType>& src, const GlobPtrSz<
CV_Assert( getRows(dst) == rows && getCols(dst) == cols );
CV_Assert( getRows(mask) == rows && getCols(mask) == cols );

grid_split_merge_detail::MergeImpl<VecTraits<DstType>::cn, Policy>::mergeVector(src,
grid_split_merge_detail::MergeImpl<VecTraits<DstType>::cn, Policy>::mergeArray(src,
shrinkPtr(dst),
shrinkPtr(mask),
rows, cols,
StreamAccessor::getStream(stream));
}

template <class Policy, typename VecType, typename DstType>
__host__ void gridMergeVector_(const std::vector<VecType>& src, GpuMat_<DstType>& dst, Stream& stream = Stream::Null())
template <class Policy, class ArrayType, size_t ArraySize, typename DstType>
__host__ void gridMergeArray_(const std::array<ArrayType, ArraySize>& src, GpuMat_<DstType>& dst, Stream& stream = Stream::Null())
{
CV_Assert( VecTraits<DstType>::cn == src.size() );

Expand All @@ -178,15 +178,15 @@ __host__ void gridMergeVector_(const std::vector<VecType>& src, GpuMat_<DstType>

dst.create(rows, cols);

grid_split_merge_detail::MergeImpl<VecTraits<DstType>::cn, Policy>::mergeVector(src,
grid_split_merge_detail::MergeImpl<VecTraits<DstType>::cn, Policy>::mergeArray(src,
shrinkPtr(dst),
WithOutMask(),
rows, cols,
StreamAccessor::getStream(stream));
}

template <class Policy, typename VecType, typename DstType>
__host__ void gridMergeVector_(const std::vector<VecType>& src, const GlobPtrSz<DstType>& dst, Stream& stream = Stream::Null())
template <class Policy, class ArrayType, size_t ArraySize, typename DstType>
__host__ void gridMergeArray_(const std::array<ArrayType, ArraySize>& src, const GlobPtrSz<DstType>& dst, Stream& stream = Stream::Null())
{
CV_Assert( VecTraits<DstType>::cn == src.size() );

Expand All @@ -195,7 +195,7 @@ __host__ void gridMergeVector_(const std::vector<VecType>& src, const GlobPtrSz<

CV_Assert( getRows(dst) == rows && getCols(dst) == cols );

grid_split_merge_detail::MergeImpl<VecTraits<DstType>::cn, Policy>::mergeVector(src,
grid_split_merge_detail::MergeImpl<VecTraits<DstType>::cn, Policy>::mergeArray(src,
shrinkPtr(dst),
WithOutMask(),
rows, cols,
Expand Down Expand Up @@ -596,28 +596,28 @@ __host__ void gridMerge(const SrcPtrTuple& src, const GlobPtrSz<DstType>& dst, S
gridMerge_<DefaultSplitMergePolicy>(src, dst, stream);
}

template <typename VecType, typename DstType, class MaskPtr>
__host__ void gridMergeVector(const std::vector<VecType>& src, GpuMat_<DstType>& dst, const MaskPtr& mask, Stream& stream = Stream::Null())
template <class ArrayType, size_t ArraySize, typename DstType, class MaskPtr>
__host__ void gridMergeArray(const std::array<ArrayType, ArraySize>& src, GpuMat_<DstType>& dst, const MaskPtr& mask, Stream& stream = Stream::Null())
{
gridMergeVector_<DefaultSplitMergePolicy>(src, dst, mask, stream);
gridMergeArray_<DefaultSplitMergePolicy>(src, dst, mask, stream);
}

template <typename VecType, typename DstType, class MaskPtr>
__host__ void gridMerge(const std::vector<VecType>& src, const GlobPtrSz<DstType>& dst, const MaskPtr& mask, Stream& stream = Stream::Null())
template <class ArrayType, size_t ArraySize, typename DstType, class MaskPtr>
__host__ void gridMerge(const std::array<ArrayType, ArraySize>& src, const GlobPtrSz<DstType>& dst, const MaskPtr& mask, Stream& stream = Stream::Null())
{
gridMergeVector_<DefaultSplitMergePolicy>(src, dst, mask, stream);
gridMergeArray_<DefaultSplitMergePolicy>(src, dst, mask, stream);
}

template <typename VecType, typename DstType>
__host__ void gridMerge(const std::vector<VecType>& src, GpuMat_<DstType>& dst, Stream& stream = Stream::Null())
template <class ArrayType, size_t ArraySize, typename DstType>
__host__ void gridMerge(const std::array<ArrayType, ArraySize>& src, GpuMat_<DstType>& dst, Stream& stream = Stream::Null())
{
gridMergeVector_<DefaultSplitMergePolicy>(src, dst, stream);
gridMergeArray_<DefaultSplitMergePolicy>(src, dst, stream);
}

template <typename VecType, typename DstType>
__host__ void gridMerge(const std::vector<VecType>& src, const GlobPtrSz<DstType>& dst, Stream& stream = Stream::Null())
template <class ArrayType, size_t ArraySize, typename DstType>
__host__ void gridMerge(const std::array<ArrayType, ArraySize>& src, const GlobPtrSz<DstType>& dst, Stream& stream = Stream::Null())
{
gridMergeVector_<DefaultSplitMergePolicy>(src, dst, stream);
gridMergeArray_<DefaultSplitMergePolicy>(src, dst, stream);
}

template <class SrcPtr, typename DstType, class MaskPtr>
Expand Down
4 changes: 2 additions & 2 deletions modules/cudev/test/test_split_merge.cu
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public:
GpuMat_<T> d_src2(src2);

GpuMat_<typename MakeVec<T, 2>::type> dst;
std::vector<GlobPtrSz<T> > d_src = {globPtr(d_src1), globPtr(d_src2)};
std::array<GlobPtrSz<T>, 2 > d_src = {globPtr(d_src1), globPtr(d_src2)};
gridMerge(d_src, dst);

Mat dst_gold;
Expand All @@ -94,7 +94,7 @@ public:
GpuMat_<T> d_src2(src2);
GpuMat_<T> d_src3(src3);

std::vector<GlobPtrSz<T> > d_src = {globPtr(d_src1), globPtr(d_src2), globPtr(d_src3)};
std::array<GlobPtrSz<T>, 3 > d_src = {globPtr(d_src1), globPtr(d_src2), globPtr(d_src3)};

GpuMat_<typename MakeVec<T, 3>::type> dst;
gridMerge(d_src, dst);
Expand Down

0 comments on commit 56a6cdf

Please sign in to comment.