StarPU Handbook
starpu_util.h
Go to the documentation of this file.
1 /* StarPU --- Runtime system for heterogeneous multicore architectures.
2  *
3  * Copyright (C) 2008-2019 Université de Bordeaux
4  * Copyright (C) 2011,2012,2017 Inria
5  * Copyright (C) 2010-2017, 2019 CNRS
6  *
7  * StarPU is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU Lesser General Public License as published by
9  * the Free Software Foundation; either version 2.1 of the License, or (at
10  * your option) any later version.
11  *
12  * StarPU is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15  *
16  * See the GNU Lesser General Public License in COPYING.LGPL for more details.
17  */
18 
19 #ifndef __STARPU_UTIL_H__
20 #define __STARPU_UTIL_H__
21 
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <stdint.h>
25 #include <string.h>
26 #include <assert.h>
27 
28 #include <starpu_config.h>
29 
30 #ifdef __GLIBC__
31 #include <execinfo.h>
32 #endif
33 
34 #ifdef STARPU_SIMGRID_MC
35 #include <simgrid/modelchecker.h>
36 #endif
37 
38 #ifdef __cplusplus
39 extern "C"
40 {
41 #endif
42 
54 #if defined __GNUC__ && defined __GNUC_MINOR__
55 # define STARPU_GNUC_PREREQ(maj, min) \
56  ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
57 #else
58 # define STARPU_GNUC_PREREQ(maj, min) 0
59 #endif
60 
65 #ifdef __GNUC__
66 # define STARPU_UNLIKELY(expr) (__builtin_expect(!!(expr),0))
67 #else
68 # define STARPU_UNLIKELY(expr) (expr)
69 #endif
70 
75 #ifdef __GNUC__
76 # define STARPU_LIKELY(expr) (__builtin_expect(!!(expr),1))
77 #else
78 # define STARPU_LIKELY(expr) (expr)
79 #endif
80 
84 #ifdef __GNUC__
85 # define STARPU_ATTRIBUTE_UNUSED __attribute__((unused))
86 #else
87 # define STARPU_ATTRIBUTE_UNUSED
88 #endif
89 
93 #ifdef __GNUC__
94 # define STARPU_ATTRIBUTE_NORETURN __attribute__((noreturn))
95 #else
96 # define STARPU_ATTRIBUTE_NORETURN
97 #endif
98 
102 #ifdef __GNUC__
103 # define STARPU_ATTRIBUTE_INTERNAL __attribute__ ((visibility ("internal")))
104 #else
105 # define STARPU_ATTRIBUTE_INTERNAL
106 #endif
107 
111 #ifdef __GNUC__
112 # define STARPU_ATTRIBUTE_MALLOC __attribute__((malloc))
113 #else
114 # define STARPU_ATTRIBUTE_MALLOC
115 #endif
116 
120 #ifdef __GNUC__
121 # define STARPU_ATTRIBUTE_WARN_UNUSED_RESULT __attribute__((warn_unused_result))
122 #else
123 # define STARPU_ATTRIBUTE_WARN_UNUSED_RESULT
124 #endif
125 
129 #ifdef __GNUC__
130 # define STARPU_ATTRIBUTE_PURE __attribute__((pure))
131 #else
132 # define STARPU_ATTRIBUTE_PURE
133 #endif
134 
138 #ifdef __GNUC__
139 # define STARPU_ATTRIBUTE_ALIGNED(size) __attribute__((aligned(size)))
140 #else
141 # define STARPU_ATTRIBUTE_ALIGNED(size)
142 #endif
143 
144 #ifdef __GNUC__
145 # define STARPU_ATTRIBUTE_FORMAT(type, string, first) __attribute__((format(type, string, first)))
146 #else
147 # define STARPU_ATTRIBUTE_FORMAT(type, string, first)
148 #endif
149 
150 /* Note that if we're compiling C++, then just use the "inline"
151  keyword, since it's part of C++ */
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
156 #else
157 # define STARPU_INLINE __inline__
158 #endif
159 
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)))
163 #else
164 # define STARPU_ATTRIBUTE_CALLOC_SIZE(num,size)
165 # define STARPU_ATTRIBUTE_ALLOC_SIZE(size)
166 #endif
167 
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__))
170 #else
171 #define STARPU_DEPRECATED
172 #endif /* __GNUC__ */
173 
174 #if STARPU_GNUC_PREREQ(3,3)
175 #define STARPU_WARN_UNUSED_RESULT __attribute__((__warn_unused_result__))
176 #else
177 #define STARPU_WARN_UNUSED_RESULT
178 #endif /* __GNUC__ */
179 
180 #define STARPU_BACKTRACE_LENGTH 32
181 #ifdef __GLIBC__
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); \
186 } while (0)
187 #else
188 # define STARPU_DUMP_BACKTRACE() do { } while (0)
189 #endif
190 
191 #ifdef STARPU_SIMGRID_MC
192 #define STARPU_SIMGRID_ASSERT(x) MC_assert(!!(x))
193 #else
194 #define STARPU_SIMGRID_ASSERT(x)
195 #endif
196 
201 #ifdef STARPU_NO_ASSERT
202 #define STARPU_ASSERT(x) do { if (0) { (void) (x); } } while(0)
203 #else
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)
206 # else
207 # define STARPU_ASSERT(x) do { if (STARPU_UNLIKELY(!(x))) { STARPU_DUMP_BACKTRACE(); STARPU_SIMGRID_ASSERT(x); assert(x); } } while (0)
208 # endif
209 #endif
210 
211 #ifdef STARPU_NO_ASSERT
212 #define STARPU_ASSERT_ACCESSIBLE(x) do { if (0) { (void) (x); } } while(0)
213 #else
214 #define STARPU_ASSERT_ACCESSIBLE(ptr) do { volatile char __c STARPU_ATTRIBUTE_UNUSED = *(char*) (ptr); } while(0)
215 #endif
216 
222 #ifdef STARPU_NO_ASSERT
223 #define STARPU_ASSERT_MSG(x, msg, ...) do { if (0) { (void) (x); (void) msg; } } while(0)
224 #else
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)
227 # else
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)
229 # endif
230 #endif
231 
232 #ifdef __APPLE_CC__
233 # ifdef __clang_analyzer__
234 # define _starpu_abort() exit(42)
235 # else
236 # define _starpu_abort() *(volatile int*)NULL = 0
237 # endif
238 #else
239 # define _starpu_abort() abort()
240 #endif
241 
245 #define STARPU_ABORT() do { \
246  STARPU_DUMP_BACKTRACE(); \
247  fprintf(stderr, "[starpu][abort][%s()@%s:%d]\n", __starpu_func__, __FILE__, __LINE__); \
248  _starpu_abort(); \
249 } while(0)
250 
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__); \
259  _starpu_abort(); \
260 } while(0)
261 
262 #if defined(STARPU_HAVE_STRERROR_R)
263 #if (! defined(__GLIBC__) || !__GLIBC__) || ((_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && (! defined(_GNU_SOURCE)))
264 /* XSI-compliant version of strerror_r returns an int */
265 # define starpu_strerror_r(errnum, buf, buflen) \
266  do \
267  { \
268  int _ret = strerror_r((errnum), (buf), (buflen)); \
269  STARPU_ASSERT(_ret == 0); \
270  } \
271  while (0)
272 #else
273 /* GNU-specific version of strerror_r returns a char * */
274 # define starpu_strerror_r(errnum, buf, buflen) \
275  do \
276  { \
277  char * const _user_buf = (buf); \
278  const size_t _user_buflen = (buflen); \
279  /* the GNU-specific behaviour when 'buf' == NULL cannot be emulated with the XSI-compliant version */ \
280  STARPU_ASSERT((buf) != NULL); \
281  char * _tmp_buf = strerror_r((errnum), _user_buf, _user_buflen); \
282  if (_tmp_buf != _user_buf) \
283  { \
284  if (_user_buflen > 0) \
285  { \
286  strncpy(_user_buf, _tmp_buf, _user_buflen-1); \
287  _user_buf[_user_buflen-1] = '\0'; \
288  } \
289  } \
290  } \
291  while (0)
292 #endif /* strerror_r ABI version */
293 #endif /* STARPU_HAVE_STRERROR_R */
294 
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__); \
303  STARPU_ABORT(); }}
304 #else
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__); \
307  STARPU_ABORT(); }}
308 #endif
309 
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__); \
318  STARPU_ABORT(); }}
319 #else
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__); \
322  STARPU_ABORT(); }}
323 #endif
324 
325 #if defined(__i386__) || defined(__x86_64__)
326 
327 static __starpu_inline unsigned starpu_cmpxchg(unsigned *ptr, unsigned old, unsigned next)
328 {
329  __asm__ __volatile__("lock cmpxchgl %2,%1": "+a" (old), "+m" (*ptr) : "q" (next) : "memory");
330  return old;
331 }
332 static __starpu_inline unsigned starpu_xchg(unsigned *ptr, unsigned next)
333 {
334  /* Note: xchg is always locked already */
335  __asm__ __volatile__("xchgl %1,%0": "+m" (*ptr), "+q" (next) : : "memory");
336  return next;
337 }
338 #define STARPU_HAVE_XCHG
339 
340 #if defined(__i386__)
341 static __starpu_inline unsigned long starpu_cmpxchgl(unsigned long *ptr, unsigned long old, unsigned long next)
342 {
343  __asm__ __volatile__("lock cmpxchgl %2,%1": "+a" (old), "+m" (*ptr) : "q" (next) : "memory");
344  return old;
345 }
346 static __starpu_inline unsigned long starpu_xchgl(unsigned long *ptr, unsigned long next)
347 {
348  /* Note: xchg is always locked already */
349  __asm__ __volatile__("xchgl %1,%0": "+m" (*ptr), "+q" (next) : : "memory");
350  return next;
351 }
352 #define STARPU_HAVE_XCHGL
353 #endif
354 
355 #if defined(__x86_64__)
356 static __starpu_inline unsigned long starpu_cmpxchgl(unsigned long *ptr, unsigned long old, unsigned long next)
357 {
358  __asm__ __volatile__("lock cmpxchgq %2,%1": "+a" (old), "+m" (*ptr) : "q" (next) : "memory");
359  return old;
360 }
361 static __starpu_inline unsigned long starpu_xchgl(unsigned long *ptr, unsigned long next)
362 {
363  /* Note: xchg is always locked already */
364  __asm__ __volatile__("xchgq %1,%0": "+m" (*ptr), "+q" (next) : : "memory");
365  return next;
366 }
367 #define STARPU_HAVE_XCHGL
368 #endif
369 
370 #endif
371 
372 #define STARPU_ATOMIC_SOMETHING(name,expr) \
373 static __starpu_inline unsigned starpu_atomic_##name(unsigned *ptr, unsigned value) \
374 { \
375  unsigned old, next; \
376  while (1) \
377  { \
378  old = *ptr; \
379  next = expr; \
380  if (starpu_cmpxchg(ptr, old, next) == old) \
381  break; \
382  }; \
383  return expr; \
384 }
385 #define STARPU_ATOMIC_SOMETHINGL(name,expr) \
386 static __starpu_inline unsigned long starpu_atomic_##name##l(unsigned long *ptr, unsigned long value) \
387 { \
388  unsigned long old, next; \
389  while (1) \
390  { \
391  old = *ptr; \
392  next = expr; \
393  if (starpu_cmpxchgl(ptr, old, next) == old) \
394  break; \
395  }; \
396  return expr; \
397 }
398 
399 /* Returns the new value */
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))
403 #else
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)
407 #endif
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)
411 #endif
412 #endif
413 
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)))
417 #else
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)
421 #endif
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)
425 #endif
426 #endif
427 
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))
432 #endif
433 
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)))
438 #endif
439 
440 /* Returns the previous 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))
447 #endif
448 
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")
459 #endif
460 
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")
472 #else
473 #define STARPU_RMB() STARPU_SYNCHRONIZE()
474 #endif
475 
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")
487 #else
488 #define STARPU_WMB() STARPU_SYNCHRONIZE()
489 #endif
490 
491 #ifdef _WIN32
492 /* Try to fetch the system definition of timespec */
493 #include <sys/types.h>
494 #include <sys/stat.h>
495 #ifdef HAVE_UNISTD_H
496 #include <unistd.h>
497 #endif
498 #include <time.h>
499 #if !defined(_MSC_VER) || defined(BUILDING_STARPU)
500 #include <pthread.h>
501 #endif
502 #if !defined(STARPU_HAVE_STRUCT_TIMESPEC) || (defined(_MSC_VER) && _MSC_VER < 1900)
503 /* If it didn't get defined in the standard places, then define it ourself */
504 #ifndef STARPU_TIMESPEC_DEFINED
505 #define STARPU_TIMESPEC_DEFINED 1
506 struct timespec
507 {
508  time_t tv_sec; /* Seconds */
509  long tv_nsec; /* Nanoseconds */
510 };
511 #endif /* STARPU_TIMESPEC_DEFINED */
512 #endif /* STARPU_HAVE_STRUCT_TIMESPEC */
513 /* Fetch gettimeofday on mingw/cygwin */
514 #if defined(__MINGW32__) || defined(__CYGWIN__)
515 #include <sys/time.h>
516 #endif
517 #else
518 #include <sys/time.h>
519 #endif /* _WIN32 */
520 
523 #ifdef __cplusplus
524 }
525 #endif
526 
527 #endif /* __STARPU_UTIL_H__ */