| 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 | #ifdef HAVE_CONFIG_H | ||
| 21 | #include <config.h> | ||
| 22 | #endif | ||
| 23 | |||
| 24 | #include "talp/talp.h" | ||
| 25 | |||
| 26 | #include "LB_core/node_barrier.h" | ||
| 27 | #include "LB_core/spd.h" | ||
| 28 | #include "LB_core/thread_ctx.h" | ||
| 29 | #include "LB_comm/shmem_talp.h" | ||
| 30 | #include "apis/dlb_errors.h" | ||
| 31 | #include "apis/dlb_talp.h" | ||
| 32 | #include "support/atomic.h" | ||
| 33 | #include "support/debug.h" | ||
| 34 | #include "support/error.h" | ||
| 35 | #include "support/gslist.h" | ||
| 36 | #include "support/gtree.h" | ||
| 37 | #include "support/mytime.h" | ||
| 38 | #include "support/tracing.h" | ||
| 39 | #include "support/options.h" | ||
| 40 | #include "support/mask_utils.h" | ||
| 41 | #include "support/gpu_mask_utils.h" | ||
| 42 | #include "talp/backend.h" | ||
| 43 | #include "talp/perf_metrics.h" | ||
| 44 | #include "talp/sample.h" | ||
| 45 | #include "talp/regions.h" | ||
| 46 | #include "talp/talp_gpu.h" | ||
| 47 | #include "talp/talp_hwc.h" | ||
| 48 | #include "talp/talp_energy.h" | ||
| 49 | #include "talp/talp_output.h" | ||
| 50 | #include "talp/talp_record.h" | ||
| 51 | #include "talp/talp_types.h" | ||
| 52 | #ifdef MPI_LIB | ||
| 53 | #include "mpi/mpi_core.h" | ||
| 54 | #endif | ||
| 55 | |||
| 56 | #include <stdlib.h> | ||
| 57 | #include <pthread.h> | ||
| 58 | |||
| 59 | |||
| 60 | #ifdef MPI_LIB | ||
| 61 | /* Returns the number of MPI processes that have HWC enabled */ | ||
| 62 | static int get_hwc_init_across_world(const subprocess_descriptor_t *spd) { | ||
| 63 | |||
| 64 | talp_info_t *talp_info = spd->talp_info; | ||
| 65 | |||
| 66 | // status = 1 means HWC are enabled | ||
| 67 | int hwc_local_status = talp_info->flags.have_hwc ? 1 : 0; | ||
| 68 | |||
| 69 | int hwc_global_statuses = 0; | ||
| 70 | |||
| 71 | PMPI_Allreduce(&hwc_local_status, &hwc_global_statuses, 1, | ||
| 72 | MPI_INT, MPI_SUM, getWorldComm()); | ||
| 73 | |||
| 74 | return hwc_global_statuses; | ||
| 75 | } | ||
| 76 | #endif | ||
| 77 | |||
| 78 | |||
| 79 | /*********************************************************************************/ | ||
| 80 | /* Init / Finalize */ | ||
| 81 | /*********************************************************************************/ | ||
| 82 | |||
| 83 | 39 | void talp_init(subprocess_descriptor_t *spd) { | |
| 84 | |||
| 85 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 39 times.
|
39 | ensure(!spd->talp_info, "TALP already initialized"); |
| 86 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 39 times.
|
39 | ensure(!thread_is_observer(), "An observer thread cannot call talp_init"); |
| 87 |
2/2✓ Branch 0 taken 11 times.
✓ Branch 1 taken 28 times.
|
39 | verbose(VB_TALP, "Initializing TALP module with worker mask: %s", |
| 88 | mu_to_str(&spd->process_mask)); | ||
| 89 | |||
| 90 | 39 | int num_cpus = CPU_COUNT(&spd->process_mask); | |
| 91 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 38 times.
|
39 | if (num_cpus == 0) { |
| 92 | /* Possibly due to testing or DLB_Init, query process mask: */ | ||
| 93 | cpu_set_t process_mask; | ||
| 94 | 1 | sched_getaffinity(0, sizeof(process_mask), &process_mask); | |
| 95 | 1 | num_cpus = CPU_COUNT(&process_mask); | |
| 96 | } | ||
| 97 | |||
| 98 | /* Initialize talp info */ | ||
| 99 | 39 | talp_info_t *talp_info = malloc(sizeof(talp_info_t)); | |
| 100 | 39 | *talp_info = (const talp_info_t) { | |
| 101 | .flags = { | ||
| 102 | 39 | .external_profiler = spd->options.talp_external_profiler, | |
| 103 | 39 | .have_shmem = spd->options.talp_external_profiler, | |
| 104 | 39 | .have_minimal_shmem = !spd->options.talp_external_profiler | |
| 105 |
3/4✓ Branch 0 taken 16 times.
✓ Branch 1 taken 23 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 16 times.
|
39 | && spd->options.talp_summary & SUMMARY_NODE, |
| 106 | }, | ||
| 107 | .num_cpus = num_cpus, | ||
| 108 | 39 | .regions = g_tree_new_full( | |
| 109 | (GCompareDataFunc)region_compare_by_name, | ||
| 110 | NULL, NULL, region_dealloc), | ||
| 111 | .regions_mutex = PTHREAD_MUTEX_INITIALIZER, | ||
| 112 | }; | ||
| 113 | 39 | spd->talp_info = talp_info; | |
| 114 | |||
| 115 | /* Initialize shared memory */ | ||
| 116 |
3/4✓ Branch 0 taken 16 times.
✓ Branch 1 taken 23 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 16 times.
|
39 | if (talp_info->flags.have_shmem || talp_info->flags.have_minimal_shmem) { |
| 117 | /* If we only need a minimal shmem, its size will be the user-provided | ||
| 118 | * multiplier times 'system_size' (usually, 1 region per process) | ||
| 119 | * Otherwise, we multiply it by DEFAULT_REGIONS_PER_PROC. | ||
| 120 | */ | ||
| 121 | enum { DEFAULT_REGIONS_PER_PROC = 100 }; | ||
| 122 | 46 | int shmem_size_multiplier = spd->options.shm_size_multiplier | |
| 123 |
1/2✓ Branch 0 taken 23 times.
✗ Branch 1 not taken.
|
23 | * (talp_info->flags.have_shmem ? DEFAULT_REGIONS_PER_PROC : 1); |
| 124 | 23 | shmem_talp__init(spd->options.shm_key, shmem_size_multiplier); | |
| 125 | } | ||
| 126 | |||
| 127 | /* Initialize TALP components */ | ||
| 128 |
2/2✓ Branch 0 taken 21 times.
✓ Branch 1 taken 18 times.
|
39 | if (spd->options.talp & (TALP_COMPONENT_DEFAULT | TALP_COMPONENT_GPU)) { |
| 129 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 21 times.
|
21 | if (talp_gpu_init(spd) == DLB_SUCCESS) { |
| 130 | ✗ | talp_info->flags.have_gpu = true; | |
| 131 | ✗ | verbose(VB_TALP, "GPU component enabled successfully"); | |
| 132 | } else { | ||
| 133 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
|
21 | if (spd->options.talp & TALP_COMPONENT_GPU) { |
| 134 | /* component was explicit and failed, warn user */ | ||
| 135 | ✗ | warning("TALP: Failed to load GPU component"); | |
| 136 | } | ||
| 137 | } | ||
| 138 | } | ||
| 139 |
2/2✓ Branch 0 taken 29 times.
✓ Branch 1 taken 10 times.
|
39 | if (spd->options.talp & (TALP_COMPONENT_DEFAULT | TALP_COMPONENT_HWC)) { |
| 140 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 29 times.
|
29 | if (talp_hwc_init(spd) == DLB_SUCCESS) { |
| 141 | ✗ | talp_info->flags.have_hwc = true; | |
| 142 | ✗ | verbose(VB_TALP, "HWC component enabled successfully"); | |
| 143 | } else { | ||
| 144 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 21 times.
|
29 | if (spd->options.talp & TALP_COMPONENT_HWC) { |
| 145 | /* component was explicit and failed, warn user */ | ||
| 146 | 8 | warning("TALP: Failed to load HWC component"); | |
| 147 | } | ||
| 148 | } | ||
| 149 | } | ||
| 150 | |||
| 151 |
2/2✓ Branch 0 taken 21 times.
✓ Branch 1 taken 18 times.
|
39 | if (spd->options.talp & (TALP_COMPONENT_DEFAULT | TALP_COMPONENT_ENERGY)) { |
| 152 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 21 times.
|
21 | if (talp_energy_init(spd) == DLB_SUCCESS) { |
| 153 | ✗ | talp_info->flags.have_energy = true; | |
| 154 | ✗ | verbose(VB_TALP, "Energy component enabled successfully"); | |
| 155 | } else { | ||
| 156 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
|
21 | if (spd->options.talp & TALP_COMPONENT_ENERGY) { |
| 157 | /* component was explicit and failed, warn user */ | ||
| 158 | ✗ | warning("TALP: Failed to load ENERGY component"); | |
| 159 | } | ||
| 160 | } | ||
| 161 | } | ||
| 162 | |||
| 163 | #ifdef MPI_LIB | ||
| 164 | /* Check HWC status across all process. Every process needs to do the check | ||
| 165 | * because it's a collective operation and some process may have been started | ||
| 166 | * without the appropriate flag. */ | ||
| 167 | if (is_mpi_ready()) { | ||
| 168 | int num_procs_with_hwc = get_hwc_init_across_world(spd); | ||
| 169 | if (num_procs_with_hwc > 0 && num_procs_with_hwc < _mpi_size) { | ||
| 170 | warning0("Hardware Counters initialization has failed, disabling option."); | ||
| 171 | talp_hwc_finalize(); | ||
| 172 | talp_info->flags.have_hwc = false; | ||
| 173 | } | ||
| 174 | } | ||
| 175 | #endif | ||
| 176 | |||
| 177 | /* Initialize sample structure */ | ||
| 178 | 39 | talp_sample_init(talp_info); | |
| 179 | |||
| 180 | /* Create the main thread's sample */ | ||
| 181 | 39 | (void)talp_sample_get(talp_info); | |
| 182 | 39 | talp_sample_record_cpuid(talp_info); | |
| 183 | 39 | talp_sample_set_state(talp_info, TALP_STATE_USEFUL); | |
| 184 | |||
| 185 | /* Initialize global region monitor | ||
| 186 | * (at this point we don't know how many CPUs, it will be fixed in talp_openmp_init) */ | ||
| 187 | 39 | talp_info->monitor = region_register(spd, region_get_global_name()); | |
| 188 | |||
| 189 | /* Start global region */ | ||
| 190 | 39 | region_start(spd, talp_info->monitor); | |
| 191 | 39 | } | |
| 192 | |||
| 193 | 39 | void talp_finalize(subprocess_descriptor_t *spd) { | |
| 194 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 39 times.
|
39 | ensure(spd->talp_info, "TALP is not initialized"); |
| 195 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 39 times.
|
39 | ensure(thread_is_main(), "Only main thread can call talp_finalize"); |
| 196 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 31 times.
|
39 | verbose(VB_TALP, "Finalizing TALP module"); |
| 197 | |||
| 198 | 39 | talp_info_t *talp_info = spd->talp_info; | |
| 199 | |||
| 200 | /* Stop open regions | ||
| 201 | * (Note that region_stop need to acquire the regions_mutex | ||
| 202 | * lock, so we we need to iterate without it) */ | ||
| 203 |
2/2✓ Branch 0 taken 28 times.
✓ Branch 1 taken 39 times.
|
67 | while(talp_info->open_regions != NULL) { |
| 204 | 28 | dlb_monitor_t *monitor = talp_info->open_regions->data; | |
| 205 | 28 | region_stop(spd, monitor); | |
| 206 | } | ||
| 207 | |||
| 208 | /* Finalize TALP components */ | ||
| 209 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 39 times.
|
39 | if (talp_info->flags.have_gpu) { |
| 210 | ✗ | talp_gpu_finalize(); | |
| 211 | } | ||
| 212 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 39 times.
|
39 | if (talp_info->flags.have_hwc) { |
| 213 | ✗ | talp_hwc_finalize(); | |
| 214 | } | ||
| 215 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 39 times.
|
39 | if (talp_info->flags.have_energy) { |
| 216 | ✗ | talp_energy_finalize(); | |
| 217 | } | ||
| 218 | |||
| 219 | /* Per-process output (no MPI or requested by user) */ | ||
| 220 |
2/2✓ Branch 0 taken 12 times.
✓ Branch 1 taken 27 times.
|
39 | if (!talp_info->flags.have_mpi |
| 221 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
|
12 | || spd->options.talp_partial_output) { |
| 222 | |||
| 223 | 27 | pthread_mutex_lock(&talp_info->regions_mutex); | |
| 224 | { | ||
| 225 | /* Record all regions */ | ||
| 226 | 27 | for (GTreeNode *node = g_tree_node_first(talp_info->regions); | |
| 227 |
2/2✓ Branch 0 taken 1435 times.
✓ Branch 1 taken 27 times.
|
1462 | node != NULL; |
| 228 | 1435 | node = g_tree_node_next(node)) { | |
| 229 | 1435 | const dlb_monitor_t *monitor = g_tree_node_value(node); | |
| 230 | 1435 | talp_record_monitor(spd, monitor); | |
| 231 | } | ||
| 232 | } | ||
| 233 | 27 | pthread_mutex_unlock(&talp_info->regions_mutex); | |
| 234 | } | ||
| 235 | |||
| 236 | /* Print/write all collected summaries */ | ||
| 237 | 39 | talp_output_finalize(spd->options.talp_output_file, spd->options.talp_partial_output); | |
| 238 | |||
| 239 | /* Deallocate samples structure */ | ||
| 240 | 39 | talp_sample_finalize(talp_info); | |
| 241 | |||
| 242 | /* Finalize shared memory */ | ||
| 243 |
3/4✓ Branch 0 taken 16 times.
✓ Branch 1 taken 23 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 16 times.
|
39 | if (talp_info->flags.have_shmem || talp_info->flags.have_minimal_shmem) { |
| 244 | 23 | shmem_talp__finalize(spd->id); | |
| 245 | } | ||
| 246 | |||
| 247 | /* Deallocate monitoring regions and talp_info */ | ||
| 248 | 39 | pthread_mutex_lock(&talp_info->regions_mutex); | |
| 249 | { | ||
| 250 | /* Destroy GTree, each node is deallocated with the function region_dealloc */ | ||
| 251 | 39 | g_tree_destroy(talp_info->regions); | |
| 252 | 39 | talp_info->regions = NULL; | |
| 253 | 39 | talp_info->monitor = NULL; | |
| 254 | |||
| 255 | /* Destroy list of open regions */ | ||
| 256 | 39 | g_slist_free(talp_info->open_regions); | |
| 257 | 39 | talp_info->open_regions = NULL; | |
| 258 | } | ||
| 259 | 39 | pthread_mutex_unlock(&talp_info->regions_mutex); | |
| 260 | 39 | free(talp_info); | |
| 261 | 39 | spd->talp_info = NULL; | |
| 262 | 39 | } | |
| 263 | |||
| 264 | |||
| 265 | /*********************************************************************************/ | ||
| 266 | /* Functions for sample aggregation to regions */ | ||
| 267 | /*********************************************************************************/ | ||
| 268 | |||
| 269 | 1 | void talp_aggregate_sample_to_region(talp_info_t *talp_info, | |
| 270 | dlb_monitor_t *monitor, const talp_sample_t *sample, int64_t elapsed) { | ||
| 271 | |||
| 272 | 1 | pthread_mutex_lock(&talp_info->regions_mutex); | |
| 273 | { | ||
| 274 | 1 | monitor_data_t *monitor_data = monitor->_data; | |
| 275 | |||
| 276 | /* CPU mask */ | ||
| 277 |
2/2✓ Branch 0 taken 16 times.
✓ Branch 1 taken 1 times.
|
17 | CPU_OR(&monitor_data->cpu_mask, &monitor_data->cpu_mask, &sample->cpu_mask); |
| 278 | |||
| 279 | /* Number of CPUs */ | ||
| 280 | 1 | monitor->num_cpus = min_int(CPU_COUNT(&monitor_data->cpu_mask), talp_info->num_cpus); | |
| 281 | |||
| 282 | /* Number of threads: we oversimplify here, and sometimes we may give | ||
| 283 | * a wrong number. The solution would be to keep track of thread ids | ||
| 284 | * that have participated. Not worth the effort. */ | ||
| 285 | 1 | monitor->num_omp_threads = monitor->num_cpus; | |
| 286 | |||
| 287 | /* Timers */ | ||
| 288 | /* Note: not_useful_omp_{during_mpi,lb,sched} are purposely not reduced | ||
| 289 | * and the entire omp_in is attributed to omp_scheduling. */ | ||
| 290 | 1 | monitor->useful_time += sample->timers.useful; | |
| 291 | 1 | monitor->mpi_time += sample->timers.not_useful_mpi; | |
| 292 | 1 | monitor->omp_scheduling_time += sample->timers.not_useful_omp_in; | |
| 293 | 1 | monitor->gpu_runtime_time += sample->timers.not_useful_gpu; | |
| 294 | |||
| 295 | /* Counters */ | ||
| 296 | 1 | monitor->cycles += sample->counters.cycles; | |
| 297 | 1 | monitor->instructions += sample->counters.instructions; | |
| 298 | |||
| 299 | /* Stats */ | ||
| 300 | 1 | monitor->num_mpi_calls += sample->stats.num_mpi_calls; | |
| 301 | 1 | monitor->num_omp_parallels += sample->stats.num_omp_parallels; | |
| 302 | 1 | monitor->num_omp_tasks += sample->stats.num_omp_tasks; | |
| 303 | 1 | monitor->num_gpu_runtime_calls += sample->stats.num_gpu_runtime_calls; | |
| 304 | |||
| 305 | 1 | monitor->elapsed_time += elapsed; | |
| 306 | 1 | ++(monitor->num_measurements); | |
| 307 | } | ||
| 308 | 1 | pthread_mutex_unlock(&talp_info->regions_mutex); | |
| 309 | 1 | } | |
| 310 | |||
| 311 | /* Update all open regions with the macrosample */ | ||
| 312 | 6213 | static void update_regions_with_macrosample(talp_info_t *restrict talp_info, | |
| 313 | const talp_macrosample_t *restrict macrosample) { | ||
| 314 | |||
| 315 | 6213 | bool have_openmp = talp_info->flags.have_openmp; | |
| 316 | 6213 | bool external_profiler = talp_info->flags.external_profiler; | |
| 317 | |||
| 318 | /* Update all open regions */ | ||
| 319 | 6213 | pthread_mutex_lock(&talp_info->regions_mutex); | |
| 320 | { | ||
| 321 | 6213 | for (GSList *node = talp_info->open_regions; | |
| 322 |
2/2✓ Branch 0 taken 87183 times.
✓ Branch 1 taken 6213 times.
|
93396 | node != NULL; |
| 323 | 87183 | node = node->next) { | |
| 324 | 87183 | dlb_monitor_t *monitor = node->data; | |
| 325 | 87183 | monitor_data_t *monitor_data = monitor->_data; | |
| 326 | |||
| 327 | /* CPU mask */ | ||
| 328 |
2/2✓ Branch 0 taken 1394928 times.
✓ Branch 1 taken 87183 times.
|
1482111 | CPU_OR(&monitor_data->cpu_mask, &monitor_data->cpu_mask, ¯osample->cpu_mask); |
| 329 | |||
| 330 | /* Number of CPUs: | ||
| 331 | * - the CPU mask in macrosample may have more CPUs than the actual | ||
| 332 | * number of threads, e.g.: when OMP_PLACES=cores or when the initial | ||
| 333 | * thread is bound only after the first parallel region. | ||
| 334 | * - */ | ||
| 335 | 87183 | int macrosample_num_cpus = min_int( | |
| 336 | 87183 | CPU_COUNT(¯osample->cpu_mask), macrosample->num_samples); | |
| 337 |
2/2✓ Branch 0 taken 2476 times.
✓ Branch 1 taken 84707 times.
|
87183 | if (monitor->num_cpus < macrosample_num_cpus) { |
| 338 | 2476 | monitor->num_cpus = macrosample_num_cpus; | |
| 339 | } | ||
| 340 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 87183 times.
|
87183 | ensure(monitor->num_cpus > 0, "Updating region with 0 CPUs. Please report."); |
| 341 | |||
| 342 | /* Number of OpenMP threads */ | ||
| 343 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 87177 times.
|
87183 | if (have_openmp) { |
| 344 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
|
6 | if (monitor->num_omp_threads < macrosample->num_samples) { |
| 345 | 3 | monitor->num_omp_threads = macrosample->num_samples; | |
| 346 | } | ||
| 347 | } | ||
| 348 | |||
| 349 | /* GPU mask */ | ||
| 350 | 87183 | monitor_data->gpu_mask |= macrosample->gpu_mask; | |
| 351 | |||
| 352 | /* Number of GPUs */ | ||
| 353 | 87183 | monitor->num_gpus = gm_count(monitor_data->gpu_mask); | |
| 354 | |||
| 355 | /* Timers */ | ||
| 356 | 87183 | monitor->useful_time += macrosample->timers.useful; | |
| 357 | 87183 | monitor->mpi_time += macrosample->timers.not_useful_mpi; | |
| 358 | 87183 | monitor->mpi_worker_idle_time += macrosample->timers.not_useful_omp_during_mpi; | |
| 359 | 87183 | monitor->omp_load_imbalance_time += macrosample->timers.not_useful_omp_in_lb; | |
| 360 | 87183 | monitor->omp_scheduling_time += macrosample->timers.not_useful_omp_in_sched; | |
| 361 | 87183 | monitor->omp_outside_parallel_time += macrosample->timers.not_useful_omp_out; | |
| 362 | 87183 | monitor->gpu_runtime_time += macrosample->timers.not_useful_gpu; | |
| 363 | |||
| 364 | /* Counters */ | ||
| 365 | 87183 | monitor->cycles += macrosample->counters.cycles; | |
| 366 | 87183 | monitor->instructions += macrosample->counters.instructions; | |
| 367 | |||
| 368 | /* Energy (save nJ reading into J)*/ | ||
| 369 | 87183 | monitor->cpu_energy += (double)macrosample->energy.cpu / 1e9; | |
| 370 | 87183 | monitor->dram_energy += (double)macrosample->energy.dram / 1e9; | |
| 371 | 87183 | monitor->node_energy += (double)macrosample->energy.node / 1e9; | |
| 372 | 87183 | monitor->gpu_energy += (double)macrosample->energy.gpu / 1e9; | |
| 373 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 87183 times.
|
87183 | if (macrosample->energy_interval_ns > 0) { |
| 374 | ✗ | double instant_node_power = (double)macrosample->energy.node | |
| 375 | ✗ | / (double)macrosample->energy_interval_ns; | |
| 376 | ✗ | monitor->node_peak_power = max_double(monitor->node_peak_power, instant_node_power); | |
| 377 | |||
| 378 | ✗ | double instant_gpu_power = (double)macrosample->energy.gpu | |
| 379 | ✗ | / (double)macrosample->energy_interval_ns; | |
| 380 | ✗ | monitor->gpu_peak_power = max_double(monitor->gpu_peak_power, instant_gpu_power); | |
| 381 | } | ||
| 382 | |||
| 383 | /* Stats */ | ||
| 384 | 87183 | monitor->num_mpi_calls += macrosample->stats.num_mpi_calls; | |
| 385 | 87183 | monitor->num_omp_parallels += macrosample->stats.num_omp_parallels; | |
| 386 | 87183 | monitor->num_omp_tasks += macrosample->stats.num_omp_tasks; | |
| 387 | 87183 | monitor->num_gpu_runtime_calls += macrosample->stats.num_gpu_runtime_calls; | |
| 388 | |||
| 389 | /* GPU Timers */ | ||
| 390 | 87183 | uint64_t mask = macrosample->gpu_mask; | |
| 391 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 87183 times.
|
87183 | while (mask) { |
| 392 | ✗ | int gpu = gm_ctz(mask); | |
| 393 | |||
| 394 | /* Aggregated into monitor */ | ||
| 395 | ✗ | monitor->gpu_useful_time += macrosample->gpu_timers[gpu].useful; | |
| 396 | ✗ | monitor->gpu_communication_time += macrosample->gpu_timers[gpu].communication; | |
| 397 | |||
| 398 | /* Also store the data decomposed by device in the private monitor | ||
| 399 | * data for use during the later MPI node reduction. */ | ||
| 400 | ✗ | monitor_data->gpu_timers[gpu].useful += macrosample->gpu_timers[gpu].useful; | |
| 401 | ✗ | monitor_data->gpu_timers[gpu].communication += | |
| 402 | ✗ | macrosample->gpu_timers[gpu].communication; | |
| 403 | |||
| 404 | ✗ | mask = gm_clear_lsb(mask); | |
| 405 | } | ||
| 406 | |||
| 407 | /* Update shared memory only if requested */ | ||
| 408 |
2/2✓ Branch 0 taken 3679 times.
✓ Branch 1 taken 83504 times.
|
87183 | if (external_profiler) { |
| 409 | 3679 | shmem_talp__set_times(monitor_data->node_shared_id, | |
| 410 | monitor->mpi_time, | ||
| 411 | monitor->useful_time); | ||
| 412 | } | ||
| 413 | } | ||
| 414 | } | ||
| 415 | 6213 | pthread_mutex_unlock(&talp_info->regions_mutex); | |
| 416 | 6213 | } | |
| 417 | |||
| 418 | /* Accumulate values from samples of all threads and update regions */ | ||
| 419 | 6214 | int talp_aggregate_samples_to_regions(talp_info_t *talp_info) { | |
| 420 | |||
| 421 | /* Observer and unknown threads can't aggregate samples */ | ||
| 422 |
2/2✓ Branch 1 taken 1 times.
✓ Branch 2 taken 6213 times.
|
6214 | if (unlikely(!thread_is_profiled())) return DLB_ERR_PERM; |
| 423 | |||
| 424 | /* Accumulate samples from all threads */ | ||
| 425 | 6213 | talp_macrosample_t macrosample = {0}; | |
| 426 | 6213 | talp_sample_aggregate_all_to_macrosample(talp_info, ¯osample); | |
| 427 | |||
| 428 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6213 times.
|
6213 | if (talp_info->flags.have_gpu) { |
| 429 | /* Collect GPU measuremnts up to this point and update macrosample */ | ||
| 430 | ✗ | talp_gpu_collect(macrosample.gpu_timers, MAX_LOCAL_GPUS, ¯osample.gpu_mask); | |
| 431 | } | ||
| 432 | |||
| 433 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6213 times.
|
6213 | if (talp_info->flags.have_energy) { |
| 434 | /* Collect energy consumed up to this point and update macrosample */ | ||
| 435 | ✗ | talp_energy_collect(¯osample.energy, ¯osample.energy_interval_ns); | |
| 436 | } | ||
| 437 | |||
| 438 | /* Update all started regions */ | ||
| 439 | 6213 | update_regions_with_macrosample(talp_info, ¯osample); | |
| 440 | |||
| 441 | 6213 | return DLB_SUCCESS; | |
| 442 | } | ||
| 443 | |||
| 444 | |||
| 445 | /*********************************************************************************/ | ||
| 446 | /* TALP collect functions for 3rd party programs: */ | ||
| 447 | /* - It's also safe to call it from a 1st party program */ | ||
| 448 | /* - Requires --talp-external-profiler set up in application */ | ||
| 449 | /* - Does not need to synchronize with application */ | ||
| 450 | /*********************************************************************************/ | ||
| 451 | |||
| 452 | /* Function that may be called from a third-party process to compute | ||
| 453 | * node_metrics for a given region */ | ||
| 454 | 19 | int talp_query_pop_node_metrics(const char *name, dlb_node_metrics_t *node_metrics) { | |
| 455 | |||
| 456 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 18 times.
|
19 | if (name == NULL) { |
| 457 | 1 | name = region_get_global_name(); | |
| 458 | } | ||
| 459 | |||
| 460 | 19 | int error = DLB_SUCCESS; | |
| 461 | 19 | int64_t total_mpi_time = 0; | |
| 462 | 19 | int64_t total_useful_time = 0; | |
| 463 | 19 | int64_t max_mpi_time = 0; | |
| 464 | 19 | int64_t max_useful_time = 0; | |
| 465 | |||
| 466 | /* Obtain a list of regions in the node associated with given region */ | ||
| 467 | 19 | int max_procs = mu_get_system_size(); | |
| 468 | 19 | talp_region_list_t *region_list = malloc(max_procs * sizeof(talp_region_list_t)); | |
| 469 | int nelems; | ||
| 470 | 19 | shmem_talp__get_regionlist(region_list, &nelems, max_procs, name); | |
| 471 | |||
| 472 | /* Count how many processes have started the region */ | ||
| 473 | 19 | int processes_per_node = 0; | |
| 474 | |||
| 475 | /* Iterate the PID list and gather times of every process */ | ||
| 476 |
2/2✓ Branch 0 taken 32 times.
✓ Branch 1 taken 19 times.
|
51 | for (int i = 0; i <nelems; ++i) { |
| 477 | 32 | int64_t mpi_time = region_list[i].mpi_time; | |
| 478 | 32 | int64_t useful_time = region_list[i].useful_time; | |
| 479 | |||
| 480 | /* Accumulate total and max values */ | ||
| 481 |
4/4✓ Branch 0 taken 17 times.
✓ Branch 1 taken 15 times.
✓ Branch 2 taken 16 times.
✓ Branch 3 taken 1 times.
|
32 | if (mpi_time > 0 || useful_time > 0) { |
| 482 | 31 | ++processes_per_node; | |
| 483 | 31 | total_mpi_time += mpi_time; | |
| 484 | 31 | total_useful_time += useful_time; | |
| 485 | 31 | max_mpi_time = max_int64(mpi_time, max_mpi_time); | |
| 486 | 31 | max_useful_time = max_int64(useful_time, max_useful_time); | |
| 487 | } | ||
| 488 | } | ||
| 489 | 19 | free(region_list); | |
| 490 | |||
| 491 | #if MPI_LIB | ||
| 492 | int node_id = _node_id; | ||
| 493 | #else | ||
| 494 | 19 | int node_id = 0; | |
| 495 | #endif | ||
| 496 | |||
| 497 |
2/2✓ Branch 0 taken 18 times.
✓ Branch 1 taken 1 times.
|
19 | if (processes_per_node > 0) { |
| 498 | /* Compute POP metrics with some inferred values */ | ||
| 499 | perf_metrics_mpi_t metrics; | ||
| 500 | 18 | perf_metrics__infer_mpi_model( | |
| 501 | &metrics, | ||
| 502 | processes_per_node, | ||
| 503 | total_useful_time, | ||
| 504 | total_mpi_time, | ||
| 505 | max_useful_time); | ||
| 506 | |||
| 507 | /* Initialize structure */ | ||
| 508 | 18 | *node_metrics = (const dlb_node_metrics_t) { | |
| 509 | .node_id = node_id, | ||
| 510 | .processes_per_node = processes_per_node, | ||
| 511 | .total_useful_time = total_useful_time, | ||
| 512 | .total_mpi_time = total_mpi_time, | ||
| 513 | .max_useful_time = max_useful_time, | ||
| 514 | .max_mpi_time = max_mpi_time, | ||
| 515 | 18 | .parallel_efficiency = metrics.parallel_efficiency, | |
| 516 | 18 | .communication_efficiency = metrics.communication_efficiency, | |
| 517 | 18 | .load_balance = metrics.load_balance, | |
| 518 | }; | ||
| 519 | 18 | snprintf(node_metrics->name, DLB_MONITOR_NAME_MAX, "%s", name); | |
| 520 | } else { | ||
| 521 | 1 | error = DLB_ERR_NOENT; | |
| 522 | } | ||
| 523 | |||
| 524 | 19 | return error; | |
| 525 | } | ||
| 526 | |||
| 527 | |||
| 528 | /*********************************************************************************/ | ||
| 529 | /* TALP collect functions for 1st party programs */ | ||
| 530 | /* - Requires synchronization (MPI or node barrier) among all processes */ | ||
| 531 | /*********************************************************************************/ | ||
| 532 | |||
| 533 | /* Compute the current POP metrics for the specified monitor. If monitor is NULL, | ||
| 534 | * the global monitoring region is assumed. | ||
| 535 | * Pre-conditions: | ||
| 536 | * - if MPI, the given monitor must have been registered in all MPI ranks | ||
| 537 | * - pop_metrics is an allocated structure | ||
| 538 | */ | ||
| 539 | 1 | int talp_collect_pop_metrics(const subprocess_descriptor_t *spd, | |
| 540 | dlb_monitor_t *monitor, dlb_pop_metrics_t *pop_metrics) { | ||
| 541 | 1 | talp_info_t *talp_info = spd->talp_info; | |
| 542 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (monitor == NULL) { |
| 543 | ✗ | monitor = talp_info->monitor; | |
| 544 | } | ||
| 545 | |||
| 546 | /* Stop monitor so that metrics are updated */ | ||
| 547 | 1 | bool resume_region = region_stop(spd, monitor) == DLB_SUCCESS; | |
| 548 | |||
| 549 | pop_base_metrics_t base_metrics; | ||
| 550 | #ifdef MPI_LIB | ||
| 551 | /* Reduce monitor among all MPI ranks and everbody collects (all-to-all) */ | ||
| 552 | perf_metrics__reduce_monitor_into_base_metrics(&base_metrics, monitor, true); | ||
| 553 | #else | ||
| 554 | /* Construct base metrics using only the monitor from this process */ | ||
| 555 | 1 | perf_metrics__local_monitor_into_base_metrics(&base_metrics, monitor, talp_info->flags); | |
| 556 | #endif | ||
| 557 | |||
| 558 | /* Construct output pop_metrics out of base metrics */ | ||
| 559 | 1 | perf_metrics__base_to_pop_metrics(monitor->name, &base_metrics, pop_metrics); | |
| 560 | |||
| 561 | /* Resume monitor */ | ||
| 562 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (resume_region) { |
| 563 | ✗ | region_start(spd, monitor); | |
| 564 | } | ||
| 565 | |||
| 566 | 1 | return DLB_SUCCESS; | |
| 567 | } | ||
| 568 | |||
| 569 | /* Node-collective function to compute node_metrics for a given region */ | ||
| 570 | 18 | int talp_collect_pop_node_metrics(const subprocess_descriptor_t *spd, | |
| 571 | dlb_monitor_t *monitor, dlb_node_metrics_t *node_metrics) { | ||
| 572 | |||
| 573 | 18 | talp_info_t *talp_info = spd->talp_info; | |
| 574 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 16 times.
|
18 | monitor = monitor ? monitor : talp_info->monitor; |
| 575 | 18 | monitor_data_t *monitor_data = monitor->_data; | |
| 576 | |||
| 577 | /* Stop monitor so that metrics are updated */ | ||
| 578 | 18 | bool resume_region = region_stop(spd, monitor) == DLB_SUCCESS; | |
| 579 | |||
| 580 | /* This functionality needs a shared memory, create a temporary one if needed */ | ||
| 581 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 12 times.
|
18 | if (!talp_info->flags.have_shmem) { |
| 582 | 6 | shmem_talp__init(spd->options.shm_key, 1); | |
| 583 | 6 | shmem_talp__register(spd->id, monitor->avg_cpus, monitor->name, | |
| 584 | &monitor_data->node_shared_id); | ||
| 585 | } | ||
| 586 | |||
| 587 | /* Update the shared memory with this process' metrics */ | ||
| 588 | 18 | shmem_talp__set_times(monitor_data->node_shared_id, | |
| 589 | monitor->mpi_time, | ||
| 590 | monitor->useful_time); | ||
| 591 | |||
| 592 | /* Perform a node barrier to ensure everyone has updated their metrics */ | ||
| 593 | 18 | node_barrier(spd, NULL); | |
| 594 | |||
| 595 | /* Compute node metrics for that region name */ | ||
| 596 | 18 | talp_query_pop_node_metrics(monitor->name, node_metrics); | |
| 597 | |||
| 598 | /* Remove shared memory if it was a temporary one */ | ||
| 599 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 12 times.
|
18 | if (!talp_info->flags.have_shmem) { |
| 600 | 6 | shmem_talp__finalize(spd->id); | |
| 601 | } | ||
| 602 | |||
| 603 | /* Resume monitor */ | ||
| 604 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 17 times.
|
18 | if (resume_region) { |
| 605 | 1 | region_start(spd, monitor); | |
| 606 | } | ||
| 607 | |||
| 608 | 18 | return DLB_SUCCESS; | |
| 609 | } | ||
| 610 |