StarPU Handbook
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
starpu_util.h
Go to the documentation of this file.
1 /* StarPU --- Runtime system for heterogeneous multicore architectures.
2  *
3  * Copyright (C) 2010-2014 Université de Bordeaux
4  * Copyright (C) 2010, 2011, 2012, 2013, 2014 Centre National de la Recherche Scientifique
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_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)))
49 #else
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)
58 #endif
59 
60 /* Note that if we're compiling C++, then just use the "inline"
61  keyword, since it's part of C++ */
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
66 #else
67 # define STARPU_INLINE __inline__
68 #endif
69 
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__))
72 #else
73 #define STARPU_DEPRECATED
74 #endif /* __GNUC__ */
75 
76 #if STARPU_GNUC_PREREQ(3,3)
77 #define STARPU_WARN_UNUSED_RESULT __attribute__((__warn_unused_result__))
78 #else
79 #define STARPU_WARN_UNUSED_RESULT
80 #endif /* __GNUC__ */
81 
82 #define STARPU_POISON_PTR ((void *)0xdeadbeef)
83 
84 #define STARPU_MIN(a,b) ((a)<(b)?(a):(b))
85 #define STARPU_MAX(a,b) ((a)<(b)?(b):(a))
86 
87 #ifdef STARPU_NO_ASSERT
88 #define STARPU_ASSERT(x) do { } while(0)
89 #define STARPU_ASSERT_MSG(x, msg, ...) do { } while(0)
90 #else
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)
94 # else
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)
97 
98 # endif
99 #endif
100 
101 #define STARPU_ABORT() do { \
102  fprintf(stderr, "[starpu][abort][%s()@%s:%d]\n", __starpu_func__, __FILE__, __LINE__); \
103  abort(); \
104 } while(0)
105 
106 #define STARPU_ABORT_MSG(msg, ...) do { \
107  fprintf(stderr, "[starpu][abort][%s()@%s:%d] " msg "\n", __starpu_func__, __FILE__, __LINE__, ## __VA_ARGS__); \
108  abort(); \
109 } while(0)
110 
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__); \
115  STARPU_ABORT(); }}
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__); \
119  STARPU_ABORT(); }}
120 #else
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__); \
123  STARPU_ABORT(); }}
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__); \
126  STARPU_ABORT(); }}
127 #endif /* STARPU_HAVE_STRERROR_R */
128 
129 #if defined(__i386__) || defined(__x86_64__)
130 
131 static __starpu_inline unsigned starpu_cmpxchg(unsigned *ptr, unsigned old, unsigned next)
132 {
133  __asm__ __volatile__("lock cmpxchgl %2,%1": "+a" (old), "+m" (*ptr) : "q" (next) : "memory");
134  return old;
135 }
136 static __starpu_inline unsigned starpu_xchg(unsigned *ptr, unsigned next)
137 {
138  /* Note: xchg is always locked already */
139  __asm__ __volatile__("xchgl %1,%0": "+m" (*ptr), "+q" (next) : : "memory");
140  return next;
141 }
142 #define STARPU_HAVE_XCHG
143 #endif
144 
145 #define STARPU_ATOMIC_SOMETHING(name,expr) \
146 static __starpu_inline unsigned starpu_atomic_##name(unsigned *ptr, unsigned value) \
147 { \
148  unsigned old, next; \
149  while (1) \
150  { \
151  old = *ptr; \
152  next = expr; \
153  if (starpu_cmpxchg(ptr, old, next) == old) \
154  break; \
155  }; \
156  return expr; \
157 }
158 
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)
164 #endif
165 
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)
171 #endif
172 
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))
177 #endif
178 
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)))
183 #endif
184 
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))
191 #endif
192 
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")
201 #endif
202 
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")
212 #else
213 #define STARPU_RMB() STARPU_SYNCHRONIZE()
214 #define STARPU_WMB() STARPU_SYNCHRONIZE()
215 #endif
216 
217 #ifdef __cplusplus
218 }
219 #endif
220 
221 /* Include this only here so that <starpu_data_interfaces.h> can use the
222  * macros above. */
223 #include <starpu_task.h>
224 
225 #ifdef __cplusplus
226 extern "C"
227 {
228 #endif
229 
230 static __starpu_inline int starpu_get_env_number(const char *str)
231 {
232  char *strval;
233 
234  strval = getenv(str);
235  if (strval)
236  {
237  /* the env variable was actually set */
238  long int val;
239  char *check;
240 
241  val = strtol(strval, &check, 10);
242  if (*check) {
243  fprintf(stderr,"The %s environment variable must contain an integer\n", str);
244  STARPU_ABORT();
245  }
246 
247  /* fprintf(stderr, "ENV %s WAS %d\n", str, val); */
248  return (int)val;
249  }
250  else
251  {
252  /* there is no such env variable */
253  /* fprintf("There was no %s ENV\n", str); */
254  return -1;
255  }
256 }
257 
258 static __starpu_inline int starpu_get_env_number_default(const char *str, int defval)
259 {
260  int ret = starpu_get_env_number(str);
261  if (ret == -1)
262  ret = defval;
263  return ret;
264 }
265 
266 static __starpu_inline float starpu_get_env_float_default(const char *str, float defval)
267 {
268  char *strval;
269 
270  strval = getenv(str);
271  if (strval)
272  {
273  /* the env variable was actually set */
274  float val;
275  char *check;
276 
277  val = strtof(strval, &check);
278  if (*check) {
279  fprintf(stderr,"The %s environment variable must contain a float\n", str);
280  STARPU_ABORT();
281  }
282 
283  /* fprintf(stderr, "ENV %s WAS %f\n", str, val); */
284  return val;
285  }
286  else
287  {
288  /* there is no such env variable */
289  /* fprintf("There was no %s ENV\n", str); */
290  return defval;
291  }
292 }
293 
294 void starpu_execute_on_each_worker(void (*func)(void *), void *arg, uint32_t where);
295 
296 void starpu_execute_on_each_worker_ex(void (*func)(void *), void *arg, uint32_t where, const char *name);
297 
298 void starpu_execute_on_specific_workers(void (*func)(void*), void *arg, unsigned num_workers, unsigned *workers, const char *name);
299 
300 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);
301 
302 double starpu_timing_now(void);
303 
304 #ifdef _WIN32
305 /* Try to fetch the system definition of timespec */
306 #include <time.h>
307 #if defined(__MINGW32__) || defined(__CYGWIN__)
308 #include <sys/time.h>
309 #endif
310 #if !defined(_MSC_VER) || defined(BUILDING_STARPU)
311 #include <pthread.h>
312 #endif
313 #if !defined(_TIMESPEC_DEFINED) && !defined(HAVE_STRUCT_TIMESPEC)
314 /* If it didn't get defined in the standard places, then define it ourself */
315 #ifndef STARPU_TIMESPEC_DEFINED
316 #define STARPU_TIMESPEC_DEFINED 1
317 struct timespec {
318  time_t tv_sec; /* Seconds */
319  long tv_nsec; /* Nanoseconds */
320 };
321 #endif /* STARPU_TIMESPEC_DEFINED */
322 #endif
323 #else
324 #include <sys/time.h>
325 #endif /* _WIN32 */
326 
327 #ifdef __cplusplus
328 }
329 #endif
330 
331 #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:230
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:101
void starpu_execute_on_each_worker(void(*func)(void *), void *arg, uint32_t where)