starpu_util.h 11 KB

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