libsc  2.8.3.341-30a7-dirty
The SC library provides support for parallel scientific applications.
sc.h
Go to the documentation of this file.
1 /*
2  This file is part of the SC Library.
3  The SC Library provides support for parallel scientific applications.
4 
5  Copyright (C) 2010 The University of Texas System
6  Additional copyright (C) 2011 individual authors
7 
8  The SC Library is free software; you can redistribute it and/or
9  modify it under the terms of the GNU Lesser General Public
10  License as published by the Free Software Foundation; either
11  version 2.1 of the License, or (at your option) any later version.
12 
13  The SC Library is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  Lesser General Public License for more details.
17 
18  You should have received a copy of the GNU Lesser General Public
19  License along with the SC Library; if not, write to the Free Software
20  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21  02110-1301, USA.
22 */
23 
34 #ifndef SC_H
35 #define SC_H
36 
37 /* we set the GNU feature test macro before including anything */
38 #ifndef _GNU_SOURCE
39 #define _GNU_SOURCE
40 #endif
41 
42 /* include the libsc config header first */
43 #include <sc_config.h>
44 #ifndef _sc_const
46 #define _sc_const const
47 #endif
48 #ifndef _sc_restrict
50 #define _sc_restrict restrict
51 #endif
52 
54 #define SC_CALC_VERSION(major,minor,patchlevel) \
55  (((major) * 1000 + (minor)) * 1000 + (patchlevel))
56 #ifdef __GNUC__
57 #define SC_GCC_VERSION \
58  SC_CALC_VERSION(__GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__)
59 #else
61 #define SC_GCC_VERSION \
62  SC_CALC_VERSION (0, 0, 0)
63 #endif
64 
65 /* use this feature macro, be minimally invasive */
66 #ifdef SC_ENABLE_MEMALIGN
67 /* we disable the system-provided functions for the time being */
68 #ifdef SC_HAVE_ANY_MEMALIGN
69 #undef SC_HAVE_ANY_MEMALIGN
70 #endif
71 /* if system-provided functions are needed, give them the prototype */
72 #ifdef SC_HAVE_ANY_MEMALIGN
73 #ifndef SC_HAVE_POSIX_MEMALIGN
74 #ifdef SC_HAVE_ALIGNED_ALLOC
75 #define _ISOC11_SOURCE
76 #endif
77 #endif
78 #endif
79 /* done with memalign macros */
80 #endif
81 
82 /* disable global counters that are not thread-safe (say when using TBB) */
83 #ifndef SC_ENABLE_USE_COUNTERS
84 #define SC_NOCOUNT_MALLOC
85 #define SC_NOCOUNT_REFCOUNT
86 #define SC_NOCOUNT_LOGINDENT
87 #endif
88 
89 /* use this in case mpi.h includes stdint.h */
90 
91 #ifndef __STDC_LIMIT_MACROS
93 #define __STDC_LIMIT_MACROS
94 #endif
95 #ifndef __STDC_CONSTANT_MACROS
97 #define __STDC_CONSTANT_MACROS
98 #endif
99 
100 /* include MPI before stdio.h */
101 
102 #ifdef SC_ENABLE_MPI
103 #include <mpi.h>
104 #else
105 #ifdef MPI_SUCCESS
106 #error "mpi.h is included. Use --enable-mpi."
107 #endif
108 #endif
109 
110 /* include system headers */
111 
112 #define _USE_MATH_DEFINES
113 #include <math.h>
114 #ifndef M_E
115 #define M_E 2.71828182845904523536
116 #endif
117 #ifndef M_LOG2E
118 #define M_LOG2E 1.44269504088896340736
119 #endif
120 #ifndef M_LOG10E
121 #define M_LOG10E 0.434294481903251827651
122 #endif
123 #ifndef M_LN2
124 #define M_LN2 0.693147180559945309417
125 #endif
126 #ifndef M_LN10
127 #define M_LN10 2.30258509299404568402
128 #endif
129 #ifndef M_PI
130 #define M_PI 3.14159265358979323846
131 #endif
132 #ifndef M_PI_2
133 #define M_PI_2 1.57079632679489661923
134 #endif
135 #ifndef M_PI_4
136 #define M_PI_4 0.785398163397448309616
137 #endif
138 #ifndef M_1_PI
139 #define M_1_PI 0.318309886183790671538
140 #endif
141 #ifndef M_2_PI
142 #define M_2_PI 0.636619772367581343076
143 #endif
144 #ifndef M_2_SQRTPI
145 #define M_2_SQRTPI 1.12837916709551257390
146 #endif
147 #ifndef M_SQRT2
148 #define M_SQRT2 1.41421356237309504880
149 #endif
150 #ifndef M_SQRT1_2
151 #define M_SQRT1_2 0.707106781186547524401
152 #endif
153 #include <ctype.h>
154 #include <float.h>
155 #if defined SC_HAVE_LIBGEN_H && !defined _MSC_VER
156 #include <libgen.h>
157 #endif
158 #include <limits.h>
159 #include <stdarg.h>
160 #include <stddef.h>
161 #ifdef SC_HAVE_STDINT_H
162 #include <stdint.h>
163 #endif
164 #include <stdio.h>
165 #ifdef SC_HAVE_STDLIB_H
166 #include <stdlib.h>
167 #endif
168 #ifdef SC_HAVE_STRING_H
169 #include <string.h>
170 #endif
171 #ifdef SC_HAVE_SYS_TIME_H
172 #include <sys/time.h>
173 #endif
174 #ifdef SC_HAVE_UNISTD_H
175 #include <unistd.h>
176 #elif defined _WIN32
177 #include <BaseTsd.h>
178 typedef SSIZE_T ssize_t;
179 #endif
180 
181 /* definitions to allow user code to query the sc library */
183 #define SC_INIT_COMM_CLEAN
184 
185 /* provide extern C defines */
186 
187 /* The hacks below enable semicolons after the SC_EXTERN_C_ macros
188  * and also take care of the different semantics of () / (...) */
189 #ifdef __cplusplus
190 #define SC_EXTERN_C_BEGIN extern "C" { void sc_extern_c_hack_1 (void)
191 #define SC_EXTERN_C_END } void sc_extern_c_hack_2 (void)
192 #define SC_NOARGS ...
193 #else
194 #define SC_EXTERN_C_BEGIN void sc_extern_c_hack_3 (void)
195 #define SC_EXTERN_C_END void sc_extern_c_hack_4 (void)
197 #define SC_NOARGS
198 #endif
199 
200 /* this header is always included */
201 #include <sc_mpi.h>
202 
203 SC_EXTERN_C_BEGIN;
204 
205 /* extern variables */
206 
208 extern const int sc_log2_lookup_table[256];
209 
215 extern int sc_package_id;
216 
219 extern FILE *sc_trace_file;
220 
222 extern int sc_trace_prio;
223 
225 #define SC_EPS 2.220446049250313e-16
226 
228 #define SC_1000_EPS (1000. * 2.220446049250313e-16)
229 
230 /* check macros, always enabled */
231 
233 #define SC_NOOP() ((void) (0))
234 #define SC_ABORT(s) \
235  sc_abort_verbose (__FILE__, __LINE__, (s))
236 #define SC_ABORT_NOT_REACHED() SC_ABORT ("Unreachable code")
237 #define SC_CHECK_ABORT(q,s) \
238  ((q) ? (void) 0 : SC_ABORT (s))
239 #define SC_CHECK_MPI(r) SC_CHECK_ABORT ((r) == sc_MPI_SUCCESS, "MPI error")
240 #define SC_CHECK_ZLIB(r) SC_CHECK_ABORT ((r) == Z_OK, "zlib error")
241 
242 /*
243  * C++98 does not allow variadic macros
244  * 1. Declare a default variadic function for C and C++
245  * 2. Use macros in C instead of the function
246  * This loses __FILE__ and __LINE__ in the C++ ..F log functions
247  */
248 void SC_ABORTF (const char *fmt, ...)
249  __attribute__ ((format (printf, 1, 2)))
250  __attribute__ ((noreturn));
251 void SC_CHECK_ABORTF (int success, const char *fmt, ...)
252  __attribute__ ((format (printf, 2, 3)));
253 #ifndef __cplusplus
254 #define SC_ABORTF(fmt,...) \
255  sc_abort_verbosef (__FILE__, __LINE__, (fmt), __VA_ARGS__)
256 #define SC_CHECK_ABORTF(q,fmt,...) \
257  ((q) ? (void) 0 : SC_ABORTF (fmt, __VA_ARGS__))
258 #endif
259 #define SC_ABORT1(fmt,a) \
260  sc_abort_verbosef (__FILE__, __LINE__, (fmt), (a))
261 #define SC_ABORT2(fmt,a,b) \
262  sc_abort_verbosef (__FILE__, __LINE__, (fmt), (a), (b))
263 #define SC_ABORT3(fmt,a,b,c) \
264  sc_abort_verbosef (__FILE__, __LINE__, (fmt), (a), (b), (c))
265 #define SC_ABORT4(fmt,a,b,c,d) \
266  sc_abort_verbosef (__FILE__, __LINE__, (fmt), (a), (b), (c), (d))
267 #define SC_ABORT5(fmt,a,b,c,d,e) \
268  sc_abort_verbosef (__FILE__, __LINE__, (fmt), (a), (b), (c), (d), (e))
269 #define SC_ABORT6(fmt,a,b,c,d,e,f) \
270  sc_abort_verbosef (__FILE__, __LINE__, (fmt), (a), (b), (c), (d), (e), (f))
271 #define SC_CHECK_ABORT1(q,fmt,a) \
272  ((q) ? (void) 0 : SC_ABORT1 ((fmt), (a)))
273 #define SC_CHECK_ABORT2(q,fmt,a,b) \
274  ((q) ? (void) 0 : SC_ABORT2 ((fmt), (a), (b)))
275 #define SC_CHECK_ABORT3(q,fmt,a,b,c) \
276  ((q) ? (void) 0 : SC_ABORT3 ((fmt), (a), (b), (c)))
277 #define SC_CHECK_ABORT4(q,fmt,a,b,c,d) \
278  ((q) ? (void) 0 : SC_ABORT4 ((fmt), (a), (b), (c), (d)))
279 #define SC_CHECK_ABORT5(q,fmt,a,b,c,d,e) \
280  ((q) ? (void) 0 : SC_ABORT5 ((fmt), (a), (b), (c), (d), (e)))
281 #define SC_CHECK_ABORT6(q,fmt,a,b,c,d,e,f) \
282  ((q) ? (void) 0 : SC_ABORT6 ((fmt), (a), (b), (c), (d), (e), (f)))
283 
284 /* assertions, only enabled in debug mode */
285 
286 #ifdef SC_ENABLE_DEBUG
287 #define SC_ASSERT(c) SC_CHECK_ABORT ((c), "Assertion '" #c "'")
288 #define SC_EXECUTE_ASSERT_FALSE(expression) \
289  do { int _sc_i = (int) (expression); \
290  SC_CHECK_ABORT (!_sc_i, "Expected false: '" #expression "'"); \
291  } while (0)
292 #define SC_EXECUTE_ASSERT_TRUE(expression) \
293  do { int _sc_i = (int) (expression); \
294  SC_CHECK_ABORT (_sc_i, "Expected true: '" #expression "'"); \
295  } while (0)
296 #else
297 #define SC_ASSERT(c) SC_NOOP ()
298 #define SC_EXECUTE_ASSERT_FALSE(expression) \
299  do { (void) (expression); } while (0)
300 #define SC_EXECUTE_ASSERT_TRUE(expression) \
301  do { (void) (expression); } while (0)
302 #endif
303 
304 /* macros for memory allocation, will abort if out of memory */
305 
306 #define SC_ALLOC(t,n) (t *) sc_malloc (sc_package_id, (n) * sizeof(t))
307 #define SC_ALLOC_ZERO(t,n) (t *) sc_calloc (sc_package_id, \
308  (size_t) (n), sizeof(t))
309 #define SC_REALLOC(p,t,n) (t *) sc_realloc (sc_package_id, \
310  (p), (n) * sizeof(t))
311 #define SC_STRDUP(s) sc_strdup (sc_package_id, (s))
312 #define SC_FREE(p) sc_free (sc_package_id, (p))
313 
314 /* macros for memory alignment */
315 /* some copied from bfam: https://github.com/bfam/bfam */
316 
317 #define SC_ALIGN_UP(x,n) ( ((n) <= 0) ? (x) : ((x) + (n) - 1) / (n) * (n) )
318 
319 #if defined (__bgq__)
320 #define SC_ARG_ALIGN(p,t,n) __alignx((n), (p))
321 #elif defined (__ICC)
322 #define SC_ARG_ALIGN(p,t,n) __assume_aligned((p), (n))
323 #elif defined (__clang__)
324 #define SC_ARG_ALIGN(p,t,n) SC_NOOP ()
325 #elif defined (__GNUC__) || defined (__GNUG__)
326 
327 #if SC_GCC_VERSION >= SC_CALC_VERSION (4, 7, 0)
328 #define SC_ARG_ALIGN(p,t,n) do { \
329  (p) = (t) __builtin_assume_aligned((void *) (p), (n)); \
330 } while (0)
331 #else
332 #define SC_ARG_ALIGN(p,t,n) SC_NOOP ()
333 #endif
334 
335 #else
336 #define SC_ARG_ALIGN(p,t,n) SC_NOOP ()
337 #endif
338 
339 #if (defined __GNUC__) || (defined __PGI) || (defined __IBMC__)
340 #define SC_ATTR_ALIGN(n) __attribute__((aligned(n)))
341 #else
342 #define SC_ATTR_ALIGN(n)
343 #endif
344 
349 #define SC_BZERO(p,n) ((void) memset ((p), 0, (n) * sizeof (*(p))))
350 
351 /* min, max and square helper macros */
352 
353 #define SC_MIN(a,b) (((a) < (b)) ? (a) : (b))
354 #define SC_MAX(a,b) (((a) > (b)) ? (a) : (b))
355 #define SC_SQR(a) ((a) * (a))
356 
357 /* hopefully fast binary logarithms and binary round up */
358 
359 #define SC_LOG2_8(x) (sc_log2_lookup_table[(x)])
360 #define SC_LOG2_16(x) (((x) > 0xff) ? \
361  (SC_LOG2_8 ((x) >> 8) + 8) : SC_LOG2_8 (x))
362 #define SC_LOG2_32(x) (((x) > 0xffff) ? \
363  (SC_LOG2_16 ((x) >> 16)) + 16 : SC_LOG2_16 (x))
364 #define SC_LOG2_64(x) (((x) > 0xffffffffLL) ? \
365  (SC_LOG2_32 ((x) >> 32)) + 32 : SC_LOG2_32 (x))
366 #define SC_ROUNDUP2_32(x) \
367  (((x) <= 0) ? 0 : (1 << (SC_LOG2_32 ((x) - 1) + 1)))
368 #define SC_ROUNDUP2_64(x) \
369  (((x) <= 0) ? 0 : (1LL << (SC_LOG2_64 ((x) - 1LL) + 1)))
370 
371 /* log categories */
372 
373 #define SC_LC_GLOBAL 1
374 #define SC_LC_NORMAL 2
404 #define SC_LP_DEFAULT (-1)
405 #define SC_LP_ALWAYS 0
406 #define SC_LP_TRACE 1
407 #define SC_LP_DEBUG 2
408 #define SC_LP_VERBOSE 3
409 #define SC_LP_INFO 4
410 #define SC_LP_STATISTICS 5
411 #define SC_LP_PRODUCTION 6
412 #define SC_LP_ESSENTIAL 7
413 #define SC_LP_ERROR 8
414 #define SC_LP_SILENT 9
420 #ifdef SC_LOG_PRIORITY
421 #define SC_LP_THRESHOLD SC_LOG_PRIORITY
422 #else
423 #ifdef SC_ENABLE_DEBUG
424 #define SC_LP_THRESHOLD SC_LP_TRACE
425 #else
426 #define SC_LP_THRESHOLD SC_LP_INFO
427 #endif
428 #endif
429 
430 /* generic log macros */
431 #define SC_GEN_LOG(package,category,priority,s) \
432  ((priority) < SC_LP_THRESHOLD ? (void) 0 : \
433  sc_log (__FILE__, __LINE__, (package), (category), (priority), (s)))
434 #define SC_GLOBAL_LOG(p,s) SC_GEN_LOG (sc_package_id, SC_LC_GLOBAL, (p), (s))
435 #define SC_LOG(p,s) SC_GEN_LOG (sc_package_id, SC_LC_NORMAL, (p), (s))
436 void SC_GEN_LOGF (int package, int category, int priority,
437  const char *fmt, ...)
438  __attribute__ ((format (printf, 4, 5)));
439 void SC_GLOBAL_LOGF (int priority, const char *fmt, ...)
440  __attribute__ ((format (printf, 2, 3)));
441 void SC_LOGF (int priority, const char *fmt, ...)
442  __attribute__ ((format (printf, 2, 3)));
443 #ifndef __cplusplus
444 #define SC_GEN_LOGF(package,category,priority,fmt,...) \
445  ((priority) < SC_LP_THRESHOLD ? (void) 0 : \
446  sc_logf (__FILE__, __LINE__, (package), (category), (priority), \
447  (fmt), __VA_ARGS__))
448 #define SC_GLOBAL_LOGF(p,fmt,...) \
449  SC_GEN_LOGF (sc_package_id, SC_LC_GLOBAL, (p), (fmt), __VA_ARGS__)
450 #define SC_LOGF(p,fmt,...) \
451  SC_GEN_LOGF (sc_package_id, SC_LC_NORMAL, (p), (fmt), __VA_ARGS__)
452 #endif
453 
454 /* convenience global log macros will only output if identifier <= 0 */
455 #define SC_GLOBAL_TRACE(s) SC_GLOBAL_LOG (SC_LP_TRACE, (s))
456 #define SC_GLOBAL_LDEBUG(s) SC_GLOBAL_LOG (SC_LP_DEBUG, (s))
457 #define SC_GLOBAL_VERBOSE(s) SC_GLOBAL_LOG (SC_LP_VERBOSE, (s))
458 #define SC_GLOBAL_INFO(s) SC_GLOBAL_LOG (SC_LP_INFO, (s))
459 #define SC_GLOBAL_STATISTICS(s) SC_GLOBAL_LOG (SC_LP_STATISTICS, (s))
460 #define SC_GLOBAL_PRODUCTION(s) SC_GLOBAL_LOG (SC_LP_PRODUCTION, (s))
461 #define SC_GLOBAL_ESSENTIAL(s) SC_GLOBAL_LOG (SC_LP_ESSENTIAL, (s))
462 #define SC_GLOBAL_LERROR(s) SC_GLOBAL_LOG (SC_LP_ERROR, (s))
463 void SC_GLOBAL_TRACEF (const char *fmt, ...)
464  __attribute__ ((format (printf, 1, 2)));
465 void SC_GLOBAL_LDEBUGF (const char *fmt, ...)
466  __attribute__ ((format (printf, 1, 2)));
467 void SC_GLOBAL_VERBOSEF (const char *fmt, ...)
468  __attribute__ ((format (printf, 1, 2)));
469 void SC_GLOBAL_INFOF (const char *fmt, ...)
470  __attribute__ ((format (printf, 1, 2)));
471 void SC_GLOBAL_STATISTICSF (const char *fmt, ...)
472  __attribute__ ((format (printf, 1, 2)));
473 void SC_GLOBAL_PRODUCTIONF (const char *fmt, ...)
474  __attribute__ ((format (printf, 1, 2)));
475 void SC_GLOBAL_ESSENTIALF (const char *fmt, ...)
476  __attribute__ ((format (printf, 1, 2)));
477 void SC_GLOBAL_LERRORF (const char *fmt, ...)
478  __attribute__ ((format (printf, 1, 2)));
479 #ifndef __cplusplus
480 #define SC_GLOBAL_TRACEF(fmt,...) \
481  SC_GLOBAL_LOGF (SC_LP_TRACE, (fmt), __VA_ARGS__)
482 #define SC_GLOBAL_LDEBUGF(fmt,...) \
483  SC_GLOBAL_LOGF (SC_LP_DEBUG, (fmt), __VA_ARGS__)
484 #define SC_GLOBAL_VERBOSEF(fmt,...) \
485  SC_GLOBAL_LOGF (SC_LP_VERBOSE, (fmt), __VA_ARGS__)
486 #define SC_GLOBAL_INFOF(fmt,...) \
487  SC_GLOBAL_LOGF (SC_LP_INFO, (fmt), __VA_ARGS__)
488 #define SC_GLOBAL_STATISTICSF(fmt,...) \
489  SC_GLOBAL_LOGF (SC_LP_STATISTICS, (fmt), __VA_ARGS__)
490 #define SC_GLOBAL_PRODUCTIONF(fmt,...) \
491  SC_GLOBAL_LOGF (SC_LP_PRODUCTION, (fmt), __VA_ARGS__)
492 #define SC_GLOBAL_ESSENTIALF(fmt,...) \
493  SC_GLOBAL_LOGF (SC_LP_ESSENTIAL, (fmt), __VA_ARGS__)
494 #define SC_GLOBAL_LERRORF(fmt,...) \
495  SC_GLOBAL_LOGF (SC_LP_ERROR, (fmt), __VA_ARGS__)
496 #endif
497 
498 /* convenience log macros that output regardless of identifier */
499 #define SC_TRACE(s) SC_LOG (SC_LP_TRACE, (s))
500 #define SC_LDEBUG(s) SC_LOG (SC_LP_DEBUG, (s))
501 #define SC_VERBOSE(s) SC_LOG (SC_LP_VERBOSE, (s))
502 #define SC_INFO(s) SC_LOG (SC_LP_INFO, (s))
503 #define SC_STATISTICS(s) SC_LOG (SC_LP_STATISTICS, (s))
504 #define SC_PRODUCTION(s) SC_LOG (SC_LP_PRODUCTION, (s))
505 #define SC_ESSENTIAL(s) SC_LOG (SC_LP_ESSENTIAL, (s))
506 #define SC_LERROR(s) SC_LOG (SC_LP_ERROR, (s))
507 void SC_TRACEF (const char *fmt, ...)
508  __attribute__ ((format (printf, 1, 2)));
509 void SC_LDEBUGF (const char *fmt, ...)
510  __attribute__ ((format (printf, 1, 2)));
511 void SC_VERBOSEF (const char *fmt, ...)
512  __attribute__ ((format (printf, 1, 2)));
513 void SC_INFOF (const char *fmt, ...)
514  __attribute__ ((format (printf, 1, 2)));
515 void SC_STATISTICSF (const char *fmt, ...)
516  __attribute__ ((format (printf, 1, 2)));
517 void SC_PRODUCTIONF (const char *fmt, ...)
518  __attribute__ ((format (printf, 1, 2)));
519 void SC_ESSENTIALF (const char *fmt, ...)
520  __attribute__ ((format (printf, 1, 2)));
521 void SC_LERRORF (const char *fmt, ...)
522  __attribute__ ((format (printf, 1, 2)));
523 #ifndef __cplusplus
524 #define SC_TRACEF(fmt,...) \
525  SC_LOGF (SC_LP_TRACE, (fmt), __VA_ARGS__)
526 #define SC_LDEBUGF(fmt,...) \
527  SC_LOGF (SC_LP_DEBUG, (fmt), __VA_ARGS__)
528 #define SC_VERBOSEF(fmt,...) \
529  SC_LOGF (SC_LP_VERBOSE, (fmt), __VA_ARGS__)
530 #define SC_INFOF(fmt,...) \
531  SC_LOGF (SC_LP_INFO, (fmt), __VA_ARGS__)
532 #define SC_STATISTICSF(fmt,...) \
533  SC_LOGF (SC_LP_STATISTICS, (fmt), __VA_ARGS__)
534 #define SC_PRODUCTIONF(fmt,...) \
535  SC_LOGF (SC_LP_PRODUCTION, (fmt), __VA_ARGS__)
536 #define SC_ESSENTIALF(fmt,...) \
537  SC_LOGF (SC_LP_ESSENTIAL, (fmt), __VA_ARGS__)
538 #define SC_LERRORF(fmt,...) \
539  SC_LOGF (SC_LP_ERROR, (fmt), __VA_ARGS__)
540 #endif
541 
544 #define _SC_TOSTRING(x) #x
545 
548 #define SC_TOSTRING(x) _SC_TOSTRING(x)
549 
550 /* callback typedefs */
551 
552 typedef void (*sc_handler_t) (void *data);
553 typedef void (*sc_log_handler_t) (FILE * log_stream,
554  const char *filename, int lineno,
555  int package, int category,
556  int priority, const char *msg);
557 
559 typedef void (*sc_abort_handler_t) (void);
560 
561 /* memory allocation functions, will abort if out of memory */
562 
563 void *sc_malloc (int package, size_t size);
564 void *sc_calloc (int package, size_t nmemb, size_t size);
565 void *sc_realloc (int package, void *ptr, size_t size);
566 char *sc_strdup (int package, const char *s);
567 void sc_free (int package, void *ptr);
568 int sc_memory_status (int package);
569 void sc_memory_check (int package);
570 
572 int sc_memory_check_noerr (int package);
573 
574 /* comparison functions for various integer sizes */
575 
576 int sc_int_compare (const void *v1, const void *v2);
577 int sc_int8_compare (const void *v1, const void *v2);
578 int sc_int16_compare (const void *v1, const void *v2);
579 int sc_int32_compare (const void *v1, const void *v2);
580 int sc_int64_compare (const void *v1, const void *v2);
581 int sc_double_compare (const void *v1, const void *v2);
582 
588 int sc_atoi (const char *nptr);
589 
595 long sc_atol (const char *nptr);
596 
603 void sc_set_log_defaults (FILE * log_stream,
604  sc_log_handler_t log_handler,
605  int log_threshold);
606 
612 
619 void sc_log (const char *filename, int lineno,
620  int package, int category, int priority,
621  const char *msg);
622 void sc_logf (const char *filename, int lineno,
623  int package, int category, int priority,
624  const char *fmt, ...)
625  __attribute__ ((format (printf, 6, 7)));
626 void sc_logv (const char *filename, int lineno,
627  int package, int category, int priority,
628  const char *fmt, va_list ap);
629 
631 void sc_log_indent_push_count (int package, int count);
632 
634 void sc_log_indent_pop_count (int package, int count);
635 
637 void sc_log_indent_push (void);
638 
640 void sc_log_indent_pop (void);
641 
643 void sc_abort (void)
644  __attribute__ ((noreturn));
645 
647 void sc_abort_verbose (const char *filename, int lineno,
648  const char *msg)
649  __attribute__ ((noreturn));
650 
652 void sc_abort_verbosef (const char *filename, int lineno,
653  const char *fmt, ...)
654  __attribute__ ((format (printf, 3, 4)))
655  __attribute__ ((noreturn));
656 
658 void sc_abort_verbosev (const char *filename, int lineno,
659  const char *fmt, va_list ap)
660  __attribute__ ((noreturn));
661 
663 void sc_abort_collective (const char *msg)
664  __attribute__ ((noreturn));
665 
671 int sc_package_register (sc_log_handler_t log_handler,
672  int log_threshold,
673  const char *name, const char *full);
674 
680 int sc_package_is_registered (int package_id);
681 
691 void sc_package_lock (int package_id);
692 
702 void sc_package_unlock (int package_id);
703 
709 void sc_package_set_verbosity (int package_id,
710  int log_priority);
711 
721  int set_abort);
722 
726 void sc_package_unregister (int package_id);
727 
732 void sc_package_print_summary (int log_priority);
733 
749 void sc_init (sc_MPI_Comm mpicomm,
750  int catch_signals, int print_backtrace,
751  sc_log_handler_t log_handler, int log_threshold);
752 
760 void sc_finalize (void);
761 
770 
777 int sc_is_root (void);
778 
790 void sc_strcopy (char *dest, size_t size, const char *src);
791 
805 void sc_snprintf (char *str, size_t size,
806  const char *format, ...)
807  __attribute__ ((format (printf, 3, 4)));
808 
817 const char *sc_version (void);
818 
823 int sc_version_major (void);
824 
829 int sc_version_minor (void);
830 
831 #if 0
832 /* Sadly, the point version macro by autoconf doesn't work with vX and vX.Y.
833  The remaining option is to use sc_version and parse its return string. */
840 int sc_version_point (void);
841 #endif /* 0 */
842 
846 int sc_have_json (void);
847 
848 SC_EXTERN_C_END;
849 
850 #endif /* SC_H */
int sc_have_json(void)
Return whether we have found a JSON library at configure time.
void sc_set_log_defaults(FILE *log_stream, sc_log_handler_t log_handler, int log_threshold)
Controls the default SC log behavior.
int sc_atoi(const char *nptr)
Safe version of the standard library atoi (3) function.
void sc_log_indent_pop(void)
Remove one space from the start of a sc's default log format.
void sc_package_unregister(int package_id)
Unregister a software package with SC.
void const char * sc_version(void)
Return the full version of libsc.
void sc_finalize(void)
Unregisters all packages, runs the memory check, removes the signal handlers and resets sc_identifier...
int sc_package_is_registered(int package_id)
Query whether an identifier matches a registered package.
int sc_memory_check_noerr(int package)
Return error count or zero if all is ok.
void sc_package_set_verbosity(int package_id, int log_priority)
Set the logging verbosity of a registered package.
void(* sc_abort_handler_t)(void)
Type of the abort handler function.
Definition: sc.h:559
int sc_finalize_noabort(void)
Unregisters all packages, runs the memory check, removes the signal handlers and resets sc_identifier...
void sc_package_unlock(int package_id)
Release a pthread mutex lock.
void sc_log_indent_pop_count(int package, int count)
Remove spaces from the start of a package's default log format.
void sc_log(const char *filename, int lineno, int package, int category, int priority, const char *msg)
The central log function to be called by all packages.
void sc_init(sc_MPI_Comm mpicomm, int catch_signals, int print_backtrace, sc_log_handler_t log_handler, int log_threshold)
Sets the global program identifier (e.g.
void sc_set_abort_handler(sc_abort_handler_t abort_handler)
Set the default SC abort behavior.
void sc_snprintf(char *str, size_t size, const char *format,...) __attribute__((format(printf
Wrap the system snprintf function, allowing for truncation.
void sc_package_lock(int package_id)
Acquire a pthread mutex lock.
int sc_package_id
libsc allows for multiple packages to use their own log priorities etc.
void sc_abort_collective(const char *msg) __attribute__((noreturn))
Collective abort where only root prints a message.
FILE * sc_trace_file
Optional trace file for logging (see sc_init).
void sc_log_indent_push_count(int package, int count)
Add spaces to the start of a package's default log format.
int sc_version_major(void)
Return the major version of libsc.
void sc_abort_verbosef(const char *filename, int lineno, const char *fmt,...) __attribute__((format(printf
Print a message to stderr and then call sc_abort ().
void sc_abort_verbosev(const char *filename, int lineno, const char *fmt, va_list ap) __attribute__((noreturn))
Print a message to stderr and then call sc_abort ().
const int sc_log2_lookup_table[256]
Lookup table to provide fast base-2 logarithm of integers.
void sc_abort(void) __attribute__((noreturn))
Print a stack trace, call the abort handler and then call abort ().
int sc_package_register(sc_log_handler_t log_handler, int log_threshold, const char *name, const char *full)
Register a software package with SC.
void sc_strcopy(char *dest, size_t size, const char *src)
Provide a string copy function.
void sc_package_set_abort_alloc_mismatch(int package_id, int set_abort)
Set the unregister behavior of sc_package_unregister().
void sc_abort_verbose(const char *filename, int lineno, const char *msg) __attribute__((noreturn))
Print a message to stderr and then call sc_abort ().
void sc_log_indent_push(void)
Add one space to the start of sc's default log format.
int sc_trace_prio
Optional minimum log priority for messages that go into the trace file.
int sc_version_minor(void)
Return the minor version of libsc.
int sc_is_root(void)
Identify the root process.
long sc_atol(const char *nptr)
Safe version of the standard library atol (3) function.
void sc_package_print_summary(int log_priority)
Print a summary of all packages registered with SC.
Provide a consistent MPI interface with and without MPI configured.
sc3_MPI_Comm_t sc_MPI_Comm
Emulate an MPI communicator.
Definition: sc_mpi.h:392