From 32781b6dab10b1b51a07ef074078f28efd8f9b4e Mon Sep 17 00:00:00 2001
From: Thierry <thierry.gautier@inrialpes.fr>
Date: Thu, 11 Oct 2018 16:33:03 +0200
Subject: [PATCH] [fix] provide patch to add priority to task without support
 frm the compiler, such as ICC 2017 ou 2018

---
 runtime/src/include/30/omp.h.var |  3 ++
 runtime/src/include/40/omp.h.var |  3 ++
 runtime/src/include/45/omp.h.var |  3 ++
 runtime/src/include/50/omp.h.var |  3 ++
 runtime/src/kmp.h                |  7 +++++
 runtime/src/kmp_ftn_entry.h      | 10 +++++++
 runtime/src/kmp_ftn_os.h         |  4 +++
 runtime/src/kmp_tasking.cpp      | 47 ++++++++++++++++++++++++++++----
 8 files changed, 75 insertions(+), 5 deletions(-)

diff --git a/runtime/src/include/30/omp.h.var b/runtime/src/include/30/omp.h.var
index 10bd890..c05c2e5 100644
--- a/runtime/src/include/30/omp.h.var
+++ b/runtime/src/include/30/omp.h.var
@@ -113,6 +113,9 @@
     extern double __KAI_KMPC_CONVENTION  omp_get_wtime (void);
     extern double __KAI_KMPC_CONVENTION  omp_get_wtick (void);
 
+    /* OpenMP 3.0 extension */
+    extern void __KAI_KMPC_CONVENTION  omp_set_task_priority (int priority);
+
 #   include <stdlib.h>
     /* kmp API functions */
     extern int    __KAI_KMPC_CONVENTION  kmp_get_stacksize          (void);
diff --git a/runtime/src/include/40/omp.h.var b/runtime/src/include/40/omp.h.var
index a825736..e0b1984 100644
--- a/runtime/src/include/40/omp.h.var
+++ b/runtime/src/include/40/omp.h.var
@@ -105,6 +105,9 @@
     extern double __KAI_KMPC_CONVENTION  omp_get_wtime (void);
     extern double __KAI_KMPC_CONVENTION  omp_get_wtick (void);
 
+    /* OpenMP 3.0 extension */
+    extern void __KAI_KMPC_CONVENTION  omp_set_task_priority (int priority);
+
     /* OpenMP 4.0 extension */
 #if OMP_EXTENSION_AFFINITY
     typedef enum omp_affinity_kind_t {
diff --git a/runtime/src/include/45/omp.h.var b/runtime/src/include/45/omp.h.var
index 8448821..746b486 100644
--- a/runtime/src/include/45/omp.h.var
+++ b/runtime/src/include/45/omp.h.var
@@ -123,6 +123,9 @@
     extern double __KAI_KMPC_CONVENTION  omp_get_wtick (void);
 
 
+    /* OpenMP 3.0 extension */
+    extern void __KAI_KMPC_CONVENTION  omp_set_task_priority (int priority);
+
     /* OpenMP 4.0 extension */
 #if OMP_EXTENSION_AFFINITY
     typedef enum omp_affinity_kind_t {
diff --git a/runtime/src/include/50/omp.h.var b/runtime/src/include/50/omp.h.var
index 9d9f0bb..765e7fe 100644
--- a/runtime/src/include/50/omp.h.var
+++ b/runtime/src/include/50/omp.h.var
@@ -123,6 +123,9 @@
     extern double __KAI_KMPC_CONVENTION  omp_get_wtime (void);
     extern double __KAI_KMPC_CONVENTION  omp_get_wtick (void);
 
+    /* OpenMP 3.0 extension */
+    extern void __KAI_KMPC_CONVENTION  omp_set_task_priority (int priority);
+
     /* OpenMP 4.0 extension */
 #if OMP_EXTENSION_AFFINITY
     typedef enum omp_affinity_kind_t {
diff --git a/runtime/src/kmp.h b/runtime/src/kmp.h
index 82ccda3..ee35b33 100644
--- a/runtime/src/kmp.h
+++ b/runtime/src/kmp.h
@@ -2063,6 +2063,11 @@ typedef struct kmp_local {
 #define set__sched(xthread, xval)                                              \
   (((xthread)->th.th_current_task->td_icvs.sched) = (xval))
 
+#define set__priority( xthread, xval )                                             \
+        ( ( (xthread)->th.th_task_priority ) = (xval) )
+#define get__priority( xthread )                                                   \
+        ( (xthread)->th.th_task_priority )
+
 #if OMP_40_ENABLED
 
 #define set__proc_bind(xthread, xval)                                          \
@@ -2667,6 +2672,7 @@ typedef struct KMP_ALIGN_CACHE kmp_base_info {
   kmp_extra_depinfo_th_t  th_edeps[2];          // [alias/noalias]
   kmp_extra_depinfo_th_t* th_edeps_tail[2];     // TG: tail pointers
 #endif
+  kmp_int32             th_task_priority;       //
 #if OMPT_SUPPORT
   const char*           th_task_name;           //
   intptr_t              th_task_key[2];         //
@@ -3887,6 +3893,7 @@ void __kmpc_omp_task_complete(ident_t *loc_ref, kmp_int32 gtid,
 KMP_EXPORT void __kmpc_omp_set_task_affinity(kmp_uint32 kind, kmp_uint64 affinity, kmp_uint32 strict);
 
 KMP_EXPORT void __kmpc_omp_set_task_name(char *name);
+KMP_EXPORT void __kmpc_omp_set_task_priority(int priority);
 KMP_EXPORT void __kmpc_omp_set_task_attr(char key, long int value);
 KMP_EXPORT void* __kmpc_omp_begin_sched_graph(int flag);
 KMP_EXPORT void __kmpc_omp_end_sched_graph(void* handle, int flag );
diff --git a/runtime/src/kmp_ftn_entry.h b/runtime/src/kmp_ftn_entry.h
index e22dfa1..cd96c7c 100644
--- a/runtime/src/kmp_ftn_entry.h
+++ b/runtime/src/kmp_ftn_entry.h
@@ -636,6 +636,16 @@ FTN_SET_TASK_NAME(char *name)
     #endif
 }
 
+void FTN_STDCALL
+FTN_SET_TASK_PRIORITY(int priority)
+{
+    #ifdef KMP_STUB
+        // nothing
+    #else
+        __kmpc_omp_set_task_priority(priority);
+    #endif
+}
+
 void FTN_STDCALL
 FTN_SET_TASK_ATTR(char key, long int value)
 {
diff --git a/runtime/src/kmp_ftn_os.h b/runtime/src/kmp_ftn_os.h
index 68de621..c305815 100644
--- a/runtime/src/kmp_ftn_os.h
+++ b/runtime/src/kmp_ftn_os.h
@@ -85,6 +85,7 @@
 #define FTN_INFO_ALLOC_SIZE omp_info_alloc_memory
 #define FTN_INFO_FREE_SIZE omp_info_free_memory
 #define FTN_SET_MEMORY_LIMIT omp_set_memory_limit
+#define FTN_SET_TASK_PRIORITY omp_set_task_priority
 #if OMP_40_ENABLED
 #if LIBOMP_USE_VARDEP
 #define FN_TASK_DECLDEPS omp_task_declare_dependencies
@@ -220,6 +221,7 @@
 #define FTN_INFO_ALLOC_SIZE omp_info_alloc_memory_
 #define FTN_INFO_FREE_SIZE omp_info_free_memory_
 #define FTN_SET_MEMORY_LIMIT omp_set_memory_limit_
+#define FTN_SET_TASK_PRIORITY omp_set_task_priority_
 #if OMP_40_ENABLED
 #if LIBOMP_USE_VARDEP
 #define FN_TASK_DECLDEPS omp_task_declare_dependencies_
@@ -356,6 +358,7 @@
 #define FTN_INFO_ALLOC_SIZE OMP_INFO_ALLOC_MEMORY
 #define FTN_INFO_FREE_SIZE OMP_INFO_FREE_MEMORY
 #define FTN_SET_MEMORY_LIMIT OMP_SET_MEMORY_LIMIT
+#define FTN_SET_TASK_PRIORITY OMP_SET_TASK_PRIORITY
 #if OMP_40_ENABLED
 #if LIBOMP_USE_VARDEP
 #define FN_TASK_DECLDEPS OMP_TASK_DECLARE_DEPENDENCIES
@@ -492,6 +495,7 @@
 #define FTN_INFO_ALLOC_SIZE OMP_INFO_ALLOC_MEMORY_
 #define FTN_INFO_FREE_SIZE OMP_INFO_FREE_MEMORY_
 #define FTN_SET_MEMORY_LIMIT OMP_SET_MEMORY_LIMIT_
+#define FTN_SET_TASK_PRIORITY OMP_SET_TASK_PRIORITY_
 #if OMP_40_ENABLED
 #if LIBOMP_USE_VARDEP
 #define FN_TASK_DECLDEPS OMP_TASK_DECLARE_DEPENDENCIES_
diff --git a/runtime/src/kmp_tasking.cpp b/runtime/src/kmp_tasking.cpp
index 60e19ef..06d4773 100644
--- a/runtime/src/kmp_tasking.cpp
+++ b/runtime/src/kmp_tasking.cpp
@@ -354,14 +354,18 @@ __kmp_push_task(kmp_int32 gtid, kmp_task_t * task )
 #endif
 
 #if OMP_45_ENABLED
+  kmp_int32 priority;
   if (taskdata->td_flags.priority_specified)
   {
-    kmp_int32 priority = task->data2.priority;
-    if (priority < 0) priority = 0;
-    else if (priority > __kmp_max_task_priority)
-      priority = __kmp_max_task_priority;
-    taskdata->priority = priority;
+//printf("Priority\n");
+    priority = task->data2.priority;
   }
+  else priority = taskdata->priority;
+
+  if (priority < 0) priority = 0;
+  else if (priority > __kmp_max_task_priority)
+    priority = __kmp_max_task_priority;
+  taskdata->priority = priority;
 #endif
 
 #if LIBOMP_USE_AFFINITY
@@ -1502,6 +1506,10 @@ kmp_task_t* __kmp_task_alloc( ident_t *loc_ref, kmp_int32 gtid, kmp_tasking_flag
 #endif
     copy_icvs(&taskdata->td_icvs, &taskdata->td_parent->td_icvs);
 
+  // for ICC 2017,2018 -> pass through OMP extension
+  taskdata->priority = thread->th.th_task_priority;
+  thread->th.th_task_priority   = 0;
+
 #if OMPT_SUPPORT
   /* reset task name after creation */
   taskdata->td_icvs.name    = thread->th.th_task_name;
@@ -2260,6 +2268,17 @@ __kmpc_end_push_init()
 {
 }
 
+
+//-------------------------------------------------------------------------------------
+// __kmpc_omp_set_task_priority: set the priority for the next task to create
+void
+__kmpc_omp_set_task_priority(int priority)
+{
+    kmp_info_t      * thread = __kmp_threads[ __kmp_entry_gtid() ];
+    set__priority(thread, priority);
+}
+
+
 //-------------------------------------------------------------------------------------
 // __kmpc_omp_set_task_name: set the name for the next task to create
 
@@ -2875,8 +2894,26 @@ __kmp_steal_task( kmp_info_t *victim, kmp_int32 cpu, kmp_int32 node,
       // GEH: why would this happen?
     return 0;
   }
+
+#if OMP_45_ENABLED
+  /* look at task priority set before my local queue */
+  for (kmp_int32 i = __kmp_max_task_priority-1; i>=0; --i)
+  {
+    if (task_team->tt.tt_task_queues_priority &&
+        !kaapi_wsqueue_empty( &(task_team->tt.tt_task_queues_priority[i]->qd.td_wsdeque)))
+    {
+      taskdata = kaapi_wsqueue_locked_pop_task( &(task_team->tt.tt_task_queues_priority[i]->qd.td_wsdeque) );
+      if (taskdata !=0)
+      {
+        task = KMP_TASKDATA_TO_TASK( taskdata );
+        return task;
+      }
+    }
+  }
 #endif
 
+#endif // LIBOMP_USE_THEQUEUE||LIBOMP_USE_LINKED_DEQUEUE
+
   threads_data = task_team->tt.tt_threads_data;
   KMP_DEBUG_ASSERT(threads_data != NULL); // Caller should check this condition
 
-- 
GitLab