PLE
Parallel Location and Exchange
 All Files Functions Pages
ple_defs.h
1 #ifndef __PLE_DEFS_H__
2 #define __PLE_DEFS_H__
3 
4 /*============================================================================
5  * Definitions, global variables, and base functions
6  *============================================================================*/
7 
8 /*
9  This file is part of the "Parallel Location and Exchange" library,
10  intended to provide mesh or particle-based code coupling services.
11 
12  Copyright (C) 2005-2016 EDF
13 
14  This library is free software; you can redistribute it and/or
15  modify it under the terms of the GNU Lesser General Public
16  License as published by the Free Software Foundation; either
17  version 2.1 of the License, or (at your option) any later version.
18 
19  This library is distributed in the hope that it will be useful,
20  but WITHOUT ANY WARRANTY; without even the implied warranty of
21  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22  Lesser General Public License for more details.
23 
24  You should have received a copy of the GNU Lesser General Public
25  License along with this library; if not, write to the Free Software
26  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
27 */
28 
29 /*----------------------------------------------------------------------------*/
30 
31 #include "ple_config.h"
32 
33 /*----------------------------------------------------------------------------*/
34 
35 #include <stdarg.h>
36 
37 #ifdef __cplusplus
38 extern "C" {
39 #if 0
40 } /* Fake brace to force back Emacs auto-indentation back to column 0 */
41 #endif
42 #endif /* __cplusplus */
43 
44 /*=============================================================================
45  * Macro definitions
46  *============================================================================*/
47 
48 /* Absolute, minimum, and maximum values */
49 
50 #define PLE_ABS(a) ((a) < 0 ? -(a) : (a)) /* Absolute value of a */
51 #define PLE_MIN(a,b) ((a) > (b) ? (b) : (a)) /* Minimum of a et b */
52 #define PLE_MAX(a,b) ((a) < (b) ? (b) : (a)) /* Maximum of a et b */
53 
54 /*
55  * Allocate memory for _ni items of type _type.
56  *
57  * This macro calls ple_mem_malloc(), automatically setting the
58  * allocated variable name and source file name and line arguments.
59  *
60  * parameters:
61  * _ptr --> pointer to allocated memory.
62  * _ni <-- number of items.
63  * _type <-- element type.
64  */
65 
66 #define PLE_MALLOC(_ptr, _ni, _type) \
67 _ptr = (_type *) ple_mem_malloc(_ni, sizeof(_type), \
68  #_ptr, __FILE__, __LINE__)
69 
70 /*
71  * Reallocate memory for _ni items of type _type.
72  *
73  * This macro calls ple_mem_realloc(), automatically setting the
74  * allocated variable name and source file name and line arguments.
75  *
76  * parameters:
77  * _ptr <-> pointer to allocated memory.
78  * _ni <-- number of items.
79  * _type <-- element type.
80  */
81 
82 #define PLE_REALLOC(_ptr, _ni, _type) \
83 _ptr = (_type *) ple_mem_realloc(_ptr, _ni, sizeof(_type), \
84  #_ptr, __FILE__, __LINE__)
85 
86 /*
87  * Free allocated memory.
88  *
89  * This macro calls ple_mem_free(), automatically setting the
90  * allocated variable name and source file name and line arguments.
91  *
92  * The freed pointer is set to NULL to avoid accidental reuse.
93  *
94  * parameters:
95  * _ptr <-> pointer to allocated memory.
96  */
97 
98 #ifdef __cplusplus /* avoid casting from void for C++ */
99 
100 #define PLE_FREE(_ptr) \
101 ple_mem_free(_ptr, #_ptr, __FILE__, __LINE__), _ptr = NULL
102 
103 #else
104 
105 #define PLE_FREE(_ptr) \
106 _ptr = ple_mem_free(_ptr, #_ptr, __FILE__, __LINE__)
107 
108 #endif /* __cplusplus */
109 
110 /*============================================================================
111  * Type definitions
112  *============================================================================*/
113 
114 /*----------------------------------------------------------------------------
115  * General C types such as size_t which should be known
116  *----------------------------------------------------------------------------*/
117 
118 /*
119  * Obtain definitions such as that of size_t through stddef.h (C99 standard)
120  * if available (preferred method), or through stdlib.h (which defines
121  * malloc() and family and so must define size_t some way) otherwise.
122  * This must be done in ple_defs.h in a way independent of the private
123  * configuration files, as size_t is used in many public FVM headers.
124  */
125 
126 #if defined(__STDC_VERSION__)
127 # if (__STDC_VERSION__ >= 199901L)
128 # include <stddef.h>
129 # else
130 # include <stdlib.h>
131 # endif
132 #else
133 # include <stdlib.h>
134 #endif
135 
136 /*----------------------------------------------------------------------------
137  * Variable value type.
138  *----------------------------------------------------------------------------*/
139 
140 typedef enum {
141 
142  PLE_DATATYPE_NULL, /* empty datatype */
143  PLE_CHAR, /* character values */
144  PLE_FLOAT, /* 4-byte floating point values */
145  PLE_DOUBLE, /* 8-byte floating point values */
146  PLE_INT32, /* 4-byte signed integer values */
147  PLE_INT64, /* 8-byte signed integer values */
148  PLE_UINT32, /* 4-byte unsigned integer values */
149  PLE_UINT64 /* 8-byte unsigned integer values */
150 
151 } ple_datatype_t;
152 
153 /*----------------------------------------------------------------------------
154  * Basic types used by PLE.
155  * They may be modified here to better map to a given library, with the
156  * following constraints:
157  * - ple_lnum_t must be signed
158  *----------------------------------------------------------------------------*/
159 
160 /* Other types */
161 
162 typedef int ple_lnum_t; /* Local integer index or number */
163 typedef double ple_coord_t; /* Real number (coordinate value) */
164 
165 /* Mappings to PLE datatypes */
166 /*---------------------------*/
167 
168 #define PLE_COORD PLE_DOUBLE
169 
170 #if (PLE_SIZEOF_INT == 4)
171  #define PLE_LNUM PLE_INT32
172 #elif (PLE_SIZEOF_INT == 8)
173  #define PLE_LNUM PLE_INT64
174 #else
175  #error
176 #endif
177 
178 /*----------------------------------------------------------------------------
179  * MPI datatypes.
180  *----------------------------------------------------------------------------*/
181 
182 #if defined(PLE_HAVE_MPI)
183 
184 #define PLE_MPI_TAG (int)('P'+'L'+'E') /* MPI tag for PLE operations */
185 
186 #define PLE_MPI_LNUM MPI_INT /* MPI type for ple_lnum_t type */
187 #define PLE_MPI_COORD MPI_DOUBLE /* MPI type for ple_coord_t type */
188 
189 #endif
190 
191 /*----------------------------------------------------------------------------
192  * Macro used to silence "unused argument" warnings.
193  *
194  * This is useful when a function must match a given function pointer
195  * type, but does not use all possible arguments.
196  *----------------------------------------------------------------------------*/
197 
198 #define PLE_UNUSED(x) (void)(x)
199 
200 /*----------------------------------------------------------------------------
201  * Macros for compilation with a C++ compiler
202  *----------------------------------------------------------------------------*/
203 
204 #undef PLE_BEGIN_C_DECLS
205 #undef PLE_END_C_DECLS
206 
207 #if defined(__cplusplus)
208 # define PLE_BEGIN_C_DECLS extern "C" {
209 # define PLE_END_C_DECLS }
210 #else
211 # define PLE_BEGIN_C_DECLS
212 # define PLE_END_C_DECLS
213 #endif
214 
215 /*----------------------------------------------------------------------------
216  * Macros for scoping of examples
217  *----------------------------------------------------------------------------*/
218 
219 #undef PLE_BEGIN_EXAMPLE_SCOPE
220 #undef PLE_END_EXAMPLE_SCOPE
221 
222 #define PLE_BEGIN_EXAMPLE_SCOPE {
223 #define PLE_END_EXAMPLE_SCOPE }
224 
225 /*----------------------------------------------------------------------------
226  * Function pointer types
227  *----------------------------------------------------------------------------*/
228 
229 typedef int
230 (ple_printf_t) (const char *const format,
231  va_list arg_ptr);
232 
233 typedef void
234 (ple_error_handler_t) (const char *file_name,
235  const int line_num,
236  const int sys_error_code,
237  const char *format,
238  va_list arg_ptr);
239 
240 typedef void *
241 (ple_mem_malloc_t)(size_t ni,
242  size_t size,
243  const char *var_name,
244  const char *file_name,
245  int line_num);
246 
247 typedef void *
248 (ple_mem_realloc_t)(void *ptr,
249  size_t ni,
250  size_t size,
251  const char *var_name,
252  const char *file_name,
253  int line_num);
254 
255 typedef void *
256 (ple_mem_free_t)(void *ptr,
257  const char *var_name,
258  const char *file_name,
259  int line_num);
260 
261 /*=============================================================================
262  * Static global variables
263  *============================================================================*/
264 
265 /* Sizes and names associated with datatypes */
266 
267 extern const size_t ple_datatype_size[];
268 extern const char *ple_datatype_name[];
269 
270 /*============================================================================
271  * Public function prototypes
272  *============================================================================*/
273 
287 int
288 ple_printf(const char *const format,
289  ...);
290 
291 /* Returns function associated with the ple_printf() function.
292  *
293  * returns:
294  * pointer to the vprintf() or replacement function.
295  */
296 
297 ple_printf_t *
299 
300 /*
301  * Associates a vprintf() type function with the ple_printf() function.
302  *
303  * parameters:
304  * f <-- pointer to a vprintf() type function.
305  */
306 
307 void
308 ple_printf_function_set(ple_printf_t *f);
309 
310 /*
311  * Calls the error handler (set by ple_error_handler_set() or default).
312  *
313  * With the default error handler, an error message is output to stderr,
314  * and the current process exits with an EXIT_FAILURE code.
315  *
316  * parameters:
317  * file_name <-- name of source file from which error handler called.
318  * line_num <-- line of source file from which error handler called.
319  * sys_error_code <-- error code if error in system or libc call,
320  * 0 otherwise.
321  * format <-- format string, as printf() and family.
322  * ... <-- variable arguments based on format string.
323  */
324 
325 void
326 ple_error(const char *file_name,
327  const int line_num,
328  const int sys_error_code,
329  const char *format,
330  ...);
331 
332 /*
333  * Returns the error handler associated with the ple_error() function.
334  *
335  * returns:
336  * pointer to the error handler function.
337  */
338 
339 ple_error_handler_t *
341 
342 /*
343  * Associates an error handler with the ple_error() function.
344  *
345  * parameters:
346  * handler <-- pointer to the error handler function.
347  */
348 
349 void
350 ple_error_handler_set(ple_error_handler_t *handler);
351 
352 /*
353  * Allocate memory for ni items of size bytes.
354  *
355  * This function calls malloc(), but adds tracing capabilities, and
356  * automatically calls the ple_error() errorhandler if it fails to
357  * allocate the required memory.
358  *
359  * parameters:
360  * ni <-- number of items.
361  * size <-- element size.
362  * var_name <-- allocated variable name string.
363  * file_name <-- name of calling source file.
364  * line_num <-- line number in calling source file.
365  *
366  * returns:
367  * pointer to allocated memory.
368  */
369 
370 void *
371 ple_mem_malloc(size_t ni,
372  size_t size,
373  const char *var_name,
374  const char *file_name,
375  int line_num);
376 
377 /*
378  * Reallocate memory for ni items of size bytes.
379  *
380  * This function calls realloc(), but adds tracing capabilities, and
381  * automatically calls the ple_error() errorhandler if it fails to
382  * allocate the required memory.
383  *
384  * parameters:
385  * ptr <-> pointer to previous memory location
386  * (if NULL, ple_alloc() called).
387  * ni <-- number of items.
388  * size <-- element size.
389  * var_name <-- allocated variable name string.
390  * file_name <-- name of calling source file.
391  * line_num -> line number in calling source file
392  *
393  * returns:
394  * pointer to allocated memory.
395  */
396 
397 void *
398 ple_mem_realloc(void *ptr,
399  size_t ni,
400  size_t size,
401  const char *var_name,
402  const char *file_name,
403  int line_num);
404 
405 /*
406  * Free allocated memory.
407  *
408  * This function calls free(), but adds tracing capabilities, and
409  * automatically calls the ple_error() errorhandler if it fails to
410  * free the corresponding memory. In case of a NULL pointer argument,
411  * the function simply returns.
412  *
413  * parameters:
414  * ptr <-> pointer to previous memory location
415  * (if NULL, ple_alloc() called).
416  * var_name <-- allocated variable name string.
417  * file_name <-- name of calling source file.
418  * line_num <-- line number in calling source file.
419  *
420  * returns:
421  * NULL pointer.
422  */
423 
424 void *
425 ple_mem_free(void *ptr,
426  const char *var_name,
427  const char *file_name,
428  int line_num);
429 
430 /* Return the function pointers associated with PLE's memory management.
431  *
432  * All arguments are optional.
433  *
434  * parameters:
435  * malloc_func <-- pointer to ple_mem_malloc function pointer (or NULL).
436  * realloc_func <-- pointer to ple_mem_realloc function pointer (or NULL).
437  * free_func <-- pointer to ple_mem_free function pointer (or NULL).
438  */
439 
440 void
441 ple_mem_functions_get(ple_mem_malloc_t **malloc_func,
442  ple_mem_realloc_t **realloc_func,
443  ple_mem_free_t **free_func);
444 
445 /* Associate functions to modifiy PLE's memory management.
446  *
447  * All arguments are optional, so the previously set functions pointers will
448  * not be modified if an argument value is NULL.
449  *
450  * parameters:
451  * malloc_func <-- ple_mem_malloc function pointer (or NULL).
452  * realloc_func <-- ple_mem_realloc function pointer (or NULL).
453  * free_func <-- ple_mem_free function pointer (or NULL).
454  */
455 
456 void
457 ple_mem_functions_set(ple_mem_malloc_t *malloc_func,
458  ple_mem_realloc_t *realloc_func,
459  ple_mem_free_t *free_func);
460 
461 /*
462  * Return Wall clock time
463  *
464  * returns:
465  * elapsed time from first call of a function of the ple_timer_...()
466  * series, or -1 if unable to compute.
467  */
468 
469 double
470 ple_timer_wtime(void);
471 
472 /*
473  * Return CPU time.
474  *
475  * Note that in the rare case that only the minimal C library clock()
476  * method is available (see ple_timer_cpu_time_method()), at least one of
477  * the ple_timer_...() functions (possibly this one) must be called
478  * upon program start for this function to be used. In addition,
479  * in this case, time may "loop" back to 0 every multiple of
480  * 2^size_t / CLOCKS_PER_SEC seconds.
481  *
482  * returns:
483  * current CPU time usage, or -1 if unable to compute.
484  */
485 
486 double
487 ple_timer_cpu_time(void);
488 
489 /*----------------------------------------------------------------------------*/
490 
491 #ifdef __cplusplus
492 }
493 #endif /* __cplusplus */
494 
495 #endif /* __PLE_DEFS_H__ */
void ple_error_handler_set(ple_error_handler_t *handler)
Associates an error handler with the ple_error() function.
Definition: ple_defs.c:457
void ple_error(const char *const file_name, const int line_num, const int sys_error_code, const char *const format,...)
Calls the error handler (set by ple_error_handler_set() or default).
Definition: ple_defs.c:423
double ple_timer_cpu_time(void)
Return CPU time.
Definition: ple_defs.c:673
ple_error_handler_t * ple_error_handler_get(void)
Returns the error handler associated with the ple_error() function.
Definition: ple_defs.c:445
int ple_printf(const char *const format,...)
Replacement for printf() with modifiable behavior.
Definition: ple_defs.c:367
void * ple_mem_realloc(void *ptr, size_t ni, size_t size, const char *var_name, const char *file_name, int line_num)
Reallocate memory for ni elements of size bytes.
Definition: ple_defs.c:507
ple_printf_t * ple_printf_function_get(void)
Returns function associated with the ple_printf() function.
Definition: ple_defs.c:389
void ple_mem_functions_set(ple_mem_malloc_t *malloc_func, ple_mem_realloc_t *realloc_func, ple_mem_free_t *free_func)
Associate functions to modifiy PLE's memory management.
Definition: ple_defs.c:583
void * ple_mem_malloc(size_t ni, size_t size, const char *var_name, const char *file_name, int line_num)
Allocate memory for ni elements of size bytes.
Definition: ple_defs.c:479
void ple_mem_functions_get(ple_mem_malloc_t **malloc_func, ple_mem_realloc_t **realloc_func, ple_mem_free_t **free_func)
Return the function pointers associated with PLE's memory management.
Definition: ple_defs.c:557
void ple_printf_function_set(ple_printf_t *const fct)
Associates a vprintf() type function with the ple_printf() function.
Definition: ple_defs.c:401
void * ple_mem_free(void *ptr, const char *var_name, const char *file_name, int line_num)
Free allocated memory.
Definition: ple_defs.c:535
double ple_timer_wtime(void)
Return Wall clock time.
Definition: ple_defs.c:605