StarPU Handbook

- Main Page
- Related Pages
- Modules
- Data Structures
- Files

- File List
- Globals

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-2014 Université de Bordeaux 1
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 __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, "[starpu][%s][assert failure] " msg "\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, "[starpu][%s][assert failure] " msg "\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_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))
185 #endif
186
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")
195 #endif
196
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")
206 #else
207 #define STARPU_RMB() STARPU_SYNCHRONIZE()
208 #define STARPU_WMB() STARPU_SYNCHRONIZE()
209 #endif
210
211 #ifdef __cplusplus
212 }
213 #endif
214
215 /* Include this only here so that <starpu_data_interfaces.h> can use the
216 * macros above. */
217 #include <starpu_task.h>
218
219 #ifdef __cplusplus
220 extern "C"
221 {
222 #endif
223
224 static __starpu_inline int starpu_get_env_number(const char *str)
225 {
226 char *strval;
227
228 strval = getenv(str);
229 if (strval)
230 {
231 /* the env variable was actually set */
232 long int val;
233 char *check;
234
235 val = strtol(strval, &check, 10);
236 if (*check) {
237 fprintf(stderr,"The %s environment variable must contain an integer\n", str);
238 STARPU_ABORT();
239 }
240
241 /* fprintf(stderr, "ENV %s WAS %d\n", str, val); */
242 return (int)val;
243 }
244 else
245 {
246 /* there is no such env variable */
247 /* fprintf("There was no %s ENV\n", str); */
248 return -1;
249 }
250 }
251
252 /* Add an event in the execution trace if FxT is enabled */
253 void starpu_trace_user_event(unsigned long code);
254
255 void starpu_execute_on_each_worker(void (*func)(void *), void *arg, uint32_t where);
256
257 /* Same as starpu_execute_on_each_worker, except that the task name is specified in the "name" parameter. */
258 void starpu_execute_on_each_worker_ex(void (*func)(void *), void *arg, uint32_t where, const char * name);
259
260 /* Call func(arg) on every worker in the "workers" array. "num_workers"
261 * indicates the number of workers in this array. This function is
262 * synchronous, but the different workers may execute the function in parallel.
263 * */
264 void starpu_execute_on_specific_workers(void (*func)(void*), void * arg, unsigned num_workers, unsigned * workers, const char * name);
265
266 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);
267
268 /* Return the current date in us */
269 double starpu_timing_now(void);
270
271 #ifdef __cplusplus
272 }
273 #endif
274
275 #endif /* __STARPU_UTIL_H__ */
starpu_timing_now
double starpu_timing_now(void)
starpu_task.h
starpu_top_param::name
const char * name
Definition: starpu_top.h:61
starpu_top_param::next
struct starpu_top_param * next
Definition: starpu_top.h:71
starpu_get_env_number
static __starpu_inline int starpu_get_env_number(const char *str)
Definition: starpu_util.h:224
starpu_config.h
starpu_data_cpy
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)
starpu_data_handle_t
struct _starpu_data_state * starpu_data_handle_t
Definition: starpu_data.h:29
STARPU_ABORT
#define STARPU_ABORT()
Definition: starpu_util.h:101
starpu_execute_on_each_worker
void starpu_execute_on_each_worker(void(*func)(void *), void *arg, uint32_t where)
starpu_top_param::value
void * value
Definition: starpu_top.h:63