starpu_util.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2015 Université de Bordeaux
  4. * Copyright (C) 2010, 2011, 2012, 2013, 2014 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))) *(int*)NULL = 0; } while(0)
  96. # define STARPU_ASSERT_MSG(x, msg, ...) do { if (STARPU_UNLIKELY(!(x))) { 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. #define STARPU_ABORT() do { \
  103. fprintf(stderr, "[starpu][abort][%s()@%s:%d]\n", __starpu_func__, __FILE__, __LINE__); \
  104. abort(); \
  105. } while(0)
  106. #define STARPU_ABORT_MSG(msg, ...) do { \
  107. fprintf(stderr, "[starpu][abort][%s()@%s:%d] " msg "\n", __starpu_func__, __FILE__, __LINE__, ## __VA_ARGS__); \
  108. abort(); \
  109. } while(0)
  110. #if defined(STARPU_HAVE_STRERROR_R)
  111. # define STARPU_CHECK_RETURN_VALUE(err, message, ...) {if (STARPU_UNLIKELY(err != 0)) { \
  112. char xmessage[256]; strerror_r(-err, xmessage, 256); \
  113. fprintf(stderr, "[starpu] Unexpected value: <%d:%s> returned for " message "\n", err, xmessage, ## __VA_ARGS__); \
  114. STARPU_ABORT(); }}
  115. # define STARPU_CHECK_RETURN_VALUE_IS(err, value, message, ...) {if (STARPU_UNLIKELY(err != value)) { \
  116. char xmessage[256]; strerror_r(-err, xmessage, 256); \
  117. fprintf(stderr, "[starpu] Unexpected value: <%d!=%d:%s> returned for " message "\n", err, value, xmessage, ## __VA_ARGS__); \
  118. STARPU_ABORT(); }}
  119. #else
  120. # define STARPU_CHECK_RETURN_VALUE(err, message, ...) {if (STARPU_UNLIKELY(err != 0)) { \
  121. fprintf(stderr, "[starpu] Unexpected value: <%d> returned for " message "\n", err, ## __VA_ARGS__); \
  122. STARPU_ABORT(); }}
  123. # define STARPU_CHECK_RETURN_VALUE_IS(err, value, message, ...) {if (STARPU_UNLIKELY(err != value)) { \
  124. fprintf(stderr, "[starpu] Unexpected value: <%d != %d> returned for " message "\n", err, value, ## __VA_ARGS__); \
  125. STARPU_ABORT(); }}
  126. #endif /* STARPU_HAVE_STRERROR_R */
  127. #if defined(__i386__) || defined(__x86_64__)
  128. static __starpu_inline unsigned starpu_cmpxchg(unsigned *ptr, unsigned old, unsigned next)
  129. {
  130. __asm__ __volatile__("lock cmpxchgl %2,%1": "+a" (old), "+m" (*ptr) : "q" (next) : "memory");
  131. return old;
  132. }
  133. static __starpu_inline unsigned starpu_xchg(unsigned *ptr, unsigned next)
  134. {
  135. /* Note: xchg is always locked already */
  136. __asm__ __volatile__("xchgl %1,%0": "+m" (*ptr), "+q" (next) : : "memory");
  137. return next;
  138. }
  139. #define STARPU_HAVE_XCHG
  140. #endif
  141. #define STARPU_ATOMIC_SOMETHING(name,expr) \
  142. static __starpu_inline unsigned starpu_atomic_##name(unsigned *ptr, unsigned value) \
  143. { \
  144. unsigned old, next; \
  145. while (1) \
  146. { \
  147. old = *ptr; \
  148. next = expr; \
  149. if (starpu_cmpxchg(ptr, old, next) == old) \
  150. break; \
  151. }; \
  152. return expr; \
  153. }
  154. #ifdef STARPU_HAVE_SYNC_FETCH_AND_ADD
  155. #define STARPU_ATOMIC_ADD(ptr, value) (__sync_fetch_and_add ((ptr), (value)) + (value))
  156. #elif defined(STARPU_HAVE_XCHG)
  157. STARPU_ATOMIC_SOMETHING(add, old + value)
  158. #define STARPU_ATOMIC_ADD(ptr, value) starpu_atomic_add(ptr, value)
  159. #endif
  160. #ifdef STARPU_HAVE_SYNC_FETCH_AND_OR
  161. #define STARPU_ATOMIC_OR(ptr, value) (__sync_fetch_and_or ((ptr), (value)))
  162. #elif defined(STARPU_HAVE_XCHG)
  163. STARPU_ATOMIC_SOMETHING(or, old | value)
  164. #define STARPU_ATOMIC_OR(ptr, value) starpu_atomic_or(ptr, value)
  165. #endif
  166. #ifdef STARPU_HAVE_SYNC_BOOL_COMPARE_AND_SWAP
  167. #define STARPU_BOOL_COMPARE_AND_SWAP(ptr, old, value) (__sync_bool_compare_and_swap ((ptr), (old), (value)))
  168. #elif defined(STARPU_HAVE_XCHG)
  169. #define STARPU_BOOL_COMPARE_AND_SWAP(ptr, old, value) (starpu_cmpxchg((ptr), (old), (value)) == (old))
  170. #endif
  171. #ifdef STARPU_HAVE_SYNC_VAL_COMPARE_AND_SWAP
  172. #define STARPU_VAL_COMPARE_AND_SWAP(ptr, old, value) (__sync_val_compare_and_swap ((ptr), (old), (value)))
  173. #elif defined(STARPU_HAVE_XCHG)
  174. #define STARPU_VAL_COMPARE_AND_SWAP(ptr, old, value) (starpu_cmpxchg((ptr), (old), (value)))
  175. #endif
  176. #ifdef STARPU_HAVE_SYNC_LOCK_TEST_AND_SET
  177. #define STARPU_TEST_AND_SET(ptr, value) (__sync_lock_test_and_set ((ptr), (value)))
  178. #define STARPU_RELEASE(ptr) (__sync_lock_release ((ptr)))
  179. #elif defined(STARPU_HAVE_XCHG)
  180. #define STARPU_TEST_AND_SET(ptr, value) (starpu_xchg((ptr), (value)))
  181. #define STARPU_RELEASE(ptr) (starpu_xchg((ptr), 0))
  182. #endif
  183. #ifdef STARPU_HAVE_SYNC_SYNCHRONIZE
  184. #define STARPU_SYNCHRONIZE() __sync_synchronize()
  185. #elif defined(__i386__)
  186. #define STARPU_SYNCHRONIZE() __asm__ __volatile__("lock; addl $0,0(%%esp)" ::: "memory")
  187. #elif defined(__x86_64__)
  188. #define STARPU_SYNCHRONIZE() __asm__ __volatile__("mfence" ::: "memory")
  189. #elif defined(__ppc__) || defined(__ppc64__)
  190. #define STARPU_SYNCHRONIZE() __asm__ __volatile__("sync" ::: "memory")
  191. #endif
  192. #if defined(__i386__) || defined(__KNC__) || defined(__KNF__)
  193. #define STARPU_RMB() __asm__ __volatile__("lock; addl $0,0(%%esp)" ::: "memory")
  194. #define STARPU_WMB() __asm__ __volatile__("lock; addl $0,0(%%esp)" ::: "memory")
  195. #elif defined(__x86_64__)
  196. #define STARPU_RMB() __asm__ __volatile__("lfence" ::: "memory")
  197. #define STARPU_WMB() __asm__ __volatile__("sfence" ::: "memory")
  198. #elif defined(__ppc__) || defined(__ppc64__)
  199. #define STARPU_RMB() __asm__ __volatile__("sync" ::: "memory")
  200. #define STARPU_WMB() __asm__ __volatile__("sync" ::: "memory")
  201. #else
  202. #define STARPU_RMB() STARPU_SYNCHRONIZE()
  203. #define STARPU_WMB() STARPU_SYNCHRONIZE()
  204. #endif
  205. #ifdef __cplusplus
  206. }
  207. #endif
  208. /* Include this only here so that <starpu_data_interfaces.h> can use the
  209. * macros above. */
  210. #include <starpu_task.h>
  211. #ifdef __cplusplus
  212. extern "C"
  213. {
  214. #endif
  215. extern int _starpu_silent;
  216. char *starpu_getenv(const char *str);
  217. static __starpu_inline int starpu_get_env_number(const char *str)
  218. {
  219. char *strval;
  220. strval = starpu_getenv(str);
  221. if (strval)
  222. {
  223. /* the env variable was actually set */
  224. long int val;
  225. char *check;
  226. val = strtol(strval, &check, 10);
  227. if (*check) {
  228. fprintf(stderr,"The %s environment variable must contain an integer\n", str);
  229. STARPU_ABORT();
  230. }
  231. /* fprintf(stderr, "ENV %s WAS %d\n", str, val); */
  232. return (int)val;
  233. }
  234. else
  235. {
  236. /* there is no such env variable */
  237. /* fprintf("There was no %s ENV\n", str); */
  238. return -1;
  239. }
  240. }
  241. static __starpu_inline int starpu_get_env_number_default(const char *str, int defval)
  242. {
  243. int ret = starpu_get_env_number(str);
  244. if (ret == -1)
  245. ret = defval;
  246. return ret;
  247. }
  248. static __starpu_inline float starpu_get_env_float_default(const char *str, float defval)
  249. {
  250. char *strval;
  251. strval = starpu_getenv(str);
  252. if (strval)
  253. {
  254. /* the env variable was actually set */
  255. float val;
  256. char *check;
  257. val = strtof(strval, &check);
  258. if (*check) {
  259. fprintf(stderr,"The %s environment variable must contain a float\n", str);
  260. STARPU_ABORT();
  261. }
  262. /* fprintf(stderr, "ENV %s WAS %f\n", str, val); */
  263. return val;
  264. }
  265. else
  266. {
  267. /* there is no such env variable */
  268. /* fprintf("There was no %s ENV\n", str); */
  269. return defval;
  270. }
  271. }
  272. void starpu_execute_on_each_worker(void (*func)(void *), void *arg, uint32_t where);
  273. void starpu_execute_on_each_worker_ex(void (*func)(void *), void *arg, uint32_t where, const char *name);
  274. void starpu_execute_on_specific_workers(void (*func)(void*), void *arg, unsigned num_workers, unsigned *workers, const char *name);
  275. 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);
  276. double starpu_timing_now(void);
  277. #ifdef _WIN32
  278. /* Try to fetch the system definition of timespec */
  279. #include <sys/types.h>
  280. #include <sys/stat.h>
  281. #ifdef HAVE_UNISTD_H
  282. #include <unistd.h>
  283. #endif
  284. #include <time.h>
  285. #if !defined(_MSC_VER) || defined(BUILDING_STARPU)
  286. #include <pthread.h>
  287. #endif
  288. #if !defined(STARPU_HAVE_STRUCT_TIMESPEC) || defined(_MSC_VER)
  289. /* If it didn't get defined in the standard places, then define it ourself */
  290. #ifndef STARPU_TIMESPEC_DEFINED
  291. #define STARPU_TIMESPEC_DEFINED 1
  292. struct timespec {
  293. time_t tv_sec; /* Seconds */
  294. long tv_nsec; /* Nanoseconds */
  295. };
  296. #endif /* STARPU_TIMESPEC_DEFINED */
  297. #endif /* STARPU_HAVE_STRUCT_TIMESPEC */
  298. /* Fetch gettimeofday on mingw/cygwin */
  299. #if defined(__MINGW32__) || defined(__CYGWIN__)
  300. #include <sys/time.h>
  301. #endif
  302. #else
  303. #include <sys/time.h>
  304. #endif /* _WIN32 */
  305. #ifdef __cplusplus
  306. }
  307. #endif
  308. #endif /* __STARPU_UTIL_H__ */