[OMPT] Fix ompt_get_task_info() and add tests for it

The thread_num parameter of ompt_get_task_info() was not being used previously,
but need to be set.

The print_task_type() function (form the task-types.c testcase) was merged into
the print_ids() function (in callback.h). Testing of ompt_get_task_info() was
added to the task-types.c testcase. It was not tested extensively previously.

Differential Revision: https://reviews.llvm.org/D42472

git-svn-id: https://llvm.org/svn/llvm-project/openmp/trunk@326338 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/runtime/src/ompt-specific.cpp b/runtime/src/ompt-specific.cpp
index 5488c6e..c284e3e 100644
--- a/runtime/src/ompt-specific.cpp
+++ b/runtime/src/ompt-specific.cpp
@@ -396,6 +396,9 @@
     if (parallel_data) {
       *parallel_data = team_info ? &(team_info->parallel_data) : NULL;
     }
+    if (thread_num) {
+      *thread_num = __kmp_get_gtid();
+    }
     return info ? 2 : 0;
   }
   return 0;
diff --git a/runtime/test/ompt/callback.h b/runtime/test/ompt/callback.h
index e11014f..bca618c 100755
--- a/runtime/test/ompt/callback.h
+++ b/runtime/test/ompt/callback.h
@@ -33,6 +33,28 @@
   "ompt_cancel_discarded_task"
 };
 
+static void format_task_type(int type, char *buffer) {
+  char *progress = buffer;
+  if (type & ompt_task_initial)
+    progress += sprintf(progress, "ompt_task_initial");
+  if (type & ompt_task_implicit)
+    progress += sprintf(progress, "ompt_task_implicit");
+  if (type & ompt_task_explicit)
+    progress += sprintf(progress, "ompt_task_explicit");
+  if (type & ompt_task_target)
+    progress += sprintf(progress, "ompt_task_target");
+  if (type & ompt_task_undeferred)
+    progress += sprintf(progress, "|ompt_task_undeferred");
+  if (type & ompt_task_untied)
+    progress += sprintf(progress, "|ompt_task_untied");
+  if (type & ompt_task_final)
+    progress += sprintf(progress, "|ompt_task_final");
+  if (type & ompt_task_mergeable)
+    progress += sprintf(progress, "|ompt_task_mergeable");
+  if (type & ompt_task_merged)
+    progress += sprintf(progress, "|ompt_task_merged");
+}
+
 static ompt_set_callback_t ompt_set_callback;
 static ompt_get_task_info_t ompt_get_task_info;
 static ompt_get_thread_data_t ompt_get_thread_data;
@@ -49,16 +71,22 @@
 
 static void print_ids(int level)
 {
-  ompt_frame_t* frame ;
-  ompt_data_t* parallel_data;
-  ompt_data_t* task_data;
-  int exists_task = ompt_get_task_info(level, NULL, &task_data, &frame, &parallel_data, NULL);
+  int task_type, thread_num;
+  ompt_frame_t *frame;
+  ompt_data_t *task_parallel_data;
+  ompt_data_t *task_data;
+  int exists_task = ompt_get_task_info(level, &task_type, &task_data, &frame,
+                                       &task_parallel_data, &thread_num);
+  char buffer[2048];
+  format_task_type(task_type, buffer);
   if (frame)
-  {
-    printf("%" PRIu64 ": task level %d: parallel_id=%" PRIu64 ", task_id=%" PRIu64 ", exit_frame=%p, reenter_frame=%p\n", ompt_get_thread_data()->value, level, exists_task ? parallel_data->value : 0, exists_task ? task_data->value : 0, frame->exit_frame, frame->enter_frame);
-  }
-  else
-    printf("%" PRIu64 ": task level %d: parallel_id=%" PRIu64 ", task_id=%" PRIu64 ", frame=%p\n", ompt_get_thread_data()->value, level, exists_task ? parallel_data->value : 0, exists_task ? task_data->value : 0, frame);
+    printf("%" PRIu64 ": task level %d: parallel_id=%" PRIu64
+           ", task_id=%" PRIu64 ", exit_frame=%p, reenter_frame=%p, "
+           "task_type=%s=%d, thread_num=%d\n",
+           ompt_get_thread_data()->value, level,
+           exists_task ? task_parallel_data->value : 0,
+           exists_task ? task_data->value : 0, frame->exit_frame,
+           frame->enter_frame, buffer, task_type, thread_num);
 }
 
 #define get_frame_address(level) __builtin_frame_address(level)
@@ -154,21 +182,6 @@
   ((uint64_t)addr) / FUZZY_ADDRESS_DISCARD_BYTES - 1, \
   ((uint64_t)addr) / FUZZY_ADDRESS_DISCARD_BYTES, addr)
 
-
-static void format_task_type(int type, char* buffer)
-{
-  char* progress = buffer;
-  if(type & ompt_task_initial) progress += sprintf(progress, "ompt_task_initial");
-  if(type & ompt_task_implicit) progress += sprintf(progress, "ompt_task_implicit");
-  if(type & ompt_task_explicit) progress += sprintf(progress, "ompt_task_explicit");
-  if(type & ompt_task_target) progress += sprintf(progress, "ompt_task_target");
-  if(type & ompt_task_undeferred) progress += sprintf(progress, "|ompt_task_undeferred");
-  if(type & ompt_task_untied) progress += sprintf(progress, "|ompt_task_untied");
-  if(type & ompt_task_final) progress += sprintf(progress, "|ompt_task_final");
-  if(type & ompt_task_mergeable) progress += sprintf(progress, "|ompt_task_mergeable");
-  if(type & ompt_task_merged) progress += sprintf(progress, "|ompt_task_merged");
-}
-
 static void
 on_ompt_callback_mutex_acquire(
   ompt_mutex_kind_t kind,
diff --git a/runtime/test/ompt/tasks/task_types.c b/runtime/test/ompt/tasks/task_types.c
index 77b82ee..30c6ca7 100644
--- a/runtime/test/ompt/tasks/task_types.c
+++ b/runtime/test/ompt/tasks/task_types.c
@@ -1,112 +1,190 @@
-// RUN: %libomp-compile-and-run | %sort-threads | FileCheck %s
+// RUN: %libomp-compile-and-run | FileCheck %s
 // REQUIRES: ompt
 #include "callback.h"
 #include <omp.h>
 #include <math.h>
 
-__attribute__ ((noinline)) // workaround for bug in icc
-void print_task_type(int id)
-{
-  #pragma omp critical
-  {
-    int task_type;
-    char buffer[2048];
-    ompt_get_task_info(0, &task_type, NULL, NULL, NULL, NULL);
-    format_task_type(task_type, buffer);
-    printf("%" PRIu64 ": id=%d task_type=%s=%d\n", ompt_get_thread_data()->value, id, buffer, task_type);
-  }
-};
-
-int main()
-{
-  //initial task
-  print_task_type(0);
+int main() {
+  // initial task
+  print_ids(0);
 
   int x;
-  //implicit task
-  #pragma omp parallel num_threads(1)
+// implicit task
+#pragma omp parallel num_threads(1)
   {
-    print_task_type(1);
+    print_ids(0);
     x++;
   }
 
-  #pragma omp parallel num_threads(2)
-  #pragma omp master
+#pragma omp parallel num_threads(2)
   {
-    //explicit task
-    #pragma omp task
+// explicit task
+#pragma omp single
+#pragma omp task
     {
-      print_task_type(2);
+      print_ids(0);
+      x++;
+    }
+// explicit task with undeferred
+#pragma omp single
+#pragma omp task if (0)
+    {
+      print_ids(0);
       x++;
     }
 
-    //explicit task with undeferred
-    #pragma omp task if(0)
+// explicit task with untied
+#pragma omp single
+#pragma omp task untied
     {
-      print_task_type(3);
+      // Output of thread_id is needed to know on which thread task is executed
+      printf("%" PRIu64 ": explicit_untied\n", ompt_get_thread_data()->value);
+      print_ids(0);
       x++;
     }
-
-    //explicit task with untied
-    #pragma omp task untied
+// explicit task with final
+#pragma omp single
+#pragma omp task final(1)
     {
-      print_task_type(4);
+      print_ids(0);
       x++;
-    }
-
-    //explicit task with final
-    #pragma omp task final(1)
-    {
-      print_task_type(5);
-      x++;
-      //nested explicit task with final and undeferred
-      #pragma omp task
+// nested explicit task with final and undeferred
+#pragma omp task
       {
-        print_task_type(6);
+        print_ids(0);
         x++;
       }
     }
 
-    //Mergeable task test deactivated for now
-    //explicit task with mergeable
+    // Mergeable task test deactivated for now
+    // explicit task with mergeable
     /*
     #pragma omp task mergeable if((int)sin(0))
     {
-      print_task_type(7);
+      print_ids(0);
       x++;
     }
     */
 
-    //TODO: merged task
+    // TODO: merged task
   }
 
-
-
   // Check if libomp supports the callbacks for this test.
   // CHECK-NOT: {{^}}0: Could not register callback 'ompt_callback_task_create'
 
-
   // CHECK: {{^}}0: NULL_POINTER=[[NULL:.*$]]
-  
-  // CHECK: {{^}}[[MASTER_ID:[0-9]+]]: ompt_event_task_create: parent_task_id=0, parent_task_frame.exit=[[NULL]], parent_task_frame.reenter=[[NULL]], new_task_id={{[0-9]+}}, codeptr_ra=[[NULL]], task_type=ompt_task_initial=1, has_dependences=no
+
+  // CHECK: {{^}}[[MASTER_ID:[0-9]+]]: ompt_event_task_create: parent_task_id=0
+  // CHECK-SAME: parent_task_frame.exit=[[NULL]]
+  // CHECK-SAME: parent_task_frame.reenter=[[NULL]]
+  // CHECK-SAME: new_task_id=[[INITIAL_TASK_ID:[0-9]+]], codeptr_ra=[[NULL]]
+  // CHECK-SAME: task_type=ompt_task_initial=1, has_dependences=no
+
   // CHECK-NOT: 0: parallel_data initially not null
-  // CHECK: {{^}}[[MASTER_ID]]: id=0 task_type=ompt_task_initial=1
-  // CHECK: {{^}}[[MASTER_ID]]: id=1 task_type=ompt_task_implicit|ompt_task_undeferred=134217730
 
-  // CHECK-DAG: {{^[0-9]+}}: ompt_event_task_create: parent_task_id={{[0-9]+}}, parent_task_frame.exit={{0x[0-f]+}}, parent_task_frame.reenter={{0x[0-f]+}}, new_task_id={{[0-9]+}}, codeptr_ra={{0x[0-f]+}}, task_type=ompt_task_explicit=4, has_dependences=no
-  // CHECK-DAG: {{^[0-9]+}}: id=2 task_type=ompt_task_explicit=4
+  // initial task
+  // CHECK: {{^}}[[MASTER_ID]]: task level 0: parallel_id={{[0-9]+}}
+  // CHECK-SAME: task_id=[[INITIAL_TASK_ID]], exit_frame=[[NULL]]
+  // CHECK-SAME: reenter_frame=[[NULL]]
+  // CHECK-SAME: task_type=ompt_task_initial=1, thread_num=0
 
-  // CHECK-DAG: {{^[0-9]+}}: ompt_event_task_create: parent_task_id={{[0-9]+}}, parent_task_frame.exit={{0x[0-f]+}}, parent_task_frame.reenter={{0x[0-f]+}}, new_task_id={{[0-9]+}}, codeptr_ra={{0x[0-f]+}}, task_type=ompt_task_explicit|ompt_task_undeferred=134217732, has_dependences=no
-  // CHECK-DAG: {{^[0-9]+}}: id=3 task_type=ompt_task_explicit|ompt_task_undeferred=134217732
+  // implicit task
+  // CHECK: {{^}}[[MASTER_ID]]: task level 0: parallel_id={{[0-9]+}}
+  // CHECK-SAME: task_id={{[0-9]+}}, exit_frame={{0x[0-f]+}}
+  // CHECK-SAME: reenter_frame=[[NULL]]
+  // CHECK-SAME: task_type=ompt_task_implicit|ompt_task_undeferred=134217730
+  // CHECK-SAME: thread_num=0
 
-  // CHECK-DAG: {{^[0-9]+}}: ompt_event_task_create: parent_task_id={{[0-9]+}}, parent_task_frame.exit={{0x[0-f]+}}, parent_task_frame.reenter={{0x[0-f]+}}, new_task_id={{[0-9]+}}, codeptr_ra={{0x[0-f]+}}, task_type=ompt_task_explicit|ompt_task_untied=268435460, has_dependences=no
-  // CHECK-DAG: {{^[0-9]+}}: id=4 task_type=ompt_task_explicit|ompt_task_untied=268435460
+  // explicit task
+  // CHECK: {{^[0-9]+}}: ompt_event_task_create: parent_task_id={{[0-9]+}}
+  // CHECK-SAME: parent_task_frame.exit={{0x[0-f]+}}
+  // CHECK-SAME: parent_task_frame.reenter={{0x[0-f]+}}
+  // CHECK-SAME: new_task_id=[[EXPLICIT_TASK_ID:[0-9]+]]
+  // CHECK-SAME: codeptr_ra={{0x[0-f]+}}
+  // CHECK-SAME: task_type=ompt_task_explicit=4
+  // CHECK-SAME: has_dependences=no
 
-  // CHECK-DAG: {{^[0-9]+}}: ompt_event_task_create: parent_task_id={{[0-9]+}}, parent_task_frame.exit={{0x[0-f]+}}, parent_task_frame.reenter={{0x[0-f]+}}, new_task_id={{[0-9]+}}, codeptr_ra={{0x[0-f]+}}, task_type=ompt_task_explicit|ompt_task_final=536870916, has_dependences=no
-  // CHECK-DAG: {{^[0-9]+}}: id=5 task_type=ompt_task_explicit|ompt_task_final=536870916
+  // CHECK: [[THREAD_ID_1:[0-9]+]]: ompt_event_task_schedule:
+  // CHECK-SAME: second_task_id=[[EXPLICIT_TASK_ID]]
 
-  // CHECK-DAG: {{^[0-9]+}}: ompt_event_task_create: parent_task_id={{[0-9]+}}, parent_task_frame.exit={{0x[0-f]+}}, parent_task_frame.reenter={{0x[0-f]+}}, new_task_id={{[0-9]+}}, codeptr_ra={{0x[0-f]+}}, task_type=ompt_task_explicit|ompt_task_undeferred|ompt_task_final=671088644, has_dependences=no
-  // CHECK-DAG: {{^[0-9]+}}: id=6 task_type=ompt_task_explicit|ompt_task_undeferred|ompt_task_final=671088644
+  // CHECK: [[THREAD_ID_1]]: task level 0: parallel_id=[[PARALLEL_ID:[0-9]+]]
+  // CHECK-SAME: task_id=[[EXPLICIT_TASK_ID]], exit_frame={{0x[0-f]+}}
+  // CHECK-SAME: reenter_frame=[[NULL]], task_type=ompt_task_explicit=4
+  // CHECK-SAME: thread_num={{[01]}}
+
+  // explicit task with undeferred
+  // CHECK: {{^[0-9]+}}: ompt_event_task_create: parent_task_id={{[0-9]+}}
+  // CHECK-SAME: parent_task_frame.exit={{0x[0-f]+}}
+  // CHECK-SAME: parent_task_frame.reenter={{0x[0-f]+}}
+  // CHECK-SAME: new_task_id=[[EXPLICIT_UNDEFERRED_TASK_ID:[0-9]+]]
+  // CHECK-SAME: codeptr_ra={{0x[0-f]+}}
+  // CHECK-SAME: task_type=ompt_task_explicit|ompt_task_undeferred=134217732
+  // CHECK-SAME: has_dependences=no
+
+  // CHECK: [[THREAD_ID_2:[0-9]+]]: ompt_event_task_schedule:
+  // CHECK-SAME: second_task_id=[[EXPLICIT_UNDEFERRED_TASK_ID]]
+
+  // CHECK: [[THREAD_ID_2]]: task level 0: parallel_id=[[PARALLEL_ID]]
+  // CHECK-SAME: task_id=[[EXPLICIT_UNDEFERRED_TASK_ID]]
+  // CHECK-SAME: exit_frame={{0x[0-f]+}}, reenter_frame=[[NULL]]
+  // CHECK-SAME: task_type=ompt_task_explicit|ompt_task_undeferred=134217732
+  // CHECK-SAME: thread_num={{[01]}}
+
+  // explicit task with untied
+  // CHECK: {{^[0-9]+}}: ompt_event_task_create: parent_task_id={{[0-9]+}}
+  // CHECK-SAME: parent_task_frame.exit={{0x[0-f]+}}
+  // CHECK-SAME: parent_task_frame.reenter={{0x[0-f]+}}
+  // CHECK-SAME: new_task_id=[[EXPLICIT_UNTIED_TASK_ID:[0-9]+]]
+  // CHECK-SAME: codeptr_ra={{0x[0-f]+}}
+  // CHECK-SAME: task_type=ompt_task_explicit|ompt_task_untied=268435460
+  // CHECK-SAME: has_dependences=no
+
+  // Here the thread_id cannot be taken from a schedule event as there
+  // may be multiple of those
+  // CHECK: [[THREAD_ID_3:[0-9]+]]: explicit_untied
+  // CHECK: [[THREAD_ID_3]]: task level 0: parallel_id=[[PARALLEL_ID]]
+  // CHECK-SAME: task_id=[[EXPLICIT_UNTIED_TASK_ID]], exit_frame={{[^\,]*}}
+  // CHECK-SAME: reenter_frame=[[NULL]]
+  // CHECK-SAME: task_type=ompt_task_explicit|ompt_task_untied=268435460
+  // CHECK-SAME: thread_num={{[01]}}
+
+  // explicit task with final
+  // CHECK: {{^[0-9]+}}: ompt_event_task_create: parent_task_id={{[0-9]+}}
+  // CHECK-SAME: parent_task_frame.exit={{0x[0-f]+}}
+  // CHECK-SAME: parent_task_frame.reenter={{0x[0-f]+}}
+  // CHECK-SAME: new_task_id=[[EXPLICIT_FINAL_TASK_ID:[0-9]+]]
+  // CHECK-SAME: codeptr_ra={{0x[0-f]+}}
+  // CHECK-SAME: task_type=ompt_task_explicit|ompt_task_final=536870916
+  // CHECK-SAME: has_dependences=no
+
+  // CHECK: [[THREAD_ID_4:[0-9]+]]: ompt_event_task_schedule:
+  // CHECK-SAME: second_task_id=[[EXPLICIT_FINAL_TASK_ID]]
+
+  // CHECK: [[THREAD_ID_4]]: task level 0: parallel_id=[[PARALLEL_ID]]
+  // CHECK-SAME: task_id=[[EXPLICIT_FINAL_TASK_ID]]
+  // CHECK-SAME: exit_frame={{0x[0-f]+}}, reenter_frame=[[NULL]]
+  // CHECK-SAME: task_type=ompt_task_explicit|ompt_task_final=536870916
+  // CHECK-SAME: thread_num={{[01]}}
+
+  // nested explicit task with final and undeferred
+  // CHECK: {{^[0-9]+}}: ompt_event_task_create: parent_task_id={{[0-9]+}}
+  // CHECK-SAME: parent_task_frame.exit={{0x[0-f]+}}
+  // CHECK-SAME: parent_task_frame.reenter={{0x[0-f]+}}
+  // CHECK-SAME: new_task_id=[[NESTED_FINAL_UNDEFERRED_TASK_ID:[0-9]+]]
+  // CHECK-SAME: codeptr_ra={{0x[0-f]+}}
+  // CHECK-SAME: task_type=ompt_task_explicit|ompt_task_undeferred
+  // CHECK-SAME:|ompt_task_final=671088644
+  // CHECK-SAME: has_dependences=no
+
+  // CHECK: [[THREAD_ID_5:[0-9]+]]: ompt_event_task_schedule:
+  // CHECK-SAME: second_task_id=[[NESTED_FINAL_UNDEFERRED_TASK_ID]]
+
+  // CHECK: [[THREAD_ID_5]]: task level 0: parallel_id=[[PARALLEL_ID]]
+  // CHECK-SAME: task_id=[[NESTED_FINAL_UNDEFERRED_TASK_ID]]
+  // CHECK-SAME: exit_frame={{0x[0-f]+}}, reenter_frame=[[NULL]]
+  // CHECK-SAME: task_type=ompt_task_explicit|ompt_task_undeferred
+  // CHECK-SAME:|ompt_task_final=671088644
+  // CHECK-SAME: thread_num={{[01]}}
 
   return 0;
 }