starpu_util.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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. #define starpu_cmpxchgl starpu_cmpxchg
  149. #define starpu_xchgl starpu_xchg
  150. #define STARPU_HAVE_XCHGL
  151. #endif
  152. #if defined(__x86_64__)
  153. static __starpu_inline unsigned long starpu_cmpxchgl(unsigned long *ptr, unsigned long old, unsigned long next)
  154. {
  155. __asm__ __volatile__("lock cmpxchgq %2,%1": "+a" (old), "+m" (*ptr) : "q" (next) : "memory");
  156. return old;
  157. }
  158. static __starpu_inline unsigned long starpu_xchgl(unsigned long *ptr, unsigned long next)
  159. {
  160. /* Note: xchg is always locked already */
  161. __asm__ __volatile__("xchgq %1,%0": "+m" (*ptr), "+q" (next) : : "memory");
  162. return next;
  163. }
  164. #define STARPU_HAVE_XCHGL
  165. #endif
  166. #endif
  167. #define STARPU_ATOMIC_SOMETHING(name,expr) \
  168. static __starpu_inline unsigned starpu_atomic_##name(unsigned *ptr, unsigned value) \
  169. { \
  170. unsigned old, next; \
  171. while (1) \
  172. { \
  173. old = *ptr; \
  174. next = expr; \
  175. if (starpu_cmpxchg(ptr, old, next) == old) \
  176. break; \
  177. }; \
  178. return expr; \
  179. }
  180. #define STARPU_ATOMIC_SOMETHINGL(name,expr) \
  181. static __starpu_inline unsigned long starpu_atomic_##name##l(unsigned long *ptr, unsigned long value) \
  182. { \
  183. unsigned long old, next; \
  184. while (1) \
  185. { \
  186. old = *ptr; \
  187. next = expr; \
  188. if (starpu_cmpxchg(ptr, old, next) == old) \
  189. break; \
  190. }; \
  191. return expr; \
  192. }
  193. #ifdef STARPU_HAVE_SYNC_FETCH_AND_ADD
  194. #define STARPU_ATOMIC_ADD(ptr, value) (__sync_fetch_and_add ((ptr), (value)) + (value))
  195. #define STARPU_ATOMIC_ADDL(ptr, value) (__sync_fetch_and_add ((ptr), (value)) + (value))
  196. #else
  197. #if defined(STARPU_HAVE_XCHG)
  198. STARPU_ATOMIC_SOMETHING(add, old + value)
  199. #define STARPU_ATOMIC_ADD(ptr, value) starpu_atomic_add(ptr, value)
  200. #endif
  201. #if defined(STARPU_HAVE_XCHGL)
  202. STARPU_ATOMIC_SOMETHINGL(add, old + value)
  203. #define STARPU_ATOMIC_ADDL(ptr, value) starpu_atomic_addl(ptr, value)
  204. #endif
  205. #endif
  206. #ifdef STARPU_HAVE_SYNC_FETCH_AND_OR
  207. #define STARPU_ATOMIC_OR(ptr, value) (__sync_fetch_and_or ((ptr), (value)))
  208. #define STARPU_ATOMIC_ORL(ptr, value) (__sync_fetch_and_or ((ptr), (value)))
  209. #else
  210. #if defined(STARPU_HAVE_XCHG)
  211. STARPU_ATOMIC_SOMETHING(or, old | value)
  212. #define STARPU_ATOMIC_OR(ptr, value) starpu_atomic_or(ptr, value)
  213. #endif
  214. #if defined(STARPU_HAVE_XCHGL)
  215. STARPU_ATOMIC_SOMETHINGL(or, old | value)
  216. #define STARPU_ATOMIC_ORL(ptr, value) starpu_atomic_orl(ptr, value)
  217. #endif
  218. #endif
  219. #ifdef STARPU_HAVE_SYNC_BOOL_COMPARE_AND_SWAP
  220. #define STARPU_BOOL_COMPARE_AND_SWAP(ptr, old, value) (__sync_bool_compare_and_swap ((ptr), (old), (value)))
  221. #elif defined(STARPU_HAVE_XCHG)
  222. #define STARPU_BOOL_COMPARE_AND_SWAP(ptr, old, value) (starpu_cmpxchg((ptr), (old), (value)) == (old))
  223. #endif
  224. #ifdef STARPU_HAVE_SYNC_VAL_COMPARE_AND_SWAP
  225. #define STARPU_VAL_COMPARE_AND_SWAP(ptr, old, value) (__sync_val_compare_and_swap ((ptr), (old), (value)))
  226. #elif defined(STARPU_HAVE_XCHG)
  227. #define STARPU_VAL_COMPARE_AND_SWAP(ptr, old, value) (starpu_cmpxchg((ptr), (old), (value)))
  228. #endif
  229. #ifdef STARPU_HAVE_SYNC_LOCK_TEST_AND_SET
  230. #define STARPU_TEST_AND_SET(ptr, value) (__sync_lock_test_and_set ((ptr), (value)))
  231. #define STARPU_RELEASE(ptr) (__sync_lock_release ((ptr)))
  232. #elif defined(STARPU_HAVE_XCHG)
  233. #define STARPU_TEST_AND_SET(ptr, value) (starpu_xchg((ptr), (value)))
  234. #define STARPU_RELEASE(ptr) (starpu_xchg((ptr), 0))
  235. #endif
  236. #ifdef STARPU_HAVE_SYNC_SYNCHRONIZE
  237. #define STARPU_SYNCHRONIZE() __sync_synchronize()
  238. #elif defined(__i386__)
  239. #define STARPU_SYNCHRONIZE() __asm__ __volatile__("lock; addl $0,0(%%esp)" ::: "memory")
  240. #elif defined(__KNC__) || defined(__KNF__)
  241. #define STARPU_SYNCHRONIZE() __asm__ __volatile__("lock; addl $0,0(%%rsp)" ::: "memory")
  242. #elif defined(__x86_64__)
  243. #define STARPU_SYNCHRONIZE() __asm__ __volatile__("mfence" ::: "memory")
  244. #elif defined(__ppc__) || defined(__ppc64__)
  245. #define STARPU_SYNCHRONIZE() __asm__ __volatile__("sync" ::: "memory")
  246. #endif
  247. #if defined(__i386__)
  248. #define STARPU_RMB() __asm__ __volatile__("lock; addl $0,0(%%esp)" ::: "memory")
  249. #define STARPU_WMB() __asm__ __volatile__("lock; addl $0,0(%%esp)" ::: "memory")
  250. #elif defined(__KNC__) || defined(__KNF__)
  251. #define STARPU_RMB() __asm__ __volatile__("lock; addl $0,0(%%rsp)" ::: "memory")
  252. #define STARPU_WMB() __asm__ __volatile__("lock; addl $0,0(%%rsp)" ::: "memory")
  253. #elif defined(__x86_64__)
  254. #define STARPU_RMB() __asm__ __volatile__("lfence" ::: "memory")
  255. #define STARPU_WMB() __asm__ __volatile__("sfence" ::: "memory")
  256. #elif defined(__ppc__) || defined(__ppc64__)
  257. #define STARPU_RMB() __asm__ __volatile__("sync" ::: "memory")
  258. #define STARPU_WMB() __asm__ __volatile__("sync" ::: "memory")
  259. #else
  260. #define STARPU_RMB() STARPU_SYNCHRONIZE()
  261. #define STARPU_WMB() STARPU_SYNCHRONIZE()
  262. #endif
  263. #ifdef __cplusplus
  264. }
  265. #endif
  266. /* Include this only here so that <starpu_data_interfaces.h> can use the
  267. * macros above. */
  268. #include <starpu_task.h>
  269. #ifdef __cplusplus
  270. extern "C"
  271. {
  272. #endif
  273. extern int _starpu_silent;
  274. char *starpu_getenv(const char *str);
  275. static __starpu_inline int starpu_get_env_number(const char *str)
  276. {
  277. char *strval;
  278. strval = starpu_getenv(str);
  279. if (strval)
  280. {
  281. /* the env variable was actually set */
  282. long int val;
  283. char *check;
  284. val = strtol(strval, &check, 10);
  285. if (*check) {
  286. fprintf(stderr,"The %s environment variable must contain an integer\n", str);
  287. STARPU_ABORT();
  288. }
  289. /* fprintf(stderr, "ENV %s WAS %d\n", str, val); */
  290. return (int)val;
  291. }
  292. else
  293. {
  294. /* there is no such env variable */
  295. /* fprintf("There was no %s ENV\n", str); */
  296. return -1;
  297. }
  298. }
  299. static __starpu_inline int starpu_get_env_number_default(const char *str, int defval)
  300. {
  301. int ret = starpu_get_env_number(str);
  302. if (ret == -1)
  303. ret = defval;
  304. return ret;
  305. }
  306. static __starpu_inline float starpu_get_env_float_default(const char *str, float defval)
  307. {
  308. char *strval;
  309. strval = starpu_getenv(str);
  310. if (strval)
  311. {
  312. /* the env variable was actually set */
  313. float val;
  314. char *check;
  315. val = strtof(strval, &check);
  316. if (*check) {
  317. fprintf(stderr,"The %s environment variable must contain a float\n", str);
  318. STARPU_ABORT();
  319. }
  320. /* fprintf(stderr, "ENV %s WAS %f\n", str, val); */
  321. return val;
  322. }
  323. else
  324. {
  325. /* there is no such env variable */
  326. /* fprintf("There was no %s ENV\n", str); */
  327. return defval;
  328. }
  329. }
  330. void starpu_execute_on_each_worker(void (*func)(void *), void *arg, uint32_t where);
  331. void starpu_execute_on_each_worker_ex(void (*func)(void *), void *arg, uint32_t where, const char *name);
  332. void starpu_execute_on_specific_workers(void (*func)(void*), void *arg, unsigned num_workers, unsigned *workers, const char *name);
  333. 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);
  334. double starpu_timing_now(void);
  335. #ifdef _WIN32
  336. /* Try to fetch the system definition of timespec */
  337. #include <sys/types.h>
  338. #include <sys/stat.h>
  339. #ifdef HAVE_UNISTD_H
  340. #include <unistd.h>
  341. #endif
  342. #include <time.h>
  343. #if !defined(_MSC_VER) || defined(BUILDING_STARPU)
  344. #include <pthread.h>
  345. #endif
  346. #if !defined(STARPU_HAVE_STRUCT_TIMESPEC) || (defined(_MSC_VER) && _MSC_VER < 1900)
  347. /* If it didn't get defined in the standard places, then define it ourself */
  348. #ifndef STARPU_TIMESPEC_DEFINED
  349. #define STARPU_TIMESPEC_DEFINED 1
  350. struct timespec
  351. {
  352. time_t tv_sec; /* Seconds */
  353. long tv_nsec; /* Nanoseconds */
  354. };
  355. #endif /* STARPU_TIMESPEC_DEFINED */
  356. #endif /* STARPU_HAVE_STRUCT_TIMESPEC */
  357. /* Fetch gettimeofday on mingw/cygwin */
  358. #if defined(__MINGW32__) || defined(__CYGWIN__)
  359. #include <sys/time.h>
  360. #endif
  361. #else
  362. #include <sys/time.h>
  363. #endif /* _WIN32 */
  364. #ifdef __cplusplus
  365. }
  366. #endif
  367. #endif /* __STARPU_UTIL_H__ */