starpu-util.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. #ifdef USE_CUDA
  24. #include <cuda.h>
  25. #include <cuda_runtime_api.h>
  26. #include <cublas.h>
  27. #endif
  28. #ifdef __cplusplus
  29. extern "C" {
  30. #endif
  31. #define STARPU_MIN(a,b) ((a)<(b)?(a):(b))
  32. #define STARPU_MAX(a,b) ((a)<(b)?(b):(a))
  33. #ifdef STARPU_NO_ASSERT
  34. #define STARPU_ASSERT(x) do {} while(0);
  35. #else
  36. #define STARPU_ASSERT(x) assert(x)
  37. #endif
  38. #define STARPU_ABORT() abort()
  39. #define STARPU_UNLIKELY(expr) (__builtin_expect(!!(expr),0))
  40. #define STARPU_LIKELY(expr) (__builtin_expect(!!(expr),1))
  41. #ifdef HAVE_SYNC_FETCH_AND_ADD
  42. #define STARPU_ATOMIC_ADD(ptr, value) (__sync_fetch_and_add ((ptr), (value)) + (value))
  43. #define STARPU_ATOMIC_OR(ptr, value) (__sync_fetch_and_or ((ptr), (value)))
  44. #else
  45. #error __sync_fetch_and_add is not available
  46. #endif
  47. #ifdef USE_CUDA
  48. #define CUBLAS_REPORT_ERROR(status) \
  49. do { \
  50. char *errormsg; \
  51. switch (status) { \
  52. case CUBLAS_STATUS_SUCCESS: \
  53. errormsg = "success"; \
  54. break; \
  55. case CUBLAS_STATUS_NOT_INITIALIZED: \
  56. errormsg = "not initialized"; \
  57. break; \
  58. case CUBLAS_STATUS_ALLOC_FAILED: \
  59. errormsg = "alloc failed"; \
  60. break; \
  61. case CUBLAS_STATUS_INVALID_VALUE: \
  62. errormsg = "invalid value"; \
  63. break; \
  64. case CUBLAS_STATUS_ARCH_MISMATCH: \
  65. errormsg = "arch mismatch"; \
  66. break; \
  67. case CUBLAS_STATUS_EXECUTION_FAILED: \
  68. errormsg = "execution failed"; \
  69. break; \
  70. case CUBLAS_STATUS_INTERNAL_ERROR: \
  71. errormsg = "internal error"; \
  72. break; \
  73. default: \
  74. errormsg = "unknown error"; \
  75. break; \
  76. } \
  77. printf("oops in %s ... %s \n", __func__, errormsg); \
  78. assert(0); \
  79. } while (0)
  80. #define CUDA_REPORT_ERROR(status) \
  81. do { \
  82. char *errormsg; \
  83. switch (status) { \
  84. case CUDA_SUCCESS: \
  85. errormsg = "success"; \
  86. break; \
  87. case CUDA_ERROR_INVALID_VALUE: \
  88. errormsg = "invalid value"; \
  89. break; \
  90. case CUDA_ERROR_OUT_OF_MEMORY: \
  91. errormsg = "out of memory"; \
  92. break; \
  93. case CUDA_ERROR_NOT_INITIALIZED: \
  94. errormsg = "not initialized"; \
  95. break; \
  96. case CUDA_ERROR_DEINITIALIZED: \
  97. errormsg = "deinitialized"; \
  98. break; \
  99. case CUDA_ERROR_NO_DEVICE: \
  100. errormsg = "no device"; \
  101. break; \
  102. case CUDA_ERROR_INVALID_DEVICE: \
  103. errormsg = "invalid device"; \
  104. break; \
  105. case CUDA_ERROR_INVALID_IMAGE: \
  106. errormsg = "invalid image"; \
  107. break; \
  108. case CUDA_ERROR_INVALID_CONTEXT: \
  109. errormsg = "invalid context"; \
  110. break; \
  111. case CUDA_ERROR_CONTEXT_ALREADY_CURRENT: \
  112. errormsg = "context already current"; \
  113. break; \
  114. case CUDA_ERROR_MAP_FAILED: \
  115. errormsg = "map failed"; \
  116. break; \
  117. case CUDA_ERROR_UNMAP_FAILED: \
  118. errormsg = "unmap failed"; \
  119. break; \
  120. case CUDA_ERROR_ARRAY_IS_MAPPED: \
  121. errormsg = "array is mapped"; \
  122. break; \
  123. case CUDA_ERROR_ALREADY_MAPPED: \
  124. errormsg = "already mapped"; \
  125. break; \
  126. case CUDA_ERROR_NO_BINARY_FOR_GPU: \
  127. errormsg = "no binary for gpu"; \
  128. break; \
  129. case CUDA_ERROR_ALREADY_ACQUIRED: \
  130. errormsg = "already acquired"; \
  131. break; \
  132. case CUDA_ERROR_NOT_MAPPED: \
  133. errormsg = "not mapped"; \
  134. break; \
  135. case CUDA_ERROR_INVALID_SOURCE: \
  136. errormsg = "invalid source"; \
  137. break; \
  138. case CUDA_ERROR_FILE_NOT_FOUND: \
  139. errormsg = "file not found"; \
  140. break; \
  141. case CUDA_ERROR_INVALID_HANDLE: \
  142. errormsg = "invalid handle"; \
  143. break; \
  144. case CUDA_ERROR_NOT_FOUND: \
  145. errormsg = "not found"; \
  146. break; \
  147. case CUDA_ERROR_NOT_READY: \
  148. errormsg = "not ready"; \
  149. break; \
  150. case CUDA_ERROR_LAUNCH_FAILED: \
  151. errormsg = "launch failed"; \
  152. break; \
  153. case CUDA_ERROR_LAUNCH_OUT_OF_RESOURCES: \
  154. errormsg = "launch out of resources"; \
  155. break; \
  156. case CUDA_ERROR_LAUNCH_TIMEOUT: \
  157. errormsg = "launch timeout"; \
  158. break; \
  159. case CUDA_ERROR_LAUNCH_INCOMPATIBLE_TEXTURING: \
  160. errormsg = "launch incompatible texturing";\
  161. break; \
  162. case CUDA_ERROR_UNKNOWN: \
  163. default: \
  164. errormsg = "unknown error"; \
  165. break; \
  166. } \
  167. printf("oops in %s ... %s \n", __func__, errormsg); \
  168. assert(0); \
  169. } while (0)
  170. #endif // USE_CUDA
  171. #define STARPU_SUCCESS 0
  172. #define STARPU_TRYAGAIN 1
  173. #define STARPU_FATAL 2
  174. static inline int starpu_get_env_number(const char *str)
  175. {
  176. char *strval;
  177. strval = getenv(str);
  178. if (strval) {
  179. /* the env variable was actually set */
  180. unsigned val;
  181. char *check;
  182. val = (int)strtol(strval, &check, 10);
  183. STARPU_ASSERT(strcmp(check, "\0") == 0);
  184. //fprintf(stderr, "ENV %s WAS %d\n", str, val);
  185. return val;
  186. }
  187. else {
  188. /* there is no such env variable */
  189. //fprintf("There was no %s ENV\n", str);
  190. return -1;
  191. }
  192. }
  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. void starpu_execute_on_each_worker(void (*func)(void *), void *arg, uint32_t where);
  198. #ifdef USE_CUDA
  199. void starpu_helper_create_per_gpu_streams(void);
  200. cudaStream_t *starpu_helper_get_local_stream(void);
  201. #endif
  202. #ifdef __cplusplus
  203. }
  204. #endif
  205. #endif // __STARPU_UTIL_H__