starpu-util.h 6.5 KB

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