(git:2bb242e)
Loading...
Searching...
No Matches
openmp_trace.c
Go to the documentation of this file.
1/*----------------------------------------------------------------------------*/
2/* CP2K: A general program to perform molecular dynamics simulations */
3/* Copyright 2000-2026 CP2K developers group <https://cp2k.org> */
4/* */
5/* SPDX-License-Identifier: BSD-3-Clause */
6/*----------------------------------------------------------------------------*/
7
8/**
9 * Development tool checking the nesting of OpenMP constructs at runtime using
10 * OMPT, i.e., the OpenMP runtime must be lib(i)omp (also when GNU-compiled
11 * code uses it at runtime), but not libgomp. CP2K_OMP_TRACE=<level>:
12 * 1: abort at the first error, i.e., non-conforming code (deadlock risk)
13 * 2: print errors (once per code location) and a summary at exit
14 * 3: print warnings as well, i.e., valid but likely unwanted code
15 * 4: print information as well, e.g., nested parallel regions
16 * A negative level selects the most verbose level. A code location is printed
17 * as function name (exported symbols only, e.g., -rdynamic) or as module with
18 * offset or address (addr2line -f -e module offset). If the executable is not
19 * linked against lib(i)omp, ompt_start_tool must be exported explicitly, e.g.,
20 * -Wl,--export-dynamic-symbol=ompt_start_tool (or -rdynamic).
21 */
22
23int openmp_trace_issues(void); /* exposed in Fortran interface */
24
25#if !defined(_OPENMP)
26int openmp_trace_issues(void) { return -1 /*disabled*/; }
27#else
28/* no #include <omp.h> since functionality called by the tool would be traced */
29#include <ctype.h>
30#include <stdatomic.h>
31#include <stdint.h>
32#include <stdio.h>
33#include <stdlib.h>
34#include <string.h>
35
36#if defined(__has_include)
37#if __has_include(<omp-tools.h>)
38#define OPENMP_TRACE_OMPT
39#endif
40#if __has_include(<execinfo.h>) && __has_include(<unistd.h>)
41#define OPENMP_TRACE_SYMBOL
42#endif
43#else
44#if defined(__clang__) || defined(__INTEL_COMPILER)
45#define OPENMP_TRACE_OMPT
46#endif
47#if !defined(_WIN32) && !defined(__CYGWIN__)
48#define OPENMP_TRACE_SYMBOL
49#endif
50#endif
51
52#if defined(OPENMP_TRACE_OMPT)
53#include <omp-tools.h>
54#else /* e.g., GNU compiler without omp-tools.h but linked with lib(i)omp */
55typedef int ompt_callbacks_t, ompt_set_result_t, ompt_scope_endpoint_t;
56typedef int ompt_sync_region_t, ompt_work_t;
57typedef struct ompt_frame_t ompt_frame_t;
58typedef union ompt_data_t {
59 uint64_t value;
60 void *ptr;
61} ompt_data_t;
62typedef void (*ompt_interface_fn_t)(void);
63typedef void (*ompt_callback_t)(void);
64typedef ompt_interface_fn_t (*ompt_function_lookup_t)(const char *);
65typedef ompt_set_result_t (*ompt_set_callback_t)(ompt_callbacks_t,
66 ompt_callback_t);
67typedef int (*ompt_get_parallel_info_t)(int, ompt_data_t **, int *);
68typedef int (*ompt_initialize_t)(ompt_function_lookup_t, int, ompt_data_t *);
69typedef void (*ompt_finalize_t)(ompt_data_t *);
70typedef struct ompt_start_tool_result_t {
71 ompt_initialize_t initialize;
72 ompt_finalize_t finalize;
73 ompt_data_t tool_data;
74} ompt_start_tool_result_t;
75ompt_start_tool_result_t *ompt_start_tool(unsigned int omp_version,
76 const char *runtime_version);
77#endif
78
79#if defined(OPENMP_TRACE_SYMBOL)
80#include <execinfo.h>
81#include <unistd.h>
82#endif
83
84/* task slot: count of constructs (4 bits), kind (4 bits), address (56 bits) */
85#define OPENMP_TRACE_PTR(PTR) ((uint64_t)(uintptr_t)(PTR))
86#define OPENMP_TRACE_ADDR(VALUE) (UINT64_C(0x00FFFFFFFFFFFFFF) & (VALUE))
87#define OPENMP_TRACE_KIND(VALUE) ((int)(0xF & ((VALUE) >> 56)))
88#define OPENMP_TRACE_COUNT(VALUE) ((int)((VALUE) >> 60))
89#define OPENMP_TRACE_SLOT(COUNT, KIND, ADDR) \
90 (((uint64_t)(COUNT) << 60) | ((uint64_t)(KIND) << 56) | \
91 OPENMP_TRACE_ADDR(ADDR))
92/* parallel_data: nesting level (upper bits) and runtime-invoked (bit 0) */
93#define OPENMP_TRACE_RUNTIME 1
94#define OPENMP_TRACE_TEAM 0x80000000u
95#define OPENMP_TRACE_INVOKER_RUNTIME 0x2u
96#define OPENMP_TRACE_SEEN 256
97#define OPENMP_TRACE_UNUSED(VAR) (void)VAR
98
99/* values of OpenMP 5.1 (independent of the version of omp-tools.h) */
100enum {
101 openmp_trace_scope_begin = 1,
102 openmp_trace_scope_end = 2,
103 openmp_trace_scope_beginend = 3,
104 openmp_trace_set_never = 1,
105 openmp_trace_callback_parallel_begin = 3,
106 openmp_trace_callback_implicit_task = 7,
107 openmp_trace_callback_work = 20,
108 openmp_trace_callback_masked = 21,
109 openmp_trace_callback_sync_region = 23
110};
111enum {
112 openmp_trace_sync_barrier = 1,
113 openmp_trace_sync_barrier_implicit = 2,
114 openmp_trace_sync_barrier_explicit = 3,
115 openmp_trace_sync_barrier_implementation = 4,
116 openmp_trace_sync_barrier_implicit_workshare = 8
117};
118enum {
119 openmp_trace_work_loop = 1,
120 openmp_trace_work_single_executor = 3,
121 openmp_trace_work_workshare = 5,
122 openmp_trace_work_scope = 8,
123 openmp_trace_work_loop_static = 10,
124 openmp_trace_work_loop_dynamic = 11,
125 openmp_trace_work_loop_guided = 12,
126 openmp_trace_work_loop_other = 13
127};
128
129enum {
130 openmp_trace_level_abort = 1,
131 openmp_trace_level_error,
132 openmp_trace_level_warn,
133 openmp_trace_level_info
134};
135enum { openmp_trace_error = 1, openmp_trace_warn, openmp_trace_info };
136
137/* kinds of constructs (must fit into 4 bits) */
138enum {
139 openmp_trace_none,
140 openmp_trace_master,
141 openmp_trace_single,
142 openmp_trace_loop,
143 openmp_trace_workshare,
144 openmp_trace_scope,
145 openmp_trace_barrier,
146 openmp_trace_parallel
147};
148static const char *const openmp_trace_names[] = {
149 "construct", "master", "single", "loop",
150 "workshare", "scope", "barrier", "parallel region"};
151
152static int openmp_trace_level;
153static atomic_int openmp_trace_errors;
154static atomic_int openmp_trace_warnings;
155static atomic_int openmp_trace_nested;
156static _Atomic(uint64_t) openmp_trace_deepest;
157static _Atomic(uint64_t) openmp_trace_seen[OPENMP_TRACE_SEEN];
158static ompt_get_parallel_info_t openmp_trace_get_parallel_info;
159
160int openmp_trace_issues(void) {
161 return 0 != openmp_trace_level ? atomic_load(&openmp_trace_errors) : -1;
162}
163
164/* nonzero if key is seen the first time (or if the table is full) */
165static int openmp_trace_unique(uint64_t key) {
166 size_t i = (size_t)((key * UINT64_C(0x9E3779B97F4A7C15)) >> 56);
167 int result = -1, n;
168 for (n = 0; 0 > result && n < OPENMP_TRACE_SEEN; ++n) {
169 uint64_t expected = 0;
170 if (atomic_compare_exchange_strong(&openmp_trace_seen[i], &expected, key)) {
171 result = 1;
172 } else if (key == expected) {
173 result = 0;
174 } else {
175 i = (i + 1) % OPENMP_TRACE_SEEN;
176 }
177 }
178 return 0 != result;
179}
180
181/* translate code address to function name or module(+offset) */
182static void openmp_trace_symbol(uint64_t addr, char *str, size_t size) {
183 *str = '\0';
184#if defined(OPENMP_TRACE_SYMBOL)
185 if (0 != addr && 1 < size) {
186 int pipefd[2];
187 if (0 == pipe(pipefd)) { /* backtrace_symbols_fd avoids malloc */
188 void *const frame[] = {(void *)(uintptr_t)addr};
189 ssize_t n, i = 0;
190 backtrace_symbols_fd(frame, 1, pipefd[1]);
191 close(pipefd[1]);
192 n = read(pipefd[0], str, size - 1);
193 close(pipefd[0]);
194 while (i < n && 0 != isprint((unsigned char)str[i])) {
195 ++i;
196 }
197 str[i] = '\0';
198 if (0 < i) { /* module(symbol+offset) [address]: keep all if no symbol */
199 char *const s = strchr(str, '(');
200 char *const t = (NULL != s ? strchr(s + 1, '+') : NULL);
201 if (NULL != t && s + 1 < t) {
202 *t = '\0';
203 memmove(str, s + 1, (size_t)(t - s));
204 }
205 }
206 }
207 }
208#else
209 OPENMP_TRACE_UNUSED(addr);
210 OPENMP_TRACE_UNUSED(size);
211#endif
212}
213
214/* count issue, print it once per location, and abort if requested */
215static void openmp_trace_issue(int category, int what, const void *codeptr,
216 uint64_t outer) {
217 const uint64_t addr = OPENMP_TRACE_PTR(codeptr);
218 if (openmp_trace_error == category) {
219 atomic_fetch_add_explicit(&openmp_trace_errors, 1, memory_order_relaxed);
220 } else {
221 atomic_fetch_add_explicit(&openmp_trace_warnings, 1, memory_order_relaxed);
222 }
223 if ((openmp_trace_error == category ||
224 openmp_trace_level_warn <= openmp_trace_level) &&
225 (openmp_trace_level_abort == openmp_trace_level ||
226 0 != openmp_trace_unique(OPENMP_TRACE_SLOT(category, what, addr)))) {
227 const char *const type =
228 (openmp_trace_error == category ? "ERROR" : "WARN");
229 char a[1024], b[1024];
230 openmp_trace_symbol(OPENMP_TRACE_ADDR(addr), a, sizeof(a));
231 openmp_trace_symbol(OPENMP_TRACE_ADDR(outer), b, sizeof(b));
232 fprintf(stderr, "OMP/TRACE %s: %s%s%s%s nested in %s%s%s%s\n", type,
233 openmp_trace_names[what], '\0' != *a ? " \"" : "", a,
234 '\0' != *a ? "\"" : "",
235 openmp_trace_names[OPENMP_TRACE_KIND(outer)],
236 '\0' != *b ? " \"" : "", b, '\0' != *b ? "\"" : "");
237 }
238 if (openmp_trace_error == category &&
239 openmp_trace_level_abort == openmp_trace_level) {
240 abort();
241 }
242}
243
244/* enter or leave a construct binding to the (implicit) task */
245static void openmp_trace_construct(ompt_scope_endpoint_t endpoint,
246 ompt_data_t *task_data, int what,
247 const void *codeptr) {
248 const uint64_t slot = task_data->value;
249 const int count = OPENMP_TRACE_COUNT(slot);
250 if (openmp_trace_scope_end != (int)endpoint) {
251 if (0 == count) {
252 task_data->value = OPENMP_TRACE_SLOT(1, what, OPENMP_TRACE_PTR(codeptr));
253 } else { /* master nested in master is valid */
254 if (openmp_trace_master != what ||
255 openmp_trace_master != OPENMP_TRACE_KIND(slot)) {
256 openmp_trace_issue(openmp_trace_error, what, codeptr, slot);
257 }
258 if (0xF > count) {
259 task_data->value = slot + OPENMP_TRACE_SLOT(1, 0, 0);
260 }
261 }
262 }
263 if (openmp_trace_scope_begin != (int)endpoint) {
264 const uint64_t value = task_data->value;
265 task_data->value =
266 (1 < OPENMP_TRACE_COUNT(value) ? (value - OPENMP_TRACE_SLOT(1, 0, 0))
267 : 0);
268 }
269}
270
271/* OMPT callback (signature as in omp-tools.h) */
272static void openmp_trace_parallel_begin(
273 ompt_data_t *encountering_task_data,
274 const ompt_frame_t *encountering_task_frame, ompt_data_t *parallel_data,
275 unsigned int requested_parallelism, int flags, const void *codeptr_ra) {
276 OPENMP_TRACE_UNUSED(encountering_task_frame);
277 OPENMP_TRACE_UNUSED(requested_parallelism);
278 if (0 != (OPENMP_TRACE_TEAM & (unsigned int)flags) && NULL != parallel_data) {
279 ompt_data_t *outer = NULL;
280 int level = 1;
281 if (0 != openmp_trace_get_parallel_info(0, &outer, NULL) && NULL != outer) {
282 level += (int)(outer->value >> 1);
283 }
284 /* GNU-compiled code (GOMP-interface) is invoked by the program */
285 parallel_data->value =
286 ((uint64_t)level << 1) |
287 (0 != (OPENMP_TRACE_INVOKER_RUNTIME & (unsigned int)flags)
288 ? OPENMP_TRACE_RUNTIME
289 : 0);
290 if (NULL != encountering_task_data && 0 != encountering_task_data->value) {
291 openmp_trace_issue(openmp_trace_warn, openmp_trace_parallel, codeptr_ra,
292 encountering_task_data->value);
293 }
294 if (1 < level) {
295 const uint64_t addr = OPENMP_TRACE_PTR(codeptr_ra);
296 const uint64_t value = ((uint64_t)(0xFF < level ? 0xFF : level) << 56) |
297 OPENMP_TRACE_ADDR(addr);
298 uint64_t deepest = atomic_load(&openmp_trace_deepest);
299 while ((deepest >> 56) < (value >> 56) &&
300 0 == atomic_compare_exchange_weak(&openmp_trace_deepest, &deepest,
301 value)) {
302 }
303 atomic_fetch_add_explicit(&openmp_trace_nested, 1, memory_order_relaxed);
304 if (openmp_trace_level_info <= openmp_trace_level &&
305 0 != openmp_trace_unique(OPENMP_TRACE_SLOT(
306 openmp_trace_info, openmp_trace_parallel, addr))) {
307 char a[1024];
308 openmp_trace_symbol(OPENMP_TRACE_ADDR(addr), a, sizeof(a));
309 fprintf(stderr, "OMP/TRACE INFO: parallel region%s%s%s at level %i\n",
310 '\0' != *a ? " \"" : "", a, '\0' != *a ? "\"" : "", level);
311 }
312 }
313 }
314}
315
316/* OMPT callback (signature as in omp-tools.h) */
317static void openmp_trace_implicit_task(ompt_scope_endpoint_t endpoint,
318 ompt_data_t *parallel_data,
319 ompt_data_t *task_data,
320 unsigned int actual_parallelism,
321 unsigned int index, int flags) {
322 OPENMP_TRACE_UNUSED(endpoint);
323 OPENMP_TRACE_UNUSED(parallel_data);
324 OPENMP_TRACE_UNUSED(actual_parallelism);
325 OPENMP_TRACE_UNUSED(index);
326 OPENMP_TRACE_UNUSED(flags);
327 if (NULL != task_data) { /* implicit tasks of hot teams are reused */
328 task_data->value = 0;
329 }
330}
331
332/* OMPT callback (signature as in omp-tools.h) */
333static void openmp_trace_masked(ompt_scope_endpoint_t endpoint,
334 ompt_data_t *parallel_data,
335 ompt_data_t *task_data,
336 const void *codeptr_ra) {
337 OPENMP_TRACE_UNUSED(parallel_data);
338 if (NULL != task_data) {
339 openmp_trace_construct(endpoint, task_data, openmp_trace_master,
340 codeptr_ra);
341 }
342}
343
344/* OMPT callback (signature as in omp-tools.h) */
345static void openmp_trace_work(ompt_work_t work_type,
346 ompt_scope_endpoint_t endpoint,
347 ompt_data_t *parallel_data,
348 ompt_data_t *task_data, uint64_t count,
349 const void *codeptr_ra) {
350 int what = openmp_trace_none;
351 OPENMP_TRACE_UNUSED(count);
352 switch ((int)work_type) {
353 case openmp_trace_work_loop:
354 case openmp_trace_work_loop_static:
355 case openmp_trace_work_loop_dynamic:
356 case openmp_trace_work_loop_guided:
357 case openmp_trace_work_loop_other:
358 what = openmp_trace_loop;
359 break;
360 case openmp_trace_work_workshare:
361 what = openmp_trace_workshare;
362 break;
363 case openmp_trace_work_scope:
364 what = openmp_trace_scope;
365 break;
366 case openmp_trace_work_single_executor:
367 /* GOMP_single_start signals no end of the region */
368 if (NULL != parallel_data &&
369 0 != (OPENMP_TRACE_RUNTIME & parallel_data->value)) {
370 what = openmp_trace_single;
371 }
372 break;
373 default: /* sections: active regions signal no end */
374 break;
375 }
376 if (openmp_trace_none != what && NULL != task_data) {
377 openmp_trace_construct(endpoint, task_data, what, codeptr_ra);
378 }
379}
380
381/* OMPT callback (signature as in omp-tools.h) */
382static void openmp_trace_sync_region(ompt_sync_region_t kind,
383 ompt_scope_endpoint_t endpoint,
384 ompt_data_t *parallel_data,
385 ompt_data_t *task_data,
386 const void *codeptr_ra) {
387 OPENMP_TRACE_UNUSED(parallel_data);
388 if (NULL != task_data && 0 != task_data->value &&
389 openmp_trace_scope_end != (int)endpoint) {
390 const uint64_t slot = task_data->value;
391 switch ((int)kind) {
392 case openmp_trace_sync_barrier:
393 case openmp_trace_sync_barrier_explicit:
394 case openmp_trace_sync_barrier_implementation: /* GOMP_barrier */
395 openmp_trace_issue(openmp_trace_error, openmp_trace_barrier, codeptr_ra,
396 slot);
397 break;
398 case openmp_trace_sync_barrier_implicit:
399 case openmp_trace_sync_barrier_implicit_workshare:
400 /* concludes a worksharing construct which signaled no end */
401 if (1 == OPENMP_TRACE_COUNT(slot) &&
402 openmp_trace_master != OPENMP_TRACE_KIND(slot)) {
403 task_data->value = 0;
404 }
405 break;
406 default: /* taskwait, taskgroup, reduction, end of parallel or teams */
407 break;
408 }
409 }
410}
411
412/* register callback; nonzero if the runtime can dispatch the event */
413static int openmp_trace_register(ompt_set_callback_t set_callback, int event,
414 ompt_callback_t callback, const char *name) {
415 const int result =
416 (openmp_trace_set_never <
417 (int)set_callback((ompt_callbacks_t)event, callback)
418 ? 1
419 : 0);
420 if (0 == result && openmp_trace_level_info <= openmp_trace_level) {
421 fprintf(stderr, "OMP/TRACE INFO: %s events are not supported\n", name);
422 }
423 return result;
424}
425
426/* initially, events of interest are registered */
427static int openmp_trace_initialize(ompt_function_lookup_t lookup,
428 int initial_device_num,
429 ompt_data_t *tool_data) {
430 const ompt_set_callback_t set_callback =
431 (ompt_set_callback_t)lookup("ompt_set_callback");
432 int result = 0;
433 OPENMP_TRACE_UNUSED(initial_device_num);
434 OPENMP_TRACE_UNUSED(tool_data);
435 openmp_trace_get_parallel_info =
436 (ompt_get_parallel_info_t)lookup("ompt_get_parallel_info");
437 if (NULL != set_callback && NULL != openmp_trace_get_parallel_info &&
438 0 != openmp_trace_register(
439 set_callback, openmp_trace_callback_parallel_begin,
440 (ompt_callback_t)openmp_trace_parallel_begin, "parallel") &&
441 0 != openmp_trace_register(
442 set_callback, openmp_trace_callback_implicit_task,
443 (ompt_callback_t)openmp_trace_implicit_task, "implicit task")) {
444 openmp_trace_register(set_callback, openmp_trace_callback_masked,
445 (ompt_callback_t)openmp_trace_masked, "master");
446 openmp_trace_register(set_callback, openmp_trace_callback_work,
447 (ompt_callback_t)openmp_trace_work, "work");
448 openmp_trace_register(set_callback, openmp_trace_callback_sync_region,
449 (ompt_callback_t)openmp_trace_sync_region, "sync");
450 result = 1;
451 } else { /* tool is not activated */
452 openmp_trace_level = 0;
453 }
454 return result;
455}
456
457/* summary when the runtime shuts down */
458static void openmp_trace_finalize(ompt_data_t *tool_data) {
459 const int errors = atomic_load(&openmp_trace_errors);
460 const int warnings = atomic_load(&openmp_trace_warnings);
461 const int nested = atomic_load(&openmp_trace_nested);
462 OPENMP_TRACE_UNUSED(tool_data);
463 if (openmp_trace_level_error <= openmp_trace_level &&
464 (0 != errors || 0 != warnings)) {
465 fprintf(stderr, "OMP/TRACE: %i error(s), %i warning(s)\n", errors,
466 warnings);
467 }
468 if (openmp_trace_level_info <= openmp_trace_level && 0 != nested) {
469 const uint64_t deepest = atomic_load(&openmp_trace_deepest);
470 char a[1024];
471 openmp_trace_symbol(OPENMP_TRACE_ADDR(deepest), a, sizeof(a));
472 fprintf(stderr,
473 "OMP/TRACE INFO: %i nested parallel region(s), deepest at level "
474 "%i%s%s%s\n",
475 nested, (int)(deepest >> 56), '\0' != *a ? " in \"" : "", a,
476 '\0' != *a ? "\"" : "");
477 }
478}
479
480/* entry point which is automatically called by the OpenMP runtime */
481ompt_start_tool_result_t *ompt_start_tool(unsigned int omp_version,
482 const char *runtime_version) {
483 static ompt_start_tool_result_t openmp_start_tool;
484 const char *const enabled_env = getenv("CP2K_OMP_TRACE");
485 const int level = (NULL == enabled_env ? 0 : atoi(enabled_env));
486 ompt_start_tool_result_t *result = NULL;
487 OPENMP_TRACE_UNUSED(omp_version);
488 OPENMP_TRACE_UNUSED(runtime_version);
489 openmp_trace_level = (0 <= level ? level : openmp_trace_level_info);
490 if (0 != openmp_trace_level) {
491 openmp_start_tool.initialize = openmp_trace_initialize;
492 openmp_start_tool.finalize = openmp_trace_finalize;
493 openmp_start_tool.tool_data.ptr = NULL;
494 result = &openmp_start_tool;
495 }
496 return result;
497}
498
499#endif
static void const int const int i
real(dp), dimension(3) a
real(dp), dimension(3) b
subroutine initialize(this, force_env)
Initialize the communicator by loading data and saving pointers to relevant data.
int openmp_trace_issues(void)