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, "\n[starpu][%s][assert failure] " msg "\n\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, "\n[starpu][%s][assert failure] " msg "\n\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_VAL_COMPARE_AND_SWAP
180 #define STARPU_VAL_COMPARE_AND_SWAP(ptr, old, value) (__sync_val_compare_and_swap ((ptr), (old), (value)))
181 #elif defined(STARPU_HAVE_XCHG)
182 #define STARPU_VAL_COMPARE_AND_SWAP(ptr, old, value) (starpu_cmpxchg((ptr), (old), (value)))
185 #ifdef STARPU_HAVE_SYNC_LOCK_TEST_AND_SET
186 #define STARPU_TEST_AND_SET(ptr, value) (__sync_lock_test_and_set ((ptr), (value)))
187 #define STARPU_RELEASE(ptr) (__sync_lock_release ((ptr)))
188 #elif defined(STARPU_HAVE_XCHG)
189 #define STARPU_TEST_AND_SET(ptr, value) (starpu_xchg((ptr), (value)))
190 #define STARPU_RELEASE(ptr) (starpu_xchg((ptr), 0))
193 #ifdef STARPU_HAVE_SYNC_SYNCHRONIZE
194 #define STARPU_SYNCHRONIZE() __sync_synchronize()
195 #elif defined(__i386__)
196 #define STARPU_SYNCHRONIZE() __asm__ __volatile__("lock; addl $0,0(%%esp)" ::: "memory")
197 #elif defined(__x86_64__)
198 #define STARPU_SYNCHRONIZE() __asm__ __volatile__("mfence" ::: "memory")
199 #elif defined(__ppc__) || defined(__ppc64__)
200 #define STARPU_SYNCHRONIZE() __asm__ __volatile__("sync" ::: "memory")
203 #if defined(__i386__) || defined(__KNC__) || defined(__KNF__)
204 #define STARPU_RMB() __asm__ __volatile__("lock; addl $0,0(%%esp)" ::: "memory")
205 #define STARPU_WMB() __asm__ __volatile__("lock; addl $0,0(%%esp)" ::: "memory")
206 #elif defined(__x86_64__)
207 #define STARPU_RMB() __asm__ __volatile__("lfence" ::: "memory")
208 #define STARPU_WMB() __asm__ __volatile__("sfence" ::: "memory")
209 #elif defined(__ppc__) || defined(__ppc64__)
210 #define STARPU_RMB() __asm__ __volatile__("sync" ::: "memory")
211 #define STARPU_WMB() __asm__ __volatile__("sync" ::: "memory")
213 #define STARPU_RMB() STARPU_SYNCHRONIZE()
214 #define STARPU_WMB() STARPU_SYNCHRONIZE()
234 strval = getenv(str);
241 val = strtol(strval, &check, 10);
243 fprintf(stderr,
"The %s environment variable must contain an integer\n", str);
258 static __starpu_inline
int starpu_get_env_number_default(
const char *str,
int defval)
266 static __starpu_inline
float starpu_get_env_float_default(
const char *str,
float defval)
270 strval = getenv(str);
277 val = strtof(strval, &check);
279 fprintf(stderr,
"The %s environment variable must contain a float\n", str);
307 #if defined(__MINGW32__) || defined(__CYGWIN__)
308 #include <sys/time.h>
310 #if !defined(_MSC_VER) || defined(BUILDING_STARPU)
313 #if !defined(_TIMESPEC_DEFINED) && !defined(HAVE_STRUCT_TIMESPEC)
315 #ifndef STARPU_TIMESPEC_DEFINED
316 #define STARPU_TIMESPEC_DEFINED 1
324 #include <sys/time.h>
double starpu_timing_now(void)
void starpu_execute_on_specific_workers(void(*func)(void *), void *arg, unsigned num_workers, unsigned *workers, const char *name)
int starpu_data_cpy(starpu_data_handle_t dst_handle, starpu_data_handle_t src_handle, int asynchronous, void(*callback_func)(void *), void *callback_arg)
void starpu_execute_on_each_worker_ex(void(*func)(void *), void *arg, uint32_t where, const char *name)
struct _starpu_data_state * starpu_data_handle_t
Definition: starpu_data.h:29
void starpu_execute_on_each_worker(void(*func)(void *), void *arg, uint32_t where)