diff --git a/inference-engine/src/cldnn_engine/cldnn_async_infer_request.cpp b/inference-engine/src/cldnn_engine/cldnn_async_infer_request.cpp index 9e69ddeb0c8..c6c5faf11c4 100644 --- a/inference-engine/src/cldnn_engine/cldnn_async_infer_request.cpp +++ b/inference-engine/src/cldnn_engine/cldnn_async_infer_request.cpp @@ -17,29 +17,34 @@ CLDNNPlugin::CLDNNAsyncInferRequest::CLDNNAsyncInferRequest(const CLDNNInferRequ _pipeline.push_back({taskExecutor, [this] { OV_ITT_SCOPED_TASK(itt::domains::CLDNNPlugin, "CLDNNAsyncInferRequest::PreprocessingAndStartPipeline"); + _inferRequest->setup_stream_graph(); _inferRequest->preprocess(); _inferRequest->enqueue(); - } }); - } - _pipeline.push_back({_waitExecutor, - [this] { - OV_ITT_SCOPED_TASK(itt::domains::CLDNNPlugin, "CLDNNAsyncInferRequest::WaitPipeline"); _inferRequest->wait(); - }}); + } }); + } else { + _pipeline.push_back({ _waitExecutor, + [this] { + OV_ITT_SCOPED_TASK(itt::domains::CLDNNPlugin, "CLDNNAsyncInferRequest::WaitPipeline"); + _inferRequest->wait_notify(); + } }); + } } void CLDNNPlugin::CLDNNAsyncInferRequest::Infer_ThreadUnsafe() { if (_inferRequest->use_external_queue()) { - _inferRequest->preprocess(); - _inferRequest->enqueue(); + _inferRequest->setup_stream_graph(); + _inferRequest->preprocess_notify(); + _inferRequest->enqueue_notify(); } Parent::Infer_ThreadUnsafe(); } void CLDNNPlugin::CLDNNAsyncInferRequest::StartAsync_ThreadUnsafe() { if (_inferRequest->use_external_queue()) { - _inferRequest->preprocess(); - _inferRequest->enqueue(); + _inferRequest->setup_stream_graph(); + _inferRequest->preprocess_notify(); + _inferRequest->enqueue_notify(); } Parent::StartAsync_ThreadUnsafe(); } diff --git a/inference-engine/src/cldnn_engine/cldnn_graph.h b/inference-engine/src/cldnn_engine/cldnn_graph.h index d65d4183d90..27a9a3f029a 100644 --- a/inference-engine/src/cldnn_engine/cldnn_graph.h +++ b/inference-engine/src/cldnn_engine/cldnn_graph.h @@ -70,6 +70,7 @@ public: } m_cv.notify_one(); } + std::mutex& get_mutex() { return m_infer_mutex; } bool use_external_queue() const; diff --git a/inference-engine/src/cldnn_engine/cldnn_infer_request.cpp b/inference-engine/src/cldnn_engine/cldnn_infer_request.cpp index c71acf4e6d9..f44aa149548 100644 --- a/inference-engine/src/cldnn_engine/cldnn_infer_request.cpp +++ b/inference-engine/src/cldnn_engine/cldnn_infer_request.cpp @@ -471,28 +471,30 @@ CLDNNInferRequest::CLDNNInferRequest(const std::vector(_exeNetwork.get())->m_graphs; - if (nullptr != streamExecutor) { - streamID = streamExecutor->GetStreamId(); - int numGraphs = streamGraphs.size(); - streamID = streamID % numGraphs; - } - m_graph = streamGraphs[streamID]; - +void CLDNNInferRequest::preprocess_notify() { m_graph->wait(CLDNNGraph::Stage::PREPROC); if (m_graph->GetMaxDynamicBatchSize() > 1) { preprocess_dynamic(); - return; + } else { + execDataPreprocessing(_inputs, true); // "true" stands for serial preprocessing in case of OpenMP } - execDataPreprocessing(_inputs, true); // "true" stands for serial preprocessing in case of OpenMP m_graph->notify(CLDNNGraph::Stage::PREPROC); } -void CLDNNInferRequest::enqueue() { +void CLDNNInferRequest::preprocess() { + if (m_graph->GetMaxDynamicBatchSize() > 1) { + preprocess_dynamic(); + } else { + execDataPreprocessing(_inputs, true); // "true" stands for serial preprocessing in case of OpenMP + } +} + +void CLDNNInferRequest::enqueue_notify() { m_graph->wait(CLDNNGraph::Stage::EXECUTE); + enqueue(); +} + +void CLDNNInferRequest::enqueue() { if (m_graph->GetMaxDynamicBatchSize() > 1) { enqueue_dynamic(); return; @@ -541,6 +543,11 @@ void CLDNNInferRequest::enqueue() { internal_outputs = m_graph->GetNetwork()->execute(dependencies); } +void CLDNNInferRequest::wait_notify() { + wait(); + m_graph->notify(CLDNNGraph::Stage::EXECUTE); +} + void CLDNNInferRequest::wait() { if (m_graph->GetMaxDynamicBatchSize() > 1) { wait_dynamic(); @@ -568,13 +575,11 @@ void CLDNNInferRequest::wait() { if (m_useProfiling) { m_graph->UpdatePerfStatistics(); } - m_graph->notify(CLDNNGraph::Stage::EXECUTE); } void CLDNNInferRequest::preprocess_dynamic() { // execute input pre-processing. execDataPreprocessing(_inputs, true); // "true" stands for serial preprocessing in case of OpenMP - m_graph->notify(CLDNNGraph::Stage::PREPROC); } void CLDNNInferRequest::enqueue_dynamic() { @@ -619,12 +624,21 @@ void CLDNNInferRequest::wait_dynamic() { } } } - m_graph->notify(CLDNNGraph::Stage::EXECUTE); } // ----------------------------------------------------------------------------------------- // // ---------------------------- internal utils --------- ----------------------------------- // // ----------------------------------------------------------------------------------------- // +void CLDNNInferRequest::setup_stream_graph() { + int streamID = 0; + auto& streamGraphs = static_cast(_exeNetwork.get())->m_graphs; + if (nullptr != streamExecutor) { + streamID = streamExecutor->GetStreamId(); + int numGraphs = streamGraphs.size(); + streamID = streamID % numGraphs; + } + m_graph = streamGraphs[streamID]; +} Blob::Ptr CLDNNInferRequest::create_host_blob(const TensorDesc& desc, uint8_t* mem_ptr) { OV_ITT_SCOPED_TASK(itt::domains::CLDNNPlugin, "CLDNNInferRequest::create_host_blob"); @@ -852,7 +866,8 @@ void CLDNNInferRequest::allocate_outputs_dynamic() { void CLDNNInferRequest::InferImpl() { OV_ITT_SCOPED_TASK(itt::domains::CLDNNPlugin, "CLDNNInferRequest::InferImpl"); - + setup_stream_graph(); + std::lock_guard lk(m_graph->get_mutex()); preprocess(); enqueue(); wait(); diff --git a/inference-engine/src/cldnn_engine/cldnn_infer_request.h b/inference-engine/src/cldnn_engine/cldnn_infer_request.h index 72c924b015e..0a52ae518f5 100644 --- a/inference-engine/src/cldnn_engine/cldnn_infer_request.h +++ b/inference-engine/src/cldnn_engine/cldnn_infer_request.h @@ -49,6 +49,11 @@ public: void EnableProfiling() { m_useProfiling = true; } void EnableStreams() { m_useStreams = true; } + void setup_stream_graph(); + void preprocess_notify(); + void enqueue_notify(); + void wait_notify(); + void preprocess(); void enqueue(); void wait();