18 #ifndef __STARPU_UTIL_H__
19 #define __STARPU_UTIL_H__
33 #if defined __GNUC__ && defined __GNUC_MINOR__
34 # define STARPU_GNUC_PREREQ(maj, min) \
35 ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
37 # define STARPU_GNUC_PREREQ(maj, min) 0
41 # define STARPU_UNLIKELY(expr) (__builtin_expect(!!(expr),0))
42 # define STARPU_LIKELY(expr) (__builtin_expect(!!(expr),1))
43 # define STARPU_ATTRIBUTE_UNUSED __attribute__((unused))
44 # define STARPU_ATTRIBUTE_INTERNAL __attribute__ ((visibility ("internal")))
45 # define STARPU_ATTRIBUTE_MALLOC __attribute__((malloc))
46 # define STARPU_ATTRIBUTE_WARN_UNUSED_RESULT __attribute__((warn_unused_result))
47 # define STARPU_ATTRIBUTE_PURE __attribute__((pure))
48 # define STARPU_ATTRIBUTE_ALIGNED(size) __attribute__((aligned(size)))
50 # define STARPU_UNLIKELY(expr) (expr)
51 # define STARPU_LIKELY(expr) (expr)
52 # define STARPU_ATTRIBUTE_UNUSED
53 # define STARPU_ATTRIBUTE_INTERNAL
54 # define STARPU_ATTRIBUTE_MALLOC
55 # define STARPU_ATTRIBUTE_WARN_UNUSED_RESULT
56 # define STARPU_ATTRIBUTE_PURE
57 # define STARPU_ATTRIBUTE_ALIGNED(size)
62 #if defined(c_plusplus) || defined(__cplusplus)
63 # define STARPU_INLINE inline
64 #elif defined(_MSC_VER) || defined(__HP_cc)
65 # define STARPU_INLINE __inline
67 # define STARPU_INLINE __inline__
70 #if STARPU_GNUC_PREREQ(3, 1) && !defined(BUILDING_STARPU) && !defined(STARPU_USE_DEPRECATED_API) && !defined(STARPU_USE_DEPRECATED_ONE_ZERO_API)
71 #define STARPU_DEPRECATED __attribute__((__deprecated__))
73 #define STARPU_DEPRECATED
76 #if STARPU_GNUC_PREREQ(3,3)
77 #define STARPU_WARN_UNUSED_RESULT __attribute__((__warn_unused_result__))
79 #define STARPU_WARN_UNUSED_RESULT
82 #define STARPU_POISON_PTR ((void *)0xdeadbeef)
84 #define STARPU_MIN(a,b) ((a)<(b)?(a):(b))
85 #define STARPU_MAX(a,b) ((a)<(b)?(b):(a))
87 #ifdef STARPU_NO_ASSERT
88 #define STARPU_ASSERT(x) do { } while(0)
89 #define STARPU_ASSERT_MSG(x, msg, ...) do { } while(0)
91 # if defined(__CUDACC__) && defined(STARPU_HAVE_WINDOWS)
92 # define STARPU_ASSERT(x) do { if (STARPU_UNLIKELY(!(x))) *(int*)NULL = 0; } while(0)
93 # define STARPU_ASSERT_MSG(x, msg, ...) do { if (STARPU_UNLIKELY(!(x))) { fprintf(stderr, "[starpu][%s][assert failure] " msg "\n", __starpu_func__, ## __VA_ARGS__); *(int*)NULL = 0; }} while(0)
95 # define STARPU_ASSERT(x) assert(x)
96 # define STARPU_ASSERT_MSG(x, msg, ...) do { if (STARPU_UNLIKELY(!(x))) { fprintf(stderr, "[starpu][%s][assert failure] " msg "\n", __starpu_func__, ## __VA_ARGS__); } ; assert(x); } while(0)
101 #define STARPU_ABORT() do { \
102 fprintf(stderr, "[starpu][abort][%s()@%s:%d]\n", __starpu_func__, __FILE__, __LINE__); \
106 #define STARPU_ABORT_MSG(msg, ...) do { \
107 fprintf(stderr, "[starpu][abort][%s()@%s:%d] " msg "\n", __starpu_func__, __FILE__, __LINE__, ## __VA_ARGS__); \
111 #if defined(STARPU_HAVE_STRERROR_R)
112 # define STARPU_CHECK_RETURN_VALUE(err, message, ...) {if (STARPU_UNLIKELY(err != 0)) { \
113 char xmessage[256]; strerror_r(-err, xmessage, 256); \
114 fprintf(stderr, "[starpu] Unexpected value: <%d:%s> returned for " message "\n", err, xmessage, ## __VA_ARGS__); \
116 # define STARPU_CHECK_RETURN_VALUE_IS(err, value, message, ...) {if (STARPU_UNLIKELY(err != value)) { \
117 char xmessage[256]; strerror_r(-err, xmessage, 256); \
118 fprintf(stderr, "[starpu] Unexpected value: <%d!=%d:%s> returned for " message "\n", err, value, xmessage, ## __VA_ARGS__); \
121 # define STARPU_CHECK_RETURN_VALUE(err, message, ...) {if (STARPU_UNLIKELY(err != 0)) { \
122 fprintf(stderr, "[starpu] Unexpected value: <%d> returned for " message "\n", err, ## __VA_ARGS__); \
124 # define STARPU_CHECK_RETURN_VALUE_IS(err, value, message, ...) {if (STARPU_UNLIKELY(err != value)) { \
125 fprintf(stderr, "[starpu] Unexpected value: <%d != %d> returned for " message "\n", err, value, ## __VA_ARGS__); \
129 #if defined(__i386__) || defined(__x86_64__)
131 static __starpu_inline
unsigned starpu_cmpxchg(
unsigned *ptr,
unsigned old,
unsigned next)
133 __asm__ __volatile__(
"lock cmpxchgl %2,%1":
"+a" (old),
"+m" (*ptr) :
"q" (next) :
"memory");
136 static __starpu_inline
unsigned starpu_xchg(
unsigned *ptr,
unsigned next)
139 __asm__ __volatile__(
"xchgl %1,%0":
"+m" (*ptr),
"+q" (next) : :
"memory");
142 #define STARPU_HAVE_XCHG
145 #define STARPU_ATOMIC_SOMETHING(name,expr) \
146 static __starpu_inline unsigned starpu_atomic_##name(unsigned *ptr, unsigned value) \
148 unsigned old, next; \
153 if (starpu_cmpxchg(ptr, old, next) == old) \
159 #ifdef STARPU_HAVE_SYNC_FETCH_AND_ADD
160 #define STARPU_ATOMIC_ADD(ptr, value) (__sync_fetch_and_add ((ptr), (value)) + (value))
161 #elif defined(STARPU_HAVE_XCHG)
162 STARPU_ATOMIC_SOMETHING(add, old +
value)
163 #define STARPU_ATOMIC_ADD(ptr, value) starpu_atomic_add(ptr, value)
166 #ifdef STARPU_HAVE_SYNC_FETCH_AND_OR
167 #define STARPU_ATOMIC_OR(ptr, value) (__sync_fetch_and_or ((ptr), (value)))
168 #elif defined(STARPU_HAVE_XCHG)
169 STARPU_ATOMIC_SOMETHING(or, old |
value)
170 #define STARPU_ATOMIC_OR(ptr, value) starpu_atomic_or(ptr, value)
173 #ifdef STARPU_HAVE_SYNC_BOOL_COMPARE_AND_SWAP
174 #define STARPU_BOOL_COMPARE_AND_SWAP(ptr, old, value) (__sync_bool_compare_and_swap ((ptr), (old), (value)))
175 #elif defined(STARPU_HAVE_XCHG)
176 #define STARPU_BOOL_COMPARE_AND_SWAP(ptr, old, value) (starpu_cmpxchg((ptr), (old), (value)) == (old))
179 #ifdef STARPU_HAVE_SYNC_LOCK_TEST_AND_SET
180 #define STARPU_TEST_AND_SET(ptr, value) (__sync_lock_test_and_set ((ptr), (value)))
181 #define STARPU_RELEASE(ptr) (__sync_lock_release ((ptr)))
182 #elif defined(STARPU_HAVE_XCHG)
183 #define STARPU_TEST_AND_SET(ptr, value) (starpu_xchg((ptr), (value)))
184 #define STARPU_RELEASE(ptr) (starpu_xchg((ptr), 0))
187 #ifdef STARPU_HAVE_SYNC_SYNCHRONIZE
188 #define STARPU_SYNCHRONIZE() __sync_synchronize()
189 #elif defined(__i386__)
190 #define STARPU_SYNCHRONIZE() __asm__ __volatile__("lock; addl $0,0(%%esp)" ::: "memory")
191 #elif defined(__x86_64__)
192 #define STARPU_SYNCHRONIZE() __asm__ __volatile__("mfence" ::: "memory")
193 #elif defined(__ppc__) || defined(__ppc64__)
194 #define STARPU_SYNCHRONIZE() __asm__ __volatile__("sync" ::: "memory")
197 #if defined(__i386__) || defined(__KNC__) || defined(__KNF__)
198 #define STARPU_RMB() __asm__ __volatile__("lock; addl $0,0(%%esp)" ::: "memory")
199 #define STARPU_WMB() __asm__ __volatile__("lock; addl $0,0(%%esp)" ::: "memory")
200 #elif defined(__x86_64__)
201 #define STARPU_RMB() __asm__ __volatile__("lfence" ::: "memory")
202 #define STARPU_WMB() __asm__ __volatile__("sfence" ::: "memory")
203 #elif defined(__ppc__) || defined(__ppc64__)
204 #define STARPU_RMB() __asm__ __volatile__("sync" ::: "memory")
205 #define STARPU_WMB() __asm__ __volatile__("sync" ::: "memory")
207 #define STARPU_RMB() STARPU_SYNCHRONIZE()
208 #define STARPU_WMB() STARPU_SYNCHRONIZE()
228 strval = getenv(str);
235 val = strtol(strval, &check, 10);
237 fprintf(stderr,
"The %s environment variable must contain an integer\n", str);
252 static __starpu_inline
int starpu_get_env_number_default(
const char *str,
int defval)
261 void starpu_trace_user_event(
unsigned long code);
266 void starpu_execute_on_each_worker_ex(
void (*func)(
void *),
void *arg, uint32_t where,
const char *
name);
272 void starpu_execute_on_specific_workers(
void (*func)(
void*),
void * arg,
unsigned num_workers,
unsigned * workers,
const char *
name);
281 #include <sys/types.h>
282 #include <sys/stat.h>
287 #if !defined(_MSC_VER) || defined(BUILDING_STARPU)
290 #if !defined(STARPU_HAVE_STRUCT_TIMESPEC) || defined(_MSC_VER)
292 #ifndef STARPU_TIMESPEC_DEFINED
293 #define STARPU_TIMESPEC_DEFINED 1
301 #if defined(__MINGW32__) || defined(__CYGWIN__)
302 #include <sys/time.h>
305 #include <sys/time.h>