StarPU Handbook
starpu_util.h
Go to the documentation of this file.
1 /* StarPU --- Runtime system for heterogeneous multicore architectures.
2  *
3  * Copyright (C) 2010-2015 Université de Bordeaux
4  * Copyright (C) 2010, 2011, 2012, 2013, 2014 CNRS
5  *
6  * StarPU is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation; either version 2.1 of the License, or (at
9  * your option) any later version.
10  *
11  * StarPU is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14  *
15  * See the GNU Lesser General Public License in COPYING.LGPL for more details.
16  */
17 
18 #ifndef __STARPU_UTIL_H__
19 #define __STARPU_UTIL_H__
20 
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <assert.h>
25 
26 #include <starpu_config.h>
27 
28 #ifdef __cplusplus
29 extern "C"
30 {
31 #endif
32 
33 #if defined __GNUC__ && defined __GNUC_MINOR__
34 # define STARPU_GNUC_PREREQ(maj, min) \
35  ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
36 #else
37 # define STARPU_GNUC_PREREQ(maj, min) 0
38 #endif
39 
40 #ifdef __GNUC__
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_NORETURN __attribute__((noreturn))
45 # define STARPU_ATTRIBUTE_INTERNAL __attribute__ ((visibility ("internal")))
46 # define STARPU_ATTRIBUTE_MALLOC __attribute__((malloc))
47 # define STARPU_ATTRIBUTE_WARN_UNUSED_RESULT __attribute__((warn_unused_result))
48 # define STARPU_ATTRIBUTE_PURE __attribute__((pure))
49 # define STARPU_ATTRIBUTE_ALIGNED(size) __attribute__((aligned(size)))
50 #else
51 # define STARPU_UNLIKELY(expr) (expr)
52 # define STARPU_LIKELY(expr) (expr)
53 # define STARPU_ATTRIBUTE_UNUSED
54 # define STARPU_ATTRIBUTE_NORETURN
55 # define STARPU_ATTRIBUTE_INTERNAL
56 # define STARPU_ATTRIBUTE_MALLOC
57 # define STARPU_ATTRIBUTE_WARN_UNUSED_RESULT
58 # define STARPU_ATTRIBUTE_PURE
59 # define STARPU_ATTRIBUTE_ALIGNED(size)
60 #endif
61 
62 /* Note that if we're compiling C++, then just use the "inline"
63  keyword, since it's part of C++ */
64 #if defined(c_plusplus) || defined(__cplusplus)
65 # define STARPU_INLINE inline
66 #elif defined(_MSC_VER) || defined(__HP_cc)
67 # define STARPU_INLINE __inline
68 #else
69 # define STARPU_INLINE __inline__
70 #endif
71 
72 #if STARPU_GNUC_PREREQ(3, 1) && !defined(BUILDING_STARPU) && !defined(STARPU_USE_DEPRECATED_API) && !defined(STARPU_USE_DEPRECATED_ONE_ZERO_API)
73 #define STARPU_DEPRECATED __attribute__((__deprecated__))
74 #else
75 #define STARPU_DEPRECATED
76 #endif /* __GNUC__ */
77 
78 #if STARPU_GNUC_PREREQ(3,3)
79 #define STARPU_WARN_UNUSED_RESULT __attribute__((__warn_unused_result__))
80 #else
81 #define STARPU_WARN_UNUSED_RESULT
82 #endif /* __GNUC__ */
83 
84 #define STARPU_POISON_PTR ((void *)0xdeadbeef)
85 
86 #define STARPU_MIN(a,b) ((a)<(b)?(a):(b))
87 #define STARPU_MAX(a,b) ((a)<(b)?(b):(a))
88 
89 #ifdef STARPU_NO_ASSERT
90 #define STARPU_ASSERT(x) do { } while(0)
91 #define STARPU_ASSERT_MSG(x, msg, ...) do { } while(0)
92 #else
93 # if defined(__CUDACC__) && defined(STARPU_HAVE_WINDOWS)
94 # define STARPU_ASSERT(x) do { if (STARPU_UNLIKELY(!(x))) *(int*)NULL = 0; } while(0)
95 # 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)
96 # else
97 # define STARPU_ASSERT(x) assert(x)
98 # 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)
99 
100 # endif
101 #endif
102 
103 #define STARPU_ABORT() do { \
104  fprintf(stderr, "[starpu][abort][%s()@%s:%d]\n", __starpu_func__, __FILE__, __LINE__); \
105  abort(); \
106 } while(0)
107 
108 #define STARPU_ABORT_MSG(msg, ...) do { \
109  fprintf(stderr, "[starpu][abort][%s()@%s:%d] " msg "\n", __starpu_func__, __FILE__, __LINE__, ## __VA_ARGS__); \
110  abort(); \
111 } while(0)
112 
113 #if defined(STARPU_HAVE_STRERROR_R)
114 # define STARPU_CHECK_RETURN_VALUE(err, message, ...) {if (STARPU_UNLIKELY(err != 0)) { \
115  char xmessage[256]; strerror_r(-err, xmessage, 256); \
116  fprintf(stderr, "[starpu] Unexpected value: <%d:%s> returned for " message "\n", err, xmessage, ## __VA_ARGS__); \
117  STARPU_ABORT(); }}
118 # define STARPU_CHECK_RETURN_VALUE_IS(err, value, message, ...) {if (STARPU_UNLIKELY(err != value)) { \
119  char xmessage[256]; strerror_r(-err, xmessage, 256); \
120  fprintf(stderr, "[starpu] Unexpected value: <%d!=%d:%s> returned for " message "\n", err, value, xmessage, ## __VA_ARGS__); \
121  STARPU_ABORT(); }}
122 #else
123 # define STARPU_CHECK_RETURN_VALUE(err, message, ...) {if (STARPU_UNLIKELY(err != 0)) { \
124  fprintf(stderr, "[starpu] Unexpected value: <%d> returned for " message "\n", err, ## __VA_ARGS__); \
125  STARPU_ABORT(); }}
126 # define STARPU_CHECK_RETURN_VALUE_IS(err, value, message, ...) {if (STARPU_UNLIKELY(err != value)) { \
127  fprintf(stderr, "[starpu] Unexpected value: <%d != %d> returned for " message "\n", err, value, ## __VA_ARGS__); \
128  STARPU_ABORT(); }}
129 #endif /* STARPU_HAVE_STRERROR_R */
130 
131 #if defined(__i386__) || defined(__x86_64__)
132 
133 static __starpu_inline unsigned starpu_cmpxchg(unsigned *ptr, unsigned old, unsigned next)
134 {
135  __asm__ __volatile__("lock cmpxchgl %2,%1": "+a" (old), "+m" (*ptr) : "q" (next) : "memory");
136  return old;
137 }
138 static __starpu_inline unsigned starpu_xchg(unsigned *ptr, unsigned next)
139 {
140  /* Note: xchg is always locked already */
141  __asm__ __volatile__("xchgl %1,%0": "+m" (*ptr), "+q" (next) : : "memory");
142  return next;
143 }
144 #define STARPU_HAVE_XCHG
145 #endif
146 
147 #define STARPU_ATOMIC_SOMETHING(name,expr) \
148 static __starpu_inline unsigned starpu_atomic_##name(unsigned *ptr, unsigned value) \
149 { \
150  unsigned old, next; \
151  while (1) \
152  { \
153  old = *ptr; \
154  next = expr; \
155  if (starpu_cmpxchg(ptr, old, next) == old) \
156  break; \
157  }; \
158  return expr; \
159 }
160 
161 #ifdef STARPU_HAVE_SYNC_FETCH_AND_ADD
162 #define STARPU_ATOMIC_ADD(ptr, value) (__sync_fetch_and_add ((ptr), (value)) + (value))
163 #elif defined(STARPU_HAVE_XCHG)
164 STARPU_ATOMIC_SOMETHING(add, old + value)
165 #define STARPU_ATOMIC_ADD(ptr, value) starpu_atomic_add(ptr, value)
166 #endif
167 
168 #ifdef STARPU_HAVE_SYNC_FETCH_AND_OR
169 #define STARPU_ATOMIC_OR(ptr, value) (__sync_fetch_and_or ((ptr), (value)))
170 #elif defined(STARPU_HAVE_XCHG)
171 STARPU_ATOMIC_SOMETHING(or, old | value)
172 #define STARPU_ATOMIC_OR(ptr, value) starpu_atomic_or(ptr, value)
173 #endif
174 
175 #ifdef STARPU_HAVE_SYNC_BOOL_COMPARE_AND_SWAP
176 #define STARPU_BOOL_COMPARE_AND_SWAP(ptr, old, value) (__sync_bool_compare_and_swap ((ptr), (old), (value)))
177 #elif defined(STARPU_HAVE_XCHG)
178 #define STARPU_BOOL_COMPARE_AND_SWAP(ptr, old, value) (starpu_cmpxchg((ptr), (old), (value)) == (old))
179 #endif
180 
181 #ifdef STARPU_HAVE_SYNC_VAL_COMPARE_AND_SWAP
182 #define STARPU_VAL_COMPARE_AND_SWAP(ptr, old, value) (__sync_val_compare_and_swap ((ptr), (old), (value)))
183 #elif defined(STARPU_HAVE_XCHG)
184 #define STARPU_VAL_COMPARE_AND_SWAP(ptr, old, value) (starpu_cmpxchg((ptr), (old), (value)))
185 #endif
186 
187 #ifdef STARPU_HAVE_SYNC_LOCK_TEST_AND_SET
188 #define STARPU_TEST_AND_SET(ptr, value) (__sync_lock_test_and_set ((ptr), (value)))
189 #define STARPU_RELEASE(ptr) (__sync_lock_release ((ptr)))
190 #elif defined(STARPU_HAVE_XCHG)
191 #define STARPU_TEST_AND_SET(ptr, value) (starpu_xchg((ptr), (value)))
192 #define STARPU_RELEASE(ptr) (starpu_xchg((ptr), 0))
193 #endif
194 
195 #ifdef STARPU_HAVE_SYNC_SYNCHRONIZE
196 #define STARPU_SYNCHRONIZE() __sync_synchronize()
197 #elif defined(__i386__)
198 #define STARPU_SYNCHRONIZE() __asm__ __volatile__("lock; addl $0,0(%%esp)" ::: "memory")
199 #elif defined(__x86_64__)
200 #define STARPU_SYNCHRONIZE() __asm__ __volatile__("mfence" ::: "memory")
201 #elif defined(__ppc__) || defined(__ppc64__)
202 #define STARPU_SYNCHRONIZE() __asm__ __volatile__("sync" ::: "memory")
203 #endif
204 
205 #if defined(__i386__) || defined(__KNC__) || defined(__KNF__)
206 #define STARPU_RMB() __asm__ __volatile__("lock; addl $0,0(%%esp)" ::: "memory")
207 #define STARPU_WMB() __asm__ __volatile__("lock; addl $0,0(%%esp)" ::: "memory")
208 #elif defined(__x86_64__)
209 #define STARPU_RMB() __asm__ __volatile__("lfence" ::: "memory")
210 #define STARPU_WMB() __asm__ __volatile__("sfence" ::: "memory")
211 #elif defined(__ppc__) || defined(__ppc64__)
212 #define STARPU_RMB() __asm__ __volatile__("sync" ::: "memory")
213 #define STARPU_WMB() __asm__ __volatile__("sync" ::: "memory")
214 #else
215 #define STARPU_RMB() STARPU_SYNCHRONIZE()
216 #define STARPU_WMB() STARPU_SYNCHRONIZE()
217 #endif
218 
219 #ifdef __cplusplus
220 }
221 #endif
222 
223 /* Include this only here so that <starpu_data_interfaces.h> can use the
224  * macros above. */
225 #include <starpu_task.h>
226 
227 #ifdef __cplusplus
228 extern "C"
229 {
230 #endif
231 
232 static __starpu_inline int starpu_get_env_number(const char *str)
233 {
234  char *strval;
235 
236  strval = getenv(str);
237  if (strval)
238  {
239  /* the env variable was actually set */
240  long int val;
241  char *check;
242 
243  val = strtol(strval, &check, 10);
244  if (*check) {
245  fprintf(stderr,"The %s environment variable must contain an integer\n", str);
246  STARPU_ABORT();
247  }
248 
249  /* fprintf(stderr, "ENV %s WAS %d\n", str, val); */
250  return (int)val;
251  }
252  else
253  {
254  /* there is no such env variable */
255  /* fprintf("There was no %s ENV\n", str); */
256  return -1;
257  }
258 }
259 
260 static __starpu_inline int starpu_get_env_number_default(const char *str, int defval)
261 {
262  int ret = starpu_get_env_number(str);
263  if (ret == -1)
264  ret = defval;
265  return ret;
266 }
267 
268 static __starpu_inline float starpu_get_env_float_default(const char *str, float defval)
269 {
270  char *strval;
271 
272  strval = getenv(str);
273  if (strval)
274  {
275  /* the env variable was actually set */
276  float val;
277  char *check;
278 
279  val = strtof(strval, &check);
280  if (*check) {
281  fprintf(stderr,"The %s environment variable must contain a float\n", str);
282  STARPU_ABORT();
283  }
284 
285  /* fprintf(stderr, "ENV %s WAS %f\n", str, val); */
286  return val;
287  }
288  else
289  {
290  /* there is no such env variable */
291  /* fprintf("There was no %s ENV\n", str); */
292  return defval;
293  }
294 }
295 
296 void starpu_execute_on_each_worker(void (*func)(void *), void *arg, uint32_t where);
297 
298 void starpu_execute_on_each_worker_ex(void (*func)(void *), void *arg, uint32_t where, const char *name);
299 
300 void starpu_execute_on_specific_workers(void (*func)(void*), void *arg, unsigned num_workers, unsigned *workers, const char *name);
301 
302 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);
303 
304 double starpu_timing_now(void);
305 
306 #ifdef _WIN32
307 /* Try to fetch the system definition of timespec */
308 #include <sys/types.h>
309 #include <sys/stat.h>
310 #ifdef HAVE_UNISTD_H
311 #include <unistd.h>
312 #endif
313 #include <time.h>
314 #if !defined(_MSC_VER) || defined(BUILDING_STARPU)
315 #include <pthread.h>
316 #endif
317 #if !defined(STARPU_HAVE_STRUCT_TIMESPEC) || defined(_MSC_VER)
318 /* If it didn't get defined in the standard places, then define it ourself */
319 #ifndef STARPU_TIMESPEC_DEFINED
320 #define STARPU_TIMESPEC_DEFINED 1
321 struct timespec {
322  time_t tv_sec; /* Seconds */
323  long tv_nsec; /* Nanoseconds */
324 };
325 #endif /* STARPU_TIMESPEC_DEFINED */
326 #endif /* STARPU_HAVE_STRUCT_TIMESPEC */
327 /* Fetch gettimeofday on mingw/cygwin */
328 #if defined(__MINGW32__) || defined(__CYGWIN__)
329 #include <sys/time.h>
330 #endif
331 #else
332 #include <sys/time.h>
333 #endif /* _WIN32 */
334 
335 #ifdef __cplusplus
336 }
337 #endif
338 
339 #endif /* __STARPU_UTIL_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)
static __starpu_inline int starpu_get_env_number(const char *str)
Definition: starpu_util.h:232
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
#define STARPU_ABORT()
Definition: starpu_util.h:103
void starpu_execute_on_each_worker(void(*func)(void *), void *arg, uint32_t where)