From 82ba3ac8948294d23dbd6757c2c353a35a984474 Mon Sep 17 00:00:00 2001 From: Alexander Alekhin Date: Thu, 5 Apr 2018 14:32:21 +0300 Subject: [PATCH] cuda: refactor MemoryPool - make non-copyable (aligns inner mutex semantic to std::mutex) - getMemoryPool() returns reference instead of pointer (NULL is not expected here) --- modules/core/src/cuda_stream.cpp | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/modules/core/src/cuda_stream.cpp b/modules/core/src/cuda_stream.cpp index 340cf102e1..ae0e0abef9 100644 --- a/modules/core/src/cuda_stream.cpp +++ b/modules/core/src/cuda_stream.cpp @@ -117,6 +117,7 @@ namespace { public: MemoryPool(); + ~MemoryPool() { release(); } void initialize(size_t stackSize, int stackCount); void release(); @@ -136,6 +137,8 @@ namespace uchar* mem_; std::vector stacks_; + + MemoryPool(const MemoryPool&); //= delete; }; MemoryPool::MemoryPool() : initialized_(false), mem_(0) @@ -336,7 +339,7 @@ namespace cv { namespace cuda ~DefaultDeviceInitializer(); Stream& getNullStream(int deviceId); - MemoryPool* getMemoryPool(int deviceId); + MemoryPool& getMemoryPool(int deviceId); private: void initStreams(); @@ -345,7 +348,7 @@ namespace cv { namespace cuda std::vector > streams_; Mutex streams_mtx_; - std::vector pools_; + std::vector > pools_; Mutex pools_mtx_; }; @@ -360,7 +363,7 @@ namespace cv { namespace cuda for (size_t i = 0; i < pools_.size(); ++i) { cudaSetDevice(static_cast(i)); - pools_[i].release(); + pools_[i]->release(); } pools_.clear(); @@ -390,7 +393,7 @@ namespace cv { namespace cuda return *streams_[deviceId]; } - MemoryPool* DefaultDeviceInitializer::getMemoryPool(int deviceId) + MemoryPool& DefaultDeviceInitializer::getMemoryPool(int deviceId) { AutoLock lock(pools_mtx_); @@ -399,12 +402,21 @@ namespace cv { namespace cuda int deviceCount = getCudaEnabledDeviceCount(); if (deviceCount > 0) + { pools_.resize(deviceCount); + for (size_t i = 0; i < pools_.size(); ++i) + { + cudaSetDevice(static_cast(i)); + pools_[i] = makePtr(); + } + } } CV_DbgAssert( deviceId >= 0 && deviceId < static_cast(pools_.size()) ); - return &pools_[deviceId]; + MemoryPool* p = pools_[deviceId]; + CV_Assert(p); + return *p; } DefaultDeviceInitializer initializer; @@ -577,7 +589,7 @@ namespace if (enableMemoryPool) { const int deviceId = getDevice(); - memStack_ = initializer.getMemoryPool(deviceId)->getFreeMemStack(); + memStack_ = initializer.getMemoryPool(deviceId).getFreeMemStack(); DeviceInfo devInfo(deviceId); alignment_ = devInfo.textureAlignment(); } @@ -668,7 +680,7 @@ void cv::cuda::setBufferPoolConfig(int deviceId, size_t stackSize, int stackCoun if (deviceId >= 0) { setDevice(deviceId); - initializer.getMemoryPool(deviceId)->initialize(stackSize, stackCount); + initializer.getMemoryPool(deviceId).initialize(stackSize, stackCount); } else { @@ -677,7 +689,7 @@ void cv::cuda::setBufferPoolConfig(int deviceId, size_t stackSize, int stackCoun for (deviceId = 0; deviceId < deviceCount; ++deviceId) { setDevice(deviceId); - initializer.getMemoryPool(deviceId)->initialize(stackSize, stackCount); + initializer.getMemoryPool(deviceId).initialize(stackSize, stackCount); } }