LCOV - code coverage report
Current view: top level - src/fft - fft_timer.c (source / functions) Coverage Total Hit
Test: CP2K Regtests (git:1be8651) Lines: 100.0 % 173 173
Test Date: 2026-11-24 01:22:08 Functions: 100.0 % 16 16

            Line data    Source code
       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              : #include "fft_timer.h"
       9              : #include "../offload/offload_library.h"
      10              : 
      11              : #include "../base/cp_assert.h"
      12              : #include <omp.h>
      13              : #include <stdarg.h>
      14              : #include <stdio.h>
      15              : #include <stdlib.h>
      16              : #include <string.h>
      17              : 
      18              : typedef struct {
      19              :   char *routine_name;
      20              :   int handle;
      21              :   double total_time;
      22              :   double self_time;
      23              :   int number_of_calls;
      24              : } fft_timed_routine;
      25              : 
      26              : typedef struct {
      27              :   char *routine_name;
      28              :   double avg_total_time;
      29              :   double avg_self_time;
      30              :   double max_total_time;
      31              :   double max_self_time;
      32              :   int number_of_calls;
      33              : } fft_timing_statistics;
      34              : 
      35              : struct fft_stack_type {
      36              :   int handle;
      37              :   double start_time;
      38              :   double time_of_called_routines;
      39              :   struct fft_stack_type *next;
      40              : };
      41              : 
      42              : static fft_timed_routine *timed_routines = NULL;
      43              : static struct fft_stack_type *stack = NULL;
      44              : 
      45              : static bool timers_initialized = false;
      46              : static int number_of_timed_routines = 0;
      47              : static int buffer_size_timed_routines = 0;
      48              : 
      49              : /*******************************************************************************
      50              :  * \brief String duplication. Can be removed with migration to C23.
      51              :  * \param handle Handle of the pushed function
      52              :  * \param src Source string to duplicate.
      53              :  * \author Frederick Stein
      54              :  ******************************************************************************/
      55       285530 : static char *fft_strdup(const char *src) {
      56       285530 :   char *dst = malloc(strlen(src) + 1);
      57       285530 :   cp_assert(dst != NULL);
      58       285530 :   strcpy(dst, src);
      59       285530 :   return dst;
      60              : }
      61              : 
      62              : /*******************************************************************************
      63              :  * \brief Find the index of a timed routine by name.
      64              :  ******************************************************************************/
      65     58015018 : static int find_routine_by_name(const char *routine_name) {
      66   1063679507 :   for (int routine = 0; routine < number_of_timed_routines; routine++)
      67   1063336667 :     if (strcmp(routine_name, timed_routines[routine].routine_name) == 0)
      68              :       return routine;
      69              :   return -1;
      70              : }
      71              : 
      72              : /*******************************************************************************
      73              :  * \brief Get a routine handle to a given routine name.
      74              :  * \param routine_name Name of the timed routine.
      75              :  * \author Frederick Stein
      76              :  ******************************************************************************/
      77     57731640 : int get_routine_handle(const char *routine_name) {
      78     57731640 :   const int found = find_routine_by_name(routine_name);
      79     57731640 :   if (found >= 0)
      80     57446110 :     return timed_routines[found].handle;
      81              :   // If we haven't found it, we create a new one
      82       285530 :   if (number_of_timed_routines == buffer_size_timed_routines) {
      83              :     // If the buffer is full, double its size
      84        12745 :     buffer_size_timed_routines *= 2;
      85        12745 :     timed_routines = realloc(timed_routines, buffer_size_timed_routines *
      86              :                                                  sizeof(fft_timed_routine));
      87              :   }
      88              :   // Copy the data
      89       571060 :   timed_routines[number_of_timed_routines].routine_name =
      90       285530 :       fft_strdup(routine_name);
      91       285530 :   timed_routines[number_of_timed_routines].handle = number_of_timed_routines;
      92       285530 :   timed_routines[number_of_timed_routines].total_time = 0.0;
      93       285530 :   timed_routines[number_of_timed_routines].self_time = 0.0;
      94       285530 :   timed_routines[number_of_timed_routines].number_of_calls = 0;
      95              :   // Update the counter and return its OLD value
      96       285530 :   return number_of_timed_routines++;
      97              : }
      98              : 
      99              : /*******************************************************************************
     100              :  * \brief Get a routine handle to a given routine name.
     101              :  * \param handle Handle of the pushed function
     102              :  * \param routine_name Name of the timed routine.
     103              :  * \param total_time Total time of the timed routine.
     104              :  * \param self_time Self time of the timed routine.
     105              :  * \param number_of_calls Number of calls of the timed routine.
     106              :  * \author Frederick Stein
     107              :  ******************************************************************************/
     108       283378 : void get_routine_infos(const char *routine_name, double *total_time,
     109              :                        double *self_time, int *number_of_calls) {
     110              :   // Use the defaults if a routine was not found
     111       283378 :   *total_time = 0.0;
     112       283378 :   *self_time = 0.0;
     113       283378 :   *number_of_calls = 0;
     114       283378 :   const int routine = find_routine_by_name(routine_name);
     115       283378 :   if (routine >= 0) {
     116       226068 :     *total_time = timed_routines[routine].total_time;
     117       226068 :     *self_time = timed_routines[routine].self_time;
     118       226068 :     *number_of_calls = timed_routines[routine].number_of_calls;
     119              :   }
     120       283378 : }
     121              : 
     122              : /*******************************************************************************
     123              :  * \brief Get a routine handle to a given routine name.
     124              :  * \param handle Handle of the pushed function
     125              :  * \param total_time Total time of the timed routine.
     126              :  * \param self_time Self time of the timed routine.
     127              :  * \author Frederick Stein
     128              :  ******************************************************************************/
     129     57731640 : void update_routine(const int handle, const double total_time,
     130              :                     const double self_time) {
     131     57731640 :   cp_assert(handle >= 0 && handle < number_of_timed_routines &&
     132              :             "Requested routine not found!");
     133     57731640 :   cp_assert(timed_routines[handle].handle == handle);
     134     57731640 :   timed_routines[handle].total_time += total_time;
     135     57731640 :   timed_routines[handle].self_time += self_time;
     136     57731640 :   timed_routines[handle].number_of_calls++;
     137     57731640 : }
     138              : 
     139              : /*******************************************************************************
     140              :  * \brief Push new routine on function stack.
     141              :  * \param handle Handle of the pushed function
     142              :  * \author Frederick Stein
     143              :  ******************************************************************************/
     144     38487760 : void push_on_stack(const int handle) {
     145     38487760 :   struct fft_stack_type *new_stack = malloc(sizeof(struct fft_stack_type));
     146     38487760 :   new_stack->handle = handle;
     147     38487760 :   new_stack->next = stack;
     148     38487760 :   new_stack->time_of_called_routines = 0.0;
     149     38487760 :   new_stack->start_time = omp_get_wtime();
     150     38487760 :   stack = new_stack;
     151     38487760 : }
     152              : 
     153              : /*******************************************************************************
     154              :  * \brief Removes the element on top of the stack and returns its data.
     155              :  * \param handle Handle of the routines (for external checks)
     156              :  * \param run_time Total run time of the routine
     157              :  * \param self_time Time spent within the routine without called routines
     158              :  * \author Frederick Stein
     159              :  ******************************************************************************/
     160     38487760 : void pop_from_stack(int *handle, double *run_time, double *self_time) {
     161     38487760 :   const double end = omp_get_wtime();
     162     38487760 :   const double my_run_time = end - stack->start_time;
     163     38487760 :   *handle = stack->handle;
     164     38487760 :   *run_time = my_run_time;
     165     38487760 :   *self_time = my_run_time - stack->time_of_called_routines;
     166     38487760 :   struct fft_stack_type *top_of_stack = stack;
     167     38487760 :   stack = top_of_stack->next;
     168     38487760 :   if (stack != NULL)
     169     19243880 :     stack->time_of_called_routines += my_run_time;
     170     38487760 :   free(top_of_stack);
     171     38487760 : }
     172              : 
     173              : /*******************************************************************************
     174              :  * \brief Comparator for sorting FFT timing entries by maximum total time.
     175              :  * \param a First timing entry.
     176              :  * \param b Second timing entry.
     177              :  * \return Negative, zero, or positive according to the sort order.
     178              :  * \author Frederick Stein
     179              :  ******************************************************************************/
     180       550095 : int compare_fft_timing_statistics(const void *a, const void *b) {
     181       550095 :   const double max_total_time_a =
     182              :       ((const fft_timing_statistics *)a)->max_total_time;
     183       550095 :   const double max_total_time_b =
     184              :       ((const fft_timing_statistics *)b)->max_total_time;
     185       550095 :   if (max_total_time_a < max_total_time_b) {
     186              :     return 1;
     187       280427 :   } else if (max_total_time_a > max_total_time_b) {
     188              :     return -1;
     189              :   } else {
     190           80 :     return 0;
     191              :   }
     192              : }
     193              : 
     194              : /*******************************************************************************
     195              :  * \brief Initializes the internal timer.
     196              :  * \note To be called by all threads or outside of a parallel region.
     197              :  * \author Frederick Stein
     198              :  ******************************************************************************/
     199        11496 : void fft_init_timer() {
     200        11496 :   cp_assert(omp_get_num_threads() == 1);
     201        11496 :   if (!timers_initialized) {
     202        11496 :     timed_routines = calloc(16, sizeof(fft_timed_routine));
     203        11496 :     buffer_size_timed_routines = 16;
     204        11496 :     timers_initialized = true;
     205              :   }
     206        11496 : }
     207              : 
     208              : /*******************************************************************************
     209              :  * \brief Write an FFT timing message to standard output.
     210              :  * \param msg Message buffer.
     211              :  * \param msglen Number of characters to write.
     212              :  * \param output_unit Unused stdout marker.
     213              :  * \author Frederick Stein
     214              :  ******************************************************************************/
     215          495 : static void fft_print_to_stdout(const char *msg, const int msglen,
     216              :                                 const int output_unit) {
     217          495 :   (void)output_unit;
     218          495 :   fwrite(msg, 1, msglen, stdout);
     219          495 : }
     220              : 
     221              : /*******************************************************************************
     222              :  * \brief Format and emit one FFT timing-report line through a callback.
     223              :  * \param print_func Output callback.
     224              :  * \param output_unit Destination output unit.
     225              :  * \param format printf-style format string.
     226              :  * \param ... Optional format arguments.
     227              :  * \author Frederick Stein
     228              :  ******************************************************************************/
     229       153102 : static void fft_report_printf(fft_print_func_t print_func,
     230              :                               const int output_unit, const char *format, ...) {
     231       153102 :   char buffer[4096];
     232       153102 :   va_list args;
     233       153102 :   va_start(args, format);
     234       153102 :   const int length = vsnprintf(buffer, sizeof(buffer), format, args);
     235       153102 :   va_end(args);
     236       153102 :   if (length > 0 && print_func != NULL) {
     237       153102 :     print_func(buffer,
     238       153102 :                length < (int)sizeof(buffer) ? length : (int)sizeof(buffer) - 1,
     239              :                output_unit);
     240              :   }
     241       153102 : }
     242              : 
     243              : /*******************************************************************************
     244              :  * \brief Prints the timing report.
     245              :  * \note To be called by all threads or outside of a parallel region.
     246              :  * \note To be called by all MPI ranks
     247              :  * \param threshold Minimum relative timing value to report.
     248              :  * \param print_func Callback used to print the timing report.
     249              :  * \param output_unit Destination output unit.
     250              :  * \param comm MPI communicator.
     251              :  * \author Frederick Stein
     252              :  ******************************************************************************/
     253        11428 : void fft_print_timing_report_unit(const double threshold,
     254              :                                   fft_print_func_t print_func,
     255              :                                   const int output_unit,
     256              :                                   const cp_mpi_comm_t comm) {
     257        11428 :   cp_assert(omp_get_num_threads() == 1);
     258              : 
     259        11428 :   int initialized_ranks = timers_initialized ? 1 : 0;
     260        11428 :   cp_mpi_sum_int(&initialized_ranks, 1, comm);
     261        11428 :   if (initialized_ranks != cp_mpi_comm_size(comm)) {
     262          252 :     if (cp_mpi_comm_rank(comm) == 0)
     263          126 :       fft_report_printf(
     264              :           print_func, output_unit,
     265              :           "FFT timer not initialized on every rank. Timing report is not "
     266              :           "available!\n");
     267          252 :     return;
     268              :   }
     269        11176 :   if (!timers_initialized)
     270              :     return;
     271              : 
     272        11176 :   const int rank = cp_mpi_comm_rank(comm);
     273        11176 :   const int number_of_ranks = cp_mpi_comm_size(comm);
     274        11176 :   int number_of_routines = rank == 0 ? number_of_timed_routines : 0;
     275        11176 :   cp_mpi_bcast_int(&number_of_routines, 1, 0, comm);
     276              : 
     277        11176 :   fft_timing_statistics *statistics =
     278         5616 :       rank == 0 ? calloc(number_of_routines, sizeof(fft_timing_statistics))
     279        11176 :                 : NULL;
     280       294554 :   for (int routine = 0; routine < number_of_routines; routine++) {
     281       566756 :     int length =
     282       283378 :         rank == 0 ? (int)strlen(timed_routines[routine].routine_name) + 1 : 0;
     283       283378 :     cp_mpi_bcast_int(&length, 1, 0, comm);
     284       283378 :     char *routine_name = malloc((size_t)length);
     285       283378 :     cp_assert(routine_name != NULL);
     286       283378 :     if (rank == 0)
     287       141941 :       memcpy(routine_name, timed_routines[routine].routine_name,
     288              :              (size_t)length);
     289       283378 :     cp_mpi_bcast_char(routine_name, length, 0, comm);
     290              : 
     291       283378 :     double total_time, self_time;
     292       283378 :     int number_of_calls;
     293       283378 :     get_routine_infos(routine_name, &total_time, &self_time, &number_of_calls);
     294       283378 :     double summed_info[2] = {total_time, self_time};
     295       283378 :     cp_mpi_sum_root_double(summed_info, 2, 0, comm);
     296       283378 :     double max_info[2] = {total_time, self_time};
     297       283378 :     cp_mpi_max_root_double(max_info, 2, 0, comm);
     298              : 
     299       283378 :     if (rank == 0) {
     300       141941 :       statistics[routine].routine_name = routine_name;
     301       141941 :       statistics[routine].avg_total_time = summed_info[0] / number_of_ranks;
     302       141941 :       statistics[routine].avg_self_time = summed_info[1] / number_of_ranks;
     303       141941 :       statistics[routine].max_total_time = max_info[0];
     304       141941 :       statistics[routine].max_self_time = max_info[1];
     305       141941 :       statistics[routine].number_of_calls = number_of_calls;
     306              :     } else {
     307       141437 :       free(routine_name);
     308              :     }
     309              :   }
     310              : 
     311        11176 :   if (rank == 0) {
     312         5616 :     qsort(statistics, number_of_routines, sizeof(fft_timing_statistics),
     313              :           compare_fft_timing_statistics);
     314         5616 :     fft_report_printf(print_func, output_unit,
     315              :                       " -------------------------------------------------------"
     316              :                       "------------------------\n"
     317              :                       " -                                                      "
     318              :                       "                       -\n"
     319              :                       " -                             FFT TIMING REPORT        "
     320              :                       "                       -\n"
     321              :                       " -                                                      "
     322              :                       "                       -\n"
     323              :                       " -------------------------------------------------------"
     324              :                       "------------------------\n"
     325              :                       " ROUTINE                          CALLS           "
     326              :                       "SELF TIME          TOTAL TIME\n"
     327              :                       "                                          "
     328              :                       "AVERAGE   MAXIMUM   AVERAGE   MAXIMUM\n");
     329       147557 :     for (int routine = 0; routine < number_of_routines; routine++) {
     330       141941 :       if (statistics[routine].max_total_time >=
     331       141941 :               threshold * statistics[0].max_total_time &&
     332       141744 :           statistics[routine].number_of_calls > 0)
     333       141744 :         fft_report_printf(print_func, output_unit,
     334              :                           " %-30s %7i %9.3f %9.3f %9.3f %9.3f\n",
     335              :                           statistics[routine].routine_name,
     336              :                           statistics[routine].number_of_calls,
     337              :                           statistics[routine].avg_self_time,
     338              :                           statistics[routine].max_self_time,
     339              :                           statistics[routine].avg_total_time,
     340              :                           statistics[routine].max_total_time);
     341       141941 :       free(statistics[routine].routine_name);
     342              :     }
     343         5616 :     fft_report_printf(print_func, output_unit,
     344              :                       " -------------------------------------------------------"
     345              :                       "------------------------\n");
     346         5616 :     free(statistics);
     347              :   }
     348        11176 :   cp_mpi_barrier(comm);
     349              : }
     350              : 
     351              : /*******************************************************************************
     352              :  * \brief Print the FFT timing report to standard output.
     353              :  * \param threshold Minimum relative timing value to report.
     354              :  * \param comm MPI communicator used to aggregate timing data.
     355              :  * \author Frederick Stein
     356              :  ******************************************************************************/
     357            8 : void fft_print_timing_report(const double threshold, const cp_mpi_comm_t comm) {
     358            8 :   fft_print_timing_report_unit(threshold, fft_print_to_stdout, 0, comm);
     359            8 :   fflush(stdout);
     360            8 : }
     361              : 
     362              : /*******************************************************************************
     363              :  * \brief Finalizes the internal timer.
     364              :  * \note Thread-safe. Only the master-thread does something.
     365              :  * \author Frederick Stein
     366              :  ******************************************************************************/
     367        11743 : void fft_finalize_timer() {
     368        11743 :   cp_assert(omp_get_num_threads() == 1);
     369        11743 :   if (timers_initialized) {
     370        11496 :     cp_assert(stack == NULL);
     371       297026 :     for (int routine = 0; routine < number_of_timed_routines; routine++) {
     372       285530 :       free(timed_routines[routine].routine_name);
     373              :     }
     374        11496 :     free(timed_routines);
     375        11496 :     number_of_timed_routines = 0;
     376        11496 :     timers_initialized = false;
     377              :   }
     378        11743 : }
     379              : 
     380              : /*******************************************************************************
     381              :  * \brief Start a timing section.
     382              :  * \note Thread-safe. Only the master-thread does something.
     383              :  * \param routine_name Name of the timed routine.
     384              :  * \author Frederick Stein
     385              :  ******************************************************************************/
     386     38487760 : int fft_start_timer(const char *routine_name) {
     387     38487760 :   offload_timeset(routine_name);
     388     38487760 :   if (omp_get_thread_num() == 0) {
     389     38487760 :     const int handle = get_routine_handle(routine_name);
     390     38487760 :     push_on_stack(handle);
     391     38487760 :     return handle;
     392              :   }
     393              :   return -1;
     394              : }
     395              : 
     396              : /*******************************************************************************
     397              :  * \brief Stop a timing.
     398              :  * \note Thread-safe. Only the master-thread does something.
     399              :  * \param handle Timer handle.
     400              :  * \author Frederick Stein
     401              :  ******************************************************************************/
     402     38487760 : void fft_stop_timer(const int handle) {
     403     38487760 :   offload_timestop();
     404     38487760 :   if (timers_initialized) {
     405     38487760 :     if (omp_get_thread_num() == 0) {
     406     38487760 :       cp_assert(stack != NULL && "Stack is empty!\n");
     407     38487760 :       int stack_handle;
     408     38487760 :       double total_time, self_time;
     409     38487760 :       pop_from_stack(&stack_handle, &total_time, &self_time);
     410     38487760 :       cp_assert(stack_handle == handle &&
     411              :                 "Incorrect order of timing regions!\n");
     412              :       // Merge with the list of routines
     413     38487760 :       update_routine(handle, total_time, self_time);
     414     38487760 :       if (stack == NULL) {
     415     19243880 :         const char routine_name[] = "library";
     416     19243880 :         const int global_handle = get_routine_handle(routine_name);
     417     19243880 :         update_routine(global_handle, total_time, 0.0);
     418              :       }
     419              :     }
     420              :   }
     421     38487760 : }
     422              : 
     423              : // EOF
        

Generated by: LCOV version 2.0-1