starpu_util.h 15 KB

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