starpu_helper.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2008-2021 Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
  4. *
  5. * StarPU is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation; either version 2.1 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * StarPU is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. *
  14. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  15. */
  16. #ifndef __STARPU_HELPER_H__
  17. #define __STARPU_HELPER_H__
  18. #include <stdio.h>
  19. #include <starpu.h>
  20. #ifdef STARPU_HAVE_HWLOC
  21. #include <hwloc.h>
  22. #endif
  23. #ifdef __cplusplus
  24. extern "C"
  25. {
  26. #endif
  27. /**
  28. @defgroup API_Miscellaneous_Helpers Miscellaneous Helpers
  29. @{
  30. */
  31. /**
  32. Return the min of the two parameters.
  33. */
  34. #define STARPU_MIN(a,b) ((a)<(b)?(a):(b))
  35. /**
  36. Return the max of the two parameters.
  37. */
  38. #define STARPU_MAX(a,b) ((a)<(b)?(b):(a))
  39. /**
  40. Define a value which can be used to mark pointers as invalid
  41. values.
  42. */
  43. #define STARPU_POISON_PTR ((void *)0xdeadbeef)
  44. extern int _starpu_silent;
  45. char *starpu_getenv(const char *str);
  46. /**
  47. If the environment variable \c str is defined and its value is contained in the array \c strings, return the array position.
  48. Raise an error if the environment variable \c str is defined with a value not in \c strings
  49. Return \c defvalue if the environment variable \c str is not defined.
  50. */
  51. int starpu_get_env_string_var_default(const char *str, const char *strings[], int defvalue);
  52. /**
  53. If the environment variable \c str is defined with a well-defined size value, return the value as a size in bytes. Expected size qualifiers are b, B, k, K, m, M, g, G. The default qualifier is K.
  54. If the environment variable \c str is not defined or is empty, return \c defval
  55. Raise an error if the value of the environment variable \c str is not well-defined.
  56. */
  57. int starpu_get_env_size_default(const char *str, int defval);
  58. /**
  59. Return the integer value of the environment variable named \p str.
  60. Return 0 otherwise (the variable does not exist or has a
  61. non-integer value).
  62. */
  63. static __starpu_inline int starpu_get_env_number(const char *str)
  64. {
  65. char *strval;
  66. strval = starpu_getenv(str);
  67. if (strval)
  68. {
  69. /* the env variable was actually set */
  70. long int val;
  71. char *pcheck;
  72. val = strtol(strval, &pcheck, 10);
  73. if (*pcheck)
  74. {
  75. fprintf(stderr,"The %s environment variable must contain an integer\n", str);
  76. STARPU_ABORT();
  77. }
  78. /* fprintf(stderr, "ENV %s WAS %d\n", str, val); */
  79. STARPU_ASSERT_MSG(val >= 0, "The value for the environment variable '%s' cannot be negative", str);
  80. return (int)val;
  81. }
  82. else
  83. {
  84. /* there is no such env variable */
  85. /* fprintf("There was no %s ENV\n", str); */
  86. return -1;
  87. }
  88. }
  89. static __starpu_inline int starpu_get_env_number_default(const char *str, int defval)
  90. {
  91. int ret = starpu_get_env_number(str);
  92. if (ret == -1)
  93. ret = defval;
  94. return ret;
  95. }
  96. static __starpu_inline float starpu_get_env_float_default(const char *str, float defval)
  97. {
  98. char *strval;
  99. strval = starpu_getenv(str);
  100. if (strval)
  101. {
  102. /* the env variable was actually set */
  103. float val;
  104. char *pcheck;
  105. val = strtof(strval, &pcheck);
  106. if (*pcheck)
  107. {
  108. fprintf(stderr,"The %s environment variable must contain a float\n", str);
  109. STARPU_ABORT();
  110. }
  111. /* fprintf(stderr, "ENV %s WAS %f\n", str, val); */
  112. return val;
  113. }
  114. else
  115. {
  116. /* there is no such env variable */
  117. /* fprintf("There was no %s ENV\n", str); */
  118. return defval;
  119. }
  120. }
  121. /**
  122. Execute the given function \p func on a subset of workers. When
  123. calling this method, the offloaded function \p func is executed by
  124. every StarPU worker that are eligible to execute the function. The
  125. argument \p arg is passed to the offloaded function. The argument
  126. \p where specifies on which types of processing units the function
  127. should be executed.
  128. Similarly to the field starpu_codelet::where, it is possible to
  129. specify that the function should be executed on every CUDA device
  130. and every CPU by passing ::STARPU_CPU|::STARPU_CUDA. This function
  131. blocks until \p func has been executed on every appropriate
  132. processing units, and thus may not be called from a callback
  133. function for instance.
  134. */
  135. void starpu_execute_on_each_worker(void (*func)(void *), void *arg, uint32_t where);
  136. /**
  137. Same as starpu_execute_on_each_worker(), except that the task name
  138. is specified in the argument \p name.
  139. */
  140. void starpu_execute_on_each_worker_ex(void (*func)(void *), void *arg, uint32_t where, const char *name);
  141. /**
  142. Call \p func(\p arg) on every worker in the \p workers array. \p
  143. num_workers indicates the number of workers in this array. This
  144. function is synchronous, but the different workers may execute the
  145. function in parallel.
  146. */
  147. void starpu_execute_on_specific_workers(void (*func)(void*), void *arg, unsigned num_workers, unsigned *workers, const char *name);
  148. /**
  149. Return the current date in micro-seconds.
  150. */
  151. double starpu_timing_now(void);
  152. /**
  153. Copy the content of \p src_handle into \p dst_handle. The parameter \p
  154. asynchronous indicates whether the function should block or not. In
  155. the case of an asynchronous call, it is possible to synchronize with
  156. the termination of this operation either by the means of implicit
  157. dependencies (if enabled) or by calling starpu_task_wait_for_all(). If
  158. \p callback_func is not <c>NULL</c>, this callback function is executed after
  159. the handle has been copied, and it is given the pointer \p
  160. callback_arg as argument.
  161. */
  162. int starpu_data_cpy(starpu_data_handle_t dst_handle, starpu_data_handle_t src_handle, int asynchronous, void (*callback_func)(void*), void *callback_arg);
  163. /**
  164. Create a copy of \p src_handle, and return a new handle in \p dst_handle,
  165. which is to be used only for read accesses. This allows StarPU to optimize it
  166. by not actually copying the data whenever possible (e.g. it may possibly
  167. simply return src_handle itself).
  168. The parameter \p asynchronous indicates whether the function should block
  169. or not. In the case of an asynchronous call, it is possible to synchronize
  170. with the termination of this operation either by the means of implicit
  171. dependencies (if enabled) or by calling starpu_task_wait_for_all(). If
  172. \p callback_func is not <c>NULL</c>, this callback function is executed after
  173. the handle has been copied, and it is given the pointer \p
  174. callback_arg as argument.
  175. */
  176. int starpu_data_dup_ro(starpu_data_handle_t *dst_handle, starpu_data_handle_t src_handle, int asynchronous);
  177. /**
  178. Call hwloc-ps to display binding of each processus and thread running on
  179. the machine.<br>
  180. Use the environment variable \ref STARPU_DISPLAY_BINDINGS to automatically
  181. call this function at the beginning of the execution of StarPU.
  182. */
  183. void starpu_display_bindings(void);
  184. /**
  185. If \c hwloc is used, convert the given \p logical_index of a PU to the OS
  186. index of this PU. If \c hwloc is not used, return \p logical_index.
  187. */
  188. int starpu_get_pu_os_index(unsigned logical_index);
  189. #ifdef STARPU_HAVE_HWLOC
  190. /**
  191. Get the hwloc topology used by StarPU. One can use this pointer to get
  192. information about topology, but not to change settings related to topology.
  193. */
  194. hwloc_topology_t starpu_get_hwloc_topology(void);
  195. #endif
  196. /** @} */
  197. #ifdef __cplusplus
  198. }
  199. #endif
  200. #endif // __STARPU_HELPER_H__