starpu-util.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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_MIN(a,b) ((a)<(b)?(a):(b))
  33. #define STARPU_MAX(a,b) ((a)<(b)?(b):(a))
  34. #ifdef STARPU_NO_ASSERT
  35. #define STARPU_ASSERT(x) do {} while(0);
  36. #else
  37. #define STARPU_ASSERT(x) assert(x)
  38. #endif
  39. #define STARPU_ABORT() abort()
  40. #define STARPU_UNLIKELY(expr) (__builtin_expect(!!(expr),0))
  41. #define STARPU_LIKELY(expr) (__builtin_expect(!!(expr),1))
  42. #ifdef HAVE_SYNC_FETCH_AND_ADD
  43. #define STARPU_ATOMIC_ADD(ptr, value) (__sync_fetch_and_add ((ptr), (value)) + (value))
  44. #define STARPU_ATOMIC_OR(ptr, value) (__sync_fetch_and_or ((ptr), (value)))
  45. #else
  46. #error __sync_fetch_and_add is not available
  47. #endif
  48. #ifdef USE_CUDA
  49. #define CUBLAS_REPORT_ERROR(status) \
  50. do { \
  51. char *errormsg; \
  52. switch (status) { \
  53. case CUBLAS_STATUS_SUCCESS: \
  54. errormsg = "success"; \
  55. break; \
  56. case CUBLAS_STATUS_NOT_INITIALIZED: \
  57. errormsg = "not initialized"; \
  58. break; \
  59. case CUBLAS_STATUS_ALLOC_FAILED: \
  60. errormsg = "alloc failed"; \
  61. break; \
  62. case CUBLAS_STATUS_INVALID_VALUE: \
  63. errormsg = "invalid value"; \
  64. break; \
  65. case CUBLAS_STATUS_ARCH_MISMATCH: \
  66. errormsg = "arch mismatch"; \
  67. break; \
  68. case CUBLAS_STATUS_EXECUTION_FAILED: \
  69. errormsg = "execution failed"; \
  70. break; \
  71. case CUBLAS_STATUS_INTERNAL_ERROR: \
  72. errormsg = "internal error"; \
  73. break; \
  74. default: \
  75. errormsg = "unknown error"; \
  76. break; \
  77. } \
  78. printf("oops in %s ... %s \n", __func__, errormsg); \
  79. assert(0); \
  80. } while (0)
  81. #define CUDA_REPORT_ERROR(status) \
  82. do { \
  83. char *errormsg; \
  84. switch (status) { \
  85. case CUDA_SUCCESS: \
  86. errormsg = "success"; \
  87. break; \
  88. case CUDA_ERROR_INVALID_VALUE: \
  89. errormsg = "invalid value"; \
  90. break; \
  91. case CUDA_ERROR_OUT_OF_MEMORY: \
  92. errormsg = "out of memory"; \
  93. break; \
  94. case CUDA_ERROR_NOT_INITIALIZED: \
  95. errormsg = "not initialized"; \
  96. break; \
  97. case CUDA_ERROR_DEINITIALIZED: \
  98. errormsg = "deinitialized"; \
  99. break; \
  100. case CUDA_ERROR_NO_DEVICE: \
  101. errormsg = "no device"; \
  102. break; \
  103. case CUDA_ERROR_INVALID_DEVICE: \
  104. errormsg = "invalid device"; \
  105. break; \
  106. case CUDA_ERROR_INVALID_IMAGE: \
  107. errormsg = "invalid image"; \
  108. break; \
  109. case CUDA_ERROR_INVALID_CONTEXT: \
  110. errormsg = "invalid context"; \
  111. break; \
  112. case CUDA_ERROR_CONTEXT_ALREADY_CURRENT: \
  113. errormsg = "context already current"; \
  114. break; \
  115. case CUDA_ERROR_MAP_FAILED: \
  116. errormsg = "map failed"; \
  117. break; \
  118. case CUDA_ERROR_UNMAP_FAILED: \
  119. errormsg = "unmap failed"; \
  120. break; \
  121. case CUDA_ERROR_ARRAY_IS_MAPPED: \
  122. errormsg = "array is mapped"; \
  123. break; \
  124. case CUDA_ERROR_ALREADY_MAPPED: \
  125. errormsg = "already mapped"; \
  126. break; \
  127. case CUDA_ERROR_NO_BINARY_FOR_GPU: \
  128. errormsg = "no binary for gpu"; \
  129. break; \
  130. case CUDA_ERROR_ALREADY_ACQUIRED: \
  131. errormsg = "already acquired"; \
  132. break; \
  133. case CUDA_ERROR_NOT_MAPPED: \
  134. errormsg = "not mapped"; \
  135. break; \
  136. case CUDA_ERROR_INVALID_SOURCE: \
  137. errormsg = "invalid source"; \
  138. break; \
  139. case CUDA_ERROR_FILE_NOT_FOUND: \
  140. errormsg = "file not found"; \
  141. break; \
  142. case CUDA_ERROR_INVALID_HANDLE: \
  143. errormsg = "invalid handle"; \
  144. break; \
  145. case CUDA_ERROR_NOT_FOUND: \
  146. errormsg = "not found"; \
  147. break; \
  148. case CUDA_ERROR_NOT_READY: \
  149. errormsg = "not ready"; \
  150. break; \
  151. case CUDA_ERROR_LAUNCH_FAILED: \
  152. errormsg = "launch failed"; \
  153. break; \
  154. case CUDA_ERROR_LAUNCH_OUT_OF_RESOURCES: \
  155. errormsg = "launch out of resources"; \
  156. break; \
  157. case CUDA_ERROR_LAUNCH_TIMEOUT: \
  158. errormsg = "launch timeout"; \
  159. break; \
  160. case CUDA_ERROR_LAUNCH_INCOMPATIBLE_TEXTURING: \
  161. errormsg = "launch incompatible texturing";\
  162. break; \
  163. case CUDA_ERROR_UNKNOWN: \
  164. default: \
  165. errormsg = "unknown error"; \
  166. break; \
  167. } \
  168. printf("oops in %s ... %s \n", __func__, errormsg); \
  169. assert(0); \
  170. } while (0)
  171. #endif // USE_CUDA
  172. static inline int starpu_get_env_number(const char *str)
  173. {
  174. char *strval;
  175. strval = getenv(str);
  176. if (strval) {
  177. /* the env variable was actually set */
  178. unsigned val;
  179. char *check;
  180. val = (int)strtol(strval, &check, 10);
  181. STARPU_ASSERT(strcmp(check, "\0") == 0);
  182. //fprintf(stderr, "ENV %s WAS %d\n", str, val);
  183. return val;
  184. }
  185. else {
  186. /* there is no such env variable */
  187. //fprintf("There was no %s ENV\n", str);
  188. return -1;
  189. }
  190. }
  191. /* Add an event in the execution trace if FxT is enabled */
  192. void starpu_trace_user_event(unsigned code);
  193. /* Some helper functions for application using CUBLAS kernels */
  194. void starpu_helper_init_cublas(void);
  195. void starpu_helper_shutdown_cublas(void);
  196. /* Call func(arg) on every worker matching the "where" mask (eg. CUDA|CORE to
  197. * execute the function on every CPUs and every CUDA devices). This function is
  198. * synchronous, but the different workers may execute the function in parallel.
  199. * */
  200. void starpu_execute_on_each_worker(void (*func)(void *), void *arg, uint32_t where);
  201. /* This creates (and submits) an empty task that unlocks a tag once all its
  202. * dependencies are fulfilled. */
  203. void starpu_create_sync_task(starpu_tag_t sync_tag, unsigned ndeps, starpu_tag_t *deps,
  204. void (*callback)(void *), void *callback_arg);
  205. #ifdef USE_CUDA
  206. /* In case the application is using streams, these functions will
  207. * repespectively create, and return, a unique stream for every GPU worker. */
  208. void starpu_helper_create_per_gpu_streams(void);
  209. cudaStream_t *starpu_helper_get_local_stream(void);
  210. #endif
  211. #ifdef __cplusplus
  212. }
  213. #endif
  214. #endif // __STARPU_UTIL_H__