starpu_util.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. #define STARPU_POISON_PTR ((void *)0xdeadbeef)
  30. #define STARPU_MIN(a,b) ((a)<(b)?(a):(b))
  31. #define STARPU_MAX(a,b) ((a)<(b)?(b):(a))
  32. #ifdef STARPU_NO_ASSERT
  33. #define STARPU_ASSERT(x) do { (void) (x);} while(0)
  34. #define STARPU_ASSERT_MSG(x, msg) do { (void) (x);} while(0)
  35. #else
  36. # if defined(__CUDACC__) && defined(STARPU_HAVE_WINDOWS)
  37. # define STARPU_ASSERT(x) do { if (!(x)) *(int*)NULL = 0; } while(0)
  38. # define STARPU_ASSERT_MSG(x, msg) do { if (!(x)) { fprintf(stderr, "%s\n", msg); *(int*)NULL = 0; }} while(0)
  39. # else
  40. # define STARPU_ASSERT(x) assert(x)
  41. # define STARPU_ASSERT_MSG(x, msg) do { if (!(x)) { fprintf(stderr, "%s\n", msg); } ; assert(x); } while(0)
  42. # endif
  43. #endif
  44. #define STARPU_ABORT() abort()
  45. #if defined(STARPU_HAVE_STRERROR_R)
  46. # define STARPU_CHECK_RETURN_VALUE(err, message) {if (err < 0) { \
  47. char xmessage[256]; strerror_r(-err, xmessage, 256); \
  48. fprintf(stderr, "StarPU function <%s> returned unexpected value: <%d:%s>\n", message, err, xmessage); \
  49. STARPU_ASSERT(0); }}
  50. # define STARPU_CHECK_RETURN_VALUE_IS(err, value, message) {if (err != value) { \
  51. char xmessage[256]; strerror_r(-err, xmessage, 256); \
  52. fprintf(stderr, "StarPU function <%s> returned unexpected value: <%d:%s>\n", message, err, xmessage); \
  53. STARPU_ASSERT(0); }}
  54. #else
  55. # define STARPU_CHECK_RETURN_VALUE(err, message) {if (err < 0) { \
  56. fprintf(stderr, "StarPU function <%s> returned unexpected value: <%d>\n", message, err); \
  57. STARPU_ASSERT(0); }}
  58. # define STARPU_CHECK_RETURN_VALUE_IS(err, value, message) {if (err != value) { \
  59. fprintf(stderr, "StarPU function <%s> returned unexpected value: <%d>\n", message, err); \
  60. STARPU_ASSERT(0); }}
  61. #endif /* STARPU_HAVE_STRERROR_R */
  62. /* Return true (non-zero) if GCC version MAJ.MIN or later is being used
  63. * (macro taken from glibc.) */
  64. #if defined __GNUC__ && defined __GNUC_MINOR__
  65. # define STARPU_GNUC_PREREQ(maj, min) \
  66. ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
  67. #else
  68. # define STARPU_GNUC_PREREQ(maj, min) 0
  69. #endif
  70. #ifdef __GNUC__
  71. # define STARPU_UNLIKELY(expr) (__builtin_expect(!!(expr),0))
  72. # define STARPU_LIKELY(expr) (__builtin_expect(!!(expr),1))
  73. # define STARPU_ATTRIBUTE_UNUSED __attribute__((unused))
  74. # define STARPU_ATTRIBUTE_INTERNAL __attribute__ ((visibility ("internal")))
  75. #else
  76. # define STARPU_UNLIKELY(expr) (expr)
  77. # define STARPU_LIKELY(expr) (expr)
  78. # define STARPU_ATTRIBUTE_UNUSED
  79. # define STARPU_ATTRIBUTE_INTERNAL
  80. #endif
  81. #if STARPU_GNUC_PREREQ(3, 1) && !defined(BUILDING_STARPU) && !defined(STARPU_USE_DEPRECATED_API)
  82. #define STARPU_DEPRECATED __attribute__((__deprecated__))
  83. #else
  84. #define STARPU_DEPRECATED
  85. #endif /* __GNUC__ */
  86. #if defined(__i386__) || defined(__x86_64__)
  87. static __inline unsigned starpu_cmpxchg(unsigned *ptr, unsigned old, unsigned next)
  88. {
  89. __asm__ __volatile__("lock cmpxchgl %2,%1": "+a" (old), "+m" (*ptr) : "q" (next) : "memory");
  90. return old;
  91. }
  92. static __inline unsigned starpu_xchg(unsigned *ptr, unsigned next)
  93. {
  94. /* Note: xchg is always locked already */
  95. __asm__ __volatile__("xchgl %1,%0": "+m" (*ptr), "+q" (next) : : "memory");
  96. return next;
  97. }
  98. #define STARPU_HAVE_XCHG
  99. #endif
  100. #define STARPU_ATOMIC_SOMETHING(name,expr) \
  101. static __inline unsigned starpu_atomic_##name(unsigned *ptr, unsigned value) \
  102. { \
  103. unsigned old, next; \
  104. while (1) \
  105. { \
  106. old = *ptr; \
  107. next = expr; \
  108. if (starpu_cmpxchg(ptr, old, next) == old) \
  109. break; \
  110. }; \
  111. return expr; \
  112. }
  113. #ifdef STARPU_HAVE_SYNC_FETCH_AND_ADD
  114. #define STARPU_ATOMIC_ADD(ptr, value) (__sync_fetch_and_add ((ptr), (value)) + (value))
  115. #elif defined(STARPU_HAVE_XCHG)
  116. STARPU_ATOMIC_SOMETHING(add, old + value)
  117. #define STARPU_ATOMIC_ADD(ptr, value) starpu_atomic_add(ptr, value)
  118. #endif
  119. #ifdef STARPU_HAVE_SYNC_FETCH_AND_OR
  120. #define STARPU_ATOMIC_OR(ptr, value) (__sync_fetch_and_or ((ptr), (value)))
  121. #elif defined(STARPU_HAVE_XCHG)
  122. STARPU_ATOMIC_SOMETHING(or, old | value)
  123. #define STARPU_ATOMIC_OR(ptr, value) starpu_atomic_or(ptr, value)
  124. #endif
  125. #ifdef STARPU_HAVE_SYNC_BOOL_COMPARE_AND_SWAP
  126. #define STARPU_BOOL_COMPARE_AND_SWAP(ptr, old, value) (__sync_bool_compare_and_swap ((ptr), (old), (value)))
  127. #elif defined(STARPU_HAVE_XCHG)
  128. #define STARPU_BOOL_COMPARE_AND_SWAP(ptr, old, value) (starpu_cmpxchg((ptr), (old), (value)) == (old))
  129. #endif
  130. #ifdef STARPU_HAVE_SYNC_LOCK_TEST_AND_SET
  131. #define STARPU_TEST_AND_SET(ptr, value) (__sync_lock_test_and_set ((ptr), (value)))
  132. #define STARPU_RELEASE(ptr) (__sync_lock_release ((ptr)))
  133. #elif defined(STARPU_HAVE_XCHG)
  134. #define STARPU_TEST_AND_SET(ptr, value) (starpu_xchg((ptr), (value)))
  135. #define STARPU_RELEASE(ptr) (starpu_xchg((ptr), 0))
  136. #endif
  137. #ifdef STARPU_HAVE_SYNC_SYNCHRONIZE
  138. #define STARPU_SYNCHRONIZE() __sync_synchronize()
  139. #elif defined(__i386__)
  140. #define STARPU_SYNCHRONIZE() __asm__ __volatile__("lock; addl $0,0(%%esp)" ::: "memory")
  141. #elif defined(__x86_64__)
  142. #define STARPU_SYNCHRONIZE() __asm__ __volatile__("mfence" ::: "memory")
  143. #elif defined(__ppc__) || defined(__ppc64__)
  144. #define STARPU_SYNCHRONIZE() __asm__ __volatile__("sync" ::: "memory")
  145. #endif
  146. #ifdef __cplusplus
  147. }
  148. #endif
  149. /* Include this only here so that <starpu_data_interfaces.h> can use the
  150. * macros above. */
  151. #include <starpu_task.h>
  152. #ifdef __cplusplus
  153. extern "C"
  154. {
  155. #endif
  156. static __inline int starpu_get_env_number(const char *str)
  157. {
  158. char *strval;
  159. strval = getenv(str);
  160. if (strval)
  161. {
  162. /* the env variable was actually set */
  163. unsigned val;
  164. char *check;
  165. val = (int)strtol(strval, &check, 10);
  166. STARPU_ASSERT(strcmp(check, "\0") == 0);
  167. /* fprintf(stderr, "ENV %s WAS %d\n", str, val); */
  168. return val;
  169. }
  170. else
  171. {
  172. /* there is no such env variable */
  173. /* fprintf("There was no %s ENV\n", str); */
  174. return -1;
  175. }
  176. }
  177. /* Add an event in the execution trace if FxT is enabled */
  178. void starpu_trace_user_event(unsigned long code);
  179. /* Some helper functions for application using CUBLAS kernels */
  180. void starpu_helper_cublas_init(void);
  181. void starpu_helper_cublas_shutdown(void);
  182. /* Call func(arg) on every worker matching the "where" mask (eg.
  183. * STARPU_CUDA|STARPU_CPU to execute the function on every CPU and every CUDA
  184. * device). This function is synchronous, but the different workers may execute
  185. * the function in parallel.
  186. * */
  187. void starpu_execute_on_each_worker(void (*func)(void *), void *arg, uint32_t where);
  188. /* This creates (and submits) an empty task that unlocks a tag once all its
  189. * dependencies are fulfilled. */
  190. void starpu_create_sync_task(starpu_tag_t sync_tag, unsigned ndeps, starpu_tag_t *deps,
  191. void (*callback)(void *), void *callback_arg);
  192. /* Copy the content of the src_handle into the dst_handle handle. The
  193. * asynchronous parameter indicates whether the function should block or not.
  194. * In the case of an asynchronous call, it is possible to synchronize with the
  195. * termination of this operation either by the means of implicit dependencies
  196. * (if enabled) or by calling starpu_task_wait_for_all(). If callback_func is
  197. * not NULL, this callback function is executed after the handle has been
  198. * copied, and it is given the callback_arg pointer as argument.*/
  199. 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);
  200. /* Constants used by the starpu_insert_task helper to determine the different types of argument */
  201. #define STARPU_VALUE (1<<4) /* Pointer to a constant value */
  202. #define STARPU_CALLBACK (1<<5) /* Callback function */
  203. #define STARPU_CALLBACK_WITH_ARG (1<<6) /* Callback function */
  204. #define STARPU_CALLBACK_ARG (1<<7) /* Argument of the callback function (of type void *) */
  205. #define STARPU_PRIORITY (1<<8) /* Priority associated to the task */
  206. #define STARPU_EXECUTE_ON_NODE (1<<9) /* Used by MPI to define which task is going to execute the codelet */
  207. #define STARPU_EXECUTE_ON_DATA (1<<10) /* Used by MPI to define which task is going to execute the codelet */
  208. #define STARPU_HYPERVISOR_TAG (1<<11) /* Used to checkpoint a task after whose execution we'll execute a code */
  209. /* Wrapper to create a task. */
  210. int starpu_insert_task(struct starpu_codelet *cl, ...);
  211. /* Retrieve the arguments of type STARPU_VALUE associated to a task
  212. * automatically created using starpu_insert_task. */
  213. void starpu_codelet_unpack_args(void *cl_arg, ...);
  214. /* Pack arguments of type STARPU_VALUE into a buffer which can be
  215. * given to a codelet and later unpacked with starpu_codelet_unpack_args */
  216. void starpu_codelet_pack_args(char **arg_buffer, size_t *arg_buffer_size, ...);
  217. #ifdef __cplusplus
  218. }
  219. #endif
  220. #endif /* __STARPU_UTIL_H__ */