From 01cdd617f696a1151772a11b2efebcf655badafd Mon Sep 17 00:00:00 2001
From: Olivier BICHLER <olivier.bichler@cea.fr>
Date: Wed, 11 Sep 2024 13:50:06 +0200
Subject: [PATCH] Improved parallel scheduler

---
 src/scheduler/ParallelScheduler.cpp | 21 +++++++++++++++++----
 1 file changed, 17 insertions(+), 4 deletions(-)

diff --git a/src/scheduler/ParallelScheduler.cpp b/src/scheduler/ParallelScheduler.cpp
index 4e5150990..1d70646b7 100644
--- a/src/scheduler/ParallelScheduler.cpp
+++ b/src/scheduler/ParallelScheduler.cpp
@@ -127,7 +127,12 @@ void Aidge::ParallelScheduler::forward(bool forwardDims, const std::vector<std::
         // in the next step
         for (size_t i = 0; i < staticSchedule.size(); ) {
             auto runnable = staticSchedule[i];
-            if (!pool.busy() && runnable->early <= latest) {
+            if (runnable->early > latest) {
+                // No more node can be run at this step (latest)
+                break;
+            }
+
+            if (!pool.busy()) {
                 // Check that potential preceding non-critical nodes are finished
                 bool ready = true;
                 for (auto elt : runnable->laterThan) {
@@ -168,9 +173,17 @@ void Aidge::ParallelScheduler::forward(bool forwardDims, const std::vector<std::
                 }
             }
             else {
-                // Thread pool is already full or no more node can be run at
-                // this step (latest)
-                break;
+                // Thread pool is already full
+                bool ready = true;
+                for (auto elt : mustFinish) {
+                    ready = ready && finished.at(elt);
+                }
+                if (!ready) {
+                    std::this_thread::yield();
+                }
+                else {
+                    break;
+                }
             }
         }
 
-- 
GitLab