1 // The OpenMP standard defines 3 ways of providing ompt_start_tool:
2 
3 // 1. "statically-linking the tool’s definition of ompt_start_tool into an
4 //     OpenMP application"
5 
6 // RUN: %libomp-compile -DCODE -DTOOL && env OMP_TOOL_VERBOSE_INIT=stdout \
7 // RUN:    %libomp-run | FileCheck %s --check-prefixes CHECK,ADDRSPACE
8 
9 // Note: We should compile the tool without -fopenmp as other tools developer
10 //      would do. Otherwise this test may pass for the wrong reasons on Darwin.
11 
12 // RUN: %clang %flags -DTOOL -shared -fPIC %s -o %T/tool.so
13 
14 // 2. "introducing a dynamically-linked library that includes the tool’s
15 //    definition of ompt_start_tool into the application’s address space"
16 
17 // 2.1 Link with tool during compilation
18 
19 // RUN: %libomp-compile -DCODE %no-as-needed-flag %T/tool.so && \
20 // RUN:    env OMP_TOOL_VERBOSE_INIT=stdout %libomp-run | FileCheck %s \
21 // RUN:    --check-prefixes CHECK,ADDRSPACE
22 
23 // 2.2 Link with tool during compilation, but AFTER the runtime
24 
25 // RUN: %libomp-compile -DCODE -lomp %no-as-needed-flag %T/tool.so && \
26 // RUN:    env OMP_TOOL_VERBOSE_INIT=stdout %libomp-run | FileCheck %s \
27 // RUN:    --check-prefixes CHECK,ADDRSPACE
28 
29 // 2.3 Inject tool via the dynamic loader
30 
31 // RUN: %libomp-compile -DCODE && env OMP_TOOL_VERBOSE_INIT=stdout \
32 // RUN:    %preload-tool %libomp-run | FileCheck %s \
33 // RUN:    --check-prefixes CHECK,ADDRSPACE
34 
35 // 3. "providing the name of a dynamically-linked library appropriate for the
36 //    architecture and operating system used by the application in the
37 //    tool-libraries-var ICV"
38 
39 // 3.1 OMP_TOOL_VERBOSE_INIT not set
40 
41 // RUN: %libomp-compile -DCODE && \
42 // RUN:    env OMP_TOOL_LIBRARIES=%T/tool.so %libomp-run | FileCheck %s
43 
44 // 3.2 OMP_TOOL_VERBOSE_INIT disabled
45 
46 // RUN: env OMP_TOOL_LIBRARIES=%T/tool.so OMP_TOOL_VERBOSE_INIT=disabled \
47 // RUN:    %libomp-run | FileCheck %s
48 
49 // 3.3 OMP_TOOL_VERBOSE_INIT to stdout
50 
51 // RUN: %libomp-compile -DCODE && env OMP_TOOL_LIBRARIES=%T/tool.so \
52 // RUN:    OMP_TOOL_VERBOSE_INIT=stdout %libomp-run | \
53 // RUN:    FileCheck %s -DPARENTPATH=%T --check-prefixes CHECK,TOOLLIB
54 
55 // 3.4 OMP_TOOL_VERBOSE_INIT to stderr, check merged stdout and stderr
56 
57 // RUN: env OMP_TOOL_LIBRARIES=%T/tool.so OMP_TOOL_VERBOSE_INIT=stderr \
58 // RUN:    %libomp-run 2>&1 | \
59 // RUN:    FileCheck %s -DPARENTPATH=%T --check-prefixes CHECK,TOOLLIB
60 
61 // 3.5 OMP_TOOL_VERBOSE_INIT to stderr, check just stderr
62 
63 // RUN: env OMP_TOOL_LIBRARIES=%T/tool.so OMP_TOOL_VERBOSE_INIT=stderr \
64 // RUN:    %libomp-run 2>&1 >/dev/null | \
65 // RUN:    FileCheck %s -DPARENTPATH=%T --check-prefixes TOOLLIB
66 
67 // 3.6 OMP_TOOL_VERBOSE_INIT to file "init.log"
68 
69 // RUN: env OMP_TOOL_LIBRARIES=%T/tool.so OMP_TOOL_VERBOSE_INIT=%T/init.log \
70 // RUN:    %libomp-run | FileCheck %s && cat %T/init.log | \
71 // RUN:    FileCheck %s -DPARENTPATH=%T --check-prefixes TOOLLIB
72 
73 
74 // REQUIRES: ompt
75 
76 /*
77  *  This file contains code for an OMPT shared library tool to be
78  *  loaded and the code for the OpenMP executable.
79  *  -DTOOL enables the code for the tool during compilation
80  *  -DCODE enables the code for the executable during compilation
81  */
82 
83 // Check if libomp supports the callbacks for this test.
84 // CHECK-NOT: {{^}}0: Could not register callback
85 
86 // ADDRSPACE: ----- START LOGGING OF TOOL REGISTRATION -----
87 // ADDRSPACE-NEXT: Search for OMP tool in current address space... Sucess.
88 // ADDRSPACE-NEXT: Tool was started and is using the OMPT interface.
89 // ADDRSPACE-NEXT: ----- END LOGGING OF TOOL REGISTRATION -----
90 
91 // TOOLLIB: ----- START LOGGING OF TOOL REGISTRATION -----
92 // TOOLLIB-NEXT: Search for OMP tool in current address space... Failed.
93 // TOOLLIB-NEXT: Searching tool libraries...
94 // TOOLLIB-NEXT: OMP_TOOL_LIBRARIES = [[PARENTPATH]]/tool.so
95 // TOOLLIB-NEXT: Opening [[PARENTPATH]]/tool.so... Success.
96 // TOOLLIB-NEXT: Searching for ompt_start_tool in
97 // TOOLLIB-SAME: [[PARENTPATH]]/tool.so... Success.
98 // TOOLLIB-NEXT: Tool was started and is using the OMPT interface.
99 // TOOLLIB-NEXT: ----- END LOGGING OF TOOL REGISTRATION -----
100 
101 #ifdef CODE
102 #include "omp.h"
103 
main()104 int main()
105 {
106   #pragma omp parallel num_threads(2)
107   {
108   }
109 
110   // CHECK-NOT: ----- START LOGGING OF TOOL REGISTRATION -----
111   // CHECK-NOT: ----- END LOGGING OF TOOL REGISTRATION -----
112 
113   // CHECK: {{^}}0: NULL_POINTER=[[NULL:.*$]]
114   // CHECK: {{^}}0: ompt_event_runtime_shutdown
115 
116   return 0;
117 }
118 
119 #endif /* CODE */
120 
121 #ifdef TOOL
122 
123 #include <stdio.h>
124 #include <omp-tools.h>
125 
ompt_initialize(ompt_function_lookup_t lookup,int initial_device_num,ompt_data_t * tool_data)126 int ompt_initialize(ompt_function_lookup_t lookup, int initial_device_num,
127                     ompt_data_t *tool_data) {
128   printf("0: NULL_POINTER=%p\n", (void*)NULL);
129   return 1; //success
130 }
131 
ompt_finalize(ompt_data_t * tool_data)132 void ompt_finalize(ompt_data_t* tool_data)
133 {
134   printf("0: ompt_event_runtime_shutdown\n");
135 }
136 
ompt_start_tool(unsigned int omp_version,const char * runtime_version)137 ompt_start_tool_result_t* ompt_start_tool(
138   unsigned int omp_version,
139   const char *runtime_version)
140 {
141   static ompt_start_tool_result_t ompt_start_tool_result = {&ompt_initialize,&ompt_finalize, 0};
142   return &ompt_start_tool_result;
143 }
144 #endif /* TOOL */
145