starpu_thread_util.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010, 2012-2013 Université de Bordeaux 1
  4. * Copyright (C) 2010, 2011, 2012, 2013 Centre National de la Recherche Scientifique
  5. *
  6. * StarPU is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation; either version 2.1 of the License, or (at
  9. * your option) any later version.
  10. *
  11. * StarPU is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. *
  15. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  16. */
  17. #ifndef __STARPU_THREAD_UTIL_H__
  18. #define __STARPU_THREAD_UTIL_H__
  19. #include <starpu.h>
  20. /*
  21. * Encapsulation of the starpu_pthread_create_* functions.
  22. */
  23. #define STARPU_PTHREAD_CREATE_ON(name, thread, attr, routine, arg, where) do { \
  24. int p_ret = starpu_pthread_create_on((name), (thread), (attr), (routine), (arg), (where)); \
  25. if (STARPU_UNLIKELY(p_ret != 0)) { \
  26. fprintf(stderr, \
  27. "%s:%d starpu_pthread_create_on: %s\n", \
  28. __FILE__, __LINE__, strerror(p_ret)); \
  29. STARPU_ABORT(); \
  30. } \
  31. } while (0)
  32. #define STARPU_PTHREAD_CREATE(thread, attr, routine, arg) do { \
  33. int p_ret = starpu_pthread_create((thread), (attr), (routine), (arg)); \
  34. if (STARPU_UNLIKELY(p_ret != 0)) { \
  35. fprintf(stderr, \
  36. "%s:%d starpu_pthread_create: %s\n", \
  37. __FILE__, __LINE__, strerror(p_ret)); \
  38. STARPU_ABORT(); \
  39. } \
  40. } while (0)
  41. /*
  42. * Encapsulation of the starpu_pthread_mutex_* functions.
  43. */
  44. #define STARPU_PTHREAD_MUTEX_INIT(mutex, attr) do { \
  45. int p_ret = starpu_pthread_mutex_init((mutex), (attr)); \
  46. if (STARPU_UNLIKELY(p_ret)) { \
  47. fprintf(stderr, \
  48. "%s:%d starpu_pthread_mutex_init: %s\n", \
  49. __FILE__, __LINE__, strerror(p_ret)); \
  50. STARPU_ABORT(); \
  51. } \
  52. } while (0)
  53. #define STARPU_PTHREAD_MUTEX_DESTROY(mutex) do { \
  54. int p_ret = starpu_pthread_mutex_destroy(mutex); \
  55. if (STARPU_UNLIKELY(p_ret)) { \
  56. fprintf(stderr, \
  57. "%s:%d starpu_pthread_mutex_destroy: %s\n", \
  58. __FILE__, __LINE__, strerror(p_ret)); \
  59. STARPU_ABORT(); \
  60. } \
  61. } while(0)
  62. #define STARPU_PTHREAD_MUTEX_LOCK(mutex) do { \
  63. int p_ret = starpu_pthread_mutex_lock(mutex); \
  64. if (STARPU_UNLIKELY(p_ret)) { \
  65. fprintf(stderr, \
  66. "%s:%d starpu_pthread_mutex_lock: %s\n", \
  67. __FILE__, __LINE__, strerror(p_ret)); \
  68. STARPU_ABORT(); \
  69. } \
  70. } while (0)
  71. #define STARPU_PTHREAD_MUTEX_UNLOCK(mutex) do { \
  72. int p_ret = starpu_pthread_mutex_unlock(mutex); \
  73. if (STARPU_UNLIKELY(p_ret)) { \
  74. fprintf(stderr, \
  75. "%s:%d starpu_pthread_mutex_unlock: %s\n", \
  76. __FILE__, __LINE__, strerror(p_ret)); \
  77. STARPU_ABORT(); \
  78. } \
  79. } while (0)
  80. /*
  81. * Encapsulation of the starpu_pthread_key_* functions.
  82. */
  83. #define STARPU_PTHREAD_KEY_CREATE(key, destr) do { \
  84. int p_ret = starpu_pthread_key_create((key), (destr)); \
  85. if (STARPU_UNLIKELY(p_ret != 0)) { \
  86. fprintf(stderr, \
  87. "%s:%d starpu_pthread_key_create: %s\n", \
  88. __FILE__, __LINE__, strerror(p_ret)); \
  89. } \
  90. } while (0)
  91. #define STARPU_PTHREAD_KEY_DELETE(key) do { \
  92. int p_ret = starpu_pthread_key_delete((key)); \
  93. if (STARPU_UNLIKELY(p_ret != 0)) { \
  94. fprintf(stderr, \
  95. "%s:%d starpu_pthread_key_delete: %s\n", \
  96. __FILE__, __LINE__, strerror(p_ret)); \
  97. } \
  98. } while (0)
  99. #define STARPU_PTHREAD_SETSPECIFIC(key, ptr) do { \
  100. int p_ret = starpu_pthread_setspecific((key), (ptr)); \
  101. if (STARPU_UNLIKELY(p_ret != 0)) { \
  102. fprintf(stderr, \
  103. "%s:%d starpu_pthread_setspecific: %s\n", \
  104. __FILE__, __LINE__, strerror(p_ret)); \
  105. }; \
  106. } while (0)
  107. #define STARPU_PTHREAD_GETSPECIFIC(key) starpu_pthread_getspecific((key))
  108. /*
  109. * Encapsulation of the starpu_pthread_rwlock_* functions.
  110. */
  111. #define STARPU_PTHREAD_RWLOCK_INIT(rwlock, attr) do { \
  112. int p_ret = starpu_pthread_rwlock_init((rwlock), (attr)); \
  113. if (STARPU_UNLIKELY(p_ret)) { \
  114. fprintf(stderr, \
  115. "%s:%d starpu_pthread_rwlock_init: %s\n", \
  116. __FILE__, __LINE__, strerror(p_ret)); \
  117. STARPU_ABORT(); \
  118. } \
  119. } while (0)
  120. #define STARPU_PTHREAD_RWLOCK_RDLOCK(rwlock) do { \
  121. int p_ret = starpu_pthread_rwlock_rdlock(rwlock); \
  122. if (STARPU_UNLIKELY(p_ret)) { \
  123. fprintf(stderr, \
  124. "%s:%d starpu_pthread_rwlock_rdlock: %s\n", \
  125. __FILE__, __LINE__, strerror(p_ret)); \
  126. STARPU_ABORT(); \
  127. } \
  128. } while (0)
  129. #define STARPU_PTHREAD_RWLOCK_WRLOCK(rwlock) do { \
  130. int p_ret = starpu_pthread_rwlock_wrlock(rwlock); \
  131. if (STARPU_UNLIKELY(p_ret)) { \
  132. fprintf(stderr, \
  133. "%s:%d starpu_pthread_rwlock_wrlock: %s\n", \
  134. __FILE__, __LINE__, strerror(p_ret)); \
  135. STARPU_ABORT(); \
  136. } \
  137. } while (0)
  138. #define STARPU_PTHREAD_RWLOCK_UNLOCK(rwlock) do { \
  139. int p_ret = starpu_pthread_rwlock_unlock(rwlock); \
  140. if (STARPU_UNLIKELY(p_ret)) { \
  141. fprintf(stderr, \
  142. "%s:%d starpu_pthread_rwlock_unlock: %s\n", \
  143. __FILE__, __LINE__, strerror(p_ret)); \
  144. STARPU_ABORT(); \
  145. } \
  146. } while (0)
  147. #define STARPU_PTHREAD_RWLOCK_DESTROY(rwlock) do { \
  148. int p_ret = starpu_pthread_rwlock_destroy(rwlock); \
  149. if (STARPU_UNLIKELY(p_ret)) { \
  150. fprintf(stderr, \
  151. "%s:%d starpu_pthread_rwlock_destroy: %s\n", \
  152. __FILE__, __LINE__, strerror(p_ret)); \
  153. STARPU_ABORT(); \
  154. } \
  155. } while (0)
  156. /*
  157. * Encapsulation of the starpu_pthread_cond_* functions.
  158. */
  159. #define STARPU_PTHREAD_COND_INIT(cond, attr) do { \
  160. int p_ret = starpu_pthread_cond_init((cond), (attr)); \
  161. if (STARPU_UNLIKELY(p_ret)) { \
  162. fprintf(stderr, \
  163. "%s:%d starpu_pthread_cond_init: %s\n", \
  164. __FILE__, __LINE__, strerror(p_ret)); \
  165. STARPU_ABORT(); \
  166. } \
  167. } while (0)
  168. #define STARPU_PTHREAD_COND_DESTROY(cond) do { \
  169. int p_ret = starpu_pthread_cond_destroy(cond); \
  170. if (STARPU_UNLIKELY(p_ret)) { \
  171. fprintf(stderr, \
  172. "%s:%d starpu_pthread_cond_destroy: %s\n", \
  173. __FILE__, __LINE__, strerror(p_ret)); \
  174. STARPU_ABORT(); \
  175. } \
  176. } while (0)
  177. #define STARPU_PTHREAD_COND_SIGNAL(cond) do { \
  178. int p_ret = starpu_pthread_cond_signal(cond); \
  179. if (STARPU_UNLIKELY(p_ret)) { \
  180. fprintf(stderr, \
  181. "%s:%d starpu_pthread_cond_signal: %s\n", \
  182. __FILE__, __LINE__, strerror(p_ret)); \
  183. STARPU_ABORT(); \
  184. } \
  185. } while (0)
  186. #define STARPU_PTHREAD_COND_BROADCAST(cond) do { \
  187. int p_ret = starpu_pthread_cond_broadcast(cond); \
  188. if (STARPU_UNLIKELY(p_ret)) { \
  189. fprintf(stderr, \
  190. "%s:%d starpu_pthread_cond_broadcast: %s\n", \
  191. __FILE__, __LINE__, strerror(p_ret)); \
  192. STARPU_ABORT(); \
  193. } \
  194. } while (0)
  195. #define STARPU_PTHREAD_COND_WAIT(cond, mutex) do { \
  196. int p_ret = starpu_pthread_cond_wait((cond), (mutex)); \
  197. if (STARPU_UNLIKELY(p_ret)) { \
  198. fprintf(stderr, \
  199. "%s:%d starpu_pthread_cond_wait: %s\n", \
  200. __FILE__, __LINE__, strerror(p_ret)); \
  201. STARPU_ABORT(); \
  202. } \
  203. } while (0)
  204. /*
  205. * Encapsulation of the starpu_pthread_barrier_* functions.
  206. */
  207. #define STARPU_PTHREAD_BARRIER_INIT(barrier, attr, count) do { \
  208. int p_ret = pthread_barrier_init((barrier), (attr), (count)); \
  209. if (STARPU_UNLIKELY(p_ret)) { \
  210. fprintf(stderr, \
  211. "%s:%d pthread_barrier_init: %s\n", \
  212. __FILE__, __LINE__, strerror(p_ret)); \
  213. STARPU_ABORT(); \
  214. } \
  215. } while (0)
  216. #define STARPU_PTHREAD_BARRIER_DESTROY(barrier) do { \
  217. int p_ret = pthread_barrier_destroy((barrier)); \
  218. if (STARPU_UNLIKELY(p_ret)) { \
  219. fprintf(stderr, \
  220. "%s:%d pthread_barrier_destroy: %s\n", \
  221. __FILE__, __LINE__, strerror(p_ret)); \
  222. STARPU_ABORT(); \
  223. } \
  224. } while (0)
  225. #define STARPU_PTHREAD_BARRIER_WAIT(barrier) do { \
  226. int p_ret = pthread_barrier_wait(barrier); \
  227. if (STARPU_UNLIKELY(!((p_ret == 0) || (p_ret == PTHREAD_BARRIER_SERIAL_THREAD)))) { \
  228. fprintf(stderr, \
  229. "%s:%d pthread_barrier_wait: %s\n", \
  230. __FILE__, __LINE__, strerror(p_ret)); \
  231. STARPU_ABORT(); \
  232. } \
  233. } while (0)
  234. #endif /* __STARPU_THREAD_UTIL_H__ */