dnn/Vulkan: make thread safe

Use a global dedicated mutex to make sure initialize once and
protect command buffer pool and queue.

Signed-off-by: Wu Zhiwen <zhiwen.wu@intel.com>
pull/13275/head
Wu Zhiwen 6 years ago
parent c6daa4aa16
commit 4e65283081
  1. 1
      modules/dnn/src/vkcom/src/common.hpp
  2. 3
      modules/dnn/src/vkcom/src/context.cpp
  3. 6
      modules/dnn/src/vkcom/src/op_base.cpp

@ -33,6 +33,7 @@ extern VkPhysicalDevice kPhysicalDevice;
extern VkDevice kDevice;
extern VkQueue kQueue;
extern VkCommandPool kCmdPool;
extern cv::Mutex kContextMtx;
enum ShapeIdx
{

@ -25,6 +25,7 @@ VkDebugReportCallbackEXT kDebugReportCallback;
uint32_t kQueueFamilyIndex;
std::vector<const char *> kEnabledLayers;
std::map<std::string, std::vector<uint32_t>> kShaders;
cv::Mutex kContextMtx;
static uint32_t getComputeQueueFamilyIndex()
{
@ -86,7 +87,7 @@ VKAPI_ATTR VkBool32 VKAPI_CALL debugReportCallbackFn(
// internally used
void createContext()
{
cv::AutoLock lock(getInitializationMutex());
cv::AutoLock lock(kContextMtx);
if (!kCtx)
{
kCtx.reset(new Context());

@ -150,6 +150,7 @@ void OpBase::recordCommandBuffer(void* push_constants, size_t push_constants_siz
VkCommandBufferBeginInfo beginInfo = {};
beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
beginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
cv::AutoLock lock(kContextMtx);
VK_CHECK_RESULT(vkBeginCommandBuffer(cmd_buffer_, &beginInfo));
if (push_constants)
vkCmdPushConstants(cmd_buffer_, pipeline_layout_,
@ -176,7 +177,10 @@ void OpBase::runCommandBuffer()
fence_create_info_.flags = 0;
VK_CHECK_RESULT(vkCreateFence(device_, &fence_create_info_, NULL, &fence));
VK_CHECK_RESULT(vkQueueSubmit(kQueue, 1, &submit_info, fence));
{
cv::AutoLock lock(kContextMtx);
VK_CHECK_RESULT(vkQueueSubmit(kQueue, 1, &submit_info, fence));
}
VK_CHECK_RESULT(vkWaitForFences(device_, 1, &fence, VK_TRUE, 100000000000));
vkDestroyFence(device_, fence, NULL);
}

Loading…
Cancel
Save