starpu_util.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /*
  2. * StarPU
  3. * Copyright (C) INRIA 2008-2009 (see AUTHORS file)
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation; either version 2.1 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. *
  14. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  15. */
  16. #ifndef __STARPU_UTIL_H__
  17. #define __STARPU_UTIL_H__
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <assert.h>
  22. #include <starpu_config.h>
  23. #include <starpu_task.h>
  24. #ifdef STARPU_USE_CUDA
  25. #include <cuda.h>
  26. #include <cuda_runtime_api.h>
  27. #include <cublas.h>
  28. #endif
  29. #ifdef __cplusplus
  30. extern "C" {
  31. #endif
  32. #define STARPU_POISON_PTR ((void *)0xdeadbeef)
  33. #define STARPU_MIN(a,b) ((a)<(b)?(a):(b))
  34. #define STARPU_MAX(a,b) ((a)<(b)?(b):(a))
  35. #ifdef STARPU_NO_ASSERT
  36. #define STARPU_ASSERT(x) do {} while(0);
  37. #else
  38. # if defined(__CUDACC__) && defined(STARPU_HAVE_WINDOWS)
  39. # define STARPU_ASSERT(x) do { if (!(x)) *(int*)NULL = 0; } while(0)
  40. # else
  41. # define STARPU_ASSERT(x) assert(x)
  42. # endif
  43. #endif
  44. #define STARPU_ABORT() abort()
  45. #define STARPU_UNLIKELY(expr) (__builtin_expect(!!(expr),0))
  46. #define STARPU_LIKELY(expr) (__builtin_expect(!!(expr),1))
  47. #ifdef __GNUC__
  48. # define STARPU_ATTRIBUTE_UNUSED __attribute__((unused))
  49. #else
  50. # define STARPU_ATTRIBUTE_UNUSED
  51. #endif
  52. #if defined(__i386__) || defined(__x86_64__)
  53. static inline unsigned starpu_cmpxchg(unsigned *ptr, unsigned old, unsigned next) {
  54. __asm__ __volatile__("lock cmpxchgl %2,%1": "+a" (old), "+m" (*ptr) : "q" (next) : "memory");
  55. return old;
  56. }
  57. static inline unsigned starpu_xchg(unsigned *ptr, unsigned next) {
  58. /* Note: xchg is always locked already */
  59. __asm__ __volatile__("xchgl %1,%0": "+m" (*ptr), "+q" (next) : : "memory");
  60. return next;
  61. }
  62. #define STARPU_HAVE_XCHG
  63. #endif
  64. #define STARPU_ATOMIC_SOMETHING(name,expr) \
  65. static inline unsigned starpu_atomic_##name(unsigned *ptr, unsigned value) { \
  66. unsigned old, next; \
  67. while (1) { \
  68. old = *ptr; \
  69. next = expr; \
  70. if (starpu_cmpxchg(ptr, old, next) == old) \
  71. break; \
  72. }; \
  73. return expr; \
  74. }
  75. #ifdef STARPU_HAVE_SYNC_FETCH_AND_ADD
  76. #define STARPU_ATOMIC_ADD(ptr, value) (__sync_fetch_and_add ((ptr), (value)) + (value))
  77. #elif defined(STARPU_HAVE_XCHG)
  78. STARPU_ATOMIC_SOMETHING(add, old + value)
  79. #define STARPU_ATOMIC_ADD(ptr, value) starpu_atomic_add(ptr, value)
  80. #endif
  81. #ifdef STARPU_HAVE_SYNC_FETCH_AND_OR
  82. #define STARPU_ATOMIC_OR(ptr, value) (__sync_fetch_and_or ((ptr), (value)))
  83. #elif defined(STARPU_HAVE_XCHG)
  84. STARPU_ATOMIC_SOMETHING(or, old | value)
  85. #define STARPU_ATOMIC_OR(ptr, value) starpu_atomic_or(ptr, value)
  86. #endif
  87. #ifdef STARPU_HAVE_SYNC_BOOL_COMPARE_AND_SWAP
  88. #define STARPU_BOOL_COMPARE_AND_SWAP(ptr, old, value) (__sync_bool_compare_and_swap ((ptr), (old), (value)))
  89. #elif defined(STARPU_HAVE_XCHG)
  90. #define STARPU_BOOL_COMPARE_AND_SWAP(ptr, old, value) (starpu_cmpxchg((ptr), (old), (value)) == (old))
  91. #endif
  92. #ifdef STARPU_HAVE_SYNC_LOCK_TEST_AND_SET
  93. #define STARPU_TEST_AND_SET(ptr, value) (__sync_lock_test_and_set ((ptr), (value)))
  94. #define STARPU_RELEASE(ptr) (__sync_lock_release ((ptr)))
  95. #elif defined(STARPU_HAVE_XCHG)
  96. #define STARPU_TEST_AND_SET(ptr, value) (starpu_xchg((ptr), (value)))
  97. #define STARPU_RELEASE(ptr) (starpu_xchg((ptr), 0))
  98. #endif
  99. #ifdef STARPU_HAVE_SYNC_SYNCHRONIZE
  100. #define STARPU_SYNCHRONIZE() __sync_synchronize()
  101. #elif defined(__i386__)
  102. #define STARPU_SYNCHRONIZE() __asm__ __volatile__("lock; addl $0,0(%%esp)" ::: "memory")
  103. #elif defined(__x86_64__)
  104. #define STARPU_SYNCHRONIZE() __asm__ __volatile__("mfence" ::: "memory")
  105. #elif defined(__ppc__) || defined(__ppc64__)
  106. #define STARPU_SYNCHRONIZE() __asm__ __volatile__("sync" ::: "memory")
  107. #endif
  108. #ifdef STARPU_USE_CUDA
  109. #if defined(__CUDACC__) && defined(STARPU_HAVE_WINDOWS)
  110. #define STARPU_CUBLAS_OOPS() do { \
  111. printf("oops %s \n", errormsg); \
  112. *(int*)NULL = 0; \
  113. } while (0);
  114. #else
  115. #define STARPU_CUBLAS_OOPS() do { \
  116. printf("oops in %s ... %s \n", __func__, errormsg); \
  117. assert(0); \
  118. } while (0);
  119. #endif
  120. #define STARPU_CUBLAS_REPORT_ERROR(status) \
  121. do { \
  122. char *errormsg; \
  123. switch (status) { \
  124. case CUBLAS_STATUS_SUCCESS: \
  125. errormsg = "success"; \
  126. break; \
  127. case CUBLAS_STATUS_NOT_INITIALIZED: \
  128. errormsg = "not initialized"; \
  129. break; \
  130. case CUBLAS_STATUS_ALLOC_FAILED: \
  131. errormsg = "alloc failed"; \
  132. break; \
  133. case CUBLAS_STATUS_INVALID_VALUE: \
  134. errormsg = "invalid value"; \
  135. break; \
  136. case CUBLAS_STATUS_ARCH_MISMATCH: \
  137. errormsg = "arch mismatch"; \
  138. break; \
  139. case CUBLAS_STATUS_EXECUTION_FAILED: \
  140. errormsg = "execution failed"; \
  141. break; \
  142. case CUBLAS_STATUS_INTERNAL_ERROR: \
  143. errormsg = "internal error"; \
  144. break; \
  145. default: \
  146. errormsg = "unknown error"; \
  147. break; \
  148. } \
  149. STARPU_CUBLAS_OOPS(); \
  150. } while (0)
  151. #define STARPU_CUDA_REPORT_ERROR(status) \
  152. do { \
  153. const char *errormsg = cudaGetErrorString(status); \
  154. STARPU_CUBLAS_OOPS(); \
  155. } while (0)
  156. #endif // STARPU_USE_CUDA
  157. static inline int starpu_get_env_number(const char *str)
  158. {
  159. char *strval;
  160. strval = getenv(str);
  161. if (strval) {
  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. /* there is no such env variable */
  172. //fprintf("There was no %s ENV\n", str);
  173. return -1;
  174. }
  175. }
  176. /* Add an event in the execution trace if FxT is enabled */
  177. void starpu_trace_user_event(unsigned code);
  178. /* Some helper functions for application using CUBLAS kernels */
  179. void starpu_helper_cublas_init(void);
  180. void starpu_helper_cublas_shutdown(void);
  181. /* Call func(arg) on every worker matching the "where" mask (eg.
  182. * STARPU_CUDA|STARPU_CPU to execute the function on every CPU and every CUDA
  183. * device). This function is synchronous, but the different workers may execute
  184. * the function in parallel.
  185. * */
  186. void starpu_execute_on_each_worker(void (*func)(void *), void *arg, uint32_t where);
  187. /* This creates (and submits) an empty task that unlocks a tag once all its
  188. * dependencies are fulfilled. */
  189. void starpu_create_sync_task(starpu_tag_t sync_tag, unsigned ndeps, starpu_tag_t *deps,
  190. void (*callback)(void *), void *callback_arg);
  191. #ifdef STARPU_USE_CUDA
  192. cudaStream_t *starpu_cuda_get_local_stream(void);
  193. #endif
  194. /* If FILE is currently on a comment line, eat it. */
  195. void starpu_drop_comments(FILE *f);
  196. #ifdef __cplusplus
  197. }
  198. #endif
  199. #endif // __STARPU_UTIL_H__