utils.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010, 2012-2016 Université de Bordeaux
  4. * Copyright (C) 2010, 2011, 2012, 2013, 2015, 2016, 2017 CNRS
  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 <common/config.h>
  20. #include <starpu.h>
  21. #include <sys/stat.h>
  22. #include <string.h>
  23. #include <stdlib.h>
  24. #include <math.h>
  25. #ifdef STARPU_HAVE_SCHED_YIELD
  26. #include <sched.h>
  27. #endif
  28. #ifdef STARPU_HAVE_HELGRIND_H
  29. #include <valgrind/helgrind.h>
  30. #endif
  31. #ifndef DO_CREQ_v_WW
  32. #define DO_CREQ_v_WW(_creqF, _ty1F, _arg1F, _ty2F, _arg2F) ((void)0)
  33. #endif
  34. #ifndef DO_CREQ_v_W
  35. #define DO_CREQ_v_W(_creqF, _ty1F, _arg1F) ((void)0)
  36. #endif
  37. #ifndef ANNOTATE_HAPPENS_BEFORE
  38. #define ANNOTATE_HAPPENS_BEFORE(obj) ((void)0)
  39. #endif
  40. #ifndef ANNOTATE_HAPPENS_BEFORE_FORGET_ALL
  41. #define ANNOTATE_HAPPENS_BEFORE_FORGET_ALL(obj) ((void)0)
  42. #endif
  43. #ifndef ANNOTATE_HAPPENS_AFTER
  44. #define ANNOTATE_HAPPENS_AFTER(obj) ((void)0)
  45. #endif
  46. #ifndef VALGRIND_HG_DISABLE_CHECKING
  47. #define VALGRIND_HG_DISABLE_CHECKING(start, len) ((void)0)
  48. #endif
  49. #ifndef VALGRIND_HG_ENABLE_CHECKING
  50. #define VALGRIND_HG_ENABLE_CHECKING(start, len) ((void)0)
  51. #endif
  52. #ifndef VALGRIND_STACK_REGISTER
  53. #define VALGRIND_STACK_REGISTER(stackbottom, stacktop) 0
  54. #endif
  55. #ifndef VALGRIND_STACK_DEREGISTER
  56. #define VALGRIND_STACK_DEREGISTER(id) ((void)0)
  57. #endif
  58. #ifndef RUNNING_ON_VALGRIND
  59. #define RUNNING_ON_VALGRIND 0
  60. #endif
  61. #ifdef STARPU_SANITIZE_THREAD
  62. #define STARPU_RUNNING_ON_VALGRIND 1
  63. #else
  64. #define STARPU_RUNNING_ON_VALGRIND RUNNING_ON_VALGRIND
  65. #endif
  66. #define STARPU_HG_DISABLE_CHECKING(variable) VALGRIND_HG_DISABLE_CHECKING(&(variable), sizeof(variable))
  67. #define STARPU_HG_ENABLE_CHECKING(variable) VALGRIND_HG_ENABLE_CHECKING(&(variable), sizeof(variable))
  68. #if defined(__KNC__) || defined(__KNF__)
  69. #define STARPU_DEBUG_PREFIX "[starpu-mic]"
  70. #else
  71. #define STARPU_DEBUG_PREFIX "[starpu]"
  72. #endif
  73. /* This is needed in some places to make valgrind yield to another thread to be
  74. * able to progress. */
  75. #if defined(__i386__) || defined(__x86_64__)
  76. #define _STARPU_UYIELD() __asm__ __volatile("rep; nop")
  77. #else
  78. #define _STARPU_UYIELD() ((void)0)
  79. #endif
  80. #if defined(STARPU_HAVE_SCHED_YIELD) && defined(STARPU_HAVE_HELGRIND_H)
  81. #define STARPU_UYIELD() do { if (STARPU_RUNNING_ON_VALGRIND) sched_yield(); else _STARPU_UYIELD(); } while (0)
  82. #else
  83. #define STARPU_UYIELD() _STARPU_UYIELD()
  84. #endif
  85. #ifdef STARPU_VERBOSE
  86. # define _STARPU_DEBUG(fmt, ...) do { if (!_starpu_silent) {fprintf(stderr, STARPU_DEBUG_PREFIX"[%s] " fmt ,__starpu_func__ ,## __VA_ARGS__); fflush(stderr); }} while(0)
  87. #else
  88. # define _STARPU_DEBUG(fmt, ...) do { } while (0)
  89. #endif
  90. #ifdef STARPU_EXTRA_VERBOSE
  91. # 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)
  92. # 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)
  93. # 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)
  94. #else
  95. # define _STARPU_LOG_IN()
  96. # define _STARPU_LOG_OUT()
  97. # define _STARPU_LOG_OUT_TAG(outtag)
  98. #endif
  99. /* TODO: cache */
  100. #define _STARPU_MSG(fmt, ...) do { fprintf(stderr, STARPU_DEBUG_PREFIX"[%s] " fmt ,__starpu_func__ ,## __VA_ARGS__); } while(0)
  101. #define _STARPU_DISP(fmt, ...) do { if (!_starpu_silent) {fprintf(stderr, STARPU_DEBUG_PREFIX"[%s] " fmt ,__starpu_func__ ,## __VA_ARGS__); }} while(0)
  102. #define _STARPU_ERROR(fmt, ...) \
  103. do { \
  104. fprintf(stderr, "\n\n[starpu][%s] Error: " fmt ,__starpu_func__ ,## __VA_ARGS__); \
  105. fprintf(stderr, "\n\n"); \
  106. STARPU_ABORT(); \
  107. } while (0)
  108. #define _STARPU_MALLOC(ptr, size) do { ptr = malloc(size); STARPU_ASSERT_MSG(ptr != NULL, "Cannot allocate %ld bytes\n", (long) size); } while (0)
  109. #define _STARPU_CALLOC(ptr, nmemb, size) do { ptr = calloc(nmemb, size); STARPU_ASSERT_MSG(ptr != NULL, "Cannot allocate %ld bytes\n", (long) (nmemb*size)); } while (0)
  110. #define _STARPU_REALLOC(ptr, size) do { ptr = realloc(ptr, size); STARPU_ASSERT_MSG(ptr != NULL, "Cannot reallocate %ld bytes\n", (long) size); } while (0)
  111. #ifdef _MSC_VER
  112. #define _STARPU_IS_ZERO(a) (a == 0.0)
  113. #else
  114. #define _STARPU_IS_ZERO(a) (fpclassify(a) == FP_ZERO)
  115. #endif
  116. int _starpu_mkpath(const char *s, mode_t mode);
  117. void _starpu_mkpath_and_check(const char *s, mode_t mode);
  118. char *_starpu_mktemp(const char *directory, int flags, int *fd);
  119. /* This version creates a hierarchy of n temporary directories, useful when
  120. * creating a lot of temporary files to be stored in the same place */
  121. char *_starpu_mktemp_many(const char *directory, int depth, int flags, int *fd);
  122. void _starpu_rmtemp_many(char *path, int depth);
  123. int _starpu_fftruncate(FILE *file, size_t length);
  124. int _starpu_ftruncate(int fd, size_t length);
  125. int _starpu_frdlock(FILE *file);
  126. int _starpu_frdunlock(FILE *file);
  127. int _starpu_fwrlock(FILE *file);
  128. int _starpu_fwrunlock(FILE *file);
  129. char *_starpu_get_home_path(void);
  130. void _starpu_gethostname(char *hostname, size_t size);
  131. /* If FILE is currently on a comment line, eat it. */
  132. void _starpu_drop_comments(FILE *f);
  133. struct _starpu_job;
  134. /* Returns the symbol associated to that job if any. */
  135. const char *_starpu_job_get_model_name(struct _starpu_job *j);
  136. /* Returns the name associated to that job if any. */
  137. const char *_starpu_job_get_task_name(struct _starpu_job *j);
  138. struct starpu_codelet;
  139. /* Returns the symbol associated to that job if any. */
  140. const char *_starpu_codelet_get_model_name(struct starpu_codelet *cl);
  141. int _starpu_check_mutex_deadlock(starpu_pthread_mutex_t *mutex);
  142. void _starpu_util_init(void);
  143. #endif // __COMMON_UTILS_H__