starpu_util.h 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2013 Université de Bordeaux 1
  4. * Copyright (C) 2010, 2011, 2012, 2013 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.h>
  24. #ifdef __cplusplus
  25. extern "C"
  26. {
  27. #endif
  28. /* Return true (non-zero) if GCC version MAJ.MIN or later is being used
  29. * (macro taken from glibc.) */
  30. #if defined __GNUC__ && defined __GNUC_MINOR__
  31. # define STARPU_GNUC_PREREQ(maj, min) \
  32. ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
  33. #else
  34. # define STARPU_GNUC_PREREQ(maj, min) 0
  35. #endif
  36. #ifdef __GNUC__
  37. # define STARPU_UNLIKELY(expr) (__builtin_expect(!!(expr),0))
  38. # define STARPU_LIKELY(expr) (__builtin_expect(!!(expr),1))
  39. # define STARPU_ATTRIBUTE_UNUSED __attribute__((unused))
  40. # define STARPU_ATTRIBUTE_INTERNAL __attribute__ ((visibility ("internal")))
  41. #else
  42. # define STARPU_UNLIKELY(expr) (expr)
  43. # define STARPU_LIKELY(expr) (expr)
  44. # define STARPU_ATTRIBUTE_UNUSED
  45. # define STARPU_ATTRIBUTE_INTERNAL
  46. #endif
  47. #if STARPU_GNUC_PREREQ(3, 1) && !defined(BUILDING_STARPU) && !defined(STARPU_USE_DEPRECATED_API) && !defined(STARPU_USE_DEPRECATED_ONE_ZERO_API)
  48. #define STARPU_DEPRECATED __attribute__((__deprecated__))
  49. #else
  50. #define STARPU_DEPRECATED
  51. #endif /* __GNUC__ */
  52. #if STARPU_GNUC_PREREQ(3,3)
  53. #define STARPU_WARN_UNUSED_RESULT __attribute__((__warn_unused_result__))
  54. #else
  55. #define STARPU_WARN_UNUSED_RESULT
  56. #endif /* __GNUC__ */
  57. #define STARPU_POISON_PTR ((void *)0xdeadbeef)
  58. #define STARPU_MIN(a,b) ((a)<(b)?(a):(b))
  59. #define STARPU_MAX(a,b) ((a)<(b)?(b):(a))
  60. #ifdef STARPU_NO_ASSERT
  61. #define STARPU_ASSERT(x) do { (void) (x);} while(0)
  62. #define STARPU_ASSERT_MSG(x, msg, ...) do { (void) (x);} while(0)
  63. #else
  64. # if defined(__CUDACC__) && defined(STARPU_HAVE_WINDOWS)
  65. # define STARPU_ASSERT(x) do { if (STARPU_UNLIKELY(!(x))) *(int*)NULL = 0; } while(0)
  66. # define STARPU_ASSERT_MSG(x, msg, ...) do { if (STARPU_UNLIKELY(!(x))) { fprintf(stderr, "[starpu][%s][assert failure] " msg "\n", __starpu_func__, ## __VA_ARGS__); *(int*)NULL = 0; }} while(0)
  67. # else
  68. # define STARPU_ASSERT(x) assert(x)
  69. # define STARPU_ASSERT_MSG(x, msg, ...) do { if (STARPU_UNLIKELY(!(x))) { fprintf(stderr, "[starpu][%s][assert failure] " msg "\n", __starpu_func__, ## __VA_ARGS__); } ; assert(x); } while(0)
  70. # endif
  71. #endif
  72. #define STARPU_ABORT() do { \
  73. fprintf(stderr, "[starpu][abort] %s:%d %s\n", __FILE__, __LINE__, __starpu_func__); \
  74. abort(); \
  75. } while(0)
  76. #if defined(STARPU_HAVE_STRERROR_R)
  77. # define STARPU_CHECK_RETURN_VALUE(err, message, ...) {if (STARPU_UNLIKELY(err != 0)) { \
  78. char xmessage[256]; strerror_r(-err, xmessage, 256); \
  79. fprintf(stderr, "[starpu] Unexpected value: <%d:%s> returned for " message "\n", err, xmessage, ## __VA_ARGS__); \
  80. STARPU_ABORT(); }}
  81. # define STARPU_CHECK_RETURN_VALUE_IS(err, value, message, ...) {if (STARPU_UNLIKELY(err != value)) { \
  82. char xmessage[256]; strerror_r(-err, xmessage, 256); \
  83. fprintf(stderr, "[starpu] Unexpected value: <%d!=%d:%s> returned for " message "\n", err, value, xmessage, ## __VA_ARGS__); \
  84. STARPU_ABORT(); }}
  85. #else
  86. # define STARPU_CHECK_RETURN_VALUE(err, message, ...) {if (STARPU_UNLIKELY(err != 0)) { \
  87. fprintf(stderr, "[starpu] Unexpected value: <%d> returned for " message "\n", err, ## __VA_ARGS__); \
  88. STARPU_ABORT(); }}
  89. # define STARPU_CHECK_RETURN_VALUE_IS(err, value, message, ...) {if (STARPU_UNLIKELY(err != value)) { \
  90. fprintf(stderr, "[starpu] Unexpected value: <%d != %d> returned for " message "\n", err, value, ## __VA_ARGS__); \
  91. STARPU_ABORT(); }}
  92. #endif /* STARPU_HAVE_STRERROR_R */
  93. #if defined(__i386__) || defined(__x86_64__)
  94. static __starpu_inline unsigned starpu_cmpxchg(unsigned *ptr, unsigned old, unsigned next)
  95. {
  96. __asm__ __volatile__("lock cmpxchgl %2,%1": "+a" (old), "+m" (*ptr) : "q" (next) : "memory");
  97. return old;
  98. }
  99. static __starpu_inline unsigned starpu_xchg(unsigned *ptr, unsigned next)
  100. {
  101. /* Note: xchg is always locked already */
  102. __asm__ __volatile__("xchgl %1,%0": "+m" (*ptr), "+q" (next) : : "memory");
  103. return next;
  104. }
  105. #define STARPU_HAVE_XCHG
  106. #endif
  107. #define STARPU_ATOMIC_SOMETHING(name,expr) \
  108. static __starpu_inline unsigned starpu_atomic_##name(unsigned *ptr, unsigned value) \
  109. { \
  110. unsigned old, next; \
  111. while (1) \
  112. { \
  113. old = *ptr; \
  114. next = expr; \
  115. if (starpu_cmpxchg(ptr, old, next) == old) \
  116. break; \
  117. }; \
  118. return expr; \
  119. }
  120. #ifdef STARPU_HAVE_SYNC_FETCH_AND_ADD
  121. #define STARPU_ATOMIC_ADD(ptr, value) (__sync_fetch_and_add ((ptr), (value)) + (value))
  122. #elif defined(STARPU_HAVE_XCHG)
  123. STARPU_ATOMIC_SOMETHING(add, old + value)
  124. #define STARPU_ATOMIC_ADD(ptr, value) starpu_atomic_add(ptr, value)
  125. #endif
  126. #ifdef STARPU_HAVE_SYNC_FETCH_AND_OR
  127. #define STARPU_ATOMIC_OR(ptr, value) (__sync_fetch_and_or ((ptr), (value)))
  128. #elif defined(STARPU_HAVE_XCHG)
  129. STARPU_ATOMIC_SOMETHING(or, old | value)
  130. #define STARPU_ATOMIC_OR(ptr, value) starpu_atomic_or(ptr, value)
  131. #endif
  132. #ifdef STARPU_HAVE_SYNC_BOOL_COMPARE_AND_SWAP
  133. #define STARPU_BOOL_COMPARE_AND_SWAP(ptr, old, value) (__sync_bool_compare_and_swap ((ptr), (old), (value)))
  134. #elif defined(STARPU_HAVE_XCHG)
  135. #define STARPU_BOOL_COMPARE_AND_SWAP(ptr, old, value) (starpu_cmpxchg((ptr), (old), (value)) == (old))
  136. #endif
  137. #ifdef STARPU_HAVE_SYNC_LOCK_TEST_AND_SET
  138. #define STARPU_TEST_AND_SET(ptr, value) (__sync_lock_test_and_set ((ptr), (value)))
  139. #define STARPU_RELEASE(ptr) (__sync_lock_release ((ptr)))
  140. #elif defined(STARPU_HAVE_XCHG)
  141. #define STARPU_TEST_AND_SET(ptr, value) (starpu_xchg((ptr), (value)))
  142. #define STARPU_RELEASE(ptr) (starpu_xchg((ptr), 0))
  143. #endif
  144. #ifdef STARPU_HAVE_SYNC_SYNCHRONIZE
  145. #define STARPU_SYNCHRONIZE() __sync_synchronize()
  146. #elif defined(__i386__)
  147. #define STARPU_SYNCHRONIZE() __asm__ __volatile__("lock; addl $0,0(%%esp)" ::: "memory")
  148. #elif defined(__x86_64__)
  149. #define STARPU_SYNCHRONIZE() __asm__ __volatile__("mfence" ::: "memory")
  150. #elif defined(__ppc__) || defined(__ppc64__)
  151. #define STARPU_SYNCHRONIZE() __asm__ __volatile__("sync" ::: "memory")
  152. #endif
  153. #if defined(__i386__)
  154. #define STARPU_RMB() __asm__ __volatile__("lock; addl $0,0(%%esp)" ::: "memory")
  155. #define STARPU_WMB() __asm__ __volatile__("lock; addl $0,0(%%esp)" ::: "memory")
  156. #elif defined(__x86_64__)
  157. #define STARPU_RMB() __asm__ __volatile__("lfence" ::: "memory")
  158. #define STARPU_WMB() __asm__ __volatile__("sfence" ::: "memory")
  159. #elif defined(__ppc__) || defined(__ppc64__)
  160. #define STARPU_RMB() __asm__ __volatile__("sync" ::: "memory")
  161. #define STARPU_WMB() __asm__ __volatile__("sync" ::: "memory")
  162. #else
  163. #define STARPU_RMB() STARPU_SYNCHRONIZE()
  164. #define STARPU_WMB() STARPU_SYNCHRONIZE()
  165. #endif
  166. /* This is needed in some places to make valgrind yield to another thread to be
  167. * able to progress. */
  168. #if defined(__i386__) || defined(__x86_64__)
  169. #define STARPU_UYIELD() __asm__ __volatile("rep; nop")
  170. #else
  171. #define STARPU_UYIELD() ((void)0)
  172. #endif
  173. #ifdef __cplusplus
  174. }
  175. #endif
  176. /* Include this only here so that <starpu_data_interfaces.h> can use the
  177. * macros above. */
  178. #include <starpu_task.h>
  179. #ifdef __cplusplus
  180. extern "C"
  181. {
  182. #endif
  183. static __starpu_inline int starpu_get_env_number(const char *str)
  184. {
  185. char *strval;
  186. strval = getenv(str);
  187. if (strval)
  188. {
  189. /* the env variable was actually set */
  190. long int val;
  191. char *check;
  192. val = strtol(strval, &check, 10);
  193. if (*check) {
  194. fprintf(stderr,"The %s environment variable must contain an integer\n", str);
  195. STARPU_ABORT();
  196. }
  197. /* fprintf(stderr, "ENV %s WAS %d\n", str, val); */
  198. return (int)val;
  199. }
  200. else
  201. {
  202. /* there is no such env variable */
  203. /* fprintf("There was no %s ENV\n", str); */
  204. return -1;
  205. }
  206. }
  207. /* Add an event in the execution trace if FxT is enabled */
  208. void starpu_trace_user_event(unsigned long code);
  209. /* Call func(arg) on every worker matching the "where" mask (eg.
  210. * STARPU_CUDA|STARPU_CPU to execute the function on every CPU and every CUDA
  211. * device). This function is synchronous, but the different workers may execute
  212. * the function in parallel.
  213. * */
  214. void starpu_execute_on_each_worker(void (*func)(void *), void *arg, uint32_t where);
  215. /* Same as starpu_execute_on_each_worker, except that the task name is specified in the "name" parameter. */
  216. void starpu_execute_on_each_worker_ex(void (*func)(void *), void *arg, uint32_t where, const char * name);
  217. /* Call func(arg) on every worker in the "workers" array. "num_workers"
  218. * indicates the number of workers in this array. This function is
  219. * synchronous, but the different workers may execute the function in parallel.
  220. * */
  221. void starpu_execute_on_specific_workers(void (*func)(void*), void * arg, unsigned num_workers, unsigned * workers, const char * name);
  222. /* Copy the content of the src_handle into the dst_handle handle. The
  223. * asynchronous parameter indicates whether the function should block or not.
  224. * In the case of an asynchronous call, it is possible to synchronize with the
  225. * termination of this operation either by the means of implicit dependencies
  226. * (if enabled) or by calling starpu_task_wait_for_all(). If callback_func is
  227. * not NULL, this callback function is executed after the handle has been
  228. * copied, and it is given the callback_arg pointer as argument.*/
  229. 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);
  230. #ifdef __cplusplus
  231. }
  232. #endif
  233. #endif /* __STARPU_UTIL_H__ */