utils.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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 <stdlib.h>
  25. #include <math.h>
  26. #ifdef STARPU_SIMGRID
  27. #include <xbt/synchro_core.h>
  28. #include <msg/msg.h>
  29. #endif
  30. #ifdef STARPU_VERBOSE
  31. # define _STARPU_DEBUG(fmt, args ...) do { if (!getenv("STARPU_SILENT")) {fprintf(stderr, "[starpu][%s] " fmt ,__func__ ,##args); fflush(stderr); }} while(0)
  32. #else
  33. # define _STARPU_DEBUG(fmt, args ...) do { } while (0)
  34. #endif
  35. #ifdef STARPU_VERBOSE0
  36. # define _STARPU_LOG_IN() do { if (!getenv("STARPU_SILENT")) {fprintf(stderr, "[starpu][%ld][%s] -->\n", pthread_self(), __func__ ); }} while(0)
  37. # define _STARPU_LOG_OUT() do { if (!getenv("STARPU_SILENT")) {fprintf(stderr, "[starpu][%ld][%s] <--\n", pthread_self(), __func__ ); }} while(0)
  38. # define _STARPU_LOG_OUT_TAG(outtag) do { if (!getenv("STARPU_SILENT")) {fprintf(stderr, "[starpu][%ld][%s] <-- (%s)\n", pthread_self(), __func__, outtag); }} while(0)
  39. #else
  40. # define _STARPU_LOG_IN()
  41. # define _STARPU_LOG_OUT()
  42. # define _STARPU_LOG_OUT_TAG(outtag)
  43. #endif
  44. #define _STARPU_DISP(fmt, args ...) do { if (!getenv("STARPU_SILENT")) {fprintf(stderr, "[starpu][%s] " fmt ,__func__ ,##args); }} while(0)
  45. #define _STARPU_ERROR(fmt, args ...) \
  46. do { \
  47. fprintf(stderr, "\n\n[starpu][%s] Error: " fmt ,__func__ ,##args); \
  48. fprintf(stderr, "\n\n"); \
  49. STARPU_ABORT(); \
  50. } while (0)
  51. #define _STARPU_IS_ZERO(a) (fpclassify(a) == FP_ZERO)
  52. #ifdef STARPU_SIMGRID
  53. typedef xbt_mutex_t _starpu_pthread_mutex_t;
  54. #else
  55. typedef pthread_mutex_t _starpu_pthread_mutex_t;
  56. #endif
  57. int _starpu_mkpath(const char *s, mode_t mode);
  58. void _starpu_mkpath_and_check(const char *s, mode_t mode);
  59. int _starpu_check_mutex_deadlock(_starpu_pthread_mutex_t *mutex);
  60. char *_starpu_get_home_path(void);
  61. /* If FILE is currently on a comment line, eat it. */
  62. void _starpu_drop_comments(FILE *f);
  63. struct _starpu_job;
  64. /* Returns the symbol associated to that job if any. */
  65. const char *_starpu_job_get_model_name(struct _starpu_job *j);
  66. struct starpu_codelet;
  67. /* Returns the symbol associated to that job if any. */
  68. const char *_starpu_codelet_get_model_name(struct starpu_codelet *cl);
  69. struct _starpu_pthread_args {
  70. void *(*f)(void*);
  71. void *arg;
  72. };
  73. int _starpu_simgrid_thread_start(int argc, char *argv[]);
  74. #ifdef STARPU_SIMGRID
  75. #define _STARPU_PTHREAD_CREATE_ON(name, thread, attr, routine, threadarg, where) do {\
  76. struct _starpu_pthread_args *_args = malloc(sizeof(*_args)); \
  77. xbt_dynar_t _hosts; \
  78. _args->f = routine; \
  79. _args->arg = threadarg; \
  80. _hosts = MSG_hosts_as_dynar(); \
  81. MSG_process_create((name), _starpu_simgrid_thread_start, _args, \
  82. xbt_dynar_get_as(_hosts, (where), msg_host_t)); \
  83. xbt_dynar_free(&_hosts); \
  84. } while (0)
  85. #else
  86. #define _STARPU_PTHREAD_CREATE_ON(name, thread, attr, routine, arg, where) do {\
  87. int p_ret = pthread_create((thread), (attr), (routine), (arg)); \
  88. if (STARPU_UNLIKELY(p_ret != 0)) { \
  89. fprintf(stderr, \
  90. "%s:%d pthread_create: %s\n", \
  91. __FILE__, __LINE__, strerror(p_ret)); \
  92. } \
  93. } while (0)
  94. #endif
  95. #define _STARPU_PTHREAD_CREATE(name, thread, attr, routine, arg) \
  96. _STARPU_PTHREAD_CREATE_ON(name, thread, attr, routine, arg, 0)
  97. /*
  98. * Encapsulation of the pthread_key_* functions.
  99. */
  100. #ifdef STARPU_SIMGRID
  101. typedef int _starpu_pthread_key_t;
  102. int _starpu_pthread_key_create(_starpu_pthread_key_t *key);
  103. #define _STARPU_PTHREAD_KEY_CREATE(key, destr) _starpu_pthread_key_create(key)
  104. int _starpu_pthread_key_delete(_starpu_pthread_key_t key);
  105. #define _STARPU_PTHREAD_KEY_DELETE(key) _starpu_pthread_key_delete(key)
  106. int _starpu_pthread_setspecific(_starpu_pthread_key_t key, void *ptr);
  107. #define _STARPU_PTHREAD_SETSPECIFIC(key, ptr) _starpu_pthread_setspecific(key, ptr)
  108. void *_starpu_pthread_getspecific(_starpu_pthread_key_t key);
  109. #define _STARPU_PTHREAD_GETSPECIFIC(key) _starpu_pthread_getspecific(key)
  110. #else
  111. typedef pthread_key_t _starpu_pthread_key_t;
  112. #define _STARPU_PTHREAD_KEY_CREATE(key, destr) do { \
  113. int p_ret = pthread_key_create((key), (destr)); \
  114. if (STARPU_UNLIKELY(p_ret != 0)) { \
  115. fprintf(stderr, \
  116. "%s:%d pthread_key_create: %s\n", \
  117. __FILE__, __LINE__, strerror(p_ret)); \
  118. } \
  119. } while (0)
  120. #define _STARPU_PTHREAD_KEY_DELETE(key) do { \
  121. int p_ret = pthread_key_delete((key)); \
  122. if (STARPU_UNLIKELY(p_ret != 0)) { \
  123. fprintf(stderr, \
  124. "%s:%d pthread_key_delete: %s\n", \
  125. __FILE__, __LINE__, strerror(p_ret)); \
  126. } \
  127. } while (0)
  128. #define _STARPU_PTHREAD_SETSPECIFIC(key, ptr) do { \
  129. int p_ret = pthread_setspecific((key), (ptr)); \
  130. if (STARPU_UNLIKELY(p_ret != 0)) { \
  131. fprintf(stderr, \
  132. "%s:%d pthread_setspecific: %s\n", \
  133. __FILE__, __LINE__, strerror(p_ret)); \
  134. }; \
  135. } while (0)
  136. #define _STARPU_PTHREAD_GETSPECIFIC(key) pthread_getspecific((key))
  137. #endif
  138. /*
  139. * Encapsulation of the pthread_mutex_* functions.
  140. */
  141. #ifdef STARPU_SIMGRID
  142. #define _STARPU_PTHREAD_MUTEX_INITIALIZER NULL
  143. #define _STARPU_PTHREAD_MUTEX_INIT(mutex, attr) do { \
  144. (*mutex) = xbt_mutex_init(); \
  145. } while (0)
  146. #else
  147. #define _STARPU_PTHREAD_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
  148. #define _STARPU_PTHREAD_MUTEX_INIT(mutex, attr) do { \
  149. int p_ret = pthread_mutex_init((mutex), (attr)); \
  150. if (STARPU_UNLIKELY(p_ret)) { \
  151. fprintf(stderr, \
  152. "%s:%d pthread_mutex_init: %s\n", \
  153. __FILE__, __LINE__, strerror(p_ret)); \
  154. STARPU_ABORT(); \
  155. } \
  156. } while (0)
  157. #endif
  158. #ifdef STARPU_SIMGRID
  159. #define _STARPU_PTHREAD_MUTEX_DESTROY(mutex) do { \
  160. if (*mutex) \
  161. xbt_mutex_destroy((*mutex)); \
  162. } while (0)
  163. #else
  164. #define _STARPU_PTHREAD_MUTEX_DESTROY(mutex) do { \
  165. int p_ret = pthread_mutex_destroy(mutex); \
  166. if (STARPU_UNLIKELY(p_ret)) { \
  167. fprintf(stderr, \
  168. "%s:%d pthread_mutex_destroy: %s\n", \
  169. __FILE__, __LINE__, strerror(p_ret)); \
  170. STARPU_ABORT(); \
  171. } \
  172. } while(0)
  173. #endif
  174. #ifdef STARPU_SIMGRID
  175. #define _STARPU_PTHREAD_MUTEX_LOCK(mutex) do { \
  176. if (!(*mutex)) _STARPU_PTHREAD_MUTEX_INIT((mutex), NULL); \
  177. xbt_mutex_acquire((*mutex)); \
  178. } while (0)
  179. #else
  180. #define _STARPU_PTHREAD_MUTEX_LOCK(mutex) do { \
  181. int p_ret = pthread_mutex_lock(mutex); \
  182. if (STARPU_UNLIKELY(p_ret)) { \
  183. fprintf(stderr, \
  184. "%s:%d pthread_mutex_lock: %s\n", \
  185. __FILE__, __LINE__, strerror(p_ret)); \
  186. STARPU_ABORT(); \
  187. } \
  188. } while (0)
  189. #endif
  190. #ifdef STARPU_SIMGRID
  191. #define _STARPU_PTHREAD_MUTEX_TRYLOCK(mutex) (xbt_mutex_acquire(*mutex), 0)
  192. #else
  193. #define _STARPU_PTHREAD_MUTEX_TRYLOCK(mutex) pthread_mutex_trylock(mutex)
  194. #endif
  195. #ifdef STARPU_SIMGRID
  196. #define _STARPU_PTHREAD_MUTEX_UNLOCK(mutex) do { \
  197. xbt_mutex_release((*mutex)); \
  198. } while (0)
  199. #else
  200. #define _STARPU_PTHREAD_MUTEX_UNLOCK(mutex) do { \
  201. int p_ret = pthread_mutex_unlock(mutex); \
  202. if (STARPU_UNLIKELY(p_ret)) { \
  203. fprintf(stderr, \
  204. "%s:%d pthread_mutex_unlock: %s\n", \
  205. __FILE__, __LINE__, strerror(p_ret)); \
  206. STARPU_ABORT(); \
  207. } \
  208. } while (0)
  209. #endif
  210. #ifdef STARPU_SIMGRID
  211. typedef xbt_mutex_t _starpu_pthread_rwlock_t;
  212. #else
  213. typedef pthread_rwlock_t _starpu_pthread_rwlock_t;
  214. #endif
  215. /*
  216. * Encapsulation of the pthread_rwlock_* functions.
  217. */
  218. #ifdef STARPU_SIMGRID
  219. #define _STARPU_PTHREAD_RWLOCK_INIT(rwlock, attr) _STARPU_PTHREAD_MUTEX_INIT(rwlock, attr)
  220. #else
  221. #define _STARPU_PTHREAD_RWLOCK_INIT(rwlock, attr) do { \
  222. int p_ret = pthread_rwlock_init((rwlock), (attr)); \
  223. if (STARPU_UNLIKELY(p_ret)) { \
  224. fprintf(stderr, \
  225. "%s:%d pthread_rwlock_init: %s\n", \
  226. __FILE__, __LINE__, strerror(p_ret)); \
  227. STARPU_ABORT(); \
  228. } \
  229. } while (0)
  230. #endif
  231. #ifdef STARPU_SIMGRID
  232. #define _STARPU_PTHREAD_RWLOCK_RDLOCK(rwlock) _STARPU_PTHREAD_MUTEX_LOCK(rwlock)
  233. #else
  234. #define _STARPU_PTHREAD_RWLOCK_RDLOCK(rwlock) do { \
  235. int p_ret = pthread_rwlock_rdlock(rwlock); \
  236. if (STARPU_UNLIKELY(p_ret)) { \
  237. fprintf(stderr, \
  238. "%s:%d pthread_rwlock_rdlock: %s\n", \
  239. __FILE__, __LINE__, strerror(p_ret)); \
  240. STARPU_ABORT(); \
  241. } \
  242. } while (0)
  243. #endif
  244. #ifdef STARPU_SIMGRID
  245. #define _STARPU_PTHREAD_RWLOCK_WRLOCK(rwlock) _STARPU_PTHREAD_MUTEX_LOCK(rwlock)
  246. #else
  247. #define _STARPU_PTHREAD_RWLOCK_WRLOCK(rwlock) do { \
  248. int p_ret = pthread_rwlock_wrlock(rwlock); \
  249. if (STARPU_UNLIKELY(p_ret)) { \
  250. fprintf(stderr, \
  251. "%s:%d pthread_rwlock_wrlock: %s\n", \
  252. __FILE__, __LINE__, strerror(p_ret)); \
  253. STARPU_ABORT(); \
  254. } \
  255. } while (0)
  256. #endif
  257. #ifdef STARPU_SIMGRID
  258. #define _STARPU_PTHREAD_RWLOCK_UNLOCK(rwlock) _STARPU_PTHREAD_MUTEX_UNLOCK(rwlock)
  259. #else
  260. #define _STARPU_PTHREAD_RWLOCK_UNLOCK(rwlock) do { \
  261. int p_ret = pthread_rwlock_unlock(rwlock); \
  262. if (STARPU_UNLIKELY(p_ret)) { \
  263. fprintf(stderr, \
  264. "%s:%d pthread_rwlock_unlock: %s\n", \
  265. __FILE__, __LINE__, strerror(p_ret)); \
  266. STARPU_ABORT(); \
  267. } \
  268. } while (0)
  269. #endif
  270. #ifdef STARPU_SIMGRID
  271. #define _STARPU_PTHREAD_RWLOCK_DESTROY(rwlock) _STARPU_PTHREAD_MUTEX_DESTROY(rwlock)
  272. #else
  273. #define _STARPU_PTHREAD_RWLOCK_DESTROY(rwlock) do { \
  274. int p_ret = pthread_rwlock_destroy(rwlock); \
  275. if (STARPU_UNLIKELY(p_ret)) { \
  276. fprintf(stderr, \
  277. "%s:%d pthread_rwlock_destroy: %s\n", \
  278. __FILE__, __LINE__, strerror(p_ret)); \
  279. STARPU_ABORT(); \
  280. } \
  281. } while (0)
  282. #endif
  283. #ifdef STARPU_SIMGRID
  284. typedef xbt_cond_t _starpu_pthread_cond_t;
  285. #else
  286. typedef pthread_cond_t _starpu_pthread_cond_t;
  287. #endif
  288. /*
  289. * Encapsulation of the pthread_cond_* functions.
  290. */
  291. #ifdef STARPU_SIMGRID
  292. #define _STARPU_PTHREAD_COND_INITIALIZER NULL
  293. #define _STARPU_PTHREAD_COND_INIT(cond, attr) do { \
  294. (*cond) = xbt_cond_init(); \
  295. } while (0)
  296. #else
  297. #define _STARPU_PTHREAD_COND_INITIALIZER PTHREAD_COND_INITIALIZER
  298. #define _STARPU_PTHREAD_COND_INIT(cond, attr) do { \
  299. int p_ret = pthread_cond_init((cond), (attr)); \
  300. if (STARPU_UNLIKELY(p_ret)) { \
  301. fprintf(stderr, \
  302. "%s:%d pthread_cond_init: %s\n", \
  303. __FILE__, __LINE__, strerror(p_ret)); \
  304. STARPU_ABORT(); \
  305. } \
  306. } while (0)
  307. #endif
  308. #ifdef STARPU_SIMGRID
  309. #define _STARPU_PTHREAD_COND_DESTROY(cond) do { \
  310. if (*cond) \
  311. xbt_cond_destroy((*cond)); \
  312. } while (0)
  313. #else
  314. #define _STARPU_PTHREAD_COND_DESTROY(cond) do { \
  315. int p_ret = pthread_cond_destroy(cond); \
  316. if (STARPU_UNLIKELY(p_ret)) { \
  317. fprintf(stderr, \
  318. "%s:%d pthread_cond_destroy: %s\n", \
  319. __FILE__, __LINE__, strerror(p_ret)); \
  320. STARPU_ABORT(); \
  321. } \
  322. } while (0)
  323. #endif
  324. #ifdef STARPU_SIMGRID
  325. #define _STARPU_PTHREAD_COND_SIGNAL(cond) do { \
  326. if (!*cond) \
  327. _STARPU_PTHREAD_COND_INIT(cond, NULL); \
  328. xbt_cond_signal((*cond)); \
  329. } while (0)
  330. #else
  331. #define _STARPU_PTHREAD_COND_SIGNAL(cond) do { \
  332. int p_ret = pthread_cond_signal(cond); \
  333. if (STARPU_UNLIKELY(p_ret)) { \
  334. fprintf(stderr, \
  335. "%s:%d pthread_cond_signal: %s\n", \
  336. __FILE__, __LINE__, strerror(p_ret)); \
  337. STARPU_ABORT(); \
  338. } \
  339. } while (0)
  340. #endif
  341. #ifdef STARPU_SIMGRID
  342. #define _STARPU_PTHREAD_COND_BROADCAST(cond) do { \
  343. if (!*cond) \
  344. _STARPU_PTHREAD_COND_INIT(cond, NULL); \
  345. xbt_cond_broadcast((*cond)); \
  346. } while (0)
  347. #else
  348. #define _STARPU_PTHREAD_COND_BROADCAST(cond) do { \
  349. int p_ret = pthread_cond_broadcast(cond); \
  350. if (STARPU_UNLIKELY(p_ret)) { \
  351. fprintf(stderr, \
  352. "%s:%d pthread_cond_broadcast: %s\n", \
  353. __FILE__, __LINE__, strerror(p_ret)); \
  354. STARPU_ABORT(); \
  355. } \
  356. } while (0)
  357. #endif
  358. #ifdef STARPU_SIMGRID
  359. #define _STARPU_PTHREAD_COND_WAIT(cond, mutex) do { \
  360. if (!*cond) \
  361. _STARPU_PTHREAD_COND_INIT(cond, NULL); \
  362. xbt_cond_wait((*cond), (*mutex)); \
  363. } while (0)
  364. #else
  365. #define _STARPU_PTHREAD_COND_WAIT(cond, mutex) do { \
  366. int p_ret = pthread_cond_wait((cond), (mutex)); \
  367. if (STARPU_UNLIKELY(p_ret)) { \
  368. fprintf(stderr, \
  369. "%s:%d pthread_cond_wait: %s\n", \
  370. __FILE__, __LINE__, strerror(p_ret)); \
  371. STARPU_ABORT(); \
  372. } \
  373. } while (0)
  374. #endif
  375. #define _starpu_pthread_barrier_t pthread_barrier_t
  376. /*
  377. * Encapsulation of the pthread_barrier_* functions.
  378. */
  379. #define _STARPU_PTHREAD_BARRIER_INIT(barrier, attr, count) do { \
  380. int p_ret = pthread_barrier_init((barrier), (attr), (count)); \
  381. if (STARPU_UNLIKELY(p_ret)) { \
  382. fprintf(stderr, \
  383. "%s:%d pthread_barrier_init: %s\n", \
  384. __FILE__, __LINE__, strerror(p_ret)); \
  385. STARPU_ABORT(); \
  386. } \
  387. } while (0)
  388. #define _STARPU_PTHREAD_BARRIER_DESTROY(barrier) do { \
  389. int p_ret = pthread_barrier_destroy((barrier)); \
  390. if (STARPU_UNLIKELY(p_ret)) { \
  391. fprintf(stderr, \
  392. "%s:%d pthread_barrier_destroy: %s\n", \
  393. __FILE__, __LINE__, strerror(p_ret)); \
  394. STARPU_ABORT(); \
  395. } \
  396. } while (0)
  397. #define _STARPU_PTHREAD_BARRIER_WAIT(barrier) do { \
  398. int p_ret = pthread_barrier_wait(barrier); \
  399. if (STARPU_UNLIKELY(!((p_ret == 0) || (p_ret == PTHREAD_BARRIER_SERIAL_THREAD)))) { \
  400. fprintf(stderr, \
  401. "%s:%d pthread_barrier_wait: %s\n", \
  402. __FILE__, __LINE__, strerror(p_ret)); \
  403. STARPU_ABORT(); \
  404. } \
  405. } while (0)
  406. #ifdef HAVE_PTHREAD_SPIN_LOCK
  407. typedef pthread_spinlock_t _starpu_pthread_spinlock_t;
  408. #endif
  409. #endif // __COMMON_UTILS_H__