starpu_util.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. #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 old, unsigned next) {
  45. __asm__ __volatile__("lock cmpxchgl %2,%1": "+a" (old), "+m" (*ptr) : "q" (next) : "memory");
  46. return old;
  47. }
  48. static inline unsigned starpu_xchg(unsigned *ptr, unsigned next) {
  49. /* Note: xchg is always locked already */
  50. __asm__ __volatile__("xchgl %1,%0": "+m" (*ptr), "+q" (next) : : "memory");
  51. return next;
  52. }
  53. #define STARPU_HAVE_XCHG
  54. #endif
  55. #define STARPU_ATOMIC_SOMETHING(name,expr) \
  56. static inline unsigned starpu_atomic_##name(unsigned *ptr, unsigned value) { \
  57. unsigned old, next; \
  58. while (1) { \
  59. old = *ptr; \
  60. next = expr; \
  61. if (starpu_cmpxchg(ptr, old, next) == old) \
  62. break; \
  63. }; \
  64. return expr; \
  65. }
  66. #ifdef STARPU_HAVE_SYNC_FETCH_AND_ADD
  67. #define STARPU_ATOMIC_ADD(ptr, value) (__sync_fetch_and_add ((ptr), (value)) + (value))
  68. #elif defined(STARPU_HAVE_XCHG)
  69. STARPU_ATOMIC_SOMETHING(add, old + value)
  70. #define STARPU_ATOMIC_ADD(ptr, value) starpu_atomic_add(ptr, value)
  71. #else
  72. #error __sync_fetch_and_add is not available
  73. #endif
  74. #ifdef STARPU_HAVE_SYNC_FETCH_AND_OR
  75. #define STARPU_ATOMIC_OR(ptr, value) (__sync_fetch_and_or ((ptr), (value)))
  76. #elif defined(STARPU_HAVE_XCHG)
  77. STARPU_ATOMIC_SOMETHING(or, old | value)
  78. #define STARPU_ATOMIC_OR(ptr, value) starpu_atomic_or(ptr, value)
  79. #else
  80. #error __sync_fetch_and_or is not available
  81. #endif
  82. #ifdef STARPU_HAVE_SYNC_BOOL_COMPARE_AND_SWAP
  83. #define STARPU_BOOL_COMPARE_AND_SWAP(ptr, old, value) (__sync_bool_compare_and_swap ((ptr), (old), (value)))
  84. #elif defined(STARPU_HAVE_XCHG)
  85. #define STARPU_BOOL_COMPARE_AND_SWAP(ptr, old, value) (starpu_cmpxchg((ptr), (old), (value)) == (old))
  86. #else
  87. #error __sync_bool_compare_and_swap is not available
  88. #endif
  89. #ifdef STARPU_HAVE_SYNC_LOCK_TEST_AND_SET
  90. #define STARPU_TEST_AND_SET(ptr, value) (__sync_lock_test_and_set ((ptr), (value)))
  91. #define STARPU_RELEASE(ptr) (__sync_lock_release ((ptr)))
  92. #elif defined(STARPU_HAVE_XCHG)
  93. #define STARPU_TEST_AND_SET(ptr, value) (starpu_xchg((ptr), (value)))
  94. #define STARPU_RELEASE(ptr) (starpu_xchg((ptr), 0))
  95. #else
  96. #error __sync_lock_test_and_set is not available
  97. #endif
  98. #ifdef STARPU_HAVE_SYNC_SYNCHRONIZE
  99. #define STARPU_SYNCHRONIZE() __sync_synchronize()
  100. #elif defined(__i386__)
  101. #define STARPU_SYNCHRONIZE() __asm__ __volatile__("lock; addl $0,0(%%esp)" ::: "memory")
  102. #elif defined(__x86_64__)
  103. #define STARPU_SYNCHRONIZE() __asm__ __volatile__("mfence" ::: "memory")
  104. #elif defined(__ppc__) || defined(__ppc64__)
  105. #define STARPU_SYNCHRONIZE() __asm__ __volatile__("sync" ::: "memory")
  106. #else
  107. #error __sync_synchronize is not available
  108. #endif
  109. #ifdef STARPU_USE_CUDA
  110. #define STARPU_CUBLAS_REPORT_ERROR(status) \
  111. do { \
  112. char *errormsg; \
  113. switch (status) { \
  114. case CUBLAS_STATUS_SUCCESS: \
  115. errormsg = "success"; \
  116. break; \
  117. case CUBLAS_STATUS_NOT_INITIALIZED: \
  118. errormsg = "not initialized"; \
  119. break; \
  120. case CUBLAS_STATUS_ALLOC_FAILED: \
  121. errormsg = "alloc failed"; \
  122. break; \
  123. case CUBLAS_STATUS_INVALID_VALUE: \
  124. errormsg = "invalid value"; \
  125. break; \
  126. case CUBLAS_STATUS_ARCH_MISMATCH: \
  127. errormsg = "arch mismatch"; \
  128. break; \
  129. case CUBLAS_STATUS_EXECUTION_FAILED: \
  130. errormsg = "execution failed"; \
  131. break; \
  132. case CUBLAS_STATUS_INTERNAL_ERROR: \
  133. errormsg = "internal error"; \
  134. break; \
  135. default: \
  136. errormsg = "unknown error"; \
  137. break; \
  138. } \
  139. printf("oops in %s ... %s \n", __func__, errormsg); \
  140. assert(0); \
  141. } while (0)
  142. #define STARPU_CUDA_REPORT_ERROR(status) \
  143. do { \
  144. const char *errormsg = cudaGetErrorString(status); \
  145. printf("oops in %s ... %s \n", __func__, errormsg); \
  146. assert(0); \
  147. } while (0)
  148. #endif // STARPU_USE_CUDA
  149. static inline int starpu_get_env_number(const char *str)
  150. {
  151. char *strval;
  152. strval = getenv(str);
  153. if (strval) {
  154. /* the env variable was actually set */
  155. unsigned val;
  156. char *check;
  157. val = (int)strtol(strval, &check, 10);
  158. STARPU_ASSERT(strcmp(check, "\0") == 0);
  159. //fprintf(stderr, "ENV %s WAS %d\n", str, val);
  160. return val;
  161. }
  162. else {
  163. /* there is no such env variable */
  164. //fprintf("There was no %s ENV\n", str);
  165. return -1;
  166. }
  167. }
  168. /* Add an event in the execution trace if FxT is enabled */
  169. void starpu_trace_user_event(unsigned code);
  170. /* Some helper functions for application using CUBLAS kernels */
  171. void starpu_helper_cublas_init(void);
  172. void starpu_helper_cublas_shutdown(void);
  173. /* Call func(arg) on every worker matching the "where" mask (eg.
  174. * STARPU_CUDA|STARPU_CPU to execute the function on every CPU and every CUDA
  175. * device). This function is synchronous, but the different workers may execute
  176. * the function in parallel.
  177. * */
  178. void starpu_execute_on_each_worker(void (*func)(void *), void *arg, uint32_t where);
  179. /* This creates (and submits) an empty task that unlocks a tag once all its
  180. * dependencies are fulfilled. */
  181. void starpu_create_sync_task(starpu_tag_t sync_tag, unsigned ndeps, starpu_tag_t *deps,
  182. void (*callback)(void *), void *callback_arg);
  183. #ifdef STARPU_USE_CUDA
  184. cudaStream_t *starpu_cuda_get_local_stream(void);
  185. #endif
  186. /* If FILE is currently on a comment line, eat it. */
  187. void starpu_drop_comments(FILE *f);
  188. #ifdef __cplusplus
  189. }
  190. #endif
  191. #endif // __STARPU_UTIL_H__