starpu_util.h 15 KB

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