9.1
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-2025 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
69
70/*============================================================================
71 * Type definitions
72 *============================================================================*/
73
74/* Function pointers for extra cleanup operations to be called when
75 entering cs_exit() or bft_error() */
76
77typedef void (cs_base_atexit_t) (void);
78
79/* Function pointers for SIGINT (or similar) handler */
80
81typedef void (cs_base_sigint_handler_t) (int signum);
82
83/*=============================================================================
84 * Global variable definitions
85 *============================================================================*/
86
87#if defined(HAVE_ACCEL)
88
89extern int cs_mpi_device_support;
90
91#else
92
93#define cs_mpi_device_support 0
94
95#endif
96
97/*=============================================================================
98 * Public function prototypes
99 *============================================================================*/
100
101/*----------------------------------------------------------------------------*/
109/*----------------------------------------------------------------------------*/
110
111static inline const char *
112cs_base_strtf(bool boolean)
113{
114 if (boolean)
115 return "*True*";
116 else
117 return "*False*";
118}
119
120/*----------------------------------------------------------------------------
121 * First analysis of the command line to determine an application name.
122 *
123 * If no name is defined by the command line, a name is determined based
124 * on the working directory.
125 *
126 * The caller is responsible for freeing the returned string.
127 *
128 * parameters:
129 * argc <-- number of command line arguments
130 * argv <-- array of command line arguments
131 *
132 * returns:
133 * pointer to character string with application name
134 *----------------------------------------------------------------------------*/
135
136char *
137cs_base_get_app_name(int argc,
138 const char *argv[]);
139
140/*----------------------------------------------------------------------------
141 * Print logfile header
142 *
143 * parameters:
144 * argc <-- number of command line arguments
145 * argv <-- array of command line arguments
146 *----------------------------------------------------------------------------*/
147
148void
149cs_base_logfile_head(int argc,
150 char *argv[]);
151
152#if defined(HAVE_MPI)
153
154/*----------------------------------------------------------------------------
155 * First analysis of the command line and environment variables to determine
156 * if we require MPI, and initialization if necessary.
157 *
158 * parameters:
159 * argc <-> number of command line arguments
160 * argv <-> array of command line arguments
161 *
162 * Global variables `cs_glob_n_ranks' (number of code_saturne processes)
163 * and `cs_glob_rank_id' (rank of local process) are set by this function.
164 *----------------------------------------------------------------------------*/
165
166void
167cs_base_mpi_init(int *argc,
168 char **argv[]);
169
170/*----------------------------------------------------------------------------*/
179/*----------------------------------------------------------------------------*/
180
181MPI_Comm
182cs_base_get_rank_step_comm(int rank_step);
183
184/*----------------------------------------------------------------------------
185 * Return a reduced communicator matching a multiple of the total
186 * number of ranks, and given a parent communicator.
187 *
188 * Compared to \ref cs_base_get_rank_step_comm, this function is
189 * collective only on the provided communicator.
190 *
191 * This updates the number of reduced communicators if necessary.
192 *
193 * parameters:
194 * parent_comm <-- associated parent communicator (must be either
195 * cs_glob_mpi_comm or a communicator returned by a
196 * previous
197 * rank_step <-- associated multiple of ranks of parent communicator
198 *----------------------------------------------------------------------------*/
199
200MPI_Comm
201cs_base_get_rank_step_comm_recursive(MPI_Comm parent_comm,
202 int rank_step);
203
204#endif /* defined(HAVE_MPI) */
205
206/*----------------------------------------------------------------------------
207 * Exit, with handling for both normal and error cases.
208 *
209 * Finalize MPI if necessary.
210 *
211 * parameters:
212 * status <-- value to be returned to the parent:
213 * EXIT_SUCCESS / 0 for the normal case,
214 * EXIT_FAILURE or other nonzero code for error cases.
215 *----------------------------------------------------------------------------*/
216
217void
218cs_exit(int status);
219
220/*----------------------------------------------------------------------------
221 * Initialize error and signal handlers.
222 *
223 * parameters:
224 * signal_defaults <-- leave default signal handlers in place if true
225 *----------------------------------------------------------------------------*/
226
227void
228cs_base_error_init(bool signal_defaults);
229
230/*----------------------------------------------------------------------------
231 * Initialize management of memory allocated through BFT.
232 *----------------------------------------------------------------------------*/
233
234void
235cs_base_mem_init(void);
236
237/*----------------------------------------------------------------------------
238 * Finalize management of memory allocated through BFT.
239 *
240 * A summary of the consumed memory is given.
241 *----------------------------------------------------------------------------*/
242
243void
245
246/*----------------------------------------------------------------------------
247 * Restore signal handlers in case they were modified by another application.
248 *----------------------------------------------------------------------------*/
249
250void
252
253/*----------------------------------------------------------------------------
254 * Print summary of running time, including CPU and elapsed times.
255 *----------------------------------------------------------------------------*/
256
257void
259
260/*----------------------------------------------------------------------------*/
269/*----------------------------------------------------------------------------*/
270
271void
272cs_base_update_status(const char *format,
273 ...);
274
275/*----------------------------------------------------------------------------
276 * Set tracing of progress on or off.
277 *
278 * This function should be called before cs_base_bft_printf_set() if tracing
279 * is activated.
280 *
281 * parameters:
282 * trace <-- trace progress to stdout
283 *----------------------------------------------------------------------------*/
284
285void
286cs_base_trace_set(bool trace);
287
288/*----------------------------------------------------------------------------
289 * Set output file name and suppression flag for bft_printf().
290 *
291 * This allows redirecting or suppressing logging for different ranks.
292 *
293 * parameters:
294 * log_name <-- base file name for log
295 * rn_log_flag <-- redirection for ranks > 0 log:
296 * false: to "/dev/null" (suppressed)
297 * true: to <log_name>_r*.log" file;
298 *----------------------------------------------------------------------------*/
299
300void
301cs_base_bft_printf_init(const char *log_name,
302 bool rn_log_flag);
303
304/*----------------------------------------------------------------------------
305 * Replace default bft_printf() mechanism with internal mechanism.
306 *
307 * This allows redirecting or suppressing logging for different ranks.
308 *
309 * parameters:
310 * log_name <-- base file name for log
311 * rn_log_flag <-- redirection for ranks > 0 log:
312 * false: to "/dev/null" (suppressed)
313 * true: to <log_name>_r*.log" file;
314 *----------------------------------------------------------------------------*/
315
316void
317cs_base_bft_printf_set(const char *log_name,
318 bool rn_log_flag);
319
320/*----------------------------------------------------------------------------
321 * Return name of default log file.
322 *
323 * cs_base_bft_printf_set or cs_base_c_bft_printf_set() must have
324 * been called before this.
325 *
326 * returns:
327 * name of default log file
328 *----------------------------------------------------------------------------*/
329
330const char *
332
333/*----------------------------------------------------------------------------
334 * Return flag indicating if the default log file output is suppressed.
335 *
336 * cs_base_bft_printf_set or cs_base_c_bft_printf_set() must have
337 * been called before this.
338 *
339 * returns:
340 * output suppression flag
341 *----------------------------------------------------------------------------*/
342
343bool
345
346/*----------------------------------------------------------------------------
347 * Print a warning message header.
348 *
349 * parameters:
350 * file_name <-- name of source file
351 * line_nume <-- line number in source file
352 *----------------------------------------------------------------------------*/
353
354void
355cs_base_warn(const char *file_name,
356 int line_num);
357
358/*----------------------------------------------------------------------------
359 * Define a function to be called when entering cs_exit() or bft_error().
360 *
361 * Compared to the C atexit(), only one function may be called (latest
362 * setting wins), but the function is called slightly before exit,
363 * so it is well adapted to cleanup such as flushing of non-C API logging.
364 *
365 * parameters:
366 * fct <-- pointer tu function to be called
367 *----------------------------------------------------------------------------*/
368
369void
371
372/*----------------------------------------------------------------------------
373 * Set handler function for SIGINT or similar.
374 *
375 * When first encountered, SIGINT will call that handler if present,
376 * then revert to the general handler if encountered again.
377 *
378 * parameters:
379 * h <-- pointer to function to be called
380 *----------------------------------------------------------------------------*/
381
382void
384
385/*----------------------------------------------------------------------------
386 * Clean a string representing options.
387 *
388 * Characters are converted to lowercase, leading and trailing whitespace
389 * is removed, and multiple whitespaces or tabs are replaced by single
390 * spaces.
391 *
392 * parameters:
393 * s <-> string to be cleaned
394 *----------------------------------------------------------------------------*/
395
396void
398
399/*----------------------------------------------------------------------------
400 * Return a string providing locale path information.
401 *
402 * This is normally the path determined upon configuration, but may be
403 * adapted for movable installs using the CS_ROOT_DIR environment variable.
404 *
405 * returns:
406 * locale path
407 *----------------------------------------------------------------------------*/
408
409const char *
411
412/*----------------------------------------------------------------------------
413 * Return a string providing package data path information.
414 *
415 * This is normally the path determined upon configuration, but may be
416 * adapted for movable installs using the CS_ROOT_DIR environment variable.
417 *
418 * returns:
419 * package data path
420 *----------------------------------------------------------------------------*/
421
422const char *
424
425/*----------------------------------------------------------------------------
426 * Return a string providing loadable library path information.
427 *
428 * This is normally the path determined upon configuration, but may be
429 * adapted for movable installs using the CS_ROOT_DIR environment variable.
430 *
431 * returns:
432 * package loadable library (plugin) path
433 *----------------------------------------------------------------------------*/
434
435const char *
437
438/*----------------------------------------------------------------------------
439 * Ensure bool argument has value 0 or 1.
440 *
441 * This allows working around issues with Intel compiler C bindings,
442 * which seem to pass incorrect values in some cases.
443 *
444 * parameters:
445 * b <-> pointer to bool
446 *----------------------------------------------------------------------------*/
447
448void
449cs_base_check_bool(bool *b);
450
451/*----------------------------------------------------------------------------
452 * Open a data file in read mode.
453 *
454 * If a file of the given name in the working directory is found, it
455 * will be opened. Otherwise, it will be searched for in the "data/thch"
456 * subdirectory of pkgdatadir.
457 *
458 * parameters:
459 * base_name <-- base file name
460 *
461 * returns:
462 * pointer to opened file
463 *----------------------------------------------------------------------------*/
464
465FILE *
466cs_base_open_properties_data_file(const char *base_name);
467
468#if defined(HAVE_DLOPEN)
469
470/*----------------------------------------------------------------------------*/
478/*----------------------------------------------------------------------------*/
479
480void*
481cs_base_dlopen(const char *filename);
482
483/*----------------------------------------------------------------------------*/
495/*----------------------------------------------------------------------------*/
496
497void*
498cs_base_dlopen_plugin(const char *name);
499
500/*----------------------------------------------------------------------------*/
506/*----------------------------------------------------------------------------*/
507
508int
509cs_base_dlopen_get_flags(void);
510
511/*----------------------------------------------------------------------------*/
517/*----------------------------------------------------------------------------*/
518
519void
520cs_base_dlopen_set_flags(int flags);
521
522/*----------------------------------------------------------------------------*/
534/*----------------------------------------------------------------------------*/
535
536void
537cs_base_dlclose(const char *filename,
538 void *handle);
539
540/*----------------------------------------------------------------------------*/
550/*----------------------------------------------------------------------------*/
551
552void *
553cs_base_get_dl_function_pointer(void *handle,
554 const char *name,
555 bool errors_are_fatal);
556
557#endif /* defined(HAVE_DLOPEN) */
558
559/*----------------------------------------------------------------------------*/
566/*----------------------------------------------------------------------------*/
567
568void
570 int lv_start);
571
572/*----------------------------------------------------------------------------*/
581/*----------------------------------------------------------------------------*/
582
583void
585
586/*----------------------------------------------------------------------------*/
593/*----------------------------------------------------------------------------*/
594
595void
597
598/*----------------------------------------------------------------------------*/
618/*----------------------------------------------------------------------------*/
619
620void
621cs_base_get_run_identity(char **run_id,
622 char **case_name,
623 char **study_name);
624
625/*----------------------------------------------------------------------------*/
626
628
629#endif /* __CS_BASE_H__ */
void cs_base_option_string_clean(char *s)
Definition: cs_base.cpp:2466
const char * cs_base_get_localedir(void)
Definition: cs_base.cpp:2499
const char * cs_base_get_pkgdatadir(void)
Definition: cs_base.cpp:2517
void cs_base_sigint_handler_set(cs_base_sigint_handler_t *const h)
Definition: cs_base.cpp:2449
void cs_base_time_summary(void)
Definition: cs_base.cpp:2067
static const char * cs_base_strtf(bool boolean)
Return a string "true" or "false" according to the boolean.
Definition: cs_base.h:112
void cs_exit(int status)
Definition: cs_base.cpp:1607
void cs_base_check_bool(bool *b)
Definition: cs_base.cpp:2553
void cs_base_error_init(bool signal_defaults)
Definition: cs_base.cpp:1649
void cs_base_mem_finalize(void)
Definition: cs_base.cpp:1779
void cs_base_signal_restore(void)
Definition: cs_base.cpp:2031
void cs_base_at_finalize(cs_base_atexit_t *func)
Register a function to be called at the finalization stage.
Definition: cs_base.cpp:2861
char * cs_base_get_app_name(int argc, const char *argv[])
Definition: cs_base.cpp:1150
void cs_base_logfile_head(int argc, char *argv[])
Definition: cs_base.cpp:1209
void() cs_base_sigint_handler_t(int signum)
Definition: cs_base.h:81
void cs_base_finalize_sequence(void)
Call sequence of finalization functions.
Definition: cs_base.cpp:2881
const char * cs_base_get_pkglibdir(void)
Definition: cs_base.cpp:2535
void cs_base_update_status(const char *format,...)
Update status file.
Definition: cs_base.cpp:2147
void cs_base_trace_set(bool trace)
Definition: cs_base.cpp:2224
void cs_base_backtrace_dump(FILE *f, int lv_start)
Dump a stack trace to a file.
Definition: cs_base.cpp:2795
void cs_base_warn(const char *file_name, int line_num)
Definition: cs_base.cpp:2414
void cs_base_mem_init(void)
Definition: cs_base.cpp:1698
#define cs_mpi_device_support
Definition: cs_base.h:93
const char * cs_base_bft_printf_name(void)
Definition: cs_base.cpp:2384
void cs_base_atexit_set(cs_base_atexit_t *const fct)
Definition: cs_base.cpp:2433
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:2914
bool cs_base_bft_printf_suppressed(void)
Definition: cs_base.cpp:2400
void cs_base_bft_printf_set(const char *log_name, bool rn_log_flag)
Definition: cs_base.cpp:2321
void cs_base_bft_printf_init(const char *log_name, bool rn_log_flag)
Definition: cs_base.cpp:2243
void() cs_base_atexit_t(void)
Definition: cs_base.h:77
FILE * cs_base_open_properties_data_file(const char *base_name)
Definition: cs_base.cpp:2583
#define BEGIN_C_DECLS
Definition: cs_defs.h:554
#define END_C_DECLS
Definition: cs_defs.h:555
@ h
Definition: cs_field_pointer.h:94