starpu_util.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2011 Université de Bordeaux 1
  4. * Copyright (C) 2010, 2011 Centre National de la Recherche Scientifique
  5. * Copyright (C) 2011 INRIA
  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 __STARPU_UTIL_H__
  19. #define __STARPU_UTIL_H__
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <assert.h>
  24. #include <starpu_config.h>
  25. #include <starpu_perfmodel.h>
  26. #ifdef __cplusplus
  27. extern "C" {
  28. #endif
  29. #define STARPU_POISON_PTR ((void *)0xdeadbeef)
  30. #define STARPU_MIN(a,b) ((a)<(b)?(a):(b))
  31. #define STARPU_MAX(a,b) ((a)<(b)?(b):(a))
  32. #ifdef STARPU_NO_ASSERT
  33. #define STARPU_ASSERT(x) do {} while(0);
  34. #else
  35. # if defined(__CUDACC__) && defined(STARPU_HAVE_WINDOWS)
  36. # define STARPU_ASSERT(x) do { if (!(x)) *(int*)NULL = 0; } while(0)
  37. # else
  38. # define STARPU_ASSERT(x) assert(x)
  39. # endif
  40. #endif
  41. #define STARPU_ABORT() abort()
  42. #ifdef __GNUC__
  43. # define STARPU_UNLIKELY(expr) (__builtin_expect(!!(expr),0))
  44. # define STARPU_LIKELY(expr) (__builtin_expect(!!(expr),1))
  45. # define STARPU_ATTRIBUTE_UNUSED __attribute__((unused))
  46. # define STARPU_ATTRIBUTE_INTERNAL __attribute__ ((visibility ("internal")))
  47. #else
  48. # define STARPU_UNLIKELY(expr) (expr)
  49. # define STARPU_LIKELY(expr) (expr)
  50. # define STARPU_ATTRIBUTE_UNUSED
  51. # define STARPU_ATTRIBUTE_INTERNAL
  52. #endif
  53. #if defined(__i386__) || defined(__x86_64__)
  54. static __inline unsigned starpu_cmpxchg(unsigned *ptr, unsigned old, unsigned next) {
  55. __asm__ __volatile__("lock cmpxchgl %2,%1": "+a" (old), "+m" (*ptr) : "q" (next) : "memory");
  56. return old;
  57. }
  58. static __inline unsigned starpu_xchg(unsigned *ptr, unsigned next) {
  59. /* Note: xchg is always locked already */
  60. __asm__ __volatile__("xchgl %1,%0": "+m" (*ptr), "+q" (next) : : "memory");
  61. return next;
  62. }
  63. #define STARPU_HAVE_XCHG
  64. #endif
  65. #define STARPU_ATOMIC_SOMETHING(name,expr) \
  66. static __inline unsigned starpu_atomic_##name(unsigned *ptr, unsigned value) { \
  67. unsigned old, next; \
  68. while (1) { \
  69. old = *ptr; \
  70. next = expr; \
  71. if (starpu_cmpxchg(ptr, old, next) == old) \
  72. break; \
  73. }; \
  74. return expr; \
  75. }
  76. #ifdef STARPU_HAVE_SYNC_FETCH_AND_ADD
  77. #define STARPU_ATOMIC_ADD(ptr, value) (__sync_fetch_and_add ((ptr), (value)) + (value))
  78. #elif defined(STARPU_HAVE_XCHG)
  79. STARPU_ATOMIC_SOMETHING(add, old + value)
  80. #define STARPU_ATOMIC_ADD(ptr, value) starpu_atomic_add(ptr, value)
  81. #endif
  82. #ifdef STARPU_HAVE_SYNC_FETCH_AND_OR
  83. #define STARPU_ATOMIC_OR(ptr, value) (__sync_fetch_and_or ((ptr), (value)))
  84. #elif defined(STARPU_HAVE_XCHG)
  85. STARPU_ATOMIC_SOMETHING(or, old | value)
  86. #define STARPU_ATOMIC_OR(ptr, value) starpu_atomic_or(ptr, value)
  87. #endif
  88. #ifdef STARPU_HAVE_SYNC_BOOL_COMPARE_AND_SWAP
  89. #define STARPU_BOOL_COMPARE_AND_SWAP(ptr, old, value) (__sync_bool_compare_and_swap ((ptr), (old), (value)))
  90. #elif defined(STARPU_HAVE_XCHG)
  91. #define STARPU_BOOL_COMPARE_AND_SWAP(ptr, old, value) (starpu_cmpxchg((ptr), (old), (value)) == (old))
  92. #endif
  93. #ifdef STARPU_HAVE_SYNC_LOCK_TEST_AND_SET
  94. #define STARPU_TEST_AND_SET(ptr, value) (__sync_lock_test_and_set ((ptr), (value)))
  95. #define STARPU_RELEASE(ptr) (__sync_lock_release ((ptr)))
  96. #elif defined(STARPU_HAVE_XCHG)
  97. #define STARPU_TEST_AND_SET(ptr, value) (starpu_xchg((ptr), (value)))
  98. #define STARPU_RELEASE(ptr) (starpu_xchg((ptr), 0))
  99. #endif
  100. #ifdef STARPU_HAVE_SYNC_SYNCHRONIZE
  101. #define STARPU_SYNCHRONIZE() __sync_synchronize()
  102. #elif defined(__i386__)
  103. #define STARPU_SYNCHRONIZE() __asm__ __volatile__("lock; addl $0,0(%%esp)" ::: "memory")
  104. #elif defined(__x86_64__)
  105. #define STARPU_SYNCHRONIZE() __asm__ __volatile__("mfence" ::: "memory")
  106. #elif defined(__ppc__) || defined(__ppc64__)
  107. #define STARPU_SYNCHRONIZE() __asm__ __volatile__("sync" ::: "memory")
  108. #endif
  109. #ifdef __cplusplus
  110. }
  111. #endif
  112. /* Include this only here so that <starpu_data_interfaces.h> can use the
  113. * macros above. */
  114. #include <starpu_task.h>
  115. #ifdef __cplusplus
  116. extern "C" {
  117. #endif
  118. static __inline int starpu_get_env_number(const char *str)
  119. {
  120. char *strval;
  121. strval = getenv(str);
  122. if (strval) {
  123. /* the env variable was actually set */
  124. unsigned val;
  125. char *check;
  126. val = (int)strtol(strval, &check, 10);
  127. STARPU_ASSERT(strcmp(check, "\0") == 0);
  128. /* fprintf(stderr, "ENV %s WAS %d\n", str, val); */
  129. return val;
  130. }
  131. else {
  132. /* there is no such env variable */
  133. /* fprintf("There was no %s ENV\n", str); */
  134. return -1;
  135. }
  136. }
  137. /* Add an event in the execution trace if FxT is enabled */
  138. void starpu_trace_user_event(unsigned long code);
  139. #define STARPU_FXT_MAX_FILES 64
  140. struct starpu_fxt_codelet_event {
  141. char symbol[256]; /* name of the codelet */
  142. int workerid;
  143. enum starpu_perf_archtype archtype;
  144. uint32_t hash;
  145. size_t size;
  146. float time;
  147. };
  148. struct starpu_fxt_options {
  149. unsigned per_task_colour;
  150. unsigned no_counter;
  151. unsigned no_bus;
  152. unsigned ninputfiles;
  153. char *filenames[STARPU_FXT_MAX_FILES];
  154. char *out_paje_path;
  155. char *distrib_time_path;
  156. char *activity_path;
  157. char *dag_path;
  158. /* In case we are going to gather multiple traces (eg in the case of
  159. * MPI processes), we may need to prefix the name of the containers. */
  160. char *file_prefix;
  161. uint64_t file_offset;
  162. int file_rank;
  163. /*
  164. * Output parameters
  165. */
  166. char worker_names[STARPU_NMAXWORKERS][256];
  167. enum starpu_perf_archtype worker_archtypes[STARPU_NMAXWORKERS];
  168. int nworkers;
  169. /* In case we want to dump the list of codelets to an external tool */
  170. struct starpu_fxt_codelet_event **dumped_codelets;
  171. long dumped_codelets_count;
  172. };
  173. void starpu_fxt_options_init(struct starpu_fxt_options *options);
  174. void starpu_fxt_generate_trace(struct starpu_fxt_options *options);
  175. /* Some helper functions for application using CUBLAS kernels */
  176. void starpu_helper_cublas_init(void);
  177. void starpu_helper_cublas_shutdown(void);
  178. /* Call func(arg) on every worker matching the "where" mask (eg.
  179. * STARPU_CUDA|STARPU_CPU to execute the function on every CPU and every CUDA
  180. * device). This function is synchronous, but the different workers may execute
  181. * the function in parallel.
  182. * */
  183. void starpu_execute_on_each_worker(void (*func)(void *), void *arg, uint32_t where);
  184. /* This creates (and submits) an empty task that unlocks a tag once all its
  185. * dependencies are fulfilled. */
  186. void starpu_create_sync_task(starpu_tag_t sync_tag, unsigned ndeps, starpu_tag_t *deps,
  187. void (*callback)(void *), void *callback_arg);
  188. /* Copy the content of the src_handle into the dst_handle handle. The
  189. * asynchronous parameter indicates whether the function should block or not.
  190. * In the case of an asynchronous call, it is possible to synchronize with the
  191. * termination of this operation either by the means of implicit dependencies
  192. * (if enabled) or by calling starpu_task_wait_for_all(). If callback_func is
  193. * not NULL, this callback function is executed after the handle has been
  194. * copied, and it is given the callback_arg pointer as argument.*/
  195. int starpu_data_cpy(starpu_data_handle dst_handle, starpu_data_handle src_handle,
  196. int asynchronous, void (*callback_func)(void*), void *callback_arg);
  197. /* Constants used by the starpu_insert_task helper to determine the different types of argument */
  198. #define STARPU_VALUE (1<<4) /* Pointer to a constant value */
  199. #define STARPU_CALLBACK (1<<5) /* Callback function */
  200. #define STARPU_CALLBACK_ARG (1<<6) /* Argument of the callback function (of type void *) */
  201. #define STARPU_PRIORITY (1<<7) /* Priority associated to the task */
  202. #define STARPU_EXECUTE_ON_NODE (1<<8) /* Used by MPI to define which task is going to execute the codelet */
  203. #define STARPU_EXECUTE_ON_DATA (1<<9) /* Used by MPI to define which task is going to execute the codelet */
  204. #define STARPU_CTX (1<<10) /* Used to define which ctx will execute the codelet */
  205. /* Wrapper to create a task. */
  206. int starpu_insert_task(starpu_codelet *cl, ...);
  207. /* Retrieve the arguments of type STARPU_VALUE associated to a task
  208. * automatically created using starpu_insert_task. */
  209. void starpu_unpack_cl_args(void *cl_arg, ...);
  210. /* Pack arguments of type STARPU_VALUE into a buffer which can be
  211. * given to a codelet and later unpacked with starpu_unpack_cl_args */
  212. void starpu_pack_cl_args(char **arg_buffer, size_t *arg_buffer_size, ...);
  213. #ifdef __cplusplus
  214. }
  215. #endif
  216. #endif /* __STARPU_UTIL_H__ */