utils.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010, 2012 Université de Bordeaux 1
  4. * Copyright (C) 2010, 2011, 2012 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 __COMMON_UTILS_H__
  18. #define __COMMON_UTILS_H__
  19. #include <starpu.h>
  20. #include <common/config.h>
  21. #include <sys/stat.h>
  22. #include <string.h>
  23. #include <pthread.h>
  24. #include <common/barrier.h>
  25. #include <stdlib.h>
  26. #include <math.h>
  27. #ifdef STARPU_VERBOSE
  28. # define _STARPU_DEBUG(fmt, args ...) do { if (!getenv("STARPU_SILENT")) {fprintf(stderr, "[starpu][%s] " fmt ,__func__ ,##args); fflush(stderr); }} while(0)
  29. #else
  30. # define _STARPU_DEBUG(fmt, args ...) do { } while (0)
  31. #endif
  32. #ifdef STARPU_VERBOSE0
  33. # define _STARPU_LOG_IN() do { if (!getenv("STARPU_SILENT")) {fprintf(stderr, "[starpu][%ld][%s] -->\n", pthread_self(), __func__ ); }} while(0)
  34. # define _STARPU_LOG_OUT() do { if (!getenv("STARPU_SILENT")) {fprintf(stderr, "[starpu][%ld][%s] <--\n", pthread_self(), __func__ ); }} while(0)
  35. # define _STARPU_LOG_OUT_TAG(outtag) do { if (!getenv("STARPU_SILENT")) {fprintf(stderr, "[starpu][%ld][%s] <-- (%s)\n", pthread_self(), __func__, outtag); }} while(0)
  36. #else
  37. # define _STARPU_LOG_IN()
  38. # define _STARPU_LOG_OUT()
  39. # define _STARPU_LOG_OUT_TAG(outtag)
  40. #endif
  41. #define _STARPU_DISP(fmt, args ...) fprintf(stderr, "[starpu][%s] " fmt ,__func__ ,##args)
  42. #define _STARPU_ERROR(fmt, args ...) \
  43. do { \
  44. fprintf(stderr, "[starpu][%s] Error: " fmt ,__func__ ,##args); \
  45. STARPU_ABORT(); \
  46. } while (0)
  47. #define _STARPU_IS_ZERO(a) (fpclassify(a) == FP_ZERO)
  48. int _starpu_mkpath(const char *s, mode_t mode);
  49. void _starpu_mkpath_and_check(const char *s, mode_t mode);
  50. int _starpu_check_mutex_deadlock(pthread_mutex_t *mutex);
  51. char *_starpu_get_home_path(void);
  52. /* If FILE is currently on a comment line, eat it. */
  53. void _starpu_drop_comments(FILE *f);
  54. struct _starpu_job;
  55. /* Returns the symbol associated to that job if any. */
  56. const char *_starpu_job_get_model_name(struct _starpu_job *j);
  57. struct starpu_codelet;
  58. /* Returns the symbol associated to that job if any. */
  59. const char *_starpu_codelet_get_model_name(struct starpu_codelet *cl);
  60. #define _STARPU_PTHREAD_CREATE(thread, attr, routine, arg) do { \
  61. int p_ret = pthread_create((thread), (attr), (routine), (arg)); \
  62. if (STARPU_UNLIKELY(p_ret != 0)) { \
  63. fprintf(stderr, \
  64. "%s:%d pthread_create: %s\n", \
  65. __FILE__, __LINE__, strerror(p_ret)); \
  66. } \
  67. } while (0)
  68. /*
  69. * Encapsulation of the pthread_mutex_* functions.
  70. */
  71. #define _STARPU_PTHREAD_MUTEX_INIT(mutex, attr) do { \
  72. int p_ret = pthread_mutex_init((mutex), (attr)); \
  73. if (STARPU_UNLIKELY(p_ret)) { \
  74. fprintf(stderr, \
  75. "%s:%d pthread_mutex_init: %s\n", \
  76. __FILE__, __LINE__, strerror(p_ret)); \
  77. STARPU_ABORT(); \
  78. } \
  79. } while (0)
  80. #define _STARPU_PTHREAD_MUTEX_DESTROY(mutex) do { \
  81. int p_ret = pthread_mutex_destroy(mutex); \
  82. if (STARPU_UNLIKELY(p_ret)) { \
  83. fprintf(stderr, \
  84. "%s:%d pthread_mutex_destroy: %s\n", \
  85. __FILE__, __LINE__, strerror(p_ret)); \
  86. STARPU_ABORT(); \
  87. } \
  88. } while(0)
  89. #define _STARPU_PTHREAD_MUTEX_LOCK(mutex) do { \
  90. int p_ret = pthread_mutex_lock(mutex); \
  91. if (STARPU_UNLIKELY(p_ret)) { \
  92. fprintf(stderr, \
  93. "%s:%d pthread_mutex_lock: %s\n", \
  94. __FILE__, __LINE__, strerror(p_ret)); \
  95. STARPU_ABORT(); \
  96. } \
  97. } while (0)
  98. #define _STARPU_PTHREAD_MUTEX_UNLOCK(mutex) do { \
  99. int p_ret = pthread_mutex_unlock(mutex); \
  100. if (STARPU_UNLIKELY(p_ret)) { \
  101. fprintf(stderr, \
  102. "%s:%d pthread_mutex_unlock: %s\n", \
  103. __FILE__, __LINE__, strerror(p_ret)); \
  104. STARPU_ABORT(); \
  105. } \
  106. } while (0)
  107. /*
  108. * Encapsulation of the pthread_rwlock_* functions.
  109. */
  110. #define _STARPU_PTHREAD_RWLOCK_INIT(rwlock, attr) do { \
  111. int p_ret = pthread_rwlock_init((rwlock), (attr)); \
  112. if (STARPU_UNLIKELY(p_ret)) { \
  113. fprintf(stderr, \
  114. "%s:%d pthread_rwlock_init: %s\n", \
  115. __FILE__, __LINE__, strerror(p_ret)); \
  116. STARPU_ABORT(); \
  117. } \
  118. } while (0)
  119. #define _STARPU_PTHREAD_RWLOCK_RDLOCK(rwlock) do { \
  120. int p_ret = pthread_rwlock_rdlock(rwlock); \
  121. if (STARPU_UNLIKELY(p_ret)) { \
  122. fprintf(stderr, \
  123. "%s:%d pthread_rwlock_rdlock: %s\n", \
  124. __FILE__, __LINE__, strerror(p_ret)); \
  125. STARPU_ABORT(); \
  126. } \
  127. } while (0)
  128. #define _STARPU_PTHREAD_RWLOCK_WRLOCK(rwlock) do { \
  129. int p_ret = pthread_rwlock_wrlock(rwlock); \
  130. if (STARPU_UNLIKELY(p_ret)) { \
  131. fprintf(stderr, \
  132. "%s:%d pthread_rwlock_wrlock: %s\n", \
  133. __FILE__, __LINE__, strerror(p_ret)); \
  134. STARPU_ABORT(); \
  135. } \
  136. } while (0)
  137. #define _STARPU_PTHREAD_RWLOCK_UNLOCK(rwlock) do { \
  138. int p_ret = pthread_rwlock_unlock(rwlock); \
  139. if (STARPU_UNLIKELY(p_ret)) { \
  140. fprintf(stderr, \
  141. "%s:%d pthread_rwlock_unlock: %s\n", \
  142. __FILE__, __LINE__, strerror(p_ret)); \
  143. STARPU_ABORT(); \
  144. } \
  145. } while (0)
  146. #define _STARPU_PTHREAD_RWLOCK_DESTROY(rwlock) do { \
  147. int p_ret = pthread_rwlock_destroy(rwlock); \
  148. if (STARPU_UNLIKELY(p_ret)) { \
  149. fprintf(stderr, \
  150. "%s:%d pthread_rwlock_destroy: %s\n", \
  151. __FILE__, __LINE__, strerror(p_ret)); \
  152. STARPU_ABORT(); \
  153. } \
  154. } while (0)
  155. /*
  156. * Encapsulation of the pthread_cond_* functions.
  157. */
  158. #define _STARPU_PTHREAD_COND_INIT(cond, attr) do { \
  159. int p_ret = pthread_cond_init((cond), (attr)); \
  160. if (STARPU_UNLIKELY(p_ret)) { \
  161. fprintf(stderr, \
  162. "%s:%d pthread_cond_init: %s\n", \
  163. __FILE__, __LINE__, strerror(p_ret)); \
  164. STARPU_ABORT(); \
  165. } \
  166. } while (0)
  167. #define _STARPU_PTHREAD_COND_DESTROY(cond) do { \
  168. int p_ret = pthread_cond_destroy(cond); \
  169. if (STARPU_UNLIKELY(p_ret)) { \
  170. fprintf(stderr, \
  171. "%s:%d pthread_cond_destroy: %s\n", \
  172. __FILE__, __LINE__, strerror(p_ret)); \
  173. STARPU_ABORT(); \
  174. } \
  175. } while (0)
  176. #define _STARPU_PTHREAD_COND_SIGNAL(cond) do { \
  177. int p_ret = pthread_cond_signal(cond); \
  178. if (STARPU_UNLIKELY(p_ret)) { \
  179. fprintf(stderr, \
  180. "%s:%d pthread_cond_signal: %s\n", \
  181. __FILE__, __LINE__, strerror(p_ret)); \
  182. STARPU_ABORT(); \
  183. } \
  184. } while (0)
  185. #define _STARPU_PTHREAD_COND_BROADCAST(cond) do { \
  186. int p_ret = pthread_cond_broadcast(cond); \
  187. if (STARPU_UNLIKELY(p_ret)) { \
  188. fprintf(stderr, \
  189. "%s:%d pthread_cond_broadcast: %s\n", \
  190. __FILE__, __LINE__, strerror(p_ret)); \
  191. STARPU_ABORT(); \
  192. } \
  193. } while (0)
  194. #define _STARPU_PTHREAD_COND_WAIT(cond, mutex) do { \
  195. int p_ret = pthread_cond_wait((cond), (mutex)); \
  196. if (STARPU_UNLIKELY(p_ret)) { \
  197. fprintf(stderr, \
  198. "%s:%d pthread_cond_wait: %s\n", \
  199. __FILE__, __LINE__, strerror(p_ret)); \
  200. STARPU_ABORT(); \
  201. } \
  202. } while (0)
  203. /*
  204. * Encapsulation of the pthread_barrier_* functions.
  205. */
  206. #define _STARPU_PTHREAD_BARRIER_INIT(barrier, attr, count) do { \
  207. int p_ret = pthread_barrier_init((barrier), (attr), (count)); \
  208. if (STARPU_UNLIKELY(p_ret)) { \
  209. fprintf(stderr, \
  210. "%s:%d pthread_barrier_init: %s\n", \
  211. __FILE__, __LINE__, strerror(p_ret)); \
  212. STARPU_ABORT(); \
  213. } \
  214. } while (0)
  215. #define _STARPU_PTHREAD_BARRIER_DESTROY(barrier) do { \
  216. int p_ret = pthread_barrier_destroy((barrier)); \
  217. if (STARPU_UNLIKELY(p_ret)) { \
  218. fprintf(stderr, \
  219. "%s:%d pthread_barrier_destroy: %s\n", \
  220. __FILE__, __LINE__, strerror(p_ret)); \
  221. STARPU_ABORT(); \
  222. } \
  223. } while (0)
  224. #define _STARPU_PTHREAD_BARRIER_WAIT(barrier) do { \
  225. int p_ret = pthread_barrier_wait(barrier); \
  226. if (STARPU_UNLIKELY(!((p_ret == 0) || (p_ret == PTHREAD_BARRIER_SERIAL_THREAD)))) { \
  227. fprintf(stderr, \
  228. "%s:%d pthread_barrier_wait: %s\n", \
  229. __FILE__, __LINE__, strerror(p_ret)); \
  230. STARPU_ABORT(); \
  231. } \
  232. } while (0)
  233. /*
  234. * Encapsulation of the pthread_spin_* functions.
  235. */
  236. #define _STARPU_PTHREAD_SPIN_DESTROY(lock) do { \
  237. int p_ret = pthread_spin_destroy(lock); \
  238. if (STARPU_UNLIKELY(p_ret)) { \
  239. fprintf(stderr, \
  240. "%s:%d pthread_spin_destroy: %s\n", \
  241. __FILE__, __LINE__, strerror(p_ret)); \
  242. STARPU_ABORT(); \
  243. } \
  244. } while (0)
  245. #define _STARPU_PTHREAD_SPIN_LOCK(lock) do { \
  246. int p_ret = pthread_spin_lock(lock); \
  247. if (STARPU_UNLIKELY(p_ret)) { \
  248. fprintf(stderr, \
  249. "%s:%d pthread_spin_lock: %s\n", \
  250. __FILE__, __LINE__, strerror(p_ret)); \
  251. STARPU_ABORT(); \
  252. } \
  253. } while (0)
  254. #define _STARPU_PTHREAD_SPIN_UNLOCK(lock) do { \
  255. int p_ret = pthread_spin_unlock(lock); \
  256. if (STARPU_UNLIKELY(p_ret)) { \
  257. fprintf(stderr,  \
  258. "%s:%d pthread_spin_unlock: %s\n", \
  259. __FILE__, __LINE__, strerror(p_ret)); \
  260. STARPU_ABORT(); \
  261. } \
  262. } while (0)
  263. #endif // __COMMON_UTILS_H__