9.2
general documentation
cs_base.h
Go to the documentation of this file.
1#ifndef CS_BASE_H
2#define CS_BASE_H
3
4/*============================================================================
5 * Definitions, global variables, and base functions
6 *============================================================================*/
7
8/*
9 This file is part of code_saturne, a general-purpose CFD tool.
10
11 Copyright (C) 1998-2026 EDF S.A.
12
13 This program is free software; you can redistribute it and/or modify it under
14 the terms of the GNU General Public License as published by the Free Software
15 Foundation; either version 2 of the License, or (at your option) any later
16 version.
17
18 This program is distributed in the hope that it will be useful, but WITHOUT
19 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
20 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
21 details.
22
23 You should have received a copy of the GNU General Public License along with
24 this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
25 Street, Fifth Floor, Boston, MA 02110-1301, USA.
26*/
27
28/*----------------------------------------------------------------------------*/
29
30#include "base/cs_defs.h"
31
32/*----------------------------------------------------------------------------
33 * Standard C library headers
34 *----------------------------------------------------------------------------*/
35
36#include <stdio.h>
37
38/*----------------------------------------------------------------------------
39 * Local headers
40 *----------------------------------------------------------------------------*/
41
42/*=============================================================================
43 * Macro definitions
44 *============================================================================*/
45
46/* Application type name */
47
48#define CS_APP_NAME "code_saturne"
49#define CS_APP_VERSION PACKAGE_VERSION /* PACKAGE_VERSION from autoconf */
50
51/* System type name */
52
53#if defined(__linux__) || defined(__linux) || defined(linux)
54#define _CS_ARCH_Linux
55
56#endif
57
58/* On certain architectures such as IBM Blue Gene, some operations may
59 * be better optimized on memory-aligned data (if 0 here, no alignment
60 * is leveraged). This alignment is not exploited yet in code_saturne. */
61
62#define CS_MEM_ALIGN 0
63
64#define CS_BASE_STRING_LEN 80
65
66/*============================================================================
67 * Type definitions
68 *============================================================================*/
69
70/* Function pointers for extra cleanup operations to be called when
71 entering cs_exit() or bft_error() */
72
73typedef void (cs_base_atexit_t) (void);
74
75/* Function pointers for SIGINT (or similar) handler */
76
77typedef void (cs_base_sigint_handler_t) (int signum);
78
79/*=============================================================================
80 * Global variable definitions
81 *============================================================================*/
82
83#if defined(HAVE_ACCEL)
84
85extern int cs_mpi_device_support;
86
87#else
88
89#define cs_mpi_device_support 0
90
91#endif
92
93/*=============================================================================
94 * Public function prototypes
95 *============================================================================*/
96
97/*----------------------------------------------------------------------------*/
105/*----------------------------------------------------------------------------*/
106
107inline const char *
108cs_base_strtf(bool boolean)
109{
110 if (boolean)
111 return "*True*";
112 else
113 return "*False*";
114}
115
116/*----------------------------------------------------------------------------
117 * First analysis of the command line to determine an application name.
118 *
119 * If no name is defined by the command line, a name is determined based
120 * on the working directory.
121 *
122 * The caller is responsible for freeing the returned string.
123 *
124 * parameters:
125 * argc <-- number of command line arguments
126 * argv <-- array of command line arguments
127 *
128 * returns:
129 * pointer to character string with application name
130 *----------------------------------------------------------------------------*/
131
132char *
133cs_base_get_app_name(int argc,
134 const char *argv[]);
135
136/*----------------------------------------------------------------------------
137 * Print logfile header
138 *
139 * parameters:
140 * argc <-- number of command line arguments
141 * argv <-- array of command line arguments
142 *----------------------------------------------------------------------------*/
143
144void
145cs_base_logfile_head(int argc,
146 char *argv[]);
147
148#if defined(HAVE_MPI)
149
150/*----------------------------------------------------------------------------
151 * First analysis of the command line and environment variables to determine
152 * if we require MPI, and initialization if necessary.
153 *
154 * parameters:
155 * argc <-> number of command line arguments
156 * argv <-> array of command line arguments
157 *
158 * Global variables `cs_glob_n_ranks' (number of code_saturne processes)
159 * and `cs_glob_rank_id' (rank of local process) are set by this function.
160 *----------------------------------------------------------------------------*/
161
162void
163cs_base_mpi_init(int *argc,
164 char **argv[]);
165
166/*----------------------------------------------------------------------------*/
175/*----------------------------------------------------------------------------*/
176
177MPI_Comm
178cs_base_get_rank_step_comm(int rank_step);
179
180/*----------------------------------------------------------------------------
181 * Return a reduced communicator matching a multiple of the total
182 * number of ranks, and given a parent communicator.
183 *
184 * Compared to \ref cs_base_get_rank_step_comm, this function is
185 * collective only on the provided communicator.
186 *
187 * This updates the number of reduced communicators if necessary.
188 *
189 * parameters:
190 * parent_comm <-- associated parent communicator (must be either
191 * cs_glob_mpi_comm or a communicator returned by a
192 * previous
193 * rank_step <-- associated multiple of ranks of parent communicator
194 *----------------------------------------------------------------------------*/
195
196MPI_Comm
197cs_base_get_rank_step_comm_recursive(MPI_Comm parent_comm,
198 int rank_step);
199
200#endif /* defined(HAVE_MPI) */
201
202/*----------------------------------------------------------------------------
203 * Exit, with handling for both normal and error cases.
204 *
205 * Finalize MPI if necessary.
206 *
207 * parameters:
208 * status <-- value to be returned to the parent:
209 * EXIT_SUCCESS / 0 for the normal case,
210 * EXIT_FAILURE or other nonzero code for error cases.
211 *----------------------------------------------------------------------------*/
212
213void
214cs_exit(int status);
215
216/*----------------------------------------------------------------------------
217 * Initialize error and signal handlers.
218 *
219 * parameters:
220 * signal_defaults <-- leave default signal handlers in place if true
221 *----------------------------------------------------------------------------*/
222
223void
224cs_base_error_init(bool signal_defaults);
225
226/*----------------------------------------------------------------------------
227 * Initialize management of memory allocated through BFT.
228 *----------------------------------------------------------------------------*/
229
230void
231cs_base_mem_init(void);
232
233/*----------------------------------------------------------------------------
234 * Finalize management of memory allocated through BFT.
235 *
236 * A summary of the consumed memory is given.
237 *----------------------------------------------------------------------------*/
238
239void
241
242/*----------------------------------------------------------------------------
243 * Restore signal handlers in case they were modified by another application.
244 *----------------------------------------------------------------------------*/
245
246void
248
249/*----------------------------------------------------------------------------
250 * Print summary of running time, including CPU and elapsed times.
251 *----------------------------------------------------------------------------*/
252
253void
255
256/*----------------------------------------------------------------------------*/
265/*----------------------------------------------------------------------------*/
266
267void
268cs_base_update_status(const char *format,
269 ...);
270
271/*----------------------------------------------------------------------------
272 * Set tracing of progress on or off.
273 *
274 * This function should be called before cs_base_bft_printf_set() if tracing
275 * is activated.
276 *
277 * parameters:
278 * trace <-- trace progress to stdout
279 *----------------------------------------------------------------------------*/
280
281void
282cs_base_trace_set(bool trace);
283
284/*----------------------------------------------------------------------------
285 * Set output file name and suppression flag for bft_printf().
286 *
287 * This allows redirecting or suppressing logging for different ranks.
288 *
289 * parameters:
290 * log_name <-- base file name for log
291 * rn_log_flag <-- redirection for ranks > 0 log:
292 * false: to "/dev/null" (suppressed)
293 * true: to <log_name>_r*.log" file;
294 *----------------------------------------------------------------------------*/
295
296void
297cs_base_bft_printf_init(const char *log_name,
298 bool rn_log_flag);
299
300/*----------------------------------------------------------------------------
301 * Replace default bft_printf() mechanism with internal mechanism.
302 *
303 * This allows redirecting or suppressing logging for different ranks.
304 *
305 * parameters:
306 * log_name <-- base file name for log
307 * rn_log_flag <-- redirection for ranks > 0 log:
308 * false: to "/dev/null" (suppressed)
309 * true: to <log_name>_r*.log" file;
310 *----------------------------------------------------------------------------*/
311
312void
313cs_base_bft_printf_set(const char *log_name,
314 bool rn_log_flag);
315
316/*----------------------------------------------------------------------------
317 * Return name of default log file.
318 *
319 * cs_base_bft_printf_set or cs_base_c_bft_printf_set() must have
320 * been called before this.
321 *
322 * returns:
323 * name of default log file
324 *----------------------------------------------------------------------------*/
325
326const char *
328
329/*----------------------------------------------------------------------------
330 * Return flag indicating if the default log file output is suppressed.
331 *
332 * cs_base_bft_printf_set or cs_base_c_bft_printf_set() must have
333 * been called before this.
334 *
335 * returns:
336 * output suppression flag
337 *----------------------------------------------------------------------------*/
338
339bool
341
342/*----------------------------------------------------------------------------
343 * Print a warning message header.
344 *
345 * parameters:
346 * file_name <-- name of source file
347 * line_nume <-- line number in source file
348 *----------------------------------------------------------------------------*/
349
350void
351cs_base_warn(const char *file_name,
352 int line_num);
353
354/*----------------------------------------------------------------------------
355 * Define a function to be called when entering cs_exit() or bft_error().
356 *
357 * Compared to the C atexit(), only one function may be called (latest
358 * setting wins), but the function is called slightly before exit,
359 * so it is well adapted to cleanup such as flushing of non-C API logging.
360 *
361 * parameters:
362 * fct <-- pointer tu function to be called
363 *----------------------------------------------------------------------------*/
364
365void
367
368/*----------------------------------------------------------------------------
369 * Set handler function for SIGINT or similar.
370 *
371 * When first encountered, SIGINT will call that handler if present,
372 * then revert to the general handler if encountered again.
373 *
374 * parameters:
375 * h <-- pointer to function to be called
376 *----------------------------------------------------------------------------*/
377
378void
380
381/*----------------------------------------------------------------------------
382 * Clean a string representing options.
383 *
384 * Characters are converted to lowercase, leading and trailing whitespace
385 * is removed, and multiple whitespaces or tabs are replaced by single
386 * spaces.
387 *
388 * parameters:
389 * s <-> string to be cleaned
390 *----------------------------------------------------------------------------*/
391
392void
394
395/*----------------------------------------------------------------------------
396 * Return a string providing locale path information.
397 *
398 * This is normally the path determined upon configuration, but may be
399 * adapted for movable installs using the CS_ROOT_DIR environment variable.
400 *
401 * returns:
402 * locale path
403 *----------------------------------------------------------------------------*/
404
405const char *
407
408/*----------------------------------------------------------------------------
409 * Return a string providing package data path information.
410 *
411 * This is normally the path determined upon configuration, but may be
412 * adapted for movable installs using the CS_ROOT_DIR environment variable.
413 *
414 * returns:
415 * package data path
416 *----------------------------------------------------------------------------*/
417
418const char *
420
421/*----------------------------------------------------------------------------
422 * Return a string providing loadable library path information.
423 *
424 * This is normally the path determined upon configuration, but may be
425 * adapted for movable installs using the CS_ROOT_DIR environment variable.
426 *
427 * returns:
428 * package loadable library (plugin) path
429 *----------------------------------------------------------------------------*/
430
431const char *
433
434/*----------------------------------------------------------------------------
435 * Ensure bool argument has value 0 or 1.
436 *
437 * This allows working around issues with Intel compiler C bindings,
438 * which seem to pass incorrect values in some cases.
439 *
440 * parameters:
441 * b <-> pointer to bool
442 *----------------------------------------------------------------------------*/
443
444void
445cs_base_check_bool(bool *b);
446
447/*----------------------------------------------------------------------------
448 * Open a data file in read mode.
449 *
450 * If a file of the given name in the working directory is found, it
451 * will be opened. Otherwise, it will be searched for in the "data/thch"
452 * subdirectory of pkgdatadir.
453 *
454 * parameters:
455 * base_name <-- base file name
456 *
457 * returns:
458 * pointer to opened file
459 *----------------------------------------------------------------------------*/
460
461FILE *
462cs_base_open_properties_data_file(const char *base_name);
463
464#if defined(HAVE_DLOPEN)
465
466/*----------------------------------------------------------------------------*/
474/*----------------------------------------------------------------------------*/
475
476void*
477cs_base_dlopen(const char *filename);
478
479/*----------------------------------------------------------------------------*/
491/*----------------------------------------------------------------------------*/
492
493void*
494cs_base_dlopen_plugin(const char *name);
495
496/*----------------------------------------------------------------------------*/
502/*----------------------------------------------------------------------------*/
503
504int
505cs_base_dlopen_get_flags(void);
506
507/*----------------------------------------------------------------------------*/
513/*----------------------------------------------------------------------------*/
514
515void
516cs_base_dlopen_set_flags(int flags);
517
518/*----------------------------------------------------------------------------*/
530/*----------------------------------------------------------------------------*/
531
532void
533cs_base_dlclose(const char *filename,
534 void *handle);
535
536/*----------------------------------------------------------------------------*/
546/*----------------------------------------------------------------------------*/
547
548void *
549cs_base_get_dl_function_pointer(void *handle,
550 const char *name,
551 bool errors_are_fatal);
552
553#endif /* defined(HAVE_DLOPEN) */
554
555/*----------------------------------------------------------------------------*/
562/*----------------------------------------------------------------------------*/
563
564void
566 int lv_start);
567
568/*----------------------------------------------------------------------------*/
577/*----------------------------------------------------------------------------*/
578
579void
581
582/*----------------------------------------------------------------------------*/
589/*----------------------------------------------------------------------------*/
590
591void
593
594/*----------------------------------------------------------------------------*/
614/*----------------------------------------------------------------------------*/
615
616void
617cs_base_get_run_identity(char **run_id,
618 char **case_name,
619 char **study_name);
620
621/*----------------------------------------------------------------------------*/
622
623#endif /* CS_BASE_H */
void cs_base_option_string_clean(char *s)
Definition: cs_base.cpp:2462
const char * cs_base_get_localedir(void)
Definition: cs_base.cpp:2495
const char * cs_base_get_pkgdatadir(void)
Definition: cs_base.cpp:2513
void cs_base_mpi_init(int *argc, char **argv[])
Definition: cs_base.cpp:1303
void cs_base_sigint_handler_set(cs_base_sigint_handler_t *const h)
Definition: cs_base.cpp:2445
void cs_base_time_summary(void)
Definition: cs_base.cpp:2063
void cs_exit(int status)
Definition: cs_base.cpp:1603
void cs_base_check_bool(bool *b)
Definition: cs_base.cpp:2549
void cs_base_error_init(bool signal_defaults)
Definition: cs_base.cpp:1645
void cs_base_mem_finalize(void)
Definition: cs_base.cpp:1775
void cs_base_signal_restore(void)
Definition: cs_base.cpp:2027
void cs_base_at_finalize(cs_base_atexit_t *func)
Register a function to be called at the finalization stage.
Definition: cs_base.cpp:2857
char * cs_base_get_app_name(int argc, const char *argv[])
Definition: cs_base.cpp:1148
void cs_base_logfile_head(int argc, char *argv[])
Definition: cs_base.cpp:1207
MPI_Comm cs_base_get_rank_step_comm_recursive(MPI_Comm parent_comm, int rank_step)
Definition: cs_base.cpp:1522
void() cs_base_sigint_handler_t(int signum)
Definition: cs_base.h:77
void cs_base_finalize_sequence(void)
Call sequence of finalization functions.
Definition: cs_base.cpp:2877
const char * cs_base_get_pkglibdir(void)
Definition: cs_base.cpp:2531
void cs_base_update_status(const char *format,...)
Update status file.
Definition: cs_base.cpp:2143
void cs_base_trace_set(bool trace)
Definition: cs_base.cpp:2220
void cs_base_backtrace_dump(FILE *f, int lv_start)
Dump a stack trace to a file.
Definition: cs_base.cpp:2791
void cs_base_warn(const char *file_name, int line_num)
Definition: cs_base.cpp:2410
void cs_base_mem_init(void)
Definition: cs_base.cpp:1694
#define cs_mpi_device_support
Definition: cs_base.h:89
const char * cs_base_strtf(bool boolean)
Return a string "true" or "false" according to the boolean.
Definition: cs_base.h:108
const char * cs_base_bft_printf_name(void)
Definition: cs_base.cpp:2380
void cs_base_atexit_set(cs_base_atexit_t *const fct)
Definition: cs_base.cpp:2429
MPI_Comm cs_base_get_rank_step_comm(int rank_step)
Return a reduced communicator matching a multiple of the total number of ranks.
Definition: cs_base.cpp:1441
void cs_base_get_run_identity(char **run_id, char **case_name, char **study_name)
Query run-time directory info, using working directory names.
Definition: cs_base.cpp:2910
bool cs_base_bft_printf_suppressed(void)
Definition: cs_base.cpp:2396
void cs_base_bft_printf_set(const char *log_name, bool rn_log_flag)
Definition: cs_base.cpp:2317
void cs_base_bft_printf_init(const char *log_name, bool rn_log_flag)
Definition: cs_base.cpp:2239
void() cs_base_atexit_t(void)
Definition: cs_base.h:73
FILE * cs_base_open_properties_data_file(const char *base_name)
Definition: cs_base.cpp:2579
@ h
Definition: cs_field_pointer.h:90