starpu_util.h 15 KB

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