| 1 |
// clang-format off |
| 2 |
// RUN: %libomp-compile-and-run | %sort-threads | FileCheck %s |
| 3 |
// REQUIRES: ompt |
| 4 |
|
| 5 |
// This test checks that values stored in task_data in a barrier_begin event |
| 6 |
// are still present in the corresponding barrier_end event. |
| 7 |
// Therefore, callback implementations different from the ones in callback.h are necessary. |
| 8 |
// This is a test for an issue reported in |
| 9 |
// https://github.com/OpenMPToolsInterface/LLVM-openmp/issues/39 |
| 10 |
// clang-format on |
| 11 |
|
| 12 |
#define _BSD_SOURCE |
| 13 |
#include <stdio.h> |
| 14 |
#include <unistd.h> |
| 15 |
#include <inttypes.h> |
| 16 |
#include <omp.h> |
| 17 |
#include <omp-tools.h> |
| 18 |
|
| 19 |
static const char *ompt_thread_t_values[] = { |
| 20 |
NULL, "ompt_thread_initial", "ompt_thread_worker", "ompt_thread_other"}; |
| 21 |
|
| 22 |
static ompt_get_unique_id_t ompt_get_unique_id; |
| 23 |
static ompt_get_thread_data_t ompt_get_thread_data; |
| 24 |
|
| 25 |
int main() { |
| 26 |
#pragma omp parallel num_threads(4) |
| 27 |
{ |
| 28 |
#pragma omp master |
| 29 |
{ |
| 30 |
sleep(1); |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
// clang-format off |
| 35 |
// Check if libomp supports the callbacks for this test. |
| 36 |
// CHECK-NOT: {{^}}0: Could not register callback 'ompt_callback_sync_region' |
| 37 |
// CHECK-NOT: {{^}}0: Could not register callback 'ompt_callback_sync_region_wait' |
| 38 |
|
| 39 |
// CHECK: 0: NULL_POINTER=[[NULL:.*$]] |
| 40 |
|
| 41 |
// master thread implicit barrier at parallel end |
| 42 |
// CHECK: {{^}}[[MASTER_ID:[0-9]+]]: ompt_event_barrier_implicit_parallel_begin: parallel_id=0, task_id=[[TASK_ID:[0-f]+]], codeptr_ra={{(0x)?[0-f]*}} |
| 43 |
// CHECK: {{^}}[[MASTER_ID]]: ompt_event_wait_barrier_implicit_parallel_begin: parallel_id=0, task_id=[[TASK_ID]], codeptr_ra={{(0x)?[0-f]*}} |
| 44 |
// CHECK: {{^}}[[MASTER_ID]]: ompt_event_wait_barrier_implicit_parallel_end: parallel_id=0, task_id=[[TASK_ID]], codeptr_ra={{(0x)?[0-f]*}} |
| 45 |
// CHECK: {{^}}[[MASTER_ID]]: ompt_event_barrier_implicit_parallel_end: parallel_id=0, task_id=[[TASK_ID]], codeptr_ra={{(0x)?[0-f]*}} |
| 46 |
|
| 47 |
|
| 48 |
// worker thread implicit barrier at parallel end |
| 49 |
// CHECK: {{^}}[[THREAD_ID:[0-9]+]]: ompt_event_barrier_implicit_parallel_begin: parallel_id=0, task_id=[[TASK_ID:[0-f]+]], codeptr_ra=[[NULL]] |
| 50 |
// CHECK: {{^}}[[THREAD_ID]]: ompt_event_wait_barrier_implicit_parallel_begin: parallel_id=0, task_id=[[TASK_ID]], codeptr_ra=[[NULL]] |
| 51 |
// CHECK: {{^}}[[THREAD_ID]]: ompt_event_wait_barrier_implicit_parallel_end: parallel_id=0, task_id=[[TASK_ID]], codeptr_ra=[[NULL]] |
| 52 |
// CHECK: {{^}}[[THREAD_ID]]: ompt_event_barrier_implicit_parallel_end: parallel_id=0, task_id=[[TASK_ID]], codeptr_ra=[[NULL]] |
| 53 |
// clang-format on |
| 54 |
|
| 55 |
return 0; |
| 56 |
} |
| 57 |
|
| 58 |
static void on_ompt_callback_thread_begin(ompt_thread_t thread_type, |
| 59 |
ompt_data_t *thread_data) { |
| 60 |
if (thread_data->ptr) |
| 61 |
printf("%s\n", "0: thread_data initially not null"); |
| 62 |
thread_data->value = ompt_get_unique_id(); |
| 63 |
printf("%" PRIu64 |
| 64 |
": ompt_event_thread_begin: thread_type=%s=%d, thread_id=%" PRIu64 |
| 65 |
"\n", |
| 66 |
ompt_get_thread_data()->value, ompt_thread_t_values[thread_type], |
| 67 |
thread_type, thread_data->value); |
| 68 |
} |
| 69 |
|
| 70 |
static void on_ompt_callback_sync_region(ompt_sync_region_t kind, |
| 71 |
ompt_scope_endpoint_t endpoint, |
| 72 |
ompt_data_t *parallel_data, |
| 73 |
ompt_data_t *task_data, |
| 74 |
const void *codeptr_ra) { |
| 75 |
// We only expect implicit parallel barrier in this code. |
| 76 |
if (kind != ompt_sync_region_barrier_implicit_parallel) { |
| 77 |
printf("unexpected ompt_sync_region_t passed to %s\n", __func__); |
| 78 |
exit(-1); |
| 79 |
} |
| 80 |
const char *event_name = NULL; |
| 81 |
if (endpoint == ompt_scope_begin) { |
| 82 |
event_name = "ompt_event_barrier_implicit_parallel_begin"; |
| 83 |
task_data->value = ompt_get_unique_id(); |
| 84 |
} else if (endpoint == ompt_scope_end) { |
| 85 |
event_name = "ompt_event_barrier_implicit_parallel_end"; |
| 86 |
} else { |
| 87 |
printf("ompt_scope_beginend should never be passed to %s\n", __func__); |
| 88 |
exit(-1); |
| 89 |
} |
| 90 |
printf("%" PRIu64 ": %s: parallel_id=%" PRIu64 ", task_id=%" PRIu64 |
| 91 |
", codeptr_ra=%p\n", |
| 92 |
ompt_get_thread_data()->value, event_name, |
| 93 |
parallel_data ? parallel_data->value : 0, task_data->value, |
| 94 |
codeptr_ra); |
| 95 |
} |
| 96 |
|
| 97 |
static void on_ompt_callback_sync_region_wait(ompt_sync_region_t kind, |
| 98 |
ompt_scope_endpoint_t endpoint, |
| 99 |
ompt_data_t *parallel_data, |
| 100 |
ompt_data_t *task_data, |
| 101 |
const void *codeptr_ra) { |
| 102 |
if (kind != ompt_sync_region_barrier_implicit_parallel) { |
| 103 |
printf("unexpected ompt_sync_region_t passed to %s\n", __func__); |
| 104 |
exit(-1); |
| 105 |
} |
| 106 |
const char *event_name = NULL; |
| 107 |
if (endpoint == ompt_scope_begin) { |
| 108 |
event_name = "ompt_event_wait_barrier_implicit_parallel_begin"; |
| 109 |
} else if (endpoint == ompt_scope_end) { |
| 110 |
event_name = "ompt_event_wait_barrier_implicit_parallel_end"; |
| 111 |
} else { |
| 112 |
printf("ompt_scope_beginend should never be passed to %s\n", __func__); |
| 113 |
exit(-1); |
| 114 |
} |
| 115 |
printf("%" PRIu64 ": %s: parallel_id=%" PRIu64 ", task_id=%" PRIu64 |
| 116 |
", codeptr_ra=%p\n", |
| 117 |
ompt_get_thread_data()->value, event_name, |
| 118 |
parallel_data ? parallel_data->value : 0, task_data->value, |
| 119 |
codeptr_ra); |
| 120 |
} |
| 121 |
|
| 122 |
#define register_ompt_callback_t(name, type) \ |
| 123 |
do { \ |
| 124 |
type f_##name = &on_##name; \ |
| 125 |
if (ompt_set_callback(name, (ompt_callback_t)f_##name) == ompt_set_never) \ |
| 126 |
printf("0: Could not register callback '" #name "'\n"); \ |
| 127 |
} while (0) |
| 128 |
|
| 129 |
#define register_ompt_callback(name) register_ompt_callback_t(name, name##_t) |
| 130 |
|
| 131 |
int ompt_initialize(ompt_function_lookup_t lookup, int initial_device_num, |
| 132 |
ompt_data_t *tool_data) { |
| 133 |
ompt_set_callback_t ompt_set_callback; |
| 134 |
ompt_set_callback = (ompt_set_callback_t)lookup("ompt_set_callback"); |
| 135 |
ompt_get_unique_id = (ompt_get_unique_id_t)lookup("ompt_get_unique_id"); |
| 136 |
ompt_get_thread_data = (ompt_get_thread_data_t)lookup("ompt_get_thread_data"); |
| 137 |
register_ompt_callback(ompt_callback_sync_region); |
| 138 |
register_ompt_callback_t(ompt_callback_sync_region_wait, |
| 139 |
ompt_callback_sync_region_t); |
| 140 |
register_ompt_callback(ompt_callback_thread_begin); |
| 141 |
printf("0: NULL_POINTER=%p\n", (void *)NULL); |
| 142 |
return 1; // success |
| 143 |
} |
| 144 |
|
| 145 |
void ompt_finalize(ompt_data_t *tool_data) { |
| 146 |
printf("0: ompt_event_runtime_shutdown\n"); |
| 147 |
} |
| 148 |
|
| 149 |
ompt_start_tool_result_t *ompt_start_tool(unsigned int omp_version, |
| 150 |
const char *runtime_version) { |
| 151 |
static ompt_start_tool_result_t ompt_start_tool_result = {&ompt_initialize, |
| 152 |
&ompt_finalize, 0}; |
| 153 |
return &ompt_start_tool_result; |
| 154 |
} |