46#define _sc_const const
50#define _sc_restrict restrict
54#define SC_CALC_VERSION(major,minor,patchlevel) \
55 (((major) * 1000 + (minor)) * 1000 + (patchlevel))
57#define SC_GCC_VERSION \
58 SC_CALC_VERSION(__GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__)
61#define SC_GCC_VERSION \
62 SC_CALC_VERSION (0, 0, 0)
66#ifdef SC_ENABLE_MEMALIGN
68#ifdef SC_HAVE_ANY_MEMALIGN
69#undef SC_HAVE_ANY_MEMALIGN
72#ifdef SC_HAVE_ANY_MEMALIGN
73#ifndef SC_HAVE_POSIX_MEMALIGN
74#ifdef SC_HAVE_ALIGNED_ALLOC
83#ifndef SC_ENABLE_USE_COUNTERS
84#define SC_NOCOUNT_MALLOC
85#define SC_NOCOUNT_REFCOUNT
86#define SC_NOCOUNT_LOGINDENT
91#ifndef __STDC_LIMIT_MACROS
93#define __STDC_LIMIT_MACROS
95#ifndef __STDC_CONSTANT_MACROS
97#define __STDC_CONSTANT_MACROS
106#error "mpi.h is included. Use --enable-mpi."
112#define _USE_MATH_DEFINES
115#define M_E 2.71828182845904523536
118#define M_LOG2E 1.44269504088896340736
121#define M_LOG10E 0.434294481903251827651
124#define M_LN2 0.693147180559945309417
127#define M_LN10 2.30258509299404568402
130#define M_PI 3.14159265358979323846
133#define M_PI_2 1.57079632679489661923
136#define M_PI_4 0.785398163397448309616
139#define M_1_PI 0.318309886183790671538
142#define M_2_PI 0.636619772367581343076
145#define M_2_SQRTPI 1.12837916709551257390
148#define M_SQRT2 1.41421356237309504880
151#define M_SQRT1_2 0.707106781186547524401
155#if defined SC_HAVE_LIBGEN_H && !defined _MSC_VER
161#ifdef SC_HAVE_STDINT_H
165#ifdef SC_HAVE_STDLIB_H
168#ifdef SC_HAVE_STRING_H
171#ifdef SC_HAVE_SYS_TIME_H
174#ifdef SC_HAVE_UNISTD_H
178typedef SSIZE_T ssize_t;
183#define SC_INIT_COMM_CLEAN
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)
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)
225#define SC_EPS 2.220446049250313e-16
228#define SC_1000_EPS (1000. * 2.220446049250313e-16)
233#define SC_NOOP() ((void) (0))
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")
247void SC_ABORTF (
const char *fmt, ...)
248 __attribute__ ((format (printf, 1, 2)))
249 __attribute__ ((noreturn));
250void SC_CHECK_ABORTF (
int success, const
char *fmt, ...)
251 __attribute__ ((format (printf, 2, 3)));
253#define SC_ABORTF(fmt,...) \
254 sc_abort_verbosef (__FILE__, __LINE__, (fmt), __VA_ARGS__)
255#define SC_CHECK_ABORTF(q,fmt,...) \
256 ((q) ? (void) 0 : SC_ABORTF (fmt, __VA_ARGS__))
258#define SC_ABORT1(fmt,a) \
259 sc_abort_verbosef (__FILE__, __LINE__, (fmt), (a))
260#define SC_ABORT2(fmt,a,b) \
261 sc_abort_verbosef (__FILE__, __LINE__, (fmt), (a), (b))
262#define SC_ABORT3(fmt,a,b,c) \
263 sc_abort_verbosef (__FILE__, __LINE__, (fmt), (a), (b), (c))
264#define SC_ABORT4(fmt,a,b,c,d) \
265 sc_abort_verbosef (__FILE__, __LINE__, (fmt), (a), (b), (c), (d))
266#define SC_ABORT5(fmt,a,b,c,d,e) \
267 sc_abort_verbosef (__FILE__, __LINE__, (fmt), (a), (b), (c), (d), (e))
268#define SC_ABORT6(fmt,a,b,c,d,e,f) \
269 sc_abort_verbosef (__FILE__, __LINE__, (fmt), (a), (b), (c), (d), (e), (f))
270#define SC_CHECK_ABORT1(q,fmt,a) \
271 ((q) ? (void) 0 : SC_ABORT1 ((fmt), (a)))
272#define SC_CHECK_ABORT2(q,fmt,a,b) \
273 ((q) ? (void) 0 : SC_ABORT2 ((fmt), (a), (b)))
274#define SC_CHECK_ABORT3(q,fmt,a,b,c) \
275 ((q) ? (void) 0 : SC_ABORT3 ((fmt), (a), (b), (c)))
276#define SC_CHECK_ABORT4(q,fmt,a,b,c,d) \
277 ((q) ? (void) 0 : SC_ABORT4 ((fmt), (a), (b), (c), (d)))
278#define SC_CHECK_ABORT5(q,fmt,a,b,c,d,e) \
279 ((q) ? (void) 0 : SC_ABORT5 ((fmt), (a), (b), (c), (d), (e)))
280#define SC_CHECK_ABORT6(q,fmt,a,b,c,d,e,f) \
281 ((q) ? (void) 0 : SC_ABORT6 ((fmt), (a), (b), (c), (d), (e), (f)))
285#ifdef SC_ENABLE_DEBUG
286#define SC_ASSERT(c) SC_CHECK_ABORT ((c), "Assertion '" #c "'")
287#define SC_EXECUTE_ASSERT_FALSE(expression) \
288 do { int _sc_i = (int) (expression); \
289 SC_CHECK_ABORT (!_sc_i, "Expected false: '" #expression "'"); \
291#define SC_EXECUTE_ASSERT_TRUE(expression) \
292 do { int _sc_i = (int) (expression); \
293 SC_CHECK_ABORT (_sc_i, "Expected true: '" #expression "'"); \
296#define SC_ASSERT(c) SC_NOOP ()
297#define SC_EXECUTE_ASSERT_FALSE(expression) \
298 do { (void) (expression); } while (0)
299#define SC_EXECUTE_ASSERT_TRUE(expression) \
300 do { (void) (expression); } while (0)
305#define SC_ALLOC(t,n) (t *) sc_malloc (sc_package_id, (n) * sizeof(t))
306#define SC_ALLOC_ZERO(t,n) (t *) sc_calloc (sc_package_id, \
307 (size_t) (n), sizeof(t))
308#define SC_REALLOC(p,t,n) (t *) sc_realloc (sc_package_id, \
309 (p), (n) * sizeof(t))
310#define SC_STRDUP(s) sc_strdup (sc_package_id, (s))
311#define SC_FREE(p) sc_free (sc_package_id, (p))
316#define SC_ALIGN_UP(x,n) ( ((n) <= 0) ? (x) : ((x) + (n) - 1) / (n) * (n) )
319#define SC_ARG_ALIGN(p,t,n) __alignx((n), (p))
321#define SC_ARG_ALIGN(p,t,n) __assume_aligned((p), (n))
322#elif defined (__clang__)
323#define SC_ARG_ALIGN(p,t,n) SC_NOOP ()
324#elif defined (__GNUC__) || defined (__GNUG__)
326#if SC_GCC_VERSION >= SC_CALC_VERSION (4, 7, 0)
327#define SC_ARG_ALIGN(p,t,n) do { \
328 (p) = (t) __builtin_assume_aligned((void *) (p), (n)); \
331#define SC_ARG_ALIGN(p,t,n) SC_NOOP ()
335#define SC_ARG_ALIGN(p,t,n) SC_NOOP ()
338#if (defined __GNUC__) || (defined __PGI) || (defined __IBMC__)
339#define SC_ATTR_ALIGN(n) __attribute__ ((aligned(n)))
341#define SC_ATTR_ALIGN(n)
348#define SC_BZERO(p,n) ((void) memset ((p), 0, (n) * sizeof (*(p))))
352#define SC_MIN(a,b) (((a) < (b)) ? (a) : (b))
353#define SC_MAX(a,b) (((a) > (b)) ? (a) : (b))
354#define SC_SQR(a) ((a) * (a))
358#define SC_LOG2_8(x) (sc_log2_lookup_table[(x)])
359#define SC_LOG2_16(x) (((x) > 0xff) ? \
360 (SC_LOG2_8 ((x) >> 8) + 8) : SC_LOG2_8 (x))
361#define SC_LOG2_32(x) (((x) > 0xffff) ? \
362 (SC_LOG2_16 ((x) >> 16)) + 16 : SC_LOG2_16 (x))
363#define SC_LOG2_64(x) (((x) > 0xffffffffLL) ? \
364 (SC_LOG2_32 ((x) >> 32)) + 32 : SC_LOG2_32 (x))
365#define SC_ROUNDUP2_32(x) \
366 (((x) <= 0) ? 0 : (1 << (SC_LOG2_32 ((x) - 1) + 1)))
367#define SC_ROUNDUP2_64(x) \
368 (((x) <= 0) ? 0 : (1LL << (SC_LOG2_64 ((x) - 1LL) + 1)))
372#define SC_LC_GLOBAL 1
373#define SC_LC_NORMAL 2
403#define SC_LP_DEFAULT (-1)
404#define SC_LP_ALWAYS 0
407#define SC_LP_VERBOSE 3
409#define SC_LP_STATISTICS 5
410#define SC_LP_PRODUCTION 6
411#define SC_LP_ESSENTIAL 7
413#define SC_LP_SILENT 9
419#ifdef SC_LOG_PRIORITY
420#define SC_LP_THRESHOLD SC_LOG_PRIORITY
422#ifdef SC_ENABLE_DEBUG
423#define SC_LP_THRESHOLD SC_LP_TRACE
425#define SC_LP_THRESHOLD SC_LP_INFO
430#define SC_GEN_LOG(package,category,priority,s) \
431 ((priority) < SC_LP_THRESHOLD ? (void) 0 : \
432 sc_log (__FILE__, __LINE__, (package), (category), (priority), (s)))
433#define SC_GLOBAL_LOG(p,s) SC_GEN_LOG (sc_package_id, SC_LC_GLOBAL, (p), (s))
434#define SC_LOG(p,s) SC_GEN_LOG (sc_package_id, SC_LC_NORMAL, (p), (s))
435void SC_GEN_LOGF (
int package,
int category,
int priority,
436 const char *fmt, ...)
437 __attribute__ ((format (printf, 4, 5)));
438void SC_GLOBAL_LOGF (
int priority, const
char *fmt, ...)
439 __attribute__ ((format (printf, 2, 3)));
440void SC_LOGF (
int priority, const
char *fmt, ...)
441 __attribute__ ((format (printf, 2, 3)));
443#define SC_GEN_LOGF(package,category,priority,fmt,...) \
444 ((priority) < SC_LP_THRESHOLD ? (void) 0 : \
445 sc_logf (__FILE__, __LINE__, (package), (category), (priority), \
447#define SC_GLOBAL_LOGF(p,fmt,...) \
448 SC_GEN_LOGF (sc_package_id, SC_LC_GLOBAL, (p), (fmt), __VA_ARGS__)
449#define SC_LOGF(p,fmt,...) \
450 SC_GEN_LOGF (sc_package_id, SC_LC_NORMAL, (p), (fmt), __VA_ARGS__)
454#define SC_GLOBAL_TRACE(s) SC_GLOBAL_LOG (SC_LP_TRACE, (s))
455#define SC_GLOBAL_LDEBUG(s) SC_GLOBAL_LOG (SC_LP_DEBUG, (s))
456#define SC_GLOBAL_VERBOSE(s) SC_GLOBAL_LOG (SC_LP_VERBOSE, (s))
457#define SC_GLOBAL_INFO(s) SC_GLOBAL_LOG (SC_LP_INFO, (s))
458#define SC_GLOBAL_STATISTICS(s) SC_GLOBAL_LOG (SC_LP_STATISTICS, (s))
459#define SC_GLOBAL_PRODUCTION(s) SC_GLOBAL_LOG (SC_LP_PRODUCTION, (s))
460#define SC_GLOBAL_ESSENTIAL(s) SC_GLOBAL_LOG (SC_LP_ESSENTIAL, (s))
461#define SC_GLOBAL_LERROR(s) SC_GLOBAL_LOG (SC_LP_ERROR, (s))
462void SC_GLOBAL_TRACEF (
const char *fmt, ...)
463 __attribute__ ((format (printf, 1, 2)));
464void SC_GLOBAL_LDEBUGF (const
char *fmt, ...)
465 __attribute__ ((format (printf, 1, 2)));
466void SC_GLOBAL_VERBOSEF (const
char *fmt, ...)
467 __attribute__ ((format (printf, 1, 2)));
468void SC_GLOBAL_INFOF (const
char *fmt, ...)
469 __attribute__ ((format (printf, 1, 2)));
470void SC_GLOBAL_STATISTICSF (const
char *fmt, ...)
471 __attribute__ ((format (printf, 1, 2)));
472void SC_GLOBAL_PRODUCTIONF (const
char *fmt, ...)
473 __attribute__ ((format (printf, 1, 2)));
474void SC_GLOBAL_ESSENTIALF (const
char *fmt, ...)
475 __attribute__ ((format (printf, 1, 2)));
476void SC_GLOBAL_LERRORF (const
char *fmt, ...)
477 __attribute__ ((format (printf, 1, 2)));
479#define SC_GLOBAL_TRACEF(fmt,...) \
480 SC_GLOBAL_LOGF (SC_LP_TRACE, (fmt), __VA_ARGS__)
481#define SC_GLOBAL_LDEBUGF(fmt,...) \
482 SC_GLOBAL_LOGF (SC_LP_DEBUG, (fmt), __VA_ARGS__)
483#define SC_GLOBAL_VERBOSEF(fmt,...) \
484 SC_GLOBAL_LOGF (SC_LP_VERBOSE, (fmt), __VA_ARGS__)
485#define SC_GLOBAL_INFOF(fmt,...) \
486 SC_GLOBAL_LOGF (SC_LP_INFO, (fmt), __VA_ARGS__)
487#define SC_GLOBAL_STATISTICSF(fmt,...) \
488 SC_GLOBAL_LOGF (SC_LP_STATISTICS, (fmt), __VA_ARGS__)
489#define SC_GLOBAL_PRODUCTIONF(fmt,...) \
490 SC_GLOBAL_LOGF (SC_LP_PRODUCTION, (fmt), __VA_ARGS__)
491#define SC_GLOBAL_ESSENTIALF(fmt,...) \
492 SC_GLOBAL_LOGF (SC_LP_ESSENTIAL, (fmt), __VA_ARGS__)
493#define SC_GLOBAL_LERRORF(fmt,...) \
494 SC_GLOBAL_LOGF (SC_LP_ERROR, (fmt), __VA_ARGS__)
498#define SC_TRACE(s) SC_LOG (SC_LP_TRACE, (s))
499#define SC_LDEBUG(s) SC_LOG (SC_LP_DEBUG, (s))
500#define SC_VERBOSE(s) SC_LOG (SC_LP_VERBOSE, (s))
501#define SC_INFO(s) SC_LOG (SC_LP_INFO, (s))
502#define SC_STATISTICS(s) SC_LOG (SC_LP_STATISTICS, (s))
503#define SC_PRODUCTION(s) SC_LOG (SC_LP_PRODUCTION, (s))
504#define SC_ESSENTIAL(s) SC_LOG (SC_LP_ESSENTIAL, (s))
505#define SC_LERROR(s) SC_LOG (SC_LP_ERROR, (s))
506void SC_TRACEF (
const char *fmt, ...)
507 __attribute__ ((format (printf, 1, 2)));
508void SC_LDEBUGF (const
char *fmt, ...)
509 __attribute__ ((format (printf, 1, 2)));
510void SC_VERBOSEF (const
char *fmt, ...)
511 __attribute__ ((format (printf, 1, 2)));
512void SC_INFOF (const
char *fmt, ...)
513 __attribute__ ((format (printf, 1, 2)));
514void SC_STATISTICSF (const
char *fmt, ...)
515 __attribute__ ((format (printf, 1, 2)));
516void SC_PRODUCTIONF (const
char *fmt, ...)
517 __attribute__ ((format (printf, 1, 2)));
518void SC_ESSENTIALF (const
char *fmt, ...)
519 __attribute__ ((format (printf, 1, 2)));
520void SC_LERRORF (const
char *fmt, ...)
521 __attribute__ ((format (printf, 1, 2)));
523#define SC_TRACEF(fmt,...) \
524 SC_LOGF (SC_LP_TRACE, (fmt), __VA_ARGS__)
525#define SC_LDEBUGF(fmt,...) \
526 SC_LOGF (SC_LP_DEBUG, (fmt), __VA_ARGS__)
527#define SC_VERBOSEF(fmt,...) \
528 SC_LOGF (SC_LP_VERBOSE, (fmt), __VA_ARGS__)
529#define SC_INFOF(fmt,...) \
530 SC_LOGF (SC_LP_INFO, (fmt), __VA_ARGS__)
531#define SC_STATISTICSF(fmt,...) \
532 SC_LOGF (SC_LP_STATISTICS, (fmt), __VA_ARGS__)
533#define SC_PRODUCTIONF(fmt,...) \
534 SC_LOGF (SC_LP_PRODUCTION, (fmt), __VA_ARGS__)
535#define SC_ESSENTIALF(fmt,...) \
536 SC_LOGF (SC_LP_ESSENTIAL, (fmt), __VA_ARGS__)
537#define SC_LERRORF(fmt,...) \
538 SC_LOGF (SC_LP_ERROR, (fmt), __VA_ARGS__)
543#define _SC_TOSTRING(x) #x
547#define SC_TOSTRING(x) _SC_TOSTRING(x)
551typedef void (*sc_handler_t) (
void *data);
552typedef void (*sc_log_handler_t) (FILE * log_stream,
553 const char *filename,
int lineno,
554 int package,
int category,
555 int priority,
const char *msg);
562void *sc_malloc (
int package,
size_t size);
563void *sc_calloc (
int package,
size_t nmemb,
size_t size);
564void *sc_realloc (
int package,
void *ptr,
size_t size);
565char *sc_strdup (
int package,
const char *s);
566void sc_free (
int package,
void *ptr);
567int sc_memory_status (
int package);
568void sc_memory_check (
int package);
575int sc_int_compare (
const void *v1,
const void *v2);
576int sc_int8_compare (
const void *v1,
const void *v2);
577int sc_int16_compare (
const void *v1,
const void *v2);
578int sc_int32_compare (
const void *v1,
const void *v2);
579int sc_int64_compare (
const void *v1,
const void *v2);
580int sc_double_compare (
const void *v1,
const void *v2);
603 sc_log_handler_t log_handler,
618void sc_log (
const char *filename,
int lineno,
619 int package,
int category,
int priority,
621void sc_logf (
const char *filename,
int lineno,
622 int package,
int category,
int priority,
623 const char *fmt, ...)
624 __attribute__ ((format (printf, 6, 7)));
625void sc_logv (const
char *filename,
int lineno,
626 int package,
int category,
int priority,
627 const
char *fmt, va_list ap);
643 __attribute__ ((noreturn));
648 __attribute__ ((noreturn));
652 const
char *fmt, ...)
653 __attribute__ ((format (printf, 3, 4)))
654 __attribute__ ((noreturn));
658 const
char *fmt, va_list ap)
659 __attribute__ ((noreturn));
663 __attribute__ ((noreturn));
672 const
char *name, const
char *full);
749 int catch_signals,
int print_backtrace,
750 sc_log_handler_t log_handler,
int log_threshold);
823 const
char *format, ...)
824 __attribute__ ((format (printf, 3, 4)));
857int sc_version_point (
void);
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.
int sc_have_zlib(void)
Return a boolean indicating whether zlib has been configured.
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:558
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.
int sc_get_package_id(void)
Query SC's own package identity.
void sc_package_lock(int package_id)
Acquire a pthread mutex lock.
const char * sc_version(void)
Return the full version of libsc.
int sc_is_initialized(void)
Return whether SC has been initialized or not.
void sc_abort_verbosef(const char *filename, int lineno, const char *fmt,...)
Print a message to stderr and then call sc_abort ().
int sc_package_id
libsc allows for multiple packages to use their own log priorities etc.
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.
const int sc_log2_lookup_table[256]
Lookup table to provide fast base-2 logarithm of integers.
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_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.
void sc_abort_verbose(const char *filename, int lineno, const char *msg)
Print a message to stderr and then call sc_abort ().
void sc_abort(void)
Print a stack trace, call the abort handler and then call abort ().
long sc_atol(const char *nptr)
Safe version of the standard library atol (3) function.
void sc_abort_verbosev(const char *filename, int lineno, const char *fmt, va_list ap)
Print a message to stderr and then call sc_abort ().
void sc_package_print_summary(int log_priority)
Print a summary of all packages registered with SC.
void sc_abort_collective(const char *msg)
Collective abort where only root prints a message.
void sc_snprintf(char *str, size_t size, const char *format,...)
Wrap the system snprintf function, allowing for truncation.
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:391