From 68e08bbecd3a5e2110204e0b0de29b625dc0d99e Mon Sep 17 00:00:00 2001 From: Vladislav Vinogradov Date: Mon, 22 Dec 2014 11:33:39 +0300 Subject: [PATCH] fix null stream initialization for multi-gpu systems --- modules/core/src/cuda_stream.cpp | 67 ++++++++++++-------------------- 1 file changed, 24 insertions(+), 43 deletions(-) diff --git a/modules/core/src/cuda_stream.cpp b/modules/core/src/cuda_stream.cpp index 87afe72a12..d3b5545e94 100644 --- a/modules/core/src/cuda_stream.cpp +++ b/modules/core/src/cuda_stream.cpp @@ -330,14 +330,11 @@ namespace cv { namespace cuda void initStreams(); void initPools(); - Mutex streams_mtx_; - volatile bool streams_initialized_; - - Mutex pools_mtx_; - volatile bool pools_initialized_; - std::vector > streams_; + Mutex streams_mtx_; + std::vector pools_; + Mutex pools_mtx_; }; DefaultDeviceInitializer::DefaultDeviceInitializer() @@ -359,59 +356,43 @@ namespace cv { namespace cuda Stream& DefaultDeviceInitializer::getNullStream(int deviceId) { - initStreams(); + AutoLock lock(streams_mtx_); + + if (streams_.empty()) + { + int deviceCount = getCudaEnabledDeviceCount(); + + if (deviceCount > 0) + streams_.resize(deviceCount); + } CV_DbgAssert( deviceId >= 0 && deviceId < static_cast(streams_.size()) ); + if (streams_[deviceId].empty()) + { + cudaStream_t stream = NULL; + Ptr impl = makePtr(stream); + streams_[deviceId] = Ptr(new Stream(impl)); + } + return *streams_[deviceId]; } MemoryPool* DefaultDeviceInitializer::getMemoryPool(int deviceId) - { - initPools(); - - CV_DbgAssert( deviceId >= 0 && deviceId < static_cast(pools_.size()) ); - - return &pools_[deviceId]; - } - - void DefaultDeviceInitializer::initStreams() - { - AutoLock lock(streams_mtx_); - - if (!streams_initialized_) - { - int deviceCount = getCudaEnabledDeviceCount(); - - if (deviceCount > 0) - { - streams_.resize(deviceCount); - - for (int i = 0; i < deviceCount; ++i) - { - cudaStream_t stream = NULL; - Ptr impl = makePtr(stream); - streams_[i] = Ptr(new Stream(impl)); - } - } - - streams_initialized_ = true; - } - } - - void DefaultDeviceInitializer::initPools() { AutoLock lock(pools_mtx_); - if (!pools_initialized_) + if (pools_.empty()) { int deviceCount = getCudaEnabledDeviceCount(); if (deviceCount > 0) pools_.resize(deviceCount); - - pools_initialized_ = true; } + + CV_DbgAssert( deviceId >= 0 && deviceId < static_cast(pools_.size()) ); + + return &pools_[deviceId]; } DefaultDeviceInitializer initializer;