From 6441521f4193acaaff231df328e1e452b51cdf14 Mon Sep 17 00:00:00 2001
From: Pedro Gonnet <pedro.gonnet@durham.ac.uk>
Date: Sat, 22 Jun 2013 11:27:03 +0000
Subject: [PATCH] prioritize tasks according to the maximum depth of their
 dependent tasks.

Former-commit-id: 7ce8137a6362d80727fd3d06ca52e9afb25767c1
---
 src/engine.c    | 4 ++--
 src/queue.c     | 6 +++---
 src/scheduler.c | 9 +++++++++
 src/task.h      | 2 +-
 4 files changed, 15 insertions(+), 6 deletions(-)

diff --git a/src/engine.c b/src/engine.c
index 1e4afdb89b..af9e66f189 100644
--- a/src/engine.c
+++ b/src/engine.c
@@ -854,7 +854,7 @@ void engine_step ( struct engine *e ) {
  
 void engine_init ( struct engine *e , struct space *s , float dt , int nr_threads , int nr_queues , int policy ) {
 
-    #if defined(HAVE_SETAFFINITY)
+    #if defined(NO_HAVE_SETAFFINITY)
         cpu_set_t cpuset;
     #endif
     int k;
@@ -900,7 +900,7 @@ void engine_init ( struct engine *e , struct space *s , float dt , int nr_thread
         e->runners[k].e = e;
         if ( pthread_create( &e->runners[k].thread , NULL , &runner_main , &e->runners[k] ) != 0 )
             error( "Failed to create runner thread." );
-        #if defined(HAVE_SETAFFINITY)
+        #if defined(NO_HAVE_SETAFFINITY)
             /* Set the cpu mask to zero | e->id. */
             CPU_ZERO( &cpuset );
             CPU_SET( e->runners[k].id , &cpuset );
diff --git a/src/queue.c b/src/queue.c
index cbea11200a..42df51dc57 100644
--- a/src/queue.c
+++ b/src/queue.c
@@ -104,7 +104,7 @@ void queue_insert ( struct queue *q , struct task *t ) {
     
     /* Shuffle up. */
     for ( int k = q->count - 1 ; k > 0 ; k /= 2 )
-        if ( q->tasks[ q->tid[k] ].rank < q->tasks[ q->tid[k/2] ].rank ) {
+        if ( q->tasks[ q->tid[k] ].maxdepth > q->tasks[ q->tid[k/2] ].maxdepth ) {
             int temp = q->tid[k];
             q->tid[k] = q->tid[k/2];
             q->tid[k/2] = temp;
@@ -234,9 +234,9 @@ struct task *queue_gettask ( struct queue *q , int qid , int blocking ) {
                     int i = 2*k;
                     if ( i >= q->count )
                         break;
-                    if ( i+1 < q->count && qtasks[ qtid[i+1] ].rank < qtasks[ qtid[i] ].rank )
+                    if ( i+1 < q->count && qtasks[ qtid[i+1] ].maxdepth > qtasks[ qtid[i] ].maxdepth )
                         i += 1;
-                    if ( qtasks[ qtid[i] ].rank < qtasks[ qtid[k] ].rank ) {
+                    if ( qtasks[ qtid[i] ].maxdepth > qtasks[ qtid[k] ].maxdepth ) {
                         int temp = qtid[i];
                         qtid[i] = qtid[k];
                         qtid[k] = temp;
diff --git a/src/scheduler.c b/src/scheduler.c
index a100d55039..6a0435a259 100644
--- a/src/scheduler.c
+++ b/src/scheduler.c
@@ -473,6 +473,15 @@ void scheduler_ranktasks ( struct scheduler *s ) {
             
         }
         
+    /* Run throught the tasks backwards and set their maxdepth. */
+    for ( k = nr_tasks-1 ; k >= 0 ; k-- ) {
+        t = &tasks[ tid[k] ];
+        t->maxdepth = 0;
+        for ( j = 0 ; j < t->nr_unlock_tasks ; j++ )
+            if ( t->unlock_tasks[j]->maxdepth > t->maxdepth )
+                t->maxdepth = t->unlock_tasks[j]->maxdepth;
+        }
+        
     }
 
 
diff --git a/src/task.h b/src/task.h
index eafe04c776..c297b1abad 100644
--- a/src/task.h
+++ b/src/task.h
@@ -51,7 +51,7 @@ extern const char *taskID_names[];
 struct task {
 
     char type, subtype, skip, tight;
-    int flags, wait, rank;
+    int flags, wait, rank, maxdepth;
     
     lock_type lock;
     
-- 
GitLab