libsc  2.8.7
The SC library provides support for parallel scientific applications.
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
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 /* implement the default visibility attribute */
90 
91 #if defined _WIN32 || defined __CYGWIN__
92 #if 0
93  /* this is currently not properly tested */
94  #ifdef BUILDING_DLL
95  #ifdef __GNUC__
96  #define SC_DLL_PUBLIC __attribute__ ((dllexport))
97  #else
98  #define SC_DLL_PUBLIC __declspec(dllexport)
99  #endif
100  #else
101  #ifdef __GNUC__
102  #define SC_DLL_PUBLIC __attribute__ ((dllimport))
103  #else
104  #define SC_DLL_PUBLIC __declspec(dllimport)
105  #endif
106  #endif
107 #else
108  /* while disabling the above definitions */
109  #define SC_DLL_PUBLIC
110 #endif
111 #else
112  #if __GNUC__ >= 4
113  #define SC_DLL_PUBLIC __attribute__ ((visibility ("default")))
114  #else
115  #define SC_DLL_PUBLIC
116  #endif
117 #endif
118 
119 /* use this in case mpi.h includes stdint.h */
120 
121 #ifndef __STDC_LIMIT_MACROS
123 #define __STDC_LIMIT_MACROS
124 #endif
125 #ifndef __STDC_CONSTANT_MACROS
127 #define __STDC_CONSTANT_MACROS
128 #endif
129 
130 /* include MPI before stdio.h */
131 
132 #ifdef SC_ENABLE_MPI
133 #include <mpi.h>
134 #else
135 #ifdef MPI_SUCCESS
136 #error "mpi.h is included. Use --enable-mpi."
137 #endif
138 #endif
139 
140 /* include system headers */
141 
142 #define _USE_MATH_DEFINES
143 #include <math.h>
144 #ifndef M_E
145 #define M_E 2.71828182845904523536
146 #endif
147 #ifndef M_LOG2E
148 #define M_LOG2E 1.44269504088896340736
149 #endif
150 #ifndef M_LOG10E
151 #define M_LOG10E 0.434294481903251827651
152 #endif
153 #ifndef M_LN2
154 #define M_LN2 0.693147180559945309417
155 #endif
156 #ifndef M_LN10
157 #define M_LN10 2.30258509299404568402
158 #endif
159 #ifndef M_PI
160 #define M_PI 3.14159265358979323846
161 #endif
162 #ifndef M_PI_2
163 #define M_PI_2 1.57079632679489661923
164 #endif
165 #ifndef M_PI_4
166 #define M_PI_4 0.785398163397448309616
167 #endif
168 #ifndef M_1_PI
169 #define M_1_PI 0.318309886183790671538
170 #endif
171 #ifndef M_2_PI
172 #define M_2_PI 0.636619772367581343076
173 #endif
174 #ifndef M_2_SQRTPI
175 #define M_2_SQRTPI 1.12837916709551257390
176 #endif
177 #ifndef M_SQRT2
178 #define M_SQRT2 1.41421356237309504880
179 #endif
180 #ifndef M_SQRT1_2
181 #define M_SQRT1_2 0.707106781186547524401
182 #endif
183 #include <ctype.h>
184 #include <float.h>
185 #ifdef SC_HAVE_LIBGEN_H
186 #include <libgen.h>
187 #endif
188 #include <limits.h>
189 #include <stdarg.h>
190 #include <stddef.h>
191 #ifdef SC_HAVE_STDINT_H
192 #include <stdint.h>
193 #endif
194 #include <stdio.h>
195 #ifdef SC_HAVE_STDLIB_H
196 #include <stdlib.h>
197 #endif
198 #ifdef SC_HAVE_STRING_H
199 #include <string.h>
200 #endif
201 #ifdef SC_HAVE_SYS_TIME_H
202 #include <sys/time.h>
203 #elif defined(_MSC_VER) && !defined(SC_HAVE_GETTIMEOFDAY)
204 #define WIN32_LEAN_AND_MEAN
205 #include <Winsock2.h>
206 struct timezone
207 {
208  int tz_minuteswest;
209  int tz_dsttime;
210 };
211 int gettimeofday (struct timeval*, struct timezone*);
212 #endif
213 #ifdef SC_HAVE_UNISTD_H
214 #include <unistd.h>
215 #elif defined _WIN32
216 #include <BaseTsd.h>
217 typedef SSIZE_T ssize_t;
218 #endif
219 
220 /* definitions to allow user code to query the sc library */
222 #define SC_INIT_COMM_CLEAN
223 
224 /* provide extern C defines */
225 
226 /* The hacks below enable semicolons after the SC_EXTERN_C_ macros
227  * and also take care of the different semantics of () / (...) */
228 #ifdef __cplusplus
229 #define SC_EXTERN_C_BEGIN extern "C" { void sc_extern_c_hack_1 (void)
230 #define SC_EXTERN_C_END } extern "C" void sc_extern_c_hack_2 (void)
231 #define SC_NOARGS ...
232 #else
233 #define SC_EXTERN_C_BEGIN void sc_extern_c_hack_1 (void)
234 #define SC_EXTERN_C_END void sc_extern_c_hack_2 (void)
236 #define SC_NOARGS
237 #endif
238 
239 /* this header is always included */
240 #include <sc_mpi.h>
241 
242 SC_EXTERN_C_BEGIN;
243 
244 /* extern variables */
245 
247 extern const int sc_log2_lookup_table[256];
248 
257 extern SC_DLL_PUBLIC int sc_package_id;
258 
261 extern SC_DLL_PUBLIC FILE *sc_trace_file;
262 
264 extern SC_DLL_PUBLIC int sc_trace_prio;
265 
267 #define SC_EPS 2.220446049250313e-16
268 
270 #define SC_1000_EPS (1000. * 2.220446049250313e-16)
271 
272 /* check macros, always enabled */
273 
275 #define SC_NOOP() ((void) (0))
276 #define SC_ABORT(s) \
277  sc_abort_verbose (__FILE__, __LINE__, (s))
278 #define SC_ABORT_NOT_REACHED() SC_ABORT ("Unreachable code")
279 #define SC_CHECK_ABORT(q,s) \
280  ((q) ? (void) 0 : SC_ABORT (s))
281 #define SC_CHECK_MPI(r) SC_CHECK_ABORT ((r) == sc_MPI_SUCCESS, "MPI error")
282 
283 /*
284  * C++98 does not allow variadic macros
285  * 1. Declare a default variadic function for C and C++
286  * 2. Use macros in C instead of the function
287  * This loses __FILE__ and __LINE__ in the C++ ..F log functions
288  */
289 void SC_ABORTF (const char *fmt, ...)
290  __attribute__ ((format (printf, 1, 2)))
291  __attribute__ ((noreturn));
292 void SC_CHECK_ABORTF (int success, const char *fmt, ...)
293  __attribute__ ((format (printf, 2, 3)));
294 #ifndef __cplusplus
295 #define SC_ABORTF(fmt,...) \
296  sc_abort_verbosef (__FILE__, __LINE__, (fmt), __VA_ARGS__)
297 #define SC_CHECK_ABORTF(q,fmt,...) \
298  ((q) ? (void) 0 : SC_ABORTF (fmt, __VA_ARGS__))
299 #endif
300 #define SC_ABORT1(fmt,a) \
301  sc_abort_verbosef (__FILE__, __LINE__, (fmt), (a))
302 #define SC_ABORT2(fmt,a,b) \
303  sc_abort_verbosef (__FILE__, __LINE__, (fmt), (a), (b))
304 #define SC_ABORT3(fmt,a,b,c) \
305  sc_abort_verbosef (__FILE__, __LINE__, (fmt), (a), (b), (c))
306 #define SC_ABORT4(fmt,a,b,c,d) \
307  sc_abort_verbosef (__FILE__, __LINE__, (fmt), (a), (b), (c), (d))
308 #define SC_ABORT5(fmt,a,b,c,d,e) \
309  sc_abort_verbosef (__FILE__, __LINE__, (fmt), (a), (b), (c), (d), (e))
310 #define SC_ABORT6(fmt,a,b,c,d,e,f) \
311  sc_abort_verbosef (__FILE__, __LINE__, (fmt), (a), (b), (c), (d), (e), (f))
312 #define SC_CHECK_ABORT1(q,fmt,a) \
313  ((q) ? (void) 0 : SC_ABORT1 ((fmt), (a)))
314 #define SC_CHECK_ABORT2(q,fmt,a,b) \
315  ((q) ? (void) 0 : SC_ABORT2 ((fmt), (a), (b)))
316 #define SC_CHECK_ABORT3(q,fmt,a,b,c) \
317  ((q) ? (void) 0 : SC_ABORT3 ((fmt), (a), (b), (c)))
318 #define SC_CHECK_ABORT4(q,fmt,a,b,c,d) \
319  ((q) ? (void) 0 : SC_ABORT4 ((fmt), (a), (b), (c), (d)))
320 #define SC_CHECK_ABORT5(q,fmt,a,b,c,d,e) \
321  ((q) ? (void) 0 : SC_ABORT5 ((fmt), (a), (b), (c), (d), (e)))
322 #define SC_CHECK_ABORT6(q,fmt,a,b,c,d,e,f) \
323  ((q) ? (void) 0 : SC_ABORT6 ((fmt), (a), (b), (c), (d), (e), (f)))
324 
325 /* assertions, only enabled in debug mode */
326 
327 #ifdef SC_ENABLE_DEBUG
328 #define SC_ASSERT(c) SC_CHECK_ABORT ((c), "Assertion '" #c "'")
329 #define SC_EXECUTE_ASSERT_FALSE(expression) \
330  do { int _sc_i = (int) (expression); \
331  SC_CHECK_ABORT (!_sc_i, "Expected false: '" #expression "'"); \
332  } while (0)
333 #define SC_EXECUTE_ASSERT_TRUE(expression) \
334  do { int _sc_i = (int) (expression); \
335  SC_CHECK_ABORT (_sc_i, "Expected true: '" #expression "'"); \
336  } while (0)
337 #else
338 #define SC_ASSERT(c) SC_NOOP ()
339 #define SC_EXECUTE_ASSERT_FALSE(expression) \
340  do { (void) (expression); } while (0)
341 #define SC_EXECUTE_ASSERT_TRUE(expression) \
342  do { (void) (expression); } while (0)
343 #endif
344 
345 /* macros for memory allocation, will abort if out of memory */
346 
347 #define SC_ALLOC(t,n) (t *) sc_malloc (sc_package_id, (n) * sizeof(t))
348 #define SC_ALLOC_ZERO(t,n) (t *) sc_calloc (sc_package_id, \
349  (size_t) (n), sizeof(t))
350 #define SC_REALLOC(p,t,n) (t *) sc_realloc (sc_package_id, \
351  (p), (n) * sizeof(t))
352 #define SC_STRDUP(s) sc_strdup (sc_package_id, (s))
353 #define SC_FREE(p) sc_free (sc_package_id, (p))
354 
355 /* macros for memory alignment */
356 /* some copied from bfam: https://github.com/bfam/bfam */
357 
358 #define SC_ALIGN_UP(x,n) ( ((n) <= 0) ? (x) : ((x) + (n) - 1) / (n) * (n) )
359 
360 #if defined (__bgq__)
361 #define SC_ARG_ALIGN(p,t,n) __alignx((n), (p))
362 #elif defined (__ICC)
363 #define SC_ARG_ALIGN(p,t,n) __assume_aligned((p), (n))
364 #elif defined (__clang__)
365 #define SC_ARG_ALIGN(p,t,n) SC_NOOP ()
366 #elif defined (__GNUC__) || defined (__GNUG__)
367 
368 #if SC_GCC_VERSION >= SC_CALC_VERSION (4, 7, 0)
369 #define SC_ARG_ALIGN(p,t,n) do { \
370  (p) = (t) __builtin_assume_aligned((void *) (p), (n)); \
371 } while (0)
372 #else
373 #define SC_ARG_ALIGN(p,t,n) SC_NOOP ()
374 #endif
375 
376 #else
377 #define SC_ARG_ALIGN(p,t,n) SC_NOOP ()
378 #endif
379 
380 #if (defined __GNUC__) || (defined __PGI) || (defined __IBMC__)
381 #define SC_ATTR_ALIGN(n) __attribute__ ((aligned(n)))
382 #else
383 #define SC_ATTR_ALIGN(n)
384 #endif
385 
390 #define SC_BZERO(p,n) ((void) memset ((p), 0, (n) * sizeof (*(p))))
391 
392 /* min, max and square helper macros */
393 
394 #define SC_MIN(a,b) (((a) < (b)) ? (a) : (b))
395 #define SC_MAX(a,b) (((a) > (b)) ? (a) : (b))
396 #define SC_SQR(a) ((a) * (a))
397 
398 /* hopefully fast binary logarithms and binary round up */
399 
400 #define SC_LOG2_8(x) (sc_log2_lookup_table[(x)])
401 #define SC_LOG2_16(x) (((x) > 0xff) ? \
402  (SC_LOG2_8 ((x) >> 8) + 8) : SC_LOG2_8 (x))
403 #define SC_LOG2_32(x) (((x) > 0xffff) ? \
404  (SC_LOG2_16 ((x) >> 16)) + 16 : SC_LOG2_16 (x))
405 #define SC_LOG2_64(x) (((x) > 0xffffffffLL) ? \
406  (SC_LOG2_32 ((x) >> 32)) + 32 : SC_LOG2_32 (x))
407 #define SC_ROUNDUP2_32(x) \
408  (((x) <= 0) ? 0 : (1 << (SC_LOG2_32 ((x) - 1) + 1)))
409 #define SC_ROUNDUP2_64(x) \
410  (((x) <= 0) ? 0 : (1LL << (SC_LOG2_64 ((x) - 1LL) + 1)))
411 
412 /* log categories */
413 
414 #define SC_LC_GLOBAL 1
415 #define SC_LC_NORMAL 2
445 #define SC_LP_DEFAULT (-1)
446 #define SC_LP_ALWAYS 0
447 #define SC_LP_TRACE 1
448 #define SC_LP_DEBUG 2
449 #define SC_LP_VERBOSE 3
450 #define SC_LP_INFO 4
451 #define SC_LP_STATISTICS 5
452 #define SC_LP_PRODUCTION 6
453 #define SC_LP_ESSENTIAL 7
454 #define SC_LP_ERROR 8
455 #define SC_LP_SILENT 9
458 /* The default log priority may be overridded by this preprocessor define. */
459 #ifdef SC_LOG_PRIORITY
460 #define SC_LP_THRESHOLD SC_LOG_PRIORITY
461 #define SC_LP_APPLICATION SC_LOG_PRIORITY
462 #else
463 #ifdef SC_ENABLE_DEBUG
464 #define SC_LP_THRESHOLD SC_LP_TRACE
465 #define SC_LP_APPLICATION SC_LP_DEBUG
466 #else
468 #define SC_LP_THRESHOLD SC_LP_INFO
470 #define SC_LP_APPLICATION SC_LP_STATISTICS
471 #endif
472 #endif
473 
474 /* generic log macros */
475 #define SC_GEN_LOG(package,category,priority,s) \
476  ((priority) < SC_LP_THRESHOLD ? (void) 0 : \
477  sc_log (__FILE__, __LINE__, (package), (category), (priority), (s)))
478 #define SC_GLOBAL_LOG(p,s) SC_GEN_LOG (sc_package_id, SC_LC_GLOBAL, (p), (s))
479 #define SC_LOG(p,s) SC_GEN_LOG (sc_package_id, SC_LC_NORMAL, (p), (s))
480 void SC_GEN_LOGF (int package, int category, int priority,
481  const char *fmt, ...)
482  __attribute__ ((format (printf, 4, 5)));
483 void SC_GLOBAL_LOGF (int priority, const char *fmt, ...)
484  __attribute__ ((format (printf, 2, 3)));
485 void SC_LOGF (int priority, const char *fmt, ...)
486  __attribute__ ((format (printf, 2, 3)));
487 #ifndef __cplusplus
488 #define SC_GEN_LOGF(package,category,priority,fmt,...) \
489  ((priority) < SC_LP_THRESHOLD ? (void) 0 : \
490  sc_logf (__FILE__, __LINE__, (package), (category), (priority), \
491  (fmt), __VA_ARGS__))
492 #define SC_GLOBAL_LOGF(p,fmt,...) \
493  SC_GEN_LOGF (sc_package_id, SC_LC_GLOBAL, (p), (fmt), __VA_ARGS__)
494 #define SC_LOGF(p,fmt,...) \
495  SC_GEN_LOGF (sc_package_id, SC_LC_NORMAL, (p), (fmt), __VA_ARGS__)
496 #endif
497 
498 /* convenience global log macros will only output if identifier <= 0 */
499 #define SC_GLOBAL_TRACE(s) SC_GLOBAL_LOG (SC_LP_TRACE, (s))
500 #define SC_GLOBAL_LDEBUG(s) SC_GLOBAL_LOG (SC_LP_DEBUG, (s))
501 #define SC_GLOBAL_VERBOSE(s) SC_GLOBAL_LOG (SC_LP_VERBOSE, (s))
502 #define SC_GLOBAL_INFO(s) SC_GLOBAL_LOG (SC_LP_INFO, (s))
503 #define SC_GLOBAL_STATISTICS(s) SC_GLOBAL_LOG (SC_LP_STATISTICS, (s))
504 #define SC_GLOBAL_PRODUCTION(s) SC_GLOBAL_LOG (SC_LP_PRODUCTION, (s))
505 #define SC_GLOBAL_ESSENTIAL(s) SC_GLOBAL_LOG (SC_LP_ESSENTIAL, (s))
506 #define SC_GLOBAL_LERROR(s) SC_GLOBAL_LOG (SC_LP_ERROR, (s))
507 void SC_GLOBAL_TRACEF (const char *fmt, ...)
508  __attribute__ ((format (printf, 1, 2)));
509 void SC_GLOBAL_LDEBUGF (const char *fmt, ...)
510  __attribute__ ((format (printf, 1, 2)));
511 void SC_GLOBAL_VERBOSEF (const char *fmt, ...)
512  __attribute__ ((format (printf, 1, 2)));
513 void SC_GLOBAL_INFOF (const char *fmt, ...)
514  __attribute__ ((format (printf, 1, 2)));
515 void SC_GLOBAL_STATISTICSF (const char *fmt, ...)
516  __attribute__ ((format (printf, 1, 2)));
517 void SC_GLOBAL_PRODUCTIONF (const char *fmt, ...)
518  __attribute__ ((format (printf, 1, 2)));
519 void SC_GLOBAL_ESSENTIALF (const char *fmt, ...)
520  __attribute__ ((format (printf, 1, 2)));
521 void SC_GLOBAL_LERRORF (const char *fmt, ...)
522  __attribute__ ((format (printf, 1, 2)));
523 #ifndef __cplusplus
524 #define SC_GLOBAL_TRACEF(fmt,...) \
525  SC_GLOBAL_LOGF (SC_LP_TRACE, (fmt), __VA_ARGS__)
526 #define SC_GLOBAL_LDEBUGF(fmt,...) \
527  SC_GLOBAL_LOGF (SC_LP_DEBUG, (fmt), __VA_ARGS__)
528 #define SC_GLOBAL_VERBOSEF(fmt,...) \
529  SC_GLOBAL_LOGF (SC_LP_VERBOSE, (fmt), __VA_ARGS__)
530 #define SC_GLOBAL_INFOF(fmt,...) \
531  SC_GLOBAL_LOGF (SC_LP_INFO, (fmt), __VA_ARGS__)
532 #define SC_GLOBAL_STATISTICSF(fmt,...) \
533  SC_GLOBAL_LOGF (SC_LP_STATISTICS, (fmt), __VA_ARGS__)
534 #define SC_GLOBAL_PRODUCTIONF(fmt,...) \
535  SC_GLOBAL_LOGF (SC_LP_PRODUCTION, (fmt), __VA_ARGS__)
536 #define SC_GLOBAL_ESSENTIALF(fmt,...) \
537  SC_GLOBAL_LOGF (SC_LP_ESSENTIAL, (fmt), __VA_ARGS__)
538 #define SC_GLOBAL_LERRORF(fmt,...) \
539  SC_GLOBAL_LOGF (SC_LP_ERROR, (fmt), __VA_ARGS__)
540 #endif
541 
542 /* convenience log macros that output regardless of identifier */
543 #define SC_TRACE(s) SC_LOG (SC_LP_TRACE, (s))
544 #define SC_LDEBUG(s) SC_LOG (SC_LP_DEBUG, (s))
545 #define SC_VERBOSE(s) SC_LOG (SC_LP_VERBOSE, (s))
546 #define SC_INFO(s) SC_LOG (SC_LP_INFO, (s))
547 #define SC_STATISTICS(s) SC_LOG (SC_LP_STATISTICS, (s))
548 #define SC_PRODUCTION(s) SC_LOG (SC_LP_PRODUCTION, (s))
549 #define SC_ESSENTIAL(s) SC_LOG (SC_LP_ESSENTIAL, (s))
550 #define SC_LERROR(s) SC_LOG (SC_LP_ERROR, (s))
551 void SC_TRACEF (const char *fmt, ...)
552  __attribute__ ((format (printf, 1, 2)));
553 void SC_LDEBUGF (const char *fmt, ...)
554  __attribute__ ((format (printf, 1, 2)));
555 void SC_VERBOSEF (const char *fmt, ...)
556  __attribute__ ((format (printf, 1, 2)));
557 void SC_INFOF (const char *fmt, ...)
558  __attribute__ ((format (printf, 1, 2)));
559 void SC_STATISTICSF (const char *fmt, ...)
560  __attribute__ ((format (printf, 1, 2)));
561 void SC_PRODUCTIONF (const char *fmt, ...)
562  __attribute__ ((format (printf, 1, 2)));
563 void SC_ESSENTIALF (const char *fmt, ...)
564  __attribute__ ((format (printf, 1, 2)));
565 void SC_LERRORF (const char *fmt, ...)
566  __attribute__ ((format (printf, 1, 2)));
567 #ifndef __cplusplus
568 #define SC_TRACEF(fmt,...) \
569  SC_LOGF (SC_LP_TRACE, (fmt), __VA_ARGS__)
570 #define SC_LDEBUGF(fmt,...) \
571  SC_LOGF (SC_LP_DEBUG, (fmt), __VA_ARGS__)
572 #define SC_VERBOSEF(fmt,...) \
573  SC_LOGF (SC_LP_VERBOSE, (fmt), __VA_ARGS__)
574 #define SC_INFOF(fmt,...) \
575  SC_LOGF (SC_LP_INFO, (fmt), __VA_ARGS__)
576 #define SC_STATISTICSF(fmt,...) \
577  SC_LOGF (SC_LP_STATISTICS, (fmt), __VA_ARGS__)
578 #define SC_PRODUCTIONF(fmt,...) \
579  SC_LOGF (SC_LP_PRODUCTION, (fmt), __VA_ARGS__)
580 #define SC_ESSENTIALF(fmt,...) \
581  SC_LOGF (SC_LP_ESSENTIAL, (fmt), __VA_ARGS__)
582 #define SC_LERRORF(fmt,...) \
583  SC_LOGF (SC_LP_ERROR, (fmt), __VA_ARGS__)
584 #endif
585 
588 #define _SC_TOSTRING(x) #x
589 
592 #define SC_TOSTRING(x) _SC_TOSTRING(x)
593 
595 typedef void (*sc_log_handler_t) (FILE * log_stream,
596  const char *filename, int lineno,
597  int package, int category,
598  int priority, const char *msg);
599 
601 typedef void (*sc_abort_handler_t) (void);
602 
603 /* memory allocation functions, will abort if out of memory */
604 
605 void *sc_malloc (int package, size_t size);
606 void *sc_calloc (int package, size_t nmemb, size_t size);
607 void *sc_realloc (int package, void *ptr, size_t size);
608 char *sc_strdup (int package, const char *s);
609 void sc_free (int package, void *ptr);
610 int sc_memory_status (int package);
611 void sc_memory_check (int package);
612 
614 int sc_memory_check_noerr (int package);
615 
616 /* comparison functions for various integer sizes */
617 
618 int sc_int_compare (const void *v1, const void *v2);
619 int sc_int8_compare (const void *v1, const void *v2);
620 int sc_int16_compare (const void *v1, const void *v2);
621 int sc_int32_compare (const void *v1, const void *v2);
622 int sc_int64_compare (const void *v1, const void *v2);
623 int sc_double_compare (const void *v1, const void *v2);
624 
630 int sc_atoi (const char *nptr);
631 
637 long sc_atol (const char *nptr);
638 
645 void sc_set_log_defaults (FILE * log_stream,
646  sc_log_handler_t log_handler,
647  int log_threshold);
648 
654 
664 void sc_log (const char *filename, int lineno,
665  int package, int category, int priority,
666  const char *msg);
667 
677 void sc_logf (const char *filename, int lineno,
678  int package, int category, int priority,
679  const char *fmt, ...)
680  __attribute__ ((format (printf, 6, 7)));
681 
692 void sc_logv (const char *filename, int lineno,
693  int package, int category, int priority,
694  const char *fmt, va_list ap);
695 
697 void sc_log_indent_push_count (int package, int count);
698 
700 void sc_log_indent_pop_count (int package, int count);
701 
703 void sc_log_indent_push (void);
704 
706 void sc_log_indent_pop (void);
707 
709 void sc_abort (void)
710  __attribute__ ((noreturn));
711 
713 void sc_abort_verbose (const char *filename, int lineno,
714  const char *msg)
715  __attribute__ ((noreturn));
716 
718 void sc_abort_verbosef (const char *filename, int lineno,
719  const char *fmt, ...)
720  __attribute__ ((format (printf, 3, 4)))
721  __attribute__ ((noreturn));
722 
724 void sc_abort_verbosev (const char *filename, int lineno,
725  const char *fmt, va_list ap)
726  __attribute__ ((noreturn));
727 
729 void sc_abort_collective (const char *msg)
730  __attribute__ ((noreturn));
731 
738  int log_threshold,
739  const char *name, const char *full);
740 
746 int sc_package_is_registered (int package_id);
747 
757 void sc_package_lock (int package_id);
758 
768 void sc_package_unlock (int package_id);
769 
776 void sc_package_set_verbosity (int package_id,
777  int log_priority);
778 
788  int set_abort);
789 
793 void sc_package_unregister (int package_id);
794 
799 void sc_package_print_summary (int log_priority);
800 
816 void sc_init (sc_MPI_Comm mpicomm,
817  int catch_signals, int print_backtrace,
818  sc_log_handler_t log_handler, int log_threshold);
819 
826 int sc_is_initialized (void);
827 
834 int sc_get_package_id (void);
835 
844 void sc_finalize (void);
845 
855 
862 int sc_is_root (void);
863 
875 void sc_strcopy (char *dest, size_t size, const char *src);
876 
890 void sc_snprintf (char *str, size_t size,
891  const char *format, ...)
892  __attribute__ ((format (printf, 3, 4)));
893 
902 const char *sc_version (void);
903 
908 int sc_version_major (void);
909 
914 int sc_version_minor (void);
915 
920 
921 #if 0
922 /* Sadly, the point version macro by autoconf doesn't work with vX and vX.Y.
923  The remaining option is to use sc_version and parse its return string. */
930 int sc_version_point (void);
931 #endif /* 0 */
932 
938 int sc_have_zlib (void);
939 
943 int sc_have_json (void);
944 
949 void sc_sleep (unsigned milliseconds);
950 
951 SC_EXTERN_C_END;
952 
953 #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.
SC_DLL_PUBLIC FILE * sc_trace_file
Optional trace file for logging (see sc_init).
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_logv(const char *filename, int lineno, int package, int category, int priority, const char *fmt, va_list ap)
The vprintf-style log function to be called by all packages.
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_is_littleendian(void)
Perform a runtime check for the integer endian convention.
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:601
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.
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 ().
const char * sc_version(void)
Return the full version of libsc.
SC_DLL_PUBLIC int sc_package_id
libsc allows for multiple packages to use their own log priorities etc.
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.
SC_DLL_PUBLIC int sc_trace_prio
Optional minimum log priority for messages that go into the trace file.
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_sleep(unsigned milliseconds)
Portable function to sleep a prescribed amount of milliseconds.
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.
void(* sc_log_handler_t)(FILE *log_stream, const char *filename, int lineno, int package, int category, int priority, const char *msg)
Type of the log handler function.
Definition: sc.h:595
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_logf(const char *filename, int lineno, int package, int category, int priority, const char *fmt,...)
The printf-style log function to be called by all packages.
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:450