starpu_util.h 15 KB

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