(git:e70887f)
Loading...
Searching...
No Matches
fft_timer.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#include "fft_timer.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
18typedef struct {
20 int handle;
21 double total_time;
22 double self_time;
25
34
41
43static struct fft_stack_type *stack = NULL;
44
45static bool timers_initialized = false;
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 ******************************************************************************/
55static char *fft_strdup(const char *src) {
56 char *dst = malloc(strlen(src) + 1);
57 cp_assert(dst != NULL);
58 strcpy(dst, src);
59 return dst;
60}
61
62/*******************************************************************************
63 * \brief Find the index of a timed routine by name.
64 ******************************************************************************/
65static int find_routine_by_name(const char *routine_name) {
66 for (int routine = 0; routine < number_of_timed_routines; routine++)
67 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 ******************************************************************************/
77int get_routine_handle(const char *routine_name) {
78 const int found = find_routine_by_name(routine_name);
79 if (found >= 0)
80 return timed_routines[found].handle;
81 // If we haven't found it, we create a new one
83 // If the buffer is full, double its size
86 sizeof(fft_timed_routine));
87 }
88 // Copy the data
90 fft_strdup(routine_name);
94 timed_routines[number_of_timed_routines].number_of_calls = 0;
95 // Update the counter and return its OLD value
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 ******************************************************************************/
108void 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 *total_time = 0.0;
112 *self_time = 0.0;
113 *number_of_calls = 0;
114 const int routine = find_routine_by_name(routine_name);
115 if (routine >= 0) {
116 *total_time = timed_routines[routine].total_time;
117 *self_time = timed_routines[routine].self_time;
118 *number_of_calls = timed_routines[routine].number_of_calls;
119 }
120}
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 ******************************************************************************/
129void update_routine(const int handle, const double total_time,
130 const double self_time) {
132 "Requested routine not found!");
134 timed_routines[handle].total_time += total_time;
135 timed_routines[handle].self_time += self_time;
136 timed_routines[handle].number_of_calls++;
137}
138
139/*******************************************************************************
140 * \brief Push new routine on function stack.
141 * \param handle Handle of the pushed function
142 * \author Frederick Stein
143 ******************************************************************************/
144void push_on_stack(const int handle) {
145 struct fft_stack_type *new_stack = malloc(sizeof(struct fft_stack_type));
146 new_stack->handle = handle;
147 new_stack->next = stack;
148 new_stack->time_of_called_routines = 0.0;
149 new_stack->start_time = omp_get_wtime();
150 stack = new_stack;
151}
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 ******************************************************************************/
160void pop_from_stack(int *handle, double *run_time, double *self_time) {
161 const double end = omp_get_wtime();
162 const double my_run_time = end - stack->start_time;
163 *handle = stack->handle;
164 *run_time = my_run_time;
165 *self_time = my_run_time - stack->time_of_called_routines;
166 struct fft_stack_type *top_of_stack = stack;
167 stack = top_of_stack->next;
168 if (stack != NULL)
169 stack->time_of_called_routines += my_run_time;
170 free(top_of_stack);
171}
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 ******************************************************************************/
180int compare_fft_timing_statistics(const void *a, const void *b) {
181 const double max_total_time_a =
182 ((const fft_timing_statistics *)a)->max_total_time;
183 const double max_total_time_b =
184 ((const fft_timing_statistics *)b)->max_total_time;
185 if (max_total_time_a < max_total_time_b) {
186 return 1;
187 } else if (max_total_time_a > max_total_time_b) {
188 return -1;
189 } else {
190 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 ******************************************************************************/
200 cp_assert(omp_get_num_threads() == 1);
201 if (!timers_initialized) {
202 timed_routines = calloc(16, sizeof(fft_timed_routine));
204 timers_initialized = true;
205 }
206}
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 ******************************************************************************/
215static void fft_print_to_stdout(const char *msg, const int msglen,
216 const int output_unit) {
217 (void)output_unit;
218 fwrite(msg, 1, msglen, stdout);
219}
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 ******************************************************************************/
230 const int output_unit, const char *format, ...) {
231 char buffer[4096];
232 va_list args;
233 va_start(args, format);
234 const int length = vsnprintf(buffer, sizeof(buffer), format, args);
235 va_end(args);
236 if (length > 0 && print_func != NULL) {
237 print_func(buffer,
238 length < (int)sizeof(buffer) ? length : (int)sizeof(buffer) - 1,
239 output_unit);
240 }
241}
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 ******************************************************************************/
253void fft_print_timing_report_unit(const double threshold,
255 const int output_unit,
256 const cp_mpi_comm_t comm) {
257 cp_assert(omp_get_num_threads() == 1);
258
259 int initialized_ranks = timers_initialized ? 1 : 0;
260 cp_mpi_sum_int(&initialized_ranks, 1, comm);
261 if (initialized_ranks != cp_mpi_comm_size(comm)) {
262 if (cp_mpi_comm_rank(comm) == 0)
264 print_func, output_unit,
265 "FFT timer not initialized on every rank. Timing report is not "
266 "available!\n");
267 return;
268 }
270 return;
271
272 const int rank = cp_mpi_comm_rank(comm);
273 const int number_of_ranks = cp_mpi_comm_size(comm);
274 int number_of_routines = rank == 0 ? number_of_timed_routines : 0;
275 cp_mpi_bcast_int(&number_of_routines, 1, 0, comm);
276
277 fft_timing_statistics *statistics =
278 rank == 0 ? calloc(number_of_routines, sizeof(fft_timing_statistics))
279 : NULL;
280 for (int routine = 0; routine < number_of_routines; routine++) {
281 int length =
282 rank == 0 ? (int)strlen(timed_routines[routine].routine_name) + 1 : 0;
283 cp_mpi_bcast_int(&length, 1, 0, comm);
284 char *routine_name = malloc((size_t)length);
285 cp_assert(routine_name != NULL);
286 if (rank == 0)
287 memcpy(routine_name, timed_routines[routine].routine_name,
288 (size_t)length);
289 cp_mpi_bcast_char(routine_name, length, 0, comm);
290
291 double total_time, self_time;
292 int number_of_calls;
293 get_routine_infos(routine_name, &total_time, &self_time, &number_of_calls);
294 double summed_info[2] = {total_time, self_time};
295 cp_mpi_sum_root_double(summed_info, 2, 0, comm);
296 double max_info[2] = {total_time, self_time};
297 cp_mpi_max_root_double(max_info, 2, 0, comm);
298
299 if (rank == 0) {
300 statistics[routine].routine_name = routine_name;
301 statistics[routine].avg_total_time = summed_info[0] / number_of_ranks;
302 statistics[routine].avg_self_time = summed_info[1] / number_of_ranks;
303 statistics[routine].max_total_time = max_info[0];
304 statistics[routine].max_self_time = max_info[1];
305 statistics[routine].number_of_calls = number_of_calls;
306 } else {
307 free(routine_name);
308 }
309 }
310
311 if (rank == 0) {
312 qsort(statistics, number_of_routines, sizeof(fft_timing_statistics),
314 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 for (int routine = 0; routine < number_of_routines; routine++) {
330 if (statistics[routine].max_total_time >=
331 threshold * statistics[0].max_total_time &&
332 statistics[routine].number_of_calls > 0)
333 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 free(statistics[routine].routine_name);
342 }
343 fft_report_printf(print_func, output_unit,
344 " -------------------------------------------------------"
345 "------------------------\n");
346 free(statistics);
347 }
348 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 ******************************************************************************/
357void fft_print_timing_report(const double threshold, const cp_mpi_comm_t comm) {
359 fflush(stdout);
360}
361
362/*******************************************************************************
363 * \brief Finalizes the internal timer.
364 * \note Thread-safe. Only the master-thread does something.
365 * \author Frederick Stein
366 ******************************************************************************/
368 cp_assert(omp_get_num_threads() == 1);
369 if (timers_initialized) {
370 cp_assert(stack == NULL);
371 for (int routine = 0; routine < number_of_timed_routines; routine++) {
372 free(timed_routines[routine].routine_name);
373 }
374 free(timed_routines);
376 timers_initialized = false;
377 }
378}
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 ******************************************************************************/
386int fft_start_timer(const char *routine_name) {
387 offload_timeset(routine_name);
388 if (omp_get_thread_num() == 0) {
389 const int handle = get_routine_handle(routine_name);
391 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 ******************************************************************************/
402void fft_stop_timer(const int handle) {
404 if (timers_initialized) {
405 if (omp_get_thread_num() == 0) {
406 cp_assert(stack != NULL && "Stack is empty!\n");
407 int stack_handle;
408 double total_time, self_time;
409 pop_from_stack(&stack_handle, &total_time, &self_time);
410 cp_assert(stack_handle == handle &&
411 "Incorrect order of timing regions!\n");
412 // Merge with the list of routines
413 update_routine(handle, total_time, self_time);
414 if (stack == NULL) {
415 const char routine_name[] = "library";
416 const int global_handle = get_routine_handle(routine_name);
417 update_routine(global_handle, total_time, 0.0);
418 }
419 }
420 }
421}
422
423// EOF
#define cp_assert(condition)
Definition cp_assert.h:22
int cp_mpi_comm_size(const cp_mpi_comm_t comm)
Wrapper around MPI_Comm_size.
Definition cp_mpi.c:208
void cp_mpi_bcast_char(char *buf, const int count, const int root, const cp_mpi_comm_t comm)
Wrapper around MPI_Bcast for datatype MPI_CHAR.
Definition cp_mpi.c:702
void cp_mpi_barrier(const cp_mpi_comm_t comm)
Wrapper around MPI_Barrier; a null communicator is a no-op.
Definition cp_mpi.c:224
void cp_mpi_max_root_double(double *values, const int count, const int root, const cp_mpi_comm_t comm)
Wrapper around MPI_Reduce for op MPI_MAX and datatype MPI_DOUBLE.
Definition cp_mpi.c:428
void cp_mpi_sum_int(int *values, const int count, const cp_mpi_comm_t comm)
Wrapper around MPI_Allreduce for op MPI_SUM and datatype MPI_INT.
Definition cp_mpi.c:453
void cp_mpi_sum_root_double(double *values, const int count, const int root, const cp_mpi_comm_t comm)
Wrapper around MPI_Reduce for op MPI_SUM and datatype MPI_DOUBLE.
Definition cp_mpi.c:547
void cp_mpi_bcast_int(int *buf, const int count, const int root, const cp_mpi_comm_t comm)
Wrapper around MPI_Bcast for datatype MPI_INT.
Definition cp_mpi.c:686
int cp_mpi_comm_rank(const cp_mpi_comm_t comm)
Wrapper around MPI_Comm_rank.
Definition cp_mpi.c:191
int cp_mpi_comm_t
Definition cp_mpi.h:18
static void print_func(const char *msg, int msglen, int output_unit)
Wrapper for printf, passed to dbm_library_print_stats.
Definition dbm_miniapp.c:26
void(* fft_print_func_t)(const char *, int, int)
Definition fft_api_F.h:42
static void fft_report_printf(fft_print_func_t print_func, const int output_unit, const char *format,...)
Format and emit one FFT timing-report line through a callback.
Definition fft_timer.c:229
static bool timers_initialized
Definition fft_timer.c:45
static int find_routine_by_name(const char *routine_name)
Find the index of a timed routine by name.
Definition fft_timer.c:65
static struct fft_stack_type * stack
Definition fft_timer.c:43
void push_on_stack(const int handle)
Push new routine on function stack.
Definition fft_timer.c:144
int compare_fft_timing_statistics(const void *a, const void *b)
Comparator for sorting FFT timing entries by maximum total time.
Definition fft_timer.c:180
void fft_stop_timer(const int handle)
Stop a timing.
Definition fft_timer.c:402
void update_routine(const int handle, const double total_time, const double self_time)
Get a routine handle to a given routine name.
Definition fft_timer.c:129
void fft_init_timer()
Initializes the internal timer.
Definition fft_timer.c:199
static fft_timed_routine * timed_routines
Definition fft_timer.c:42
int fft_start_timer(const char *routine_name)
Start a timing section.
Definition fft_timer.c:386
void get_routine_infos(const char *routine_name, double *total_time, double *self_time, int *number_of_calls)
Get a routine handle to a given routine name.
Definition fft_timer.c:108
static int number_of_timed_routines
Definition fft_timer.c:46
static int buffer_size_timed_routines
Definition fft_timer.c:47
void pop_from_stack(int *handle, double *run_time, double *self_time)
Removes the element on top of the stack and returns its data.
Definition fft_timer.c:160
int get_routine_handle(const char *routine_name)
Get a routine handle to a given routine name.
Definition fft_timer.c:77
void fft_print_timing_report_unit(const double threshold, fft_print_func_t print_func, const int output_unit, const cp_mpi_comm_t comm)
Prints the timing report.
Definition fft_timer.c:253
static char * fft_strdup(const char *src)
String duplication. Can be removed with migration to C23.
Definition fft_timer.c:55
void fft_print_timing_report(const double threshold, const cp_mpi_comm_t comm)
Print the FFT timing report to standard output.
Definition fft_timer.c:357
void fft_finalize_timer()
Finalizes the internal timer.
Definition fft_timer.c:367
static void fft_print_to_stdout(const char *msg, const int msglen, const int output_unit)
Write an FFT timing message to standard output.
Definition fft_timer.c:215
void offload_timestop(void)
Ends a timing range.
void offload_timeset(const char *message)
Starts a timing range.
double time_of_called_routines
Definition fft_timer.c:38
double start_time
Definition fft_timer.c:37
struct fft_stack_type * next
Definition fft_timer.c:39
char * routine_name
Definition fft_timer.c:19