Commit 82ba3ac8 authored by Alexander Alekhin's avatar Alexander Alekhin

cuda: refactor MemoryPool

- make non-copyable (aligns inner mutex semantic to std::mutex)
- getMemoryPool() returns reference instead of pointer (NULL is not expected here)
parent f93c1b94
...@@ -117,6 +117,7 @@ namespace ...@@ -117,6 +117,7 @@ namespace
{ {
public: public:
MemoryPool(); MemoryPool();
~MemoryPool() { release(); }
void initialize(size_t stackSize, int stackCount); void initialize(size_t stackSize, int stackCount);
void release(); void release();
...@@ -136,6 +137,8 @@ namespace ...@@ -136,6 +137,8 @@ namespace
uchar* mem_; uchar* mem_;
std::vector<MemoryStack> stacks_; std::vector<MemoryStack> stacks_;
MemoryPool(const MemoryPool&); //= delete;
}; };
MemoryPool::MemoryPool() : initialized_(false), mem_(0) MemoryPool::MemoryPool() : initialized_(false), mem_(0)
...@@ -336,7 +339,7 @@ namespace cv { namespace cuda ...@@ -336,7 +339,7 @@ namespace cv { namespace cuda
~DefaultDeviceInitializer(); ~DefaultDeviceInitializer();
Stream& getNullStream(int deviceId); Stream& getNullStream(int deviceId);
MemoryPool* getMemoryPool(int deviceId); MemoryPool& getMemoryPool(int deviceId);
private: private:
void initStreams(); void initStreams();
...@@ -345,7 +348,7 @@ namespace cv { namespace cuda ...@@ -345,7 +348,7 @@ namespace cv { namespace cuda
std::vector<Ptr<Stream> > streams_; std::vector<Ptr<Stream> > streams_;
Mutex streams_mtx_; Mutex streams_mtx_;
std::vector<MemoryPool> pools_; std::vector<Ptr<MemoryPool> > pools_;
Mutex pools_mtx_; Mutex pools_mtx_;
}; };
...@@ -360,7 +363,7 @@ namespace cv { namespace cuda ...@@ -360,7 +363,7 @@ namespace cv { namespace cuda
for (size_t i = 0; i < pools_.size(); ++i) for (size_t i = 0; i < pools_.size(); ++i)
{ {
cudaSetDevice(static_cast<int>(i)); cudaSetDevice(static_cast<int>(i));
pools_[i].release(); pools_[i]->release();
} }
pools_.clear(); pools_.clear();
...@@ -390,7 +393,7 @@ namespace cv { namespace cuda ...@@ -390,7 +393,7 @@ namespace cv { namespace cuda
return *streams_[deviceId]; return *streams_[deviceId];
} }
MemoryPool* DefaultDeviceInitializer::getMemoryPool(int deviceId) MemoryPool& DefaultDeviceInitializer::getMemoryPool(int deviceId)
{ {
AutoLock lock(pools_mtx_); AutoLock lock(pools_mtx_);
...@@ -399,12 +402,21 @@ namespace cv { namespace cuda ...@@ -399,12 +402,21 @@ namespace cv { namespace cuda
int deviceCount = getCudaEnabledDeviceCount(); int deviceCount = getCudaEnabledDeviceCount();
if (deviceCount > 0) if (deviceCount > 0)
{
pools_.resize(deviceCount); pools_.resize(deviceCount);
for (size_t i = 0; i < pools_.size(); ++i)
{
cudaSetDevice(static_cast<int>(i));
pools_[i] = makePtr<MemoryPool>();
}
}
} }
CV_DbgAssert( deviceId >= 0 && deviceId < static_cast<int>(pools_.size()) ); CV_DbgAssert( deviceId >= 0 && deviceId < static_cast<int>(pools_.size()) );
return &pools_[deviceId]; MemoryPool* p = pools_[deviceId];
CV_Assert(p);
return *p;
} }
DefaultDeviceInitializer initializer; DefaultDeviceInitializer initializer;
...@@ -577,7 +589,7 @@ namespace ...@@ -577,7 +589,7 @@ namespace
if (enableMemoryPool) if (enableMemoryPool)
{ {
const int deviceId = getDevice(); const int deviceId = getDevice();
memStack_ = initializer.getMemoryPool(deviceId)->getFreeMemStack(); memStack_ = initializer.getMemoryPool(deviceId).getFreeMemStack();
DeviceInfo devInfo(deviceId); DeviceInfo devInfo(deviceId);
alignment_ = devInfo.textureAlignment(); alignment_ = devInfo.textureAlignment();
} }
...@@ -668,7 +680,7 @@ void cv::cuda::setBufferPoolConfig(int deviceId, size_t stackSize, int stackCoun ...@@ -668,7 +680,7 @@ void cv::cuda::setBufferPoolConfig(int deviceId, size_t stackSize, int stackCoun
if (deviceId >= 0) if (deviceId >= 0)
{ {
setDevice(deviceId); setDevice(deviceId);
initializer.getMemoryPool(deviceId)->initialize(stackSize, stackCount); initializer.getMemoryPool(deviceId).initialize(stackSize, stackCount);
} }
else else
{ {
...@@ -677,7 +689,7 @@ void cv::cuda::setBufferPoolConfig(int deviceId, size_t stackSize, int stackCoun ...@@ -677,7 +689,7 @@ void cv::cuda::setBufferPoolConfig(int deviceId, size_t stackSize, int stackCoun
for (deviceId = 0; deviceId < deviceCount; ++deviceId) for (deviceId = 0; deviceId < deviceCount; ++deviceId)
{ {
setDevice(deviceId); setDevice(deviceId);
initializer.getMemoryPool(deviceId)->initialize(stackSize, stackCount); initializer.getMemoryPool(deviceId).initialize(stackSize, stackCount);
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment