starpu_util.h 14 KB

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