My Project
programmer's 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-2019 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 "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(__bg__)
54 #define _CS_ARCH_Blue_Gene
55 
56 #elif defined(__linux__) || defined(__linux) || defined(linux)
57 #define _CS_ARCH_Linux
58 
59 #elif defined(__sun__) || defined(__sun) || defined(sun)
60 #define _CS_ARCH_SunOS
61 
62 #endif
63 
64 /* On certain architectures such as IBM Blue Gene, some operations may
65  * be better optimized on memory-aligned data (if 0 here, no alignment
66  * is leveraged). This alignment is not exploited yet in Code_Saturne. */
67 
68 #if defined(__bgq__)
69 #define CS_MEM_ALIGN 32
70 #else
71 #define CS_MEM_ALIGN 0
72 #endif
73 
74 #define CS_BASE_STRING_LEN 80
75 
76 /*----------------------------------------------------------------------------*/
77 
79 
80 /*============================================================================
81  * Type definitions
82  *============================================================================*/
83 
84 /* Function pointers for extra cleanup operations to be called when
85  entering cs_exit() or bft_error() */
86 
87 typedef void (cs_base_atexit_t) (void);
88 
89 /*=============================================================================
90  * Global variable definitions
91  *============================================================================*/
92 
93 /*=============================================================================
94  * Public function prototypes
95  *============================================================================*/
96 
97 /*----------------------------------------------------------------------------*/
105 /*----------------------------------------------------------------------------*/
106 
107 static inline const char *
108 cs_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 
132 char *
133 cs_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 
144 void
145 cs_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 
162 void
163 cs_base_mpi_init(int *argc,
164  char **argv[]);
165 
166 #endif /* defined(HAVE_MPI) */
167 
168 /*----------------------------------------------------------------------------
169  * Exit, with handling for both normal and error cases.
170  *
171  * Finalize MPI if necessary.
172  *
173  * parameters:
174  * status <-- value to be returned to the parent:
175  * EXIT_SUCCESS / 0 for the normal case,
176  * EXIT_FAILURE or other nonzero code for error cases.
177  *----------------------------------------------------------------------------*/
178 
179 void
180 cs_exit(int status);
181 
182 /*----------------------------------------------------------------------------
183  * Initialize error and signal handlers.
184  *
185  * parameters:
186  * signal_defaults <-- leave default signal handlers in place if true.
187  *----------------------------------------------------------------------------*/
188 
189 void
190 cs_base_error_init(bool signal_defaults);
191 
192 /*----------------------------------------------------------------------------
193  * Initialize management of memory allocated through BFT.
194  *----------------------------------------------------------------------------*/
195 
196 void
197 cs_base_mem_init(void);
198 
199 /*----------------------------------------------------------------------------
200  * Finalize management of memory allocated through BFT.
201  *
202  * A summary of the consumed memory is given.
203  *----------------------------------------------------------------------------*/
204 
205 void
207 
208 /*----------------------------------------------------------------------------
209  * Print summary of running time, including CPU and elapsed times.
210  *----------------------------------------------------------------------------*/
211 
212 void
214 
215 /*----------------------------------------------------------------------------*/
224 /*----------------------------------------------------------------------------*/
225 
226 void
227 cs_base_update_status(const char *format,
228  ...);
229 
230 /*----------------------------------------------------------------------------
231  * Set tracing of progress on or off.
232  *
233  * parameters:
234  * trace <-- trace progress to stdout
235  *----------------------------------------------------------------------------*/
236 
237 void
238 cs_base_trace_set(bool trace);
239 
240 
241 /*----------------------------------------------------------------------------
242  * Set output file name and suppression flag for bft_printf().
243  *
244  * This allows redirecting or suppressing logging for different ranks.
245  *
246  * parameters:
247  * log_name <-- base file name for log
248  * rn_log_flag <-- redirection for ranks > 0 log:
249  * rn_log_flag <-- redirection for ranks > 0 log:
250  * false: to "/dev/null" (suppressed)
251  * true: to <log_name>_r*.log" file;
252  *----------------------------------------------------------------------------*/
253 
254 void
255 cs_base_bft_printf_init(const char *log_name,
256  bool rn_log_flag);
257 
258 /*----------------------------------------------------------------------------
259  * Replace default bft_printf() mechanism with internal mechanism.
260  *
261  * This allows redirecting or suppressing logging for different ranks.
262  *
263  * parameters:
264  * log_name <-- base file name for log
265  *----------------------------------------------------------------------------*/
266 
267 void
268 cs_base_bft_printf_set(const char *log_name,
269  bool rn_log_flag);
270 
271 /*----------------------------------------------------------------------------
272  * Return name of default log file.
273  *
274  * cs_base_bft_printf_set or cs_base_c_bft_printf_set() must have
275  * been called before this.
276  *
277  * returns:
278  * name of default log file
279  *----------------------------------------------------------------------------*/
280 
281 const char *
283 
284 /*----------------------------------------------------------------------------
285  * Return flag indicating if the default log file output is suppressed.
286  *
287  * cs_base_bft_printf_set or cs_base_c_bft_printf_set() must have
288  * been called before this.
289  *
290  * returns:
291  * output suppression flag
292  *----------------------------------------------------------------------------*/
293 
294 bool
296 
297 /*----------------------------------------------------------------------------
298  * Print a warning message header.
299  *
300  * parameters:
301  * file_name <-- name of source file
302  * line_nume <-- line number in source file
303  *----------------------------------------------------------------------------*/
304 
305 void
306 cs_base_warn(const char *file_name,
307  int line_num);
308 
309 /*----------------------------------------------------------------------------
310  * Define a function to be called when entering cs_exit() or bft_error().
311  *
312  * Compared to the C atexit(), only one function may be called (latest
313  * setting wins), but the function is called slighty before exit,
314  * so it is well adapted to cleanup such as flushing of non-C API logging.
315  *
316  * parameters:
317  * fct <-- pointer tu function to be called
318  *----------------------------------------------------------------------------*/
319 
320 void
322 
323 /*----------------------------------------------------------------------------
324  * Convert a character string from the Fortran API to the C API.
325  *
326  * Eventual leading and trailing blanks are removed.
327  *
328  * parameters:
329  * f_str <-- Fortran string
330  * f_len <-- Fortran string length
331  *
332  * returns:
333  * pointer to C string
334  *----------------------------------------------------------------------------*/
335 
336 char *
337 cs_base_string_f_to_c_create(const char *f_str,
338  int f_len);
339 
340 /*----------------------------------------------------------------------------
341  * Free a string converted from the Fortran API to the C API.
342  *
343  * parameters:
344  * str <-> pointer to C string
345  *----------------------------------------------------------------------------*/
346 
347 void
348 cs_base_string_f_to_c_free(char **c_str);
349 
350 /*----------------------------------------------------------------------------
351  * Clean a string representing options.
352  *
353  * Characters are converted to lowercase, leading and trailing whitespace
354  * is removed, and multiple whitespaces or tabs are replaced by single
355  * spaces.
356  *
357  * parameters:
358  * s <-> string to be cleaned
359  *----------------------------------------------------------------------------*/
360 
361 void
363 
364 /*----------------------------------------------------------------------------
365  * Return a string providing locale path information.
366  *
367  * This is normally the path determined upon configuration, but may be
368  * adapted for movable installs using the CS_ROOT_DIR environment variable.
369  *
370  * returns:
371  * locale path
372  *----------------------------------------------------------------------------*/
373 
374 const char *
376 
377 /*----------------------------------------------------------------------------
378  * Return a string providing package data path information.
379  *
380  * This is normally the path determined upon configuration, but may be
381  * adapted for movable installs using the CS_ROOT_DIR environment variable.
382  *
383  * returns:
384  * package data path
385  *----------------------------------------------------------------------------*/
386 
387 const char *
389 
390 /*----------------------------------------------------------------------------
391  * Return a string providing loadable library path information.
392  *
393  * This is normally the path determined upon configuration, but may be
394  * adapted for movable installs using the CS_ROOT_DIR environment variable.
395  *
396  * returns:
397  * package loadable library (plugin) path
398  *----------------------------------------------------------------------------*/
399 
400 const char *
402 
403 /*----------------------------------------------------------------------------
404  * Ensure bool argument has value 0 or 1.
405  *
406  * This allows working around issues with Intel compiler C bindings,
407  * which seem to pass incorrect values in some cases.
408  *
409  * parameters:
410  * b <-> pointer to bool
411  *----------------------------------------------------------------------------*/
412 
413 void
414 cs_base_check_bool(bool *b);
415 
416 /*----------------------------------------------------------------------------
417  * Open a data file in read mode.
418  *
419  * If a file of the given name in the working directory is found, it
420  * will be opened. Otherwise, it will be searched for in the "data/thch"
421  * subdirectory of pkgdatadir.
422  *
423  * parameters:
424  * base_name <-- base file name
425  *
426  * returns:
427  * pointer to opened file
428  *----------------------------------------------------------------------------*/
429 
430 FILE *
431 cs_base_open_properties_data_file(const char *base_name);
432 
433 #if defined(HAVE_DLOPEN)
434 
435 /*----------------------------------------------------------------------------*/
443 /*----------------------------------------------------------------------------*/
444 
445 void*
446 cs_base_dlopen(const char *filename);
447 
448 /*----------------------------------------------------------------------------*/
460 /*----------------------------------------------------------------------------*/
461 
462 void*
463 cs_base_dlopen_plugin(const char *name);
464 
465 /*----------------------------------------------------------------------------*/
471 /*----------------------------------------------------------------------------*/
472 
473 int
475 
476 /*----------------------------------------------------------------------------*/
482 /*----------------------------------------------------------------------------*/
483 
484 void
485 cs_base_dlopen_set_flags(int flags);
486 
487 /*----------------------------------------------------------------------------*/
499 /*----------------------------------------------------------------------------*/
500 
501 void
502 cs_base_dlclose(const char *filename,
503  void *handle);
504 
505 /*----------------------------------------------------------------------------*/
515 /*----------------------------------------------------------------------------*/
516 
517 void *
519  const char *name,
520  bool errors_are_fatal);
521 
522 
523 #endif /* defined(HAVE_DLOPEN) */
524 
525 /*----------------------------------------------------------------------------*/
532 /*----------------------------------------------------------------------------*/
533 
534 void
535 cs_base_backtrace_dump(FILE *f,
536  int lv_start);
537 
538 /*----------------------------------------------------------------------------*/
539 
541 
542 #endif /* __CS_BASE_H__ */
cs_base_open_properties_data_file
FILE * cs_base_open_properties_data_file(const char *base_name)
Definition: cs_base.c:2269
cs_base_strtf
static const char * cs_base_strtf(bool boolean)
Return a string "true" or "false" according to the boolean.
Definition: cs_base.h:108
cs_defs.h
cs_base_string_f_to_c_create
char * cs_base_string_f_to_c_create(const char *f_str, int f_len)
Definition: cs_base.c:2073
cs_base_dlopen
void * cs_base_dlopen(const char *filename)
Load a dynamic library.
Definition: cs_base.c:2312
cs_base_error_init
void cs_base_error_init(bool signal_defaults)
Definition: cs_base.c:1443
cs_base_get_pkgdatadir
const char * cs_base_get_pkgdatadir(void)
Definition: cs_base.c:2203
cs_base_get_localedir
const char * cs_base_get_localedir(void)
Definition: cs_base.c:2188
cs_base_time_summary
void cs_base_time_summary(void)
Definition: cs_base.c:1688
cs_base_atexit_set
void cs_base_atexit_set(cs_base_atexit_t *const fct)
Definition: cs_base.c:2054
cs_base_bft_printf_name
const char * cs_base_bft_printf_name(void)
Definition: cs_base.c:2005
cs_fuel_incl::b
double precision, save b
Definition: cs_fuel_incl.f90:146
cs_base_trace_set
void cs_base_trace_set(bool trace)
Definition: cs_base.c:1845
cs_base_mem_finalize
void cs_base_mem_finalize(void)
Definition: cs_base.c:1558
END_C_DECLS
#define END_C_DECLS
Definition: cs_defs.h:468
BEGIN_C_DECLS
#define BEGIN_C_DECLS
Definition: cs_defs.h:467
cs_base_mpi_init
void cs_base_mpi_init(int *argc, char **argv[])
Definition: cs_base.c:1289
cs_base_dlopen_get_flags
int cs_base_dlopen_get_flags(void)
Get flags for dlopen.
Definition: cs_base.c:2383
cs_base_get_app_name
char * cs_base_get_app_name(int argc, const char *argv[])
Definition: cs_base.c:1040
cs_exit
void cs_exit(int status)
Definition: cs_base.c:1401
cs_base_get_pkglibdir
const char * cs_base_get_pkglibdir(void)
Definition: cs_base.c:2221
cs_base_warn
void cs_base_warn(const char *file_name, int line_num)
Definition: cs_base.c:2035
cs_base_get_dl_function_pointer
void * cs_base_get_dl_function_pointer(void *handle, const char *name, bool errors_are_fatal)
Get a shared library function pointer.
Definition: cs_base.c:2450
cs_base_check_bool
void cs_base_check_bool(bool *b)
Definition: cs_base.c:2239
cs_base_option_string_clean
void cs_base_option_string_clean(char *s)
Definition: cs_base.c:2158
cs_base_dlopen_set_flags
void cs_base_dlopen_set_flags(int flags)
Set flags for dlopen.
Definition: cs_base.c:2397
cs_base_mem_init
void cs_base_mem_init(void)
Definition: cs_base.c:1489
cs_base_bft_printf_set
void cs_base_bft_printf_set(const char *log_name, bool rn_log_flag)
Definition: cs_base.c:1942
cs_base_bft_printf_init
void cs_base_bft_printf_init(const char *log_name, bool rn_log_flag)
Definition: cs_base.c:1864
cs_base_dlclose
void cs_base_dlclose(const char *filename, void *handle)
Unload a dynamic library.
Definition: cs_base.c:2417
cs_base_bft_printf_suppressed
bool cs_base_bft_printf_suppressed(void)
Definition: cs_base.c:2021
cs_base_logfile_head
void cs_base_logfile_head(int argc, char *argv[])
Definition: cs_base.c:1099
cs_base_string_f_to_c_free
void cs_base_string_f_to_c_free(char **c_str)
Definition: cs_base.c:2130
cs_base_backtrace_dump
void cs_base_backtrace_dump(FILE *f, int lv_start)
Dump a stack trace to a file.
Definition: cs_base.c:2481
cs_base_dlopen_plugin
void * cs_base_dlopen_plugin(const char *name)
Load a plugin's dynamic library.
Definition: cs_base.c:2352
cs_base_atexit_t
void() cs_base_atexit_t(void)
Definition: cs_base.h:87
cs_base_update_status
void cs_base_update_status(const char *format,...)
Update status file.
Definition: cs_base.c:1768