From 00d9296e11d55bd898e232058726d8942e51a894 Mon Sep 17 00:00:00 2001
From: Olivier BICHLER <olivier.bichler@cea.fr>
Date: Tue, 6 Feb 2024 10:41:48 +0100
Subject: [PATCH] Fixed initial consumers list in Scheduler

---
 src/scheduler/Scheduler.cpp | 32 ++++++++++++++------------------
 1 file changed, 14 insertions(+), 18 deletions(-)

diff --git a/src/scheduler/Scheduler.cpp b/src/scheduler/Scheduler.cpp
index 953804f62..2093aa5af 100644
--- a/src/scheduler/Scheduler.cpp
+++ b/src/scheduler/Scheduler.cpp
@@ -41,31 +41,27 @@ void Aidge::SequentialScheduler::generateScheduling(bool verbose) {
     // TODO: handle memory allocation in scheduler
     // TODO: optimize memory usage
 
-    // setup initial producers list
+    // Setup initial potential consumers list:
+    // List of input nodes
+    std::set<std::shared_ptr<Node>> consumers = mGraphView->inputNodes();
+    // Plus the list of nodes inside the graph connected to an inner producer
     std::set<std::shared_ptr<Node>> producers;
     for (const std::shared_ptr<Node>& nodePtr : mGraphView->getNodes()) {
         if (nodePtr->type() == Producer_Op::Type) {
             producers.insert(nodePtr);
         }
     }
-    // add Data Input
-    // FIXME : should be changed when the real system for providing
-    // data is implemented
-    for (const std::shared_ptr<Node>& nodePtr : mGraphView->inputNodes()) {
-        for (const auto& parentPtr : nodePtr->getParents()) {
-            if ((mGraphView->getNodes()).find(parentPtr) == (mGraphView->getNodes()).end()) {
-                // Node not found in the graph, it's an outside producer
-                producers.insert(parentPtr);
-            }
-        }
-    }
-
-    // setup consumer list
-    // std::set<std::shared_ptr<Node>> consumers = getConsumers(producers);
-
-    /* It may not be necessary to initialize producer */
-    std::set<std::shared_ptr<Node>> consumers = mGraphView->inputNodes();
+    const auto producersConsumers = getConsumers(producers);
+    consumers.insert(producersConsumers.begin(), producersConsumers.end());
+
+    // Frozen consumers is used as a stop condition of the scheduling loop:
+    // The first time no consumer is runnable, frozenConsumers is updated to the
+    // current list of consumer. If after successive iterations, all with no
+    // runnable consumer, the list of consumer is again equal to frozenConsumers
+    // it means we are in cycle with no more scheduling update, a.k.a. a
+    // frozen state.
     std::set<std::shared_ptr<Node>> frozenConsumers;
+
     do {
         // Check required producers
         std::set<std::shared_ptr<Node>> requiredProducers;
-- 
GitLab