Skip to content

Commit 34c3f0f

Browse files
committed
add cuda::Stream constructor with cuda flags
1 parent 1363496 commit 34c3f0f

File tree

4 files changed

+54
-0
lines changed

4 files changed

+54
-0
lines changed

modules/core/include/opencv2/core/cuda.hpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -656,6 +656,18 @@ class CV_EXPORTS_W Stream
656656
//! creates a new asynchronous stream with custom allocator
657657
CV_WRAP Stream(const Ptr<GpuMat::Allocator>& allocator);
658658

659+
/** @brief creates a new Stream using the cudaFlags argument to determine the behaviors of the stream
660+
661+
@note The cudaFlags parameter is passed to the underlying api cudaStreamCreateWithFlags() and
662+
supports the same parameter values.
663+
@code
664+
// creates an OpenCV cuda::Stream that manages an asynchronous, non-blocking,
665+
// non-default CUDA stream
666+
cv::cuda::Stream cvStream(cudaStreamNonBlocking);
667+
@endcode
668+
*/
669+
CV_WRAP Stream(const size_t cudaFlags);
670+
659671
/** @brief Returns true if the current stream queue is finished. Otherwise, it returns false.
660672
*/
661673
CV_WRAP bool queryIfComplete() const;

modules/core/src/cuda_stream.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
//M*/
4242

4343
#include "precomp.hpp"
44+
#include <cstdint>
4445

4546
using namespace cv;
4647
using namespace cv::cuda;
@@ -293,6 +294,7 @@ class cv::cuda::Stream::Impl
293294

294295
Impl();
295296
Impl(const Ptr<GpuMat::Allocator>& allocator);
297+
Impl(const unsigned int cudaFlags);
296298
explicit Impl(cudaStream_t stream);
297299

298300
~Impl();
@@ -312,6 +314,13 @@ cv::cuda::Stream::Impl::Impl(const Ptr<GpuMat::Allocator>& allocator) : stream(0
312314
ownStream = true;
313315
}
314316

317+
cv::cuda::Stream::Impl::Impl(const unsigned int cudaFlags) : stream(0), ownStream(false)
318+
{
319+
cudaSafeCall(cudaStreamCreateWithFlags(&stream, cudaFlags));
320+
ownStream = true;
321+
allocator = makePtr<StackAllocator>(stream);
322+
}
323+
315324
cv::cuda::Stream::Impl::Impl(cudaStream_t stream_) : stream(stream_), ownStream(false)
316325
{
317326
allocator = makePtr<StackAllocator>(stream);
@@ -450,6 +459,16 @@ cv::cuda::Stream::Stream(const Ptr<GpuMat::Allocator>& allocator)
450459
#endif
451460
}
452461

462+
cv::cuda::Stream::Stream(const size_t cudaFlags)
463+
{
464+
#ifndef HAVE_CUDA
465+
CV_UNUSED(cudaFlags);
466+
throw_no_cuda();
467+
#else
468+
impl_ = makePtr<Impl>(cudaFlags & UINT_MAX);
469+
#endif
470+
}
471+
453472
bool cv::cuda::Stream::queryIfComplete() const
454473
{
455474
#ifndef HAVE_CUDA

modules/core/test/test_cuda.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// This file is part of OpenCV project.
2+
// It is subject to the license terms in the LICENSE file found in the top-level directory
3+
// of this distribution and at http://opencv.org/license.html.
4+
5+
#if defined(HAVE_CUDA)
6+
7+
#include "test_precomp.hpp"
8+
#include <cuda_runtime.h>
9+
#include "opencv2/core/cuda.hpp"
10+
11+
namespace opencv_test { namespace {
12+
13+
TEST(CUDA_Stream, construct_cudaFlags)
14+
{
15+
cv::cuda::Stream stream(cudaStreamNonBlocking);
16+
EXPECT_NE(stream.cudaPtr(), nullptr);
17+
}
18+
19+
}} // namespace
20+
21+
#endif

modules/python/test/test_cuda.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ def test_cuda_interop(self):
3333
self.assertTrue(cuMat.cudaPtr() != 0)
3434
stream = cv.cuda_Stream()
3535
self.assertTrue(stream.cudaPtr() != 0)
36+
asyncstream = cv.cuda_Stream(1) # cudaStreamNonBlocking
37+
self.assertTrue(asyncstream.cudaPtr() != 0)
3638

3739
if __name__ == '__main__':
3840
NewOpenCVTests.bootstrap()

0 commit comments

Comments
 (0)