GCC Code Coverage Report


Directory: ./
File: tests/unit/04_core/talp_03.c
Date: 2026-12-01 10:07:21
Exec Total Coverage
Lines: 80 80 100.0%
Functions: 2 2 100.0%
Branches: 25 46 54.3%

Line Branch Exec Source
1 /*********************************************************************************/
2 /* Copyright 2009-2026 Barcelona Supercomputing Center */
3 /* */
4 /* This file is part of the DLB library. */
5 /* */
6 /* DLB is free software: you can redistribute it and/or modify */
7 /* it under the terms of the GNU Lesser General Public License as published by */
8 /* the Free Software Foundation, either version 3 of the License, or */
9 /* (at your option) any later version. */
10 /* */
11 /* DLB is distributed in the hope that it will be useful, */
12 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
13 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
14 /* GNU Lesser General Public License for more details. */
15 /* */
16 /* You should have received a copy of the GNU Lesser General Public License */
17 /* along with DLB. If not, see <https://www.gnu.org/licenses/>. */
18 /*********************************************************************************/
19
20 #include "unique_shmem.h"
21
22 #include "LB_core/spd.h"
23 #include "LB_core/thread_ctx.h"
24 #include "LB_numThreads/omptool.h"
25 #include "LB_numThreads/omp-tools.h"
26 #include "apis/dlb_errors.h"
27 #include "support/options.h"
28 #include "talp/regions.h"
29 #include "talp/sample.h"
30 #include "talp/talp.h"
31 #include "talp/talp_openmp.h"
32 #include "talp/talp_types.h"
33
34 #include <sched.h>
35 #include <pthread.h>
36 #include <string.h>
37 #include <assert.h>
38
39 /* Test OpenMP TALP */
40
41 /* enum { USLEEP_TIME = 1000 }; */
42
43 typedef struct parallel_func_args_t {
44 int index;
45 omptool_parallel_data_t *parallel_data;
46 subprocess_descriptor_t *spd;
47 } parallel_func_args_t;
48
49 3 static void* parallel_func(void *arg) {
50 3 parallel_func_args_t *args = arg;
51 3 int index = args->index;
52 3 omptool_parallel_data_t *parallel_data = args->parallel_data;
53 3 subprocess_descriptor_t *spd = args->spd;
54
55
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
3 if (index != 0) {
56 1 spd_enter_dlb(spd);
57 1 talp_openmp_thread_begin(ompt_thread_worker);
58 }
59
60 3 talp_openmp_into_parallel_function(parallel_data, index);
61 3 talp_openmp_into_parallel_sync(parallel_data);
62 3 talp_openmp_outof_parallel_sync(parallel_data);
63 3 talp_openmp_task_create();
64 3 talp_openmp_task_switch();
65 3 talp_openmp_task_complete();
66 3 talp_openmp_task_switch();
67 3 talp_openmp_into_parallel_implicit_barrier(parallel_data);
68
69
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
3 if (index != 0) {
70 1 talp_openmp_outof_parallel_function();
71 1 talp_openmp_thread_end();
72 }
73
74 3 return NULL;
75 }
76
77 1 int main(int argc, char *argv[]) {
78
79 1 int cpu = sched_getcpu();
80 cpu_set_t process_mask;
81 1 CPU_ZERO(&process_mask);
82
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 CPU_SET(cpu, &process_mask);
83
84 1 char options[64] = "--talp=papi --shm-key=";
85 1 strcat(options, SHMEM_KEY);
86 1 subprocess_descriptor_t spd = {.id = 111};
87 1 options_init(&spd.options, options);
88
89 1 memcpy(&spd.process_mask, &process_mask, sizeof(cpu_set_t));
90 1 spd_enter_dlb(&spd);
91 1 thread_ctx_set_main(THREAD_MAIN_SEQUENTIAL);
92 1 talp_init(&spd);
93
94 1 talp_info_t *talp_info = spd.talp_info;
95 1 dlb_monitor_t *global_monitor = talp_info->monitor;
96
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( region_is_started(global_monitor) );
97
98 /* OpenMP Init */
99 1 talp_openmp_init(spd.id, &spd.options);
100 1 talp_openmp_thread_begin(ompt_thread_initial);
101
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( region_is_started(global_monitor) );
102
103 /* Parallel region of 1 thread */
104 {
105 /* Create parallel region */
106 1 omptool_parallel_data_t parallel_data = {
107 .level = 1,
108 .requested_parallelism = 1,
109 .actual_parallelism = 1,
110 };
111 1 talp_openmp_parallel_begin(&parallel_data);
112
113 /* Create storage for parallel function */
114 1 parallel_func_args_t args = {
115 .index = 0,
116 .parallel_data = &parallel_data,
117 .spd = &spd,
118 };
119
120 /* Primary thred executes parallel function */
121 1 parallel_func(&args);
122
123 /* End parallel */
124 1 talp_openmp_parallel_end(&parallel_data);
125
126 1 talp_sample_update(talp_info);
127
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( talp_aggregate_samples_to_regions(talp_info) == DLB_SUCCESS );
128
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( global_monitor->num_omp_parallels == 1 );
129
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( global_monitor->num_omp_tasks == 1 );
130
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( global_monitor->useful_time > 0 );
131
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( global_monitor->mpi_time == 0 );
132
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( global_monitor->omp_load_imbalance_time == 0 );
133
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( global_monitor->omp_scheduling_time > 0 );
134
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( global_monitor->omp_outside_parallel_time == 0 );
135 }
136
137 /* Parallel region of 2 threads */
138 {
139 1 dlb_monitor_t *monitor = region_register(&spd, "Parallel 2");
140
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( monitor != NULL );
141
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( region_start(&spd, monitor) == DLB_SUCCESS );
142
143 /* Create parallel region */
144 1 omptool_parallel_data_t parallel_data = {
145 .level = 1,
146 .requested_parallelism = 2,
147 .actual_parallelism = 2,
148 };
149 1 talp_openmp_parallel_begin(&parallel_data);
150
151 /* Create storage for parallel function */
152 1 parallel_func_args_t args[2] = {
153 {
154 .index = 0,
155 .parallel_data = &parallel_data,
156 .spd = &spd,
157 }, {
158 .index = 1,
159 .parallel_data = &parallel_data,
160 .spd = &spd,
161 }
162 };
163
164 /* Create worker thread */
165 pthread_t worker_thread;
166 1 pthread_create(&worker_thread, NULL, parallel_func, &args[1]);
167
168 /* Primary thread executes parallel function */
169 1 parallel_func(&args[0]);
170
171 /* End parallel */
172 1 pthread_join(worker_thread, NULL);
173 1 talp_openmp_parallel_end(&parallel_data);
174
175
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( region_stop(&spd, monitor) == DLB_SUCCESS );
176 1 talp_sample_update(talp_info);
177
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( talp_aggregate_samples_to_regions(talp_info) == DLB_SUCCESS );
178
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( global_monitor->num_omp_parallels == 2 );
179
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( monitor->num_omp_parallels == 1 );
180
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( monitor->omp_scheduling_time > 0 );
181
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( monitor->omp_outside_parallel_time > 0 );
182
183 /* The first parallel of 1 thread did not have any LB time, so both
184 * monitors should have only the values of the seccond parallel */
185
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( monitor->omp_load_imbalance_time == global_monitor->omp_load_imbalance_time );
186
187 /* Since the second thread was created when the global and the user
188 * monitor were started, its time is added as OpenMP outside parallel time to
189 * both. The time added to the global monitor is greater because it
190 * was started before. */
191
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( monitor->omp_outside_parallel_time < global_monitor->omp_outside_parallel_time );
192 }
193
194 /* Fake num MPI/GPU calls to force printing all metrics */
195 1 global_monitor->num_mpi_calls = 1;
196 1 global_monitor->num_gpu_runtime_calls = 1;
197
198 /* Fake some device times */
199 1 global_monitor->gpu_useful_time = 4;
200 1 global_monitor->gpu_communication_time = 2;
201 1 global_monitor->gpu_inactive_time = 6;
202
203 1 talp_finalize(&spd);
204 1 talp_openmp_thread_end();
205 1 talp_openmp_finalize();
206
207 1 return 0;
208 }
209