StarPU Handbook
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Groups Pages
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 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 __GLIBC__
29 #include <execinfo.h>
30 #endif
31 
32 #ifdef __cplusplus
33 extern "C"
34 {
35 #endif
36 
37 #if defined __GNUC__ && defined __GNUC_MINOR__
38 # define STARPU_GNUC_PREREQ(maj, min) \
39  ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
40 #else
41 # define STARPU_GNUC_PREREQ(maj, min) 0
42 #endif
43 
44 #ifdef __GNUC__
45 # define STARPU_UNLIKELY(expr) (__builtin_expect(!!(expr),0))
46 # define STARPU_LIKELY(expr) (__builtin_expect(!!(expr),1))
47 # define STARPU_ATTRIBUTE_UNUSED __attribute__((unused))
48 # define STARPU_ATTRIBUTE_NORETURN __attribute__((noreturn))
49 # define STARPU_ATTRIBUTE_INTERNAL __attribute__ ((visibility ("internal")))
50 # define STARPU_ATTRIBUTE_MALLOC __attribute__((malloc))
51 # define STARPU_ATTRIBUTE_WARN_UNUSED_RESULT __attribute__((warn_unused_result))
52 # define STARPU_ATTRIBUTE_PURE __attribute__((pure))
53 # define STARPU_ATTRIBUTE_ALIGNED(size) __attribute__((aligned(size)))
54 #else
55 # define STARPU_UNLIKELY(expr) (expr)
56 # define STARPU_LIKELY(expr) (expr)
57 # define STARPU_ATTRIBUTE_UNUSED
58 # define STARPU_ATTRIBUTE_NORETURN
59 # define STARPU_ATTRIBUTE_INTERNAL
60 # define STARPU_ATTRIBUTE_MALLOC
61 # define STARPU_ATTRIBUTE_WARN_UNUSED_RESULT
62 # define STARPU_ATTRIBUTE_PURE
63 # define STARPU_ATTRIBUTE_ALIGNED(size)
64 #endif
65 
66 /* Note that if we're compiling C++, then just use the "inline"
67  keyword, since it's part of C++ */
68 #if defined(c_plusplus) || defined(__cplusplus)
69 # define STARPU_INLINE inline
70 #elif defined(_MSC_VER) || defined(__HP_cc)
71 # define STARPU_INLINE __inline
72 #else
73 # define STARPU_INLINE __inline__
74 #endif
75 
76 #if STARPU_GNUC_PREREQ(3, 1) && !defined(BUILDING_STARPU) && !defined(STARPU_USE_DEPRECATED_API) && !defined(STARPU_USE_DEPRECATED_ONE_ZERO_API)
77 #define STARPU_DEPRECATED __attribute__((__deprecated__))
78 #else
79 #define STARPU_DEPRECATED
80 #endif /* __GNUC__ */
81 
82 #if STARPU_GNUC_PREREQ(3,3)
83 #define STARPU_WARN_UNUSED_RESULT __attribute__((__warn_unused_result__))
84 #else
85 #define STARPU_WARN_UNUSED_RESULT
86 #endif /* __GNUC__ */
87 
88 #define STARPU_POISON_PTR ((void *)0xdeadbeef)
89 
90 #define STARPU_MIN(a,b) ((a)<(b)?(a):(b))
91 #define STARPU_MAX(a,b) ((a)<(b)?(b):(a))
92 
93 #define STARPU_BACKTRACE_LENGTH 32
94 #ifdef __GLIBC__
95 # define STARPU_DUMP_BACKTRACE() do { \
96  void *__ptrs[STARPU_BACKTRACE_LENGTH]; \
97  int __n = backtrace(__ptrs, STARPU_BACKTRACE_LENGTH); \
98  backtrace_symbols_fd(__ptrs, __n, 2); \
99 } while (0)
100 #else
101 # define STARPU_DUMP_BACKTRACE() do { } while (0)
102 #endif
103 
104 #ifdef STARPU_NO_ASSERT
105 #define STARPU_ASSERT(x) do { } while(0)
106 #define STARPU_ASSERT_MSG(x, msg, ...) do { } while(0)
107 #else
108 # if defined(__CUDACC__) && defined(STARPU_HAVE_WINDOWS)
109 # define STARPU_ASSERT(x) do { if (STARPU_UNLIKELY(!(x))) *(int*)NULL = 0; } while(0)
110 # 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)
111 # else
112 # define STARPU_ASSERT(x) do { if (STARPU_UNLIKELY(!(x))) { STARPU_DUMP_BACKTRACE(); assert(x); } } while (0)
113 # define STARPU_ASSERT_MSG(x, msg, ...) do { if (STARPU_UNLIKELY(!(x))) { fprintf(stderr, "[starpu][%s][assert failure] " msg "\n", __starpu_func__, ## __VA_ARGS__); STARPU_DUMP_BACKTRACE(); assert(x); } } while(0)
114 
115 # endif
116 #endif
117 
118 #define STARPU_ABORT() do { \
119  fprintf(stderr, "[starpu][abort][%s()@%s:%d]\n", __starpu_func__, __FILE__, __LINE__); \
120  abort(); \
121 } while(0)
122 
123 #define STARPU_ABORT_MSG(msg, ...) do { \
124  fprintf(stderr, "[starpu][abort][%s()@%s:%d] " msg "\n", __starpu_func__, __FILE__, __LINE__, ## __VA_ARGS__); \
125  abort(); \
126 } while(0)
127 
128 #if defined(STARPU_HAVE_STRERROR_R)
129 # define STARPU_CHECK_RETURN_VALUE(err, message, ...) {if (STARPU_UNLIKELY(err != 0)) { \
130  char xmessage[256]; strerror_r(-err, xmessage, 256); \
131  fprintf(stderr, "[starpu] Unexpected value: <%d:%s> returned for " message "\n", err, xmessage, ## __VA_ARGS__); \
132  STARPU_ABORT(); }}
133 # define STARPU_CHECK_RETURN_VALUE_IS(err, value, message, ...) {if (STARPU_UNLIKELY(err != value)) { \
134  char xmessage[256]; strerror_r(-err, xmessage, 256); \
135  fprintf(stderr, "[starpu] Unexpected value: <%d!=%d:%s> returned for " message "\n", err, value, xmessage, ## __VA_ARGS__); \
136  STARPU_ABORT(); }}
137 #else
138 # define STARPU_CHECK_RETURN_VALUE(err, message, ...) {if (STARPU_UNLIKELY(err != 0)) { \
139  fprintf(stderr, "[starpu] Unexpected value: <%d> returned for " message "\n", err, ## __VA_ARGS__); \
140  STARPU_ABORT(); }}
141 # define STARPU_CHECK_RETURN_VALUE_IS(err, value, message, ...) {if (STARPU_UNLIKELY(err != value)) { \
142  fprintf(stderr, "[starpu] Unexpected value: <%d != %d> returned for " message "\n", err, value, ## __VA_ARGS__); \
143  STARPU_ABORT(); }}
144 #endif /* STARPU_HAVE_STRERROR_R */
145 
146 #if defined(__i386__) || defined(__x86_64__)
147 
148 static __starpu_inline unsigned starpu_cmpxchg(unsigned *ptr, unsigned old, unsigned next)
149 {
150  __asm__ __volatile__("lock cmpxchgl %2,%1": "+a" (old), "+m" (*ptr) : "q" (next) : "memory");
151  return old;
152 }
153 static __starpu_inline unsigned starpu_xchg(unsigned *ptr, unsigned next)
154 {
155  /* Note: xchg is always locked already */
156  __asm__ __volatile__("xchgl %1,%0": "+m" (*ptr), "+q" (next) : : "memory");
157  return next;
158 }
159 #define STARPU_HAVE_XCHG
160 #endif
161 
162 #define STARPU_ATOMIC_SOMETHING(name,expr) \
163 static __starpu_inline unsigned starpu_atomic_##name(unsigned *ptr, unsigned value) \
164 { \
165  unsigned old, next; \
166  while (1) \
167  { \
168  old = *ptr; \
169  next = expr; \
170  if (starpu_cmpxchg(ptr, old, next) == old) \
171  break; \
172  }; \
173  return expr; \
174 }
175 
176 #ifdef STARPU_HAVE_SYNC_FETCH_AND_ADD
177 #define STARPU_ATOMIC_ADD(ptr, value) (__sync_fetch_and_add ((ptr), (value)) + (value))
178 #elif defined(STARPU_HAVE_XCHG)
179 STARPU_ATOMIC_SOMETHING(add, old + value)
180 #define STARPU_ATOMIC_ADD(ptr, value) starpu_atomic_add(ptr, value)
181 #endif
182 
183 #ifdef STARPU_HAVE_SYNC_FETCH_AND_OR
184 #define STARPU_ATOMIC_OR(ptr, value) (__sync_fetch_and_or ((ptr), (value)))
185 #elif defined(STARPU_HAVE_XCHG)
186 STARPU_ATOMIC_SOMETHING(or, old | value)
187 #define STARPU_ATOMIC_OR(ptr, value) starpu_atomic_or(ptr, value)
188 #endif
189 
190 #ifdef STARPU_HAVE_SYNC_BOOL_COMPARE_AND_SWAP
191 #define STARPU_BOOL_COMPARE_AND_SWAP(ptr, old, value) (__sync_bool_compare_and_swap ((ptr), (old), (value)))
192 #elif defined(STARPU_HAVE_XCHG)
193 #define STARPU_BOOL_COMPARE_AND_SWAP(ptr, old, value) (starpu_cmpxchg((ptr), (old), (value)) == (old))
194 #endif
195 
196 #ifdef STARPU_HAVE_SYNC_LOCK_TEST_AND_SET
197 #define STARPU_TEST_AND_SET(ptr, value) (__sync_lock_test_and_set ((ptr), (value)))
198 #define STARPU_RELEASE(ptr) (__sync_lock_release ((ptr)))
199 #elif defined(STARPU_HAVE_XCHG)
200 #define STARPU_TEST_AND_SET(ptr, value) (starpu_xchg((ptr), (value)))
201 #define STARPU_RELEASE(ptr) (starpu_xchg((ptr), 0))
202 #endif
203 
204 #ifdef STARPU_HAVE_SYNC_SYNCHRONIZE
205 #define STARPU_SYNCHRONIZE() __sync_synchronize()
206 #elif defined(__i386__)
207 #define STARPU_SYNCHRONIZE() __asm__ __volatile__("lock; addl $0,0(%%esp)" ::: "memory")
208 #elif defined(__x86_64__)
209 #define STARPU_SYNCHRONIZE() __asm__ __volatile__("mfence" ::: "memory")
210 #elif defined(__ppc__) || defined(__ppc64__)
211 #define STARPU_SYNCHRONIZE() __asm__ __volatile__("sync" ::: "memory")
212 #endif
213 
214 #if defined(__i386__) || defined(__KNC__) || defined(__KNF__)
215 #define STARPU_RMB() __asm__ __volatile__("lock; addl $0,0(%%esp)" ::: "memory")
216 #define STARPU_WMB() __asm__ __volatile__("lock; addl $0,0(%%esp)" ::: "memory")
217 #elif defined(__x86_64__)
218 #define STARPU_RMB() __asm__ __volatile__("lfence" ::: "memory")
219 #define STARPU_WMB() __asm__ __volatile__("sfence" ::: "memory")
220 #elif defined(__ppc__) || defined(__ppc64__)
221 #define STARPU_RMB() __asm__ __volatile__("sync" ::: "memory")
222 #define STARPU_WMB() __asm__ __volatile__("sync" ::: "memory")
223 #else
224 #define STARPU_RMB() STARPU_SYNCHRONIZE()
225 #define STARPU_WMB() STARPU_SYNCHRONIZE()
226 #endif
227 
228 #ifdef __cplusplus
229 }
230 #endif
231 
232 /* Include this only here so that <starpu_data_interfaces.h> can use the
233  * macros above. */
234 #include <starpu_task.h>
235 
236 #ifdef __cplusplus
237 extern "C"
238 {
239 #endif
240 
241 extern int _starpu_silent;
242 
243 char *starpu_getenv(const char *str);
244 
245 
246 static __starpu_inline int starpu_get_env_number(const char *str)
247 {
248  char *strval;
249 
250  strval = starpu_getenv(str);
251  if (strval)
252  {
253  /* the env variable was actually set */
254  long int val;
255  char *check;
256 
257  val = strtol(strval, &check, 10);
258  if (*check) {
259  fprintf(stderr,"The %s environment variable must contain an integer\n", str);
260  STARPU_ABORT();
261  }
262 
263  /* fprintf(stderr, "ENV %s WAS %d\n", str, val); */
264  return (int)val;
265  }
266  else
267  {
268  /* there is no such env variable */
269  /* fprintf("There was no %s ENV\n", str); */
270  return -1;
271  }
272 }
273 
274 static __starpu_inline int starpu_get_env_number_default(const char *str, int defval)
275 {
276  int ret = starpu_get_env_number(str);
277  if (ret == -1)
278  ret = defval;
279  return ret;
280 }
281 
282 /* Add an event in the execution trace if FxT is enabled */
283 void starpu_trace_user_event(unsigned long code);
284 
285 void starpu_execute_on_each_worker(void (*func)(void *), void *arg, uint32_t where);
286 
287 /* Same as starpu_execute_on_each_worker, except that the task name is specified in the "name" parameter. */
288 void starpu_execute_on_each_worker_ex(void (*func)(void *), void *arg, uint32_t where, const char * name);
289 
290 /* Call func(arg) on every worker in the "workers" array. "num_workers"
291  * indicates the number of workers in this array. This function is
292  * synchronous, but the different workers may execute the function in parallel.
293  * */
294 void starpu_execute_on_specific_workers(void (*func)(void*), void * arg, unsigned num_workers, unsigned * workers, const char * name);
295 
296 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);
297 
298 /* Return the current date in us */
299 double starpu_timing_now(void);
300 
301 #ifdef _WIN32
302 /* Try to fetch the system definition of timespec */
303 #include <sys/types.h>
304 #include <sys/stat.h>
305 #ifdef HAVE_UNISTD_H
306 #include <unistd.h>
307 #endif
308 #include <time.h>
309 #if !defined(_MSC_VER) || defined(BUILDING_STARPU)
310 #include <pthread.h>
311 #endif
312 #if !defined(STARPU_HAVE_STRUCT_TIMESPEC) || defined(_MSC_VER)
313 /* If it didn't get defined in the standard places, then define it ourself */
314 #ifndef STARPU_TIMESPEC_DEFINED
315 #define STARPU_TIMESPEC_DEFINED 1
316 struct timespec {
317  time_t tv_sec; /* Seconds */
318  long tv_nsec; /* Nanoseconds */
319 };
320 #endif /* STARPU_TIMESPEC_DEFINED */
321 #endif /* STARPU_HAVE_STRUCT_TIMESPEC */
322 /* Fetch gettimeofday on mingw/cygwin */
323 #if defined(__MINGW32__) || defined(__CYGWIN__)
324 #include <sys/time.h>
325 #endif
326 #else
327 #include <sys/time.h>
328 #endif /* _WIN32 */
329 
330 #ifdef __cplusplus
331 }
332 #endif
333 
334 #endif /* __STARPU_UTIL_H__ */