19 #ifndef __STARPU_UTIL_H__ 20 #define __STARPU_UTIL_H__ 34 #ifdef STARPU_SIMGRID_MC 35 #include <simgrid/modelchecker.h> 54 #if defined __GNUC__ && defined __GNUC_MINOR__ 55 # define STARPU_GNUC_PREREQ(maj, min) \ 56 ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min)) 58 # define STARPU_GNUC_PREREQ(maj, min) 0 66 # define STARPU_UNLIKELY(expr) (__builtin_expect(!!(expr),0)) 68 # define STARPU_UNLIKELY(expr) (expr) 76 # define STARPU_LIKELY(expr) (__builtin_expect(!!(expr),1)) 78 # define STARPU_LIKELY(expr) (expr) 85 # define STARPU_ATTRIBUTE_UNUSED __attribute__((unused)) 87 # define STARPU_ATTRIBUTE_UNUSED 94 # define STARPU_ATTRIBUTE_NORETURN __attribute__((noreturn)) 96 # define STARPU_ATTRIBUTE_NORETURN 103 # define STARPU_ATTRIBUTE_INTERNAL __attribute__ ((visibility ("internal"))) 105 # define STARPU_ATTRIBUTE_INTERNAL 112 # define STARPU_ATTRIBUTE_MALLOC __attribute__((malloc)) 114 # define STARPU_ATTRIBUTE_MALLOC 121 # define STARPU_ATTRIBUTE_WARN_UNUSED_RESULT __attribute__((warn_unused_result)) 123 # define STARPU_ATTRIBUTE_WARN_UNUSED_RESULT 130 # define STARPU_ATTRIBUTE_PURE __attribute__((pure)) 132 # define STARPU_ATTRIBUTE_PURE 139 # define STARPU_ATTRIBUTE_ALIGNED(size) __attribute__((aligned(size))) 141 # define STARPU_ATTRIBUTE_ALIGNED(size) 145 # define STARPU_ATTRIBUTE_FORMAT(type, string, first) __attribute__((format(type, string, first))) 147 # define STARPU_ATTRIBUTE_FORMAT(type, string, first) 152 #if defined(c_plusplus) || defined(__cplusplus) 153 # define STARPU_INLINE inline 154 #elif defined(_MSC_VER) || defined(__HP_cc) 155 # define STARPU_INLINE __inline 157 # define STARPU_INLINE __inline__ 160 #if STARPU_GNUC_PREREQ(4, 3) 161 # define STARPU_ATTRIBUTE_CALLOC_SIZE(num,size) __attribute__((alloc_size(num,size))) 162 # define STARPU_ATTRIBUTE_ALLOC_SIZE(size) __attribute__((alloc_size(size))) 164 # define STARPU_ATTRIBUTE_CALLOC_SIZE(num,size) 165 # define STARPU_ATTRIBUTE_ALLOC_SIZE(size) 168 #if STARPU_GNUC_PREREQ(3, 1) && !defined(BUILDING_STARPU) && !defined(STARPU_USE_DEPRECATED_API) && !defined(STARPU_USE_DEPRECATED_ONE_ZERO_API) 169 #define STARPU_DEPRECATED __attribute__((__deprecated__)) 171 #define STARPU_DEPRECATED 174 #if STARPU_GNUC_PREREQ(3,3) 175 #define STARPU_WARN_UNUSED_RESULT __attribute__((__warn_unused_result__)) 177 #define STARPU_WARN_UNUSED_RESULT 180 #define STARPU_BACKTRACE_LENGTH 32 182 # define STARPU_DUMP_BACKTRACE() do { \ 183 void *__ptrs[STARPU_BACKTRACE_LENGTH]; \ 184 int __n = backtrace(__ptrs, STARPU_BACKTRACE_LENGTH); \ 185 backtrace_symbols_fd(__ptrs, __n, 2); \ 188 # define STARPU_DUMP_BACKTRACE() do { } while (0) 191 #ifdef STARPU_SIMGRID_MC 192 #define STARPU_SIMGRID_ASSERT(x) MC_assert(!!(x)) 194 #define STARPU_SIMGRID_ASSERT(x) 201 #ifdef STARPU_NO_ASSERT 202 #define STARPU_ASSERT(x) do { if (0) { (void) (x); } } while(0) 204 # if defined(__CUDACC__) || defined(STARPU_HAVE_WINDOWS) 205 # define STARPU_ASSERT(x) do { if (STARPU_UNLIKELY(!(x))) { STARPU_DUMP_BACKTRACE(); STARPU_SIMGRID_ASSERT(x); *(int*)NULL = 0; } } while(0) 207 # define STARPU_ASSERT(x) do { if (STARPU_UNLIKELY(!(x))) { STARPU_DUMP_BACKTRACE(); STARPU_SIMGRID_ASSERT(x); assert(x); } } while (0) 211 #ifdef STARPU_NO_ASSERT 212 #define STARPU_ASSERT_ACCESSIBLE(x) do { if (0) { (void) (x); } } while(0) 214 #define STARPU_ASSERT_ACCESSIBLE(ptr) do { volatile char __c STARPU_ATTRIBUTE_UNUSED = *(char*) (ptr); } while(0) 222 #ifdef STARPU_NO_ASSERT 223 #define STARPU_ASSERT_MSG(x, msg, ...) do { if (0) { (void) (x); (void) msg; } } while(0) 225 # if defined(__CUDACC__) || defined(STARPU_HAVE_WINDOWS) 226 # define STARPU_ASSERT_MSG(x, msg, ...) do { if (STARPU_UNLIKELY(!(x))) { STARPU_DUMP_BACKTRACE(); fprintf(stderr, "\n[starpu][%s][assert failure] " msg "\n\n", __starpu_func__, ## __VA_ARGS__); STARPU_SIMGRID_ASSERT(x); *(int*)NULL = 0; }} while(0) 228 # define STARPU_ASSERT_MSG(x, msg, ...) do { if (STARPU_UNLIKELY(!(x))) { STARPU_DUMP_BACKTRACE(); fprintf(stderr, "\n[starpu][%s][assert failure] " msg "\n\n", __starpu_func__, ## __VA_ARGS__); STARPU_SIMGRID_ASSERT(x); assert(x); } } while(0) 233 # ifdef __clang_analyzer__ 234 # define _starpu_abort() exit(42) 236 # define _starpu_abort() *(volatile int*)NULL = 0 239 # define _starpu_abort() abort() 245 #define STARPU_ABORT() do { \ 246 STARPU_DUMP_BACKTRACE(); \ 247 fprintf(stderr, "[starpu][abort][%s()@%s:%d]\n", __starpu_func__, __FILE__, __LINE__); \ 256 #define STARPU_ABORT_MSG(msg, ...) do { \ 257 STARPU_DUMP_BACKTRACE(); \ 258 fprintf(stderr, "[starpu][abort][%s()@%s:%d] " msg "\n", __starpu_func__, __FILE__, __LINE__, ## __VA_ARGS__); \ 262 #if defined(STARPU_HAVE_STRERROR_R) 263 #if (! defined(__GLIBC__) || !__GLIBC__) || ((_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && (! defined(_GNU_SOURCE))) 265 # define starpu_strerror_r(errnum, buf, buflen) \ 268 int _ret = strerror_r((errnum), (buf), (buflen)); \ 269 STARPU_ASSERT(_ret == 0); \ 274 # define starpu_strerror_r(errnum, buf, buflen) \ 277 char * const _user_buf = (buf); \ 278 const size_t _user_buflen = (buflen); \ 280 STARPU_ASSERT((buf) != NULL); \ 281 char * _tmp_buf = strerror_r((errnum), _user_buf, _user_buflen); \ 282 if (_tmp_buf != _user_buf) \ 284 if (_user_buflen > 0) \ 286 strncpy(_user_buf, _tmp_buf, _user_buflen-1); \ 287 _user_buf[_user_buflen-1] = '\0'; \ 299 #if defined(STARPU_HAVE_STRERROR_R) 300 # define STARPU_CHECK_RETURN_VALUE(err, message, ...) {if (STARPU_UNLIKELY(err != 0)) { \ 301 char xmessage[256]; starpu_strerror_r(-err, xmessage, 256); \ 302 fprintf(stderr, "[starpu] Unexpected value: <%d:%s> returned for " message "\n", err, xmessage, ## __VA_ARGS__); \ 305 # define STARPU_CHECK_RETURN_VALUE(err, message, ...) {if (STARPU_UNLIKELY(err != 0)) { \ 306 fprintf(stderr, "[starpu] Unexpected value: <%d> returned for " message "\n", err, ## __VA_ARGS__); \ 314 #if defined(STARPU_HAVE_STRERROR_R) 315 # define STARPU_CHECK_RETURN_VALUE_IS(err, value, message, ...) {if (STARPU_UNLIKELY(err != value)) { \ 316 char xmessage[256]; starpu_strerror_r(-err, xmessage, 256); \ 317 fprintf(stderr, "[starpu] Unexpected value: <%d!=%d:%s> returned for " message "\n", err, value, xmessage, ## __VA_ARGS__); \ 320 # define STARPU_CHECK_RETURN_VALUE_IS(err, value, message, ...) {if (STARPU_UNLIKELY(err != value)) { \ 321 fprintf(stderr, "[starpu] Unexpected value: <%d != %d> returned for " message "\n", err, value, ## __VA_ARGS__); \ 325 #if defined(__i386__) || defined(__x86_64__) 327 static __starpu_inline
unsigned starpu_cmpxchg(
unsigned *ptr,
unsigned old,
unsigned next)
329 __asm__ __volatile__(
"lock cmpxchgl %2,%1":
"+a" (old),
"+m" (*ptr) :
"q" (next) :
"memory");
332 static __starpu_inline
unsigned starpu_xchg(
unsigned *ptr,
unsigned next)
335 __asm__ __volatile__(
"xchgl %1,%0":
"+m" (*ptr),
"+q" (next) : :
"memory");
338 #define STARPU_HAVE_XCHG 340 #if defined(__i386__) 341 static __starpu_inline
unsigned long starpu_cmpxchgl(
unsigned long *ptr,
unsigned long old,
unsigned long next)
343 __asm__ __volatile__(
"lock cmpxchgl %2,%1":
"+a" (old),
"+m" (*ptr) :
"q" (next) :
"memory");
346 static __starpu_inline
unsigned long starpu_xchgl(
unsigned long *ptr,
unsigned long next)
349 __asm__ __volatile__(
"xchgl %1,%0":
"+m" (*ptr),
"+q" (next) : :
"memory");
352 #define STARPU_HAVE_XCHGL 355 #if defined(__x86_64__) 356 static __starpu_inline
unsigned long starpu_cmpxchgl(
unsigned long *ptr,
unsigned long old,
unsigned long next)
358 __asm__ __volatile__(
"lock cmpxchgq %2,%1":
"+a" (old),
"+m" (*ptr) :
"q" (next) :
"memory");
361 static __starpu_inline
unsigned long starpu_xchgl(
unsigned long *ptr,
unsigned long next)
364 __asm__ __volatile__(
"xchgq %1,%0":
"+m" (*ptr),
"+q" (next) : :
"memory");
367 #define STARPU_HAVE_XCHGL 372 #define STARPU_ATOMIC_SOMETHING(name,expr) \ 373 static __starpu_inline unsigned starpu_atomic_##name(unsigned *ptr, unsigned value) \ 375 unsigned old, next; \ 380 if (starpu_cmpxchg(ptr, old, next) == old) \ 385 #define STARPU_ATOMIC_SOMETHINGL(name,expr) \ 386 static __starpu_inline unsigned long starpu_atomic_##name##l(unsigned long *ptr, unsigned long value) \ 388 unsigned long old, next; \ 393 if (starpu_cmpxchgl(ptr, old, next) == old) \ 400 #ifdef STARPU_HAVE_SYNC_FETCH_AND_ADD 401 #define STARPU_ATOMIC_ADD(ptr, value) (__sync_fetch_and_add ((ptr), (value)) + (value)) 402 #define STARPU_ATOMIC_ADDL(ptr, value) (__sync_fetch_and_add ((ptr), (value)) + (value)) 404 #if defined(STARPU_HAVE_XCHG) 405 STARPU_ATOMIC_SOMETHING(add, old + value)
406 #define STARPU_ATOMIC_ADD(ptr, value) starpu_atomic_add(ptr, value) 408 #if defined(STARPU_HAVE_XCHGL) 409 STARPU_ATOMIC_SOMETHINGL(add, old + value)
410 #define STARPU_ATOMIC_ADDL(ptr, value) starpu_atomic_addl(ptr, value) 414 #ifdef STARPU_HAVE_SYNC_FETCH_AND_OR 415 #define STARPU_ATOMIC_OR(ptr, value) (__sync_fetch_and_or ((ptr), (value))) 416 #define STARPU_ATOMIC_ORL(ptr, value) (__sync_fetch_and_or ((ptr), (value))) 418 #if defined(STARPU_HAVE_XCHG) 419 STARPU_ATOMIC_SOMETHING(or, old | value)
420 #define STARPU_ATOMIC_OR(ptr, value) starpu_atomic_or(ptr, value) 422 #if defined(STARPU_HAVE_XCHGL) 423 STARPU_ATOMIC_SOMETHINGL(or, old | value)
424 #define STARPU_ATOMIC_ORL(ptr, value) starpu_atomic_orl(ptr, value) 428 #ifdef STARPU_HAVE_SYNC_BOOL_COMPARE_AND_SWAP 429 #define STARPU_BOOL_COMPARE_AND_SWAP(ptr, old, value) (__sync_bool_compare_and_swap ((ptr), (old), (value))) 430 #elif defined(STARPU_HAVE_XCHG) 431 #define STARPU_BOOL_COMPARE_AND_SWAP(ptr, old, value) (starpu_cmpxchg((ptr), (old), (value)) == (old)) 434 #ifdef STARPU_HAVE_SYNC_VAL_COMPARE_AND_SWAP 435 #define STARPU_VAL_COMPARE_AND_SWAP(ptr, old, value) (__sync_val_compare_and_swap ((ptr), (old), (value))) 436 #elif defined(STARPU_HAVE_XCHG) 437 #define STARPU_VAL_COMPARE_AND_SWAP(ptr, old, value) (starpu_cmpxchg((ptr), (old), (value))) 441 #ifdef STARPU_HAVE_SYNC_LOCK_TEST_AND_SET 442 #define STARPU_TEST_AND_SET(ptr, value) (__sync_lock_test_and_set ((ptr), (value))) 443 #define STARPU_RELEASE(ptr) (__sync_lock_release ((ptr))) 444 #elif defined(STARPU_HAVE_XCHG) 445 #define STARPU_TEST_AND_SET(ptr, value) (starpu_xchg((ptr), (value))) 446 #define STARPU_RELEASE(ptr) (starpu_xchg((ptr), 0)) 449 #ifdef STARPU_HAVE_SYNC_SYNCHRONIZE 450 #define STARPU_SYNCHRONIZE() __sync_synchronize() 451 #elif defined(__i386__) 452 #define STARPU_SYNCHRONIZE() __asm__ __volatile__("lock; addl $0,0(%%esp)" ::: "memory") 453 #elif defined(__KNC__) || defined(__KNF__) 454 #define STARPU_SYNCHRONIZE() __asm__ __volatile__("lock; addl $0,0(%%rsp)" ::: "memory") 455 #elif defined(__x86_64__) 456 #define STARPU_SYNCHRONIZE() __asm__ __volatile__("mfence" ::: "memory") 457 #elif defined(__ppc__) || defined(__ppc64__) 458 #define STARPU_SYNCHRONIZE() __asm__ __volatile__("sync" ::: "memory") 464 #if defined(__i386__) 465 #define STARPU_RMB() __asm__ __volatile__("lock; addl $0,0(%%esp)" ::: "memory") 466 #elif defined(__KNC__) || defined(__KNF__) 467 #define STARPU_RMB() __asm__ __volatile__("lock; addl $0,0(%%rsp)" ::: "memory") 468 #elif defined(__x86_64__) 469 #define STARPU_RMB() __asm__ __volatile__("lfence" ::: "memory") 470 #elif defined(__ppc__) || defined(__ppc64__) 471 #define STARPU_RMB() __asm__ __volatile__("sync" ::: "memory") 473 #define STARPU_RMB() STARPU_SYNCHRONIZE() 479 #if defined(__i386__) 480 #define STARPU_WMB() __asm__ __volatile__("lock; addl $0,0(%%esp)" ::: "memory") 481 #elif defined(__KNC__) || defined(__KNF__) 482 #define STARPU_WMB() __asm__ __volatile__("lock; addl $0,0(%%rsp)" ::: "memory") 483 #elif defined(__x86_64__) 484 #define STARPU_WMB() __asm__ __volatile__("sfence" ::: "memory") 485 #elif defined(__ppc__) || defined(__ppc64__) 486 #define STARPU_WMB() __asm__ __volatile__("sync" ::: "memory") 488 #define STARPU_WMB() STARPU_SYNCHRONIZE() 493 #include <sys/types.h> 494 #include <sys/stat.h> 499 #if !defined(_MSC_VER) || defined(BUILDING_STARPU) 502 #if !defined(STARPU_HAVE_STRUCT_TIMESPEC) || (defined(_MSC_VER) && _MSC_VER < 1900) 504 #ifndef STARPU_TIMESPEC_DEFINED 505 #define STARPU_TIMESPEC_DEFINED 1 514 #if defined(__MINGW32__) || defined(__CYGWIN__) 515 #include <sys/time.h> 518 #include <sys/time.h>