starpu_util.h 7.6 KB

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