starpu-util.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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 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. #define STARPU_ASSERT(x) assert(x)
  39. #endif
  40. #define STARPU_ABORT() abort()
  41. #define STARPU_UNLIKELY(expr) (__builtin_expect(!!(expr),0))
  42. #define STARPU_LIKELY(expr) (__builtin_expect(!!(expr),1))
  43. #if defined(__i386__) || defined(__x86_64__)
  44. static inline unsigned starpu_cmpxchg(unsigned *ptr, unsigned next) {
  45. unsigned tmp = *ptr;
  46. __asm__ __volatile__("lock cmpxchgl %2,%1": "+a" (tmp), "+m" (*ptr) : "q" (next) : "memory");
  47. return tmp;
  48. }
  49. static inline unsigned starpu_xchg(unsigned *ptr, unsigned next) {
  50. /* Note: xchg is always locked already */
  51. __asm__ __volatile__("xchgl %1,%0": "+m" (*ptr), "+q" (next) : : "memory");
  52. return next;
  53. }
  54. #define STARPU_HAVE_XCHG
  55. #endif
  56. #define STARPU_ATOMIC_SOMETHING(name,expr) \
  57. static inline unsigned starpu_atomic_##name(unsigned *ptr, unsigned value) { \
  58. unsigned old, next; \
  59. while (1) { \
  60. old = *ptr; \
  61. next = expr; \
  62. if (starpu_cmpxchg(ptr, next) == old) \
  63. break; \
  64. }; \
  65. return expr; \
  66. }
  67. #ifdef STARPU_HAVE_SYNC_FETCH_AND_ADD
  68. #define STARPU_ATOMIC_ADD(ptr, value) (__sync_fetch_and_add ((ptr), (value)) + (value))
  69. #elif defined(STARPU_HAVE_XCHG)
  70. STARPU_ATOMIC_SOMETHING(add, old + value)
  71. #define STARPU_ATOMIC_ADD(ptr, value) starpu_atomic_add(ptr, value)
  72. #else
  73. #error __sync_fetch_and_add is not available
  74. #endif
  75. #ifdef STARPU_HAVE_SYNC_FETCH_AND_OR
  76. #define STARPU_ATOMIC_OR(ptr, value) (__sync_fetch_and_or ((ptr), (value)))
  77. #elif defined(STARPU_HAVE_XCHG)
  78. STARPU_ATOMIC_SOMETHING(or, old | value)
  79. #define STARPU_ATOMIC_OR(ptr, value) starpu_atomic_or(ptr, value)
  80. #else
  81. #error __sync_fetch_and_or is not available
  82. #endif
  83. #ifdef STARPU_HAVE_SYNC_BOOL_COMPARE_AND_SWAP
  84. #define STARPU_BOOL_COMPARE_AND_SWAP(ptr, old, value) (__sync_bool_compare_and_swap ((ptr), (old), (value)))
  85. #elif defined(STARPU_HAVE_XCHG)
  86. #define STARPU_BOOL_COMPARE_AND_SWAP(ptr, old, value) (starpu_cmpxchg((ptr), (value)) == (old))
  87. #else
  88. #error __sync_bool_compare_and_swap is not available
  89. #endif
  90. #ifdef STARPU_HAVE_SYNC_LOCK_TEST_AND_SET
  91. #define STARPU_TEST_AND_SET(ptr, value) (__sync_lock_test_and_set ((ptr), (value)))
  92. #define STARPU_RELEASE(ptr) (__sync_lock_release ((ptr)))
  93. #elif defined(STARPU_HAVE_XCHG)
  94. #define STARPU_TEST_AND_SET(ptr, value) (starpu_xchg((ptr), (value)))
  95. #define STARPU_RELEASE(ptr) (starpu_xchg((ptr), 0))
  96. #else
  97. #error __sync_lock_test_and_set is not available
  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. #else
  108. #error __sync_synchronize is not available
  109. #endif
  110. #ifdef USE_CUDA
  111. #define CUBLAS_REPORT_ERROR(status) \
  112. do { \
  113. char *errormsg; \
  114. switch (status) { \
  115. case CUBLAS_STATUS_SUCCESS: \
  116. errormsg = "success"; \
  117. break; \
  118. case CUBLAS_STATUS_NOT_INITIALIZED: \
  119. errormsg = "not initialized"; \
  120. break; \
  121. case CUBLAS_STATUS_ALLOC_FAILED: \
  122. errormsg = "alloc failed"; \
  123. break; \
  124. case CUBLAS_STATUS_INVALID_VALUE: \
  125. errormsg = "invalid value"; \
  126. break; \
  127. case CUBLAS_STATUS_ARCH_MISMATCH: \
  128. errormsg = "arch mismatch"; \
  129. break; \
  130. case CUBLAS_STATUS_EXECUTION_FAILED: \
  131. errormsg = "execution failed"; \
  132. break; \
  133. case CUBLAS_STATUS_INTERNAL_ERROR: \
  134. errormsg = "internal error"; \
  135. break; \
  136. default: \
  137. errormsg = "unknown error"; \
  138. break; \
  139. } \
  140. printf("oops in %s ... %s \n", __func__, errormsg); \
  141. assert(0); \
  142. } while (0)
  143. #define CUDA_REPORT_ERROR(status) \
  144. do { \
  145. char *errormsg; \
  146. switch (status) { \
  147. case CUDA_SUCCESS: \
  148. errormsg = "success"; \
  149. break; \
  150. case CUDA_ERROR_INVALID_VALUE: \
  151. errormsg = "invalid value"; \
  152. break; \
  153. case CUDA_ERROR_OUT_OF_MEMORY: \
  154. errormsg = "out of memory"; \
  155. break; \
  156. case CUDA_ERROR_NOT_INITIALIZED: \
  157. errormsg = "not initialized"; \
  158. break; \
  159. case CUDA_ERROR_DEINITIALIZED: \
  160. errormsg = "deinitialized"; \
  161. break; \
  162. case CUDA_ERROR_NO_DEVICE: \
  163. errormsg = "no device"; \
  164. break; \
  165. case CUDA_ERROR_INVALID_DEVICE: \
  166. errormsg = "invalid device"; \
  167. break; \
  168. case CUDA_ERROR_INVALID_IMAGE: \
  169. errormsg = "invalid image"; \
  170. break; \
  171. case CUDA_ERROR_INVALID_CONTEXT: \
  172. errormsg = "invalid context"; \
  173. break; \
  174. case CUDA_ERROR_CONTEXT_ALREADY_CURRENT: \
  175. errormsg = "context already current"; \
  176. break; \
  177. case CUDA_ERROR_MAP_FAILED: \
  178. errormsg = "map failed"; \
  179. break; \
  180. case CUDA_ERROR_UNMAP_FAILED: \
  181. errormsg = "unmap failed"; \
  182. break; \
  183. case CUDA_ERROR_ARRAY_IS_MAPPED: \
  184. errormsg = "array is mapped"; \
  185. break; \
  186. case CUDA_ERROR_ALREADY_MAPPED: \
  187. errormsg = "already mapped"; \
  188. break; \
  189. case CUDA_ERROR_NO_BINARY_FOR_GPU: \
  190. errormsg = "no binary for gpu"; \
  191. break; \
  192. case CUDA_ERROR_ALREADY_ACQUIRED: \
  193. errormsg = "already acquired"; \
  194. break; \
  195. case CUDA_ERROR_NOT_MAPPED: \
  196. errormsg = "not mapped"; \
  197. break; \
  198. case CUDA_ERROR_INVALID_SOURCE: \
  199. errormsg = "invalid source"; \
  200. break; \
  201. case CUDA_ERROR_FILE_NOT_FOUND: \
  202. errormsg = "file not found"; \
  203. break; \
  204. case CUDA_ERROR_INVALID_HANDLE: \
  205. errormsg = "invalid handle"; \
  206. break; \
  207. case CUDA_ERROR_NOT_FOUND: \
  208. errormsg = "not found"; \
  209. break; \
  210. case CUDA_ERROR_NOT_READY: \
  211. errormsg = "not ready"; \
  212. break; \
  213. case CUDA_ERROR_LAUNCH_FAILED: \
  214. errormsg = "launch failed"; \
  215. break; \
  216. case CUDA_ERROR_LAUNCH_OUT_OF_RESOURCES: \
  217. errormsg = "launch out of resources"; \
  218. break; \
  219. case CUDA_ERROR_LAUNCH_TIMEOUT: \
  220. errormsg = "launch timeout"; \
  221. break; \
  222. case CUDA_ERROR_LAUNCH_INCOMPATIBLE_TEXTURING: \
  223. errormsg = "launch incompatible texturing";\
  224. break; \
  225. case CUDA_ERROR_UNKNOWN: \
  226. default: \
  227. errormsg = "unknown error"; \
  228. break; \
  229. } \
  230. printf("oops in %s ... %s \n", __func__, errormsg); \
  231. assert(0); \
  232. } while (0)
  233. #endif // USE_CUDA
  234. static inline int starpu_get_env_number(const char *str)
  235. {
  236. char *strval;
  237. strval = getenv(str);
  238. if (strval) {
  239. /* the env variable was actually set */
  240. unsigned val;
  241. char *check;
  242. val = (int)strtol(strval, &check, 10);
  243. STARPU_ASSERT(strcmp(check, "\0") == 0);
  244. //fprintf(stderr, "ENV %s WAS %d\n", str, val);
  245. return val;
  246. }
  247. else {
  248. /* there is no such env variable */
  249. //fprintf("There was no %s ENV\n", str);
  250. return -1;
  251. }
  252. }
  253. /* Add an event in the execution trace if FxT is enabled */
  254. void starpu_trace_user_event(unsigned code);
  255. /* Some helper functions for application using CUBLAS kernels */
  256. void starpu_helper_init_cublas(void);
  257. void starpu_helper_shutdown_cublas(void);
  258. /* Call func(arg) on every worker matching the "where" mask (eg. CUDA|CPU to
  259. * execute the function on every CPUs and every CUDA devices). This function is
  260. * synchronous, but the different workers may execute the function in parallel.
  261. * */
  262. void starpu_execute_on_each_worker(void (*func)(void *), void *arg, uint32_t where);
  263. /* This creates (and submits) an empty task that unlocks a tag once all its
  264. * dependencies are fulfilled. */
  265. void starpu_create_sync_task(starpu_tag_t sync_tag, unsigned ndeps, starpu_tag_t *deps,
  266. void (*callback)(void *), void *callback_arg);
  267. #ifdef USE_CUDA
  268. cudaStream_t *starpu_get_local_cuda_stream(void);
  269. #endif
  270. /* If FILE is currently on a comment line, eat it. */
  271. void starpu_drop_comments(FILE *f);
  272. #ifdef __cplusplus
  273. }
  274. #endif
  275. #endif // __STARPU_UTIL_H__