utils.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2012,2015,2017 Inria
  4. * Copyright (C) 2010-2019 Université de Bordeaux
  5. * Copyright (C) 2010-2018 CNRS
  6. *
  7. * StarPU is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation; either version 2.1 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * StarPU is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. *
  16. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  17. */
  18. #ifndef __COMMON_UTILS_H__
  19. #define __COMMON_UTILS_H__
  20. #include <common/config.h>
  21. #include <starpu.h>
  22. #include <sys/stat.h>
  23. #include <string.h>
  24. #include <stdlib.h>
  25. #include <math.h>
  26. #ifdef STARPU_HAVE_SCHED_YIELD
  27. #include <sched.h>
  28. #endif
  29. #ifdef STARPU_HAVE_HELGRIND_H
  30. #include <valgrind/helgrind.h>
  31. #endif
  32. #ifndef DO_CREQ_v_WW
  33. #define DO_CREQ_v_WW(_creqF, _ty1F, _arg1F, _ty2F, _arg2F) ((void)0)
  34. #endif
  35. #ifndef DO_CREQ_v_W
  36. #define DO_CREQ_v_W(_creqF, _ty1F, _arg1F) ((void)0)
  37. #endif
  38. #ifndef ANNOTATE_HAPPENS_BEFORE
  39. #define ANNOTATE_HAPPENS_BEFORE(obj) ((void)0)
  40. #endif
  41. #ifndef ANNOTATE_HAPPENS_BEFORE_FORGET_ALL
  42. #define ANNOTATE_HAPPENS_BEFORE_FORGET_ALL(obj) ((void)0)
  43. #endif
  44. #ifndef ANNOTATE_HAPPENS_AFTER
  45. #define ANNOTATE_HAPPENS_AFTER(obj) ((void)0)
  46. #endif
  47. #ifndef VALGRIND_HG_DISABLE_CHECKING
  48. #define VALGRIND_HG_DISABLE_CHECKING(start, len) ((void)0)
  49. #endif
  50. #ifndef VALGRIND_HG_ENABLE_CHECKING
  51. #define VALGRIND_HG_ENABLE_CHECKING(start, len) ((void)0)
  52. #endif
  53. #ifndef VALGRIND_STACK_REGISTER
  54. #define VALGRIND_STACK_REGISTER(stackbottom, stacktop) 0
  55. #endif
  56. #ifndef VALGRIND_STACK_DEREGISTER
  57. #define VALGRIND_STACK_DEREGISTER(id) ((void)0)
  58. #endif
  59. #ifndef RUNNING_ON_VALGRIND
  60. #define RUNNING_ON_VALGRIND 0
  61. #endif
  62. #ifdef STARPU_SANITIZE_THREAD
  63. #define STARPU_RUNNING_ON_VALGRIND 1
  64. #else
  65. #define STARPU_RUNNING_ON_VALGRIND RUNNING_ON_VALGRIND
  66. #endif
  67. #define STARPU_HG_DISABLE_CHECKING(variable) VALGRIND_HG_DISABLE_CHECKING(&(variable), sizeof(variable))
  68. #define STARPU_HG_ENABLE_CHECKING(variable) VALGRIND_HG_ENABLE_CHECKING(&(variable), sizeof(variable))
  69. #if defined(__KNC__) || defined(__KNF__)
  70. #define STARPU_DEBUG_PREFIX "[starpu-mic]"
  71. #else
  72. #define STARPU_DEBUG_PREFIX "[starpu]"
  73. #endif
  74. /* This is needed in some places to make valgrind yield to another thread to be
  75. * able to progress. */
  76. #if defined(__i386__) || defined(__x86_64__)
  77. #define _STARPU_UYIELD() __asm__ __volatile("rep; nop")
  78. #else
  79. #define _STARPU_UYIELD() ((void)0)
  80. #endif
  81. #if defined(STARPU_HAVE_SCHED_YIELD) && defined(STARPU_HAVE_HELGRIND_H)
  82. #define STARPU_UYIELD() do { if (STARPU_RUNNING_ON_VALGRIND) sched_yield(); else _STARPU_UYIELD(); } while (0)
  83. #else
  84. #define STARPU_UYIELD() _STARPU_UYIELD()
  85. #endif
  86. #ifdef STARPU_VERBOSE
  87. # define _STARPU_DEBUG(fmt, ...) do { if (!_starpu_silent) {fprintf(stderr, STARPU_DEBUG_PREFIX"[%s] " fmt ,__starpu_func__ ,## __VA_ARGS__); fflush(stderr); }} while(0)
  88. # define _STARPU_DEBUG_NO_HEADER(fmt, ...) do { if (!_starpu_silent) {fprintf(stderr, fmt , ## __VA_ARGS__); fflush(stderr); }} while(0)
  89. #else
  90. # define _STARPU_DEBUG(fmt, ...) do { } while (0)
  91. # define _STARPU_DEBUG_NO_HEADER(fmt, ...) do { } while (0)
  92. #endif
  93. #ifdef STARPU_EXTRA_VERBOSE
  94. # define _STARPU_EXTRA_DEBUG(fmt, ...) do { if (!_starpu_silent) {fprintf(stderr, STARPU_DEBUG_PREFIX"[%s] " fmt ,__starpu_func__ ,## __VA_ARGS__); fflush(stderr); }} while(0)
  95. #else
  96. # define _STARPU_EXTRA_DEBUG(fmt, ...) do { } while (0)
  97. #endif
  98. #ifdef STARPU_EXTRA_VERBOSE
  99. # define _STARPU_LOG_IN() do { if (!_starpu_silent) {fprintf(stderr, STARPU_DEBUG_PREFIX"[%ld][%s:%s@%d] -->\n", starpu_pthread_self(), __starpu_func__,__FILE__, __LINE__); }} while(0)
  100. # define _STARPU_LOG_OUT() do { if (!_starpu_silent) {fprintf(stderr, STARPU_DEBUG_PREFIX"[%ld][%s:%s@%d] <--\n", starpu_pthread_self(), __starpu_func__, __FILE__, __LINE__); }} while(0)
  101. # define _STARPU_LOG_OUT_TAG(outtag) do { if (!_starpu_silent) {fprintf(stderr, STARPU_DEBUG_PREFIX"[%ld][%s:%s@%d] <-- (%s)\n", starpu_pthread_self(), __starpu_func__, __FILE__, __LINE__, outtag); }} while(0)
  102. #else
  103. # define _STARPU_LOG_IN()
  104. # define _STARPU_LOG_OUT()
  105. # define _STARPU_LOG_OUT_TAG(outtag)
  106. #endif
  107. /* TODO: cache */
  108. #define _STARPU_MSG(fmt, ...) do { fprintf(stderr, STARPU_DEBUG_PREFIX"[%s] " fmt ,__starpu_func__ ,## __VA_ARGS__); } while(0)
  109. #define _STARPU_DISP(fmt, ...) do { if (!_starpu_silent) {fprintf(stderr, STARPU_DEBUG_PREFIX"[%s] " fmt ,__starpu_func__ ,## __VA_ARGS__); }} while(0)
  110. #define _STARPU_ERROR(fmt, ...) \
  111. do { \
  112. fprintf(stderr, "\n\n[starpu][%s] Error: " fmt ,__starpu_func__ ,## __VA_ARGS__); \
  113. fprintf(stderr, "\n\n"); \
  114. STARPU_ABORT(); \
  115. } while (0)
  116. #ifdef _MSC_VER
  117. # if defined(__cplusplus)
  118. # define _STARPU_DECLTYPE(x) (decltype(x))
  119. # else
  120. # define _STARPU_DECLTYPE(x)
  121. # endif
  122. #else
  123. # define _STARPU_DECLTYPE(x) (__typeof(x))
  124. #endif
  125. #define _STARPU_MALLOC(ptr, size) do { ptr = _STARPU_DECLTYPE(ptr) malloc(size); STARPU_ASSERT_MSG(ptr != NULL || size == 0, "Cannot allocate %ld bytes\n", (long) (size)); } while (0)
  126. #define _STARPU_CALLOC(ptr, nmemb, size) do { ptr = _STARPU_DECLTYPE(ptr) calloc(nmemb, size); STARPU_ASSERT_MSG(ptr != NULL || size == 0, "Cannot allocate %ld bytes\n", (long) (nmemb*size)); } while (0)
  127. #define _STARPU_REALLOC(ptr, size) do { void *_new_ptr = realloc(ptr, size); STARPU_ASSERT_MSG(_new_ptr != NULL || size == 0, "Cannot reallocate %ld bytes\n", (long) (size)); ptr = _STARPU_DECLTYPE(ptr) _new_ptr;} while (0)
  128. #ifdef _MSC_VER
  129. #define _STARPU_IS_ZERO(a) (a == 0.0)
  130. #else
  131. #define _STARPU_IS_ZERO(a) (fpclassify(a) == FP_ZERO)
  132. #endif
  133. char *_starpu_mkdtemp_internal(char *tmpl);
  134. char *_starpu_mkdtemp(char *tmpl);
  135. int _starpu_mkpath(const char *s, mode_t mode);
  136. void _starpu_mkpath_and_check(const char *s, mode_t mode);
  137. char *_starpu_mktemp(const char *directory, int flags, int *fd);
  138. /* This version creates a hierarchy of n temporary directories, useful when
  139. * creating a lot of temporary files to be stored in the same place */
  140. char *_starpu_mktemp_many(const char *directory, int depth, int flags, int *fd);
  141. void _starpu_rmtemp_many(char *path, int depth);
  142. void _starpu_rmdir_many(char *path, int depth);
  143. int _starpu_fftruncate(FILE *file, size_t length);
  144. int _starpu_ftruncate(int fd, size_t length);
  145. int _starpu_frdlock(FILE *file);
  146. int _starpu_frdunlock(FILE *file);
  147. int _starpu_fwrlock(FILE *file);
  148. int _starpu_fwrunlock(FILE *file);
  149. char *_starpu_get_home_path(void);
  150. void _starpu_gethostname(char *hostname, size_t size);
  151. /* If FILE is currently on a comment line, eat it. */
  152. void _starpu_drop_comments(FILE *f);
  153. struct _starpu_job;
  154. /* Returns the symbol associated to that job if any. */
  155. const char *_starpu_job_get_model_name(struct _starpu_job *j);
  156. /* Returns the name associated to that job if any. */
  157. const char *_starpu_job_get_task_name(struct _starpu_job *j);
  158. struct starpu_codelet;
  159. /* Returns the symbol associated to that job if any. */
  160. const char *_starpu_codelet_get_model_name(struct starpu_codelet *cl);
  161. int _starpu_check_mutex_deadlock(starpu_pthread_mutex_t *mutex);
  162. void _starpu_util_init(void);
  163. #endif // __COMMON_UTILS_H__