1 // RUN: %clang %flags -shared -fPIC %s -o %T/first_tool.so
2 // RUN: %clang %flags -DTOOL -DSECOND_TOOL -shared -fPIC %s -o %T/second_tool.so
3 // RUN: %clang %flags -DTOOL -DTHIRD_TOOL -shared -fPIC %s -o %T/third_tool.so
4 // RUN: %libomp-compile -DCODE && env OMP_TOOL_LIBRARIES=%T/non_existing_file.so:%T/first_tool.so:%T/second_tool.so:%T/third_tool.so %libomp-run | FileCheck %s
5 
6 // REQUIRES: ompt
7 
8 /*
9  *  This file contains code for three OMPT shared library tool to be
10  *  loaded and the code for the OpenMP executable.
11  *  No option enables code for the first shared library
12  *  (without an implementation of ompt_start_tool) during compilation
13  *  -DTOOL -DSECOND_TOOL enables the code for the second tool during compilation
14  *  -DTOOL -DTHIRD_TOOL enables the code for the third tool during compilation
15  *  -DCODE enables the code for the executable during compilation
16  */
17 
18 #ifdef CODE
19 #include "stdio.h"
20 #include "omp.h"
21 #include "omp-tools.h"
22 
main()23 int main()
24 {
25   #pragma omp parallel num_threads(2)
26   {
27     #pragma omp master
28     {
29       int result = omp_control_tool(omp_control_tool_start, 0, NULL);
30       printf("0: control_tool()=%d\n", result);
31     }
32   }
33 
34 
35   // Check if libomp supports the callbacks for this test.
36   // CHECK-NOT: {{^}}0: Could not register callback
37 
38   // CHECK: {{^}}0: Do not initialize tool
39 
40   // CHECK: {{^}}0: Do initialize tool
41   // CHECK: {{^}}0: Tool initialized
42   // CHECK: {{^}}0: ompt_event_thread_begin
43   // CHECK-DAG: {{^}}0: ompt_event_thread_begin
44   // CHECK-DAG: {{^}}0: control_tool()=-1
45   // CHECK: {{^}}0: Tool finalized
46 
47 
48   return 0;
49 }
50 
51 #endif /* CODE */
52 
53 #ifdef TOOL
54 
55 #include <omp-tools.h>
56 #include "stdio.h"
57 
58 #ifdef SECOND_TOOL
59 // The second tool has an implementation of ompt_start_tool that returns NULL
ompt_start_tool(unsigned int omp_version,const char * runtime_version)60 ompt_start_tool_result_t* ompt_start_tool(
61   unsigned int omp_version,
62   const char *runtime_version)
63 {
64   printf("0: Do not initialize tool\n");
65   return NULL;
66 }
67 #elif defined(THIRD_TOOL)
68 // The third tool has an implementation of ompt_start_tool that returns a
69 // pointer to a valid instance of ompt_start_tool_result_t
70 
71 static void
on_ompt_callback_thread_begin(ompt_thread_t thread_type,ompt_data_t * thread_data)72 on_ompt_callback_thread_begin(
73   ompt_thread_t thread_type,
74   ompt_data_t *thread_data)
75 {
76   printf("0: ompt_event_thread_begin\n");
77 }
78 
ompt_initialize(ompt_function_lookup_t lookup,ompt_data_t * tool_data)79 int ompt_initialize(
80   ompt_function_lookup_t lookup,
81   ompt_data_t *tool_data)
82 {
83   ompt_set_callback_t ompt_set_callback = (ompt_set_callback_t) lookup("ompt_set_callback");
84   ompt_set_callback(ompt_callback_thread_begin, (ompt_callback_t)on_ompt_callback_thread_begin);
85   printf("0: Tool initialized\n");
86   return 1;
87 }
88 
ompt_finalize(ompt_data_t * tool_data)89 void ompt_finalize(ompt_data_t *tool_data)
90 {
91   printf("0: Tool finalized\n");
92 }
93 
ompt_start_tool(unsigned int omp_version,const char * runtime_version)94 ompt_start_tool_result_t* ompt_start_tool(
95   unsigned int omp_version,
96   const char *runtime_version)
97 {
98   printf("0: Do initialize tool\n");
99   static ompt_start_tool_result_t ompt_start_tool_result = {&ompt_initialize,&ompt_finalize, 0};
100   return &ompt_start_tool_result;
101 }
102 #endif
103 
104 #endif /* TOOL */
105