From 015362fd294ac7f0236db96c1a8e05a135157c90 Mon Sep 17 00:00:00 2001
From: Guillaume Sylvand <guillaume.sylvand@airbus.com>
Date: Wed, 12 Oct 2016 12:08:56 +0000
Subject: [PATCH] Add API to allow user defined progress indicator (default
 indicator is a percentage)

To use your own progress indicator,
- Define a function with prototype "void my_update_progress(int currentValue, int maximumValue)"
- Pass it to chameleon with "ierr=MORSE_Set_update_progress_callback(my_update_progress)"
- Activate progress indicator with MORSE_Enable(MORSE_PROGRESS)
---
 control/auxiliary.c                    | 38 ++++++++++++++++++++++++++
 include/morse.h.in                     |  1 +
 runtime/starpu/control/runtime_async.c | 31 ++++-----------------
 3 files changed, 44 insertions(+), 26 deletions(-)

diff --git a/control/auxiliary.c b/control/auxiliary.c
index 82e0b5f6a..949d8eee4 100644
--- a/control/auxiliary.c
+++ b/control/auxiliary.c
@@ -37,6 +37,7 @@
 
 #include <stdio.h>
 #include <stdlib.h>
+#include <limits.h>
 
 /*******************************************************************************
  *
@@ -235,3 +236,40 @@ int MORSE_My_Mpi_Rank(void)
     return MORSE_SUCCESS;
 #endif
 }
+/*******************************************************************************
+ *  Display a progress percentage in stderr
+ **/
+void update_progress(int currentValue, int maximumValue) {
+  div_t res ;
+  static int progress = -1; /* varie de 0 a 100 au cours du calcul concerne */
+
+  if (maximumValue==0)
+    res.quot=100 ;
+  else {
+    if (currentValue<INT_MAX/100)
+      res=div(currentValue*100, maximumValue) ;
+    /* Calcule le quotient de la division */
+    else
+      res.quot=(int)( (long long) currentValue*100/maximumValue) ;
+  }
+
+  // Print the percentage
+  if (res.quot > progress)
+    fprintf(stderr, "%3d%%\b\b\b\b", res.quot) ;
+  progress=res.quot ;
+
+  if (currentValue>=maximumValue) {
+    progress=-1 ;
+  }
+}
+
+// A function to display the progress indicator.
+// By default it is update_progress()
+// The user can change it with MORSE_Set_update_progress_callback()
+void (*update_progress_callback)(int, int) = update_progress;
+
+int MORSE_Set_update_progress_callback(void (*p)(int, int)) {
+  update_progress_callback=p ;
+  return MORSE_SUCCESS;
+}
+
diff --git a/include/morse.h.in b/include/morse.h.in
index 115984360..f1e5d57b9 100644
--- a/include/morse.h.in
+++ b/include/morse.h.in
@@ -105,6 +105,7 @@ int MORSE_Enable  (MORSE_enum option);
 int MORSE_Disable (MORSE_enum option);
 int MORSE_Set     (MORSE_enum param, int  value);
 int MORSE_Get     (MORSE_enum param, int *value);
+int MORSE_Set_update_progress_callback(void (*p)(int, int)) ;
 
 /* Sequences */
 int MORSE_Sequence_Create  (MORSE_sequence_t **sequence);
diff --git a/runtime/starpu/control/runtime_async.c b/runtime/starpu/control/runtime_async.c
index 894d041f2..03f5a6fa7 100644
--- a/runtime/starpu/control/runtime_async.c
+++ b/runtime/starpu/control/runtime_async.c
@@ -23,7 +23,6 @@
  *
  **/
 #include <stdlib.h>
-#include <limits.h>
 #include "runtime/starpu/include/morse_starpu.h"
 
 /*******************************************************************************
@@ -46,28 +45,8 @@ int RUNTIME_sequence_destroy( MORSE_context_t *morse, MORSE_sequence_t *sequence
     return MORSE_SUCCESS;
 }
 
-void update_progress(int currentValue, int maximumValue) {
-  div_t res ;
-  static int progress = -1; /* varie de 0 a 100 au cours du calcul concerne */
-
-  if (maximumValue==0)
-    res.quot=100 ;
-  else {
-    if (currentValue<INT_MAX/100)
-      res=div(currentValue*100, maximumValue) ;
-    /* Calcule le quotient de la division */
-    else
-      res.quot=(int)( (long long) currentValue*100/maximumValue) ;
-  }
-  // Print the percentage
-  if (res.quot > progress)
-    fprintf(stderr, "%3d%%\b\b\b\b", res.quot) ;
-  progress=res.quot ;
-
-  if (currentValue>=maximumValue) {
-    progress=-1 ;
-  }
-}
+// Defined in control/auxilliary.c
+extern void (*update_progress_callback)(int, int) ;
 
 // no progress indicator for algorithms faster than 'PROGRESS_MINIMUM_DURATION' seconds
 #define PROGRESS_MINIMUM_DURATION 10
@@ -85,16 +64,16 @@ int RUNTIME_progress( MORSE_context_t *morse)
   int max = starpu_task_nsubmitted();
   if (max==0)
     return MORSE_SUCCESS;
-  //  update_progress(0, max);
+  //  update_progress_callback(0, max);
   while ((tasksLeft = starpu_task_nsubmitted()) > 0) {
     current = max - tasksLeft;
     if (timer > PROGRESS_MINIMUM_DURATION)
-      update_progress(current, max);
+      update_progress_callback(current, max);
     sleep(1);
     timer++;
   }
   if (timer > PROGRESS_MINIMUM_DURATION)
-    update_progress(max, max);
+    update_progress_callback(max, max);
 
   return MORSE_SUCCESS;
 }
-- 
GitLab