starpu_util.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2014 Université de Bordeaux 1
  4. * Copyright (C) 2010, 2011, 2012, 2013, 2014 Centre National de la Recherche Scientifique
  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 __cplusplus
  25. extern "C"
  26. {
  27. #endif
  28. #if defined __GNUC__ && defined __GNUC_MINOR__
  29. # define STARPU_GNUC_PREREQ(maj, min) \
  30. ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
  31. #else
  32. # define STARPU_GNUC_PREREQ(maj, min) 0
  33. #endif
  34. #ifdef __GNUC__
  35. # define STARPU_UNLIKELY(expr) (__builtin_expect(!!(expr),0))
  36. # define STARPU_LIKELY(expr) (__builtin_expect(!!(expr),1))
  37. # define STARPU_ATTRIBUTE_UNUSED __attribute__((unused))
  38. # define STARPU_ATTRIBUTE_INTERNAL __attribute__ ((visibility ("internal")))
  39. # define STARPU_ATTRIBUTE_MALLOC __attribute__((malloc))
  40. # define STARPU_ATTRIBUTE_WARN_UNUSED_RESULT __attribute__((warn_unused_result))
  41. # define STARPU_ATTRIBUTE_PURE __attribute__((pure))
  42. # define STARPU_ATTRIBUTE_ALIGNED(size) __attribute__((aligned(size)))
  43. #else
  44. # define STARPU_UNLIKELY(expr) (expr)
  45. # define STARPU_LIKELY(expr) (expr)
  46. # define STARPU_ATTRIBUTE_UNUSED
  47. # define STARPU_ATTRIBUTE_INTERNAL
  48. # define STARPU_ATTRIBUTE_MALLOC
  49. # define STARPU_ATTRIBUTE_WARN_UNUSED_RESULT
  50. # define STARPU_ATTRIBUTE_PURE
  51. # define STARPU_ATTRIBUTE_ALIGNED(size)
  52. #endif
  53. /* Note that if we're compiling C++, then just use the "inline"
  54. keyword, since it's part of C++ */
  55. #if defined(c_plusplus) || defined(__cplusplus)
  56. # define STARPU_INLINE inline
  57. #elif defined(_MSC_VER) || defined(__HP_cc)
  58. # define STARPU_INLINE __inline
  59. #else
  60. # define STARPU_INLINE __inline__
  61. #endif
  62. #if STARPU_GNUC_PREREQ(3, 1) && !defined(BUILDING_STARPU) && !defined(STARPU_USE_DEPRECATED_API) && !defined(STARPU_USE_DEPRECATED_ONE_ZERO_API)
  63. #define STARPU_DEPRECATED __attribute__((__deprecated__))
  64. #else
  65. #define STARPU_DEPRECATED
  66. #endif /* __GNUC__ */
  67. #if STARPU_GNUC_PREREQ(3,3)
  68. #define STARPU_WARN_UNUSED_RESULT __attribute__((__warn_unused_result__))
  69. #else
  70. #define STARPU_WARN_UNUSED_RESULT
  71. #endif /* __GNUC__ */
  72. #define STARPU_POISON_PTR ((void *)0xdeadbeef)
  73. #define STARPU_MIN(a,b) ((a)<(b)?(a):(b))
  74. #define STARPU_MAX(a,b) ((a)<(b)?(b):(a))
  75. #ifdef STARPU_NO_ASSERT
  76. #define STARPU_ASSERT(x) do { } while(0)
  77. #define STARPU_ASSERT_MSG(x, msg, ...) do { } while(0)
  78. #else
  79. # if defined(__CUDACC__) && defined(STARPU_HAVE_WINDOWS)
  80. # define STARPU_ASSERT(x) do { if (STARPU_UNLIKELY(!(x))) *(int*)NULL = 0; } while(0)
  81. # 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)
  82. # else
  83. # define STARPU_ASSERT(x) assert(x)
  84. # 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__); } ; assert(x); } while(0)
  85. # endif
  86. #endif
  87. #define STARPU_ABORT() do { \
  88. fprintf(stderr, "[starpu][abort][%s()@%s:%d]\n", __starpu_func__, __FILE__, __LINE__); \
  89. abort(); \
  90. } while(0)
  91. #define STARPU_ABORT_MSG(msg, ...) do { \
  92. fprintf(stderr, "[starpu][abort][%s()@%s:%d] " msg "\n", __starpu_func__, __FILE__, __LINE__, ## __VA_ARGS__); \
  93. abort(); \
  94. } while(0)
  95. #if defined(STARPU_HAVE_STRERROR_R)
  96. # define STARPU_CHECK_RETURN_VALUE(err, message, ...) {if (STARPU_UNLIKELY(err != 0)) { \
  97. char xmessage[256]; strerror_r(-err, xmessage, 256); \
  98. fprintf(stderr, "[starpu] Unexpected value: <%d:%s> returned for " message "\n", err, xmessage, ## __VA_ARGS__); \
  99. STARPU_ABORT(); }}
  100. # define STARPU_CHECK_RETURN_VALUE_IS(err, value, message, ...) {if (STARPU_UNLIKELY(err != value)) { \
  101. char xmessage[256]; strerror_r(-err, xmessage, 256); \
  102. fprintf(stderr, "[starpu] Unexpected value: <%d!=%d:%s> returned for " message "\n", err, value, xmessage, ## __VA_ARGS__); \
  103. STARPU_ABORT(); }}
  104. #else
  105. # define STARPU_CHECK_RETURN_VALUE(err, message, ...) {if (STARPU_UNLIKELY(err != 0)) { \
  106. fprintf(stderr, "[starpu] Unexpected value: <%d> returned for " message "\n", err, ## __VA_ARGS__); \
  107. STARPU_ABORT(); }}
  108. # define STARPU_CHECK_RETURN_VALUE_IS(err, value, message, ...) {if (STARPU_UNLIKELY(err != value)) { \
  109. fprintf(stderr, "[starpu] Unexpected value: <%d != %d> returned for " message "\n", err, value, ## __VA_ARGS__); \
  110. STARPU_ABORT(); }}
  111. #endif /* STARPU_HAVE_STRERROR_R */
  112. #if defined(__i386__) || defined(__x86_64__)
  113. static __starpu_inline unsigned starpu_cmpxchg(unsigned *ptr, unsigned old, unsigned next)
  114. {
  115. __asm__ __volatile__("lock cmpxchgl %2,%1": "+a" (old), "+m" (*ptr) : "q" (next) : "memory");
  116. return old;
  117. }
  118. static __starpu_inline unsigned starpu_xchg(unsigned *ptr, unsigned next)
  119. {
  120. /* Note: xchg is always locked already */
  121. __asm__ __volatile__("xchgl %1,%0": "+m" (*ptr), "+q" (next) : : "memory");
  122. return next;
  123. }
  124. #define STARPU_HAVE_XCHG
  125. #endif
  126. #define STARPU_ATOMIC_SOMETHING(name,expr) \
  127. static __starpu_inline unsigned starpu_atomic_##name(unsigned *ptr, unsigned value) \
  128. { \
  129. unsigned old, next; \
  130. while (1) \
  131. { \
  132. old = *ptr; \
  133. next = expr; \
  134. if (starpu_cmpxchg(ptr, old, next) == old) \
  135. break; \
  136. }; \
  137. return expr; \
  138. }
  139. #ifdef STARPU_HAVE_SYNC_FETCH_AND_ADD
  140. #define STARPU_ATOMIC_ADD(ptr, value) (__sync_fetch_and_add ((ptr), (value)) + (value))
  141. #elif defined(STARPU_HAVE_XCHG)
  142. STARPU_ATOMIC_SOMETHING(add, old + value)
  143. #define STARPU_ATOMIC_ADD(ptr, value) starpu_atomic_add(ptr, value)
  144. #endif
  145. #ifdef STARPU_HAVE_SYNC_FETCH_AND_OR
  146. #define STARPU_ATOMIC_OR(ptr, value) (__sync_fetch_and_or ((ptr), (value)))
  147. #elif defined(STARPU_HAVE_XCHG)
  148. STARPU_ATOMIC_SOMETHING(or, old | value)
  149. #define STARPU_ATOMIC_OR(ptr, value) starpu_atomic_or(ptr, value)
  150. #endif
  151. #ifdef STARPU_HAVE_SYNC_BOOL_COMPARE_AND_SWAP
  152. #define STARPU_BOOL_COMPARE_AND_SWAP(ptr, old, value) (__sync_bool_compare_and_swap ((ptr), (old), (value)))
  153. #elif defined(STARPU_HAVE_XCHG)
  154. #define STARPU_BOOL_COMPARE_AND_SWAP(ptr, old, value) (starpu_cmpxchg((ptr), (old), (value)) == (old))
  155. #endif
  156. #ifdef STARPU_HAVE_SYNC_VAL_COMPARE_AND_SWAP
  157. #define STARPU_VAL_COMPARE_AND_SWAP(ptr, old, value) (__sync_val_compare_and_swap ((ptr), (old), (value)))
  158. #elif defined(STARPU_HAVE_XCHG)
  159. #define STARPU_VAL_COMPARE_AND_SWAP(ptr, old, value) (starpu_cmpxchg((ptr), (old), (value)))
  160. #endif
  161. #ifdef STARPU_HAVE_SYNC_LOCK_TEST_AND_SET
  162. #define STARPU_TEST_AND_SET(ptr, value) (__sync_lock_test_and_set ((ptr), (value)))
  163. #define STARPU_RELEASE(ptr) (__sync_lock_release ((ptr)))
  164. #elif defined(STARPU_HAVE_XCHG)
  165. #define STARPU_TEST_AND_SET(ptr, value) (starpu_xchg((ptr), (value)))
  166. #define STARPU_RELEASE(ptr) (starpu_xchg((ptr), 0))
  167. #endif
  168. #ifdef STARPU_HAVE_SYNC_SYNCHRONIZE
  169. #define STARPU_SYNCHRONIZE() __sync_synchronize()
  170. #elif defined(__i386__)
  171. #define STARPU_SYNCHRONIZE() __asm__ __volatile__("lock; addl $0,0(%%esp)" ::: "memory")
  172. #elif defined(__x86_64__)
  173. #define STARPU_SYNCHRONIZE() __asm__ __volatile__("mfence" ::: "memory")
  174. #elif defined(__ppc__) || defined(__ppc64__)
  175. #define STARPU_SYNCHRONIZE() __asm__ __volatile__("sync" ::: "memory")
  176. #endif
  177. #if defined(__i386__) || defined(__KNC__) || defined(__KNF__)
  178. #define STARPU_RMB() __asm__ __volatile__("lock; addl $0,0(%%esp)" ::: "memory")
  179. #define STARPU_WMB() __asm__ __volatile__("lock; addl $0,0(%%esp)" ::: "memory")
  180. #elif defined(__x86_64__)
  181. #define STARPU_RMB() __asm__ __volatile__("lfence" ::: "memory")
  182. #define STARPU_WMB() __asm__ __volatile__("sfence" ::: "memory")
  183. #elif defined(__ppc__) || defined(__ppc64__)
  184. #define STARPU_RMB() __asm__ __volatile__("sync" ::: "memory")
  185. #define STARPU_WMB() __asm__ __volatile__("sync" ::: "memory")
  186. #else
  187. #define STARPU_RMB() STARPU_SYNCHRONIZE()
  188. #define STARPU_WMB() STARPU_SYNCHRONIZE()
  189. #endif
  190. #ifdef __cplusplus
  191. }
  192. #endif
  193. /* Include this only here so that <starpu_data_interfaces.h> can use the
  194. * macros above. */
  195. #include <starpu_task.h>
  196. #ifdef __cplusplus
  197. extern "C"
  198. {
  199. #endif
  200. static __starpu_inline int starpu_get_env_number(const char *str)
  201. {
  202. char *strval;
  203. strval = getenv(str);
  204. if (strval)
  205. {
  206. /* the env variable was actually set */
  207. long int val;
  208. char *check;
  209. val = strtol(strval, &check, 10);
  210. if (*check) {
  211. fprintf(stderr,"The %s environment variable must contain an integer\n", str);
  212. STARPU_ABORT();
  213. }
  214. /* fprintf(stderr, "ENV %s WAS %d\n", str, val); */
  215. return (int)val;
  216. }
  217. else
  218. {
  219. /* there is no such env variable */
  220. /* fprintf("There was no %s ENV\n", str); */
  221. return -1;
  222. }
  223. }
  224. static __starpu_inline int starpu_get_env_number_default(const char *str, int defval)
  225. {
  226. int ret = starpu_get_env_number(str);
  227. if (ret == -1)
  228. ret = defval;
  229. return ret;
  230. }
  231. void starpu_execute_on_each_worker(void (*func)(void *), void *arg, uint32_t where);
  232. void starpu_execute_on_each_worker_ex(void (*func)(void *), void *arg, uint32_t where, const char *name);
  233. void starpu_execute_on_specific_workers(void (*func)(void*), void *arg, unsigned num_workers, unsigned *workers, const char *name);
  234. 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);
  235. double starpu_timing_now(void);
  236. #ifdef _WIN32
  237. /* Try to fetch the system definition of timespec */
  238. #include <time.h>
  239. #if !defined(_MSC_VER) || defined(BUILDING_STARPU)
  240. #include <pthread.h>
  241. #endif
  242. #ifndef _TIMESPEC_DEFINED
  243. /* If it didn't get defined in the standard places, then define it ourself */
  244. #ifndef STARPU_TIMESPEC_DEFINED
  245. #define STARPU_TIMESPEC_DEFINED 1
  246. struct timespec {
  247. time_t tv_sec; /* Seconds */
  248. long tv_nsec; /* Nanoseconds */
  249. };
  250. #endif /* STARPU_TIMESPEC_DEFINED */
  251. #endif
  252. #else
  253. #include <sys/time.h>
  254. #endif /* _WIN32 */
  255. #ifdef __cplusplus
  256. }
  257. #endif
  258. #endif /* __STARPU_UTIL_H__ */