starpu_util.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2012 Université de Bordeaux 1
  4. * Copyright (C) 2010, 2011, 2012 Centre National de la Recherche Scientifique
  5. * Copyright (C) 2011 INRIA
  6. *
  7. * StarPU is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation; either version 2.1 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * StarPU is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. *
  16. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  17. */
  18. #ifndef __STARPU_UTIL_H__
  19. #define __STARPU_UTIL_H__
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <assert.h>
  24. #include <starpu.h>
  25. #ifdef __cplusplus
  26. extern "C"
  27. {
  28. #endif
  29. /* Return true (non-zero) if GCC version MAJ.MIN or later is being used
  30. * (macro taken from glibc.) */
  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_INTERNAL __attribute__ ((visibility ("internal")))
  42. #else
  43. # define STARPU_UNLIKELY(expr) (expr)
  44. # define STARPU_LIKELY(expr) (expr)
  45. # define STARPU_ATTRIBUTE_UNUSED
  46. # define STARPU_ATTRIBUTE_INTERNAL
  47. #endif
  48. #if STARPU_GNUC_PREREQ(3, 1) && !defined(BUILDING_STARPU) && !defined(STARPU_USE_DEPRECATED_API)
  49. #define STARPU_DEPRECATED __attribute__((__deprecated__))
  50. #else
  51. #define STARPU_DEPRECATED
  52. #endif /* __GNUC__ */
  53. #if STARPU_GNUC_PREREQ(3,3)
  54. #define STARPU_WARN_UNUSED_RESULT __attribute__((__warn_unused_result__))
  55. #else
  56. #define STARPU_WARN_UNUSED_RESULT
  57. #endif /* __GNUC__ */
  58. #define STARPU_POISON_PTR ((void *)0xdeadbeef)
  59. #define STARPU_MIN(a,b) ((a)<(b)?(a):(b))
  60. #define STARPU_MAX(a,b) ((a)<(b)?(b):(a))
  61. #ifdef STARPU_NO_ASSERT
  62. #define STARPU_ASSERT(x) do { (void) (x);} while(0)
  63. #define STARPU_ASSERT_MSG(x, msg) do { (void) (x);} while(0)
  64. #else
  65. # if defined(__CUDACC__) && defined(STARPU_HAVE_WINDOWS)
  66. # define STARPU_ASSERT(x) do { if (STARPU_UNLIKELY(!(x))) *(int*)NULL = 0; } while(0)
  67. # define STARPU_ASSERT_MSG(x, msg) do { if (STARPU_UNLIKELY(!(x))) { fprintf(stderr, "[starpu][%s][assert failure] %s\n", __func__, msg); *(int*)NULL = 0; }} while(0)
  68. # else
  69. # define STARPU_ASSERT(x) assert(x)
  70. # define STARPU_ASSERT_MSG(x, msg) do { if (STARPU_UNLIKELY(!(x))) { fprintf(stderr, "[starpu][%s][assert failure] %s\n", __func__, msg); } ; assert(x); } while(0)
  71. # endif
  72. #endif
  73. #define STARPU_ABORT() do { \
  74. fprintf(stderr, "[starpu][abort] %s:%d %s\n", __FILE__, __LINE__, __func__); \
  75. abort(); \
  76. } while(0)
  77. #if defined(STARPU_HAVE_STRERROR_R)
  78. # define STARPU_CHECK_RETURN_VALUE(err, message) {if (STARPU_UNLIKELY(err != 0)) { \
  79. char xmessage[256]; strerror_r(-err, xmessage, 256); \
  80. fprintf(stderr, "StarPU function <%s> returned unexpected value: <%d:%s>\n", message, err, xmessage); \
  81. STARPU_ABORT(); }}
  82. # define STARPU_CHECK_RETURN_VALUE_IS(err, value, message) {if (STARPU_UNLIKELY(err != value)) { \
  83. char xmessage[256]; strerror_r(-err, xmessage, 256); \
  84. fprintf(stderr, "StarPU function <%s> returned unexpected value: <%d:%s>\n", message, err, xmessage); \
  85. STARPU_ABORT(); }}
  86. #else
  87. # define STARPU_CHECK_RETURN_VALUE(err, message) {if (STARPU_UNLIKELY(err != 0)) { \
  88. fprintf(stderr, "StarPU function <%s> returned unexpected value: <%d>\n", message, err); \
  89. STARPU_ABORT(); }}
  90. # define STARPU_CHECK_RETURN_VALUE_IS(err, value, message) {if (STARPU_UNLIKELY(err != value)) { \
  91. fprintf(stderr, "StarPU function <%s> returned unexpected value: <%d>\n", message, err); \
  92. STARPU_ABORT(); }}
  93. #endif /* STARPU_HAVE_STRERROR_R */
  94. #if defined(__i386__) || defined(__x86_64__)
  95. static __inline unsigned starpu_cmpxchg(unsigned *ptr, unsigned old, unsigned next)
  96. {
  97. __asm__ __volatile__("lock cmpxchgl %2,%1": "+a" (old), "+m" (*ptr) : "q" (next) : "memory");
  98. return old;
  99. }
  100. static __inline unsigned starpu_xchg(unsigned *ptr, unsigned next)
  101. {
  102. /* Note: xchg is always locked already */
  103. __asm__ __volatile__("xchgl %1,%0": "+m" (*ptr), "+q" (next) : : "memory");
  104. return next;
  105. }
  106. #define STARPU_HAVE_XCHG
  107. #endif
  108. #define STARPU_ATOMIC_SOMETHING(name,expr) \
  109. static __inline unsigned starpu_atomic_##name(unsigned *ptr, unsigned value) \
  110. { \
  111. unsigned old, next; \
  112. while (1) \
  113. { \
  114. old = *ptr; \
  115. next = expr; \
  116. if (starpu_cmpxchg(ptr, old, next) == old) \
  117. break; \
  118. }; \
  119. return expr; \
  120. }
  121. #ifdef STARPU_HAVE_SYNC_FETCH_AND_ADD
  122. #define STARPU_ATOMIC_ADD(ptr, value) (__sync_fetch_and_add ((ptr), (value)) + (value))
  123. #elif defined(STARPU_HAVE_XCHG)
  124. STARPU_ATOMIC_SOMETHING(add, old + value)
  125. #define STARPU_ATOMIC_ADD(ptr, value) starpu_atomic_add(ptr, value)
  126. #endif
  127. #ifdef STARPU_HAVE_SYNC_FETCH_AND_OR
  128. #define STARPU_ATOMIC_OR(ptr, value) (__sync_fetch_and_or ((ptr), (value)))
  129. #elif defined(STARPU_HAVE_XCHG)
  130. STARPU_ATOMIC_SOMETHING(or, old | value)
  131. #define STARPU_ATOMIC_OR(ptr, value) starpu_atomic_or(ptr, value)
  132. #endif
  133. #ifdef STARPU_HAVE_SYNC_BOOL_COMPARE_AND_SWAP
  134. #define STARPU_BOOL_COMPARE_AND_SWAP(ptr, old, value) (__sync_bool_compare_and_swap ((ptr), (old), (value)))
  135. #elif defined(STARPU_HAVE_XCHG)
  136. #define STARPU_BOOL_COMPARE_AND_SWAP(ptr, old, value) (starpu_cmpxchg((ptr), (old), (value)) == (old))
  137. #endif
  138. #ifdef STARPU_HAVE_SYNC_LOCK_TEST_AND_SET
  139. #define STARPU_TEST_AND_SET(ptr, value) (__sync_lock_test_and_set ((ptr), (value)))
  140. #define STARPU_RELEASE(ptr) (__sync_lock_release ((ptr)))
  141. #elif defined(STARPU_HAVE_XCHG)
  142. #define STARPU_TEST_AND_SET(ptr, value) (starpu_xchg((ptr), (value)))
  143. #define STARPU_RELEASE(ptr) (starpu_xchg((ptr), 0))
  144. #endif
  145. #ifdef STARPU_HAVE_SYNC_SYNCHRONIZE
  146. #define STARPU_SYNCHRONIZE() __sync_synchronize()
  147. #elif defined(__i386__)
  148. #define STARPU_SYNCHRONIZE() __asm__ __volatile__("lock; addl $0,0(%%esp)" ::: "memory")
  149. #elif defined(__x86_64__)
  150. #define STARPU_SYNCHRONIZE() __asm__ __volatile__("mfence" ::: "memory")
  151. #elif defined(__ppc__) || defined(__ppc64__)
  152. #define STARPU_SYNCHRONIZE() __asm__ __volatile__("sync" ::: "memory")
  153. #endif
  154. #if defined(__i386__)
  155. #define STARPU_RMB() __asm__ __volatile__("lock; addl $0,0(%%esp)" ::: "memory")
  156. #define STARPU_WMB() __asm__ __volatile__("lock; addl $0,0(%%esp)" ::: "memory")
  157. #elif defined(__x86_64__)
  158. #define STARPU_RMB() __asm__ __volatile__("lfence" ::: "memory")
  159. #define STARPU_WMB() __asm__ __volatile__("sfence" ::: "memory")
  160. #elif defined(__ppc__) || defined(__ppc64__)
  161. #define STARPU_RMB() __asm__ __volatile__("sync" ::: "memory")
  162. #define STARPU_WMB() __asm__ __volatile__("sync" ::: "memory")
  163. #endif
  164. #ifdef __cplusplus
  165. }
  166. #endif
  167. /* Include this only here so that <starpu_data_interfaces.h> can use the
  168. * macros above. */
  169. #include <starpu_task.h>
  170. #ifdef __cplusplus
  171. extern "C"
  172. {
  173. #endif
  174. static __inline int starpu_get_env_number(const char *str)
  175. {
  176. char *strval;
  177. strval = getenv(str);
  178. if (strval)
  179. {
  180. /* the env variable was actually set */
  181. unsigned val;
  182. char *check;
  183. val = (int)strtol(strval, &check, 10);
  184. STARPU_ASSERT(strcmp(check, "\0") == 0);
  185. /* fprintf(stderr, "ENV %s WAS %d\n", str, val); */
  186. return val;
  187. }
  188. else
  189. {
  190. /* there is no such env variable */
  191. /* fprintf("There was no %s ENV\n", str); */
  192. return -1;
  193. }
  194. }
  195. /* Add an event in the execution trace if FxT is enabled */
  196. void starpu_trace_user_event(unsigned long code);
  197. /* Call func(arg) on every worker matching the "where" mask (eg.
  198. * STARPU_CUDA|STARPU_CPU to execute the function on every CPU and every CUDA
  199. * device). This function is synchronous, but the different workers may execute
  200. * the function in parallel.
  201. * */
  202. void starpu_execute_on_each_worker(void (*func)(void *), void *arg, uint32_t where);
  203. /* Same as starpu_execute_on_each_worker, except that the task name is specified in the "name" parameter. */
  204. void starpu_execute_on_each_worker_ex(void (*func)(void *), void *arg, uint32_t where, const char * name);
  205. /* Call func(arg) on every worker in the "workers" array. "num_workers"
  206. * indicates the number of workers in this array. This function is
  207. * synchronous, but the different workers may execute the function in parallel.
  208. * */
  209. void starpu_execute_on_specific_workers(void (*func)(void*), void * arg, unsigned num_workers, unsigned * workers, const char * name);
  210. /* Copy the content of the src_handle into the dst_handle handle. The
  211. * asynchronous parameter indicates whether the function should block or not.
  212. * In the case of an asynchronous call, it is possible to synchronize with the
  213. * termination of this operation either by the means of implicit dependencies
  214. * (if enabled) or by calling starpu_task_wait_for_all(). If callback_func is
  215. * not NULL, this callback function is executed after the handle has been
  216. * copied, and it is given the callback_arg pointer as argument.*/
  217. 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);
  218. /* Constants used by the starpu_insert_task helper to determine the different types of argument */
  219. #define STARPU_VALUE (1<<4) /* Pointer to a constant value */
  220. #define STARPU_CALLBACK (1<<5) /* Callback function */
  221. #define STARPU_CALLBACK_WITH_ARG (1<<6) /* Callback function */
  222. #define STARPU_CALLBACK_ARG (1<<7) /* Argument of the callback function (of type void *) */
  223. #define STARPU_PRIORITY (1<<8) /* Priority associated to the task */
  224. #define STARPU_EXECUTE_ON_NODE (1<<9) /* Used by MPI to define which task is going to execute the codelet */
  225. #define STARPU_EXECUTE_ON_DATA (1<<10) /* Used by MPI to define which task is going to execute the codelet */
  226. #define STARPU_HYPERVISOR_TAG (1<<11) /* Used to tag a task after whose execution we'll execute a code */
  227. #define STARPU_HYPERVISOR_FLOPS (1<<12) /* Used to specify the number of flops needed to be executed by a task */
  228. /* Wrapper to create a task. */
  229. int starpu_insert_task(struct starpu_codelet *cl, ...);
  230. /* Retrieve the arguments of type STARPU_VALUE associated to a task
  231. * automatically created using starpu_insert_task. */
  232. void starpu_codelet_unpack_args(void *cl_arg, ...);
  233. /* Pack arguments of type STARPU_VALUE into a buffer which can be
  234. * given to a codelet and later unpacked with starpu_codelet_unpack_args */
  235. void starpu_codelet_pack_args(char **arg_buffer, size_t *arg_buffer_size, ...);
  236. #ifdef __cplusplus
  237. }
  238. #endif
  239. #endif /* __STARPU_UTIL_H__ */