mocks.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /* GCC-StarPU
  2. Copyright (C) 2011, 2012 Institut National de Recherche en Informatique et Automatique
  3. GCC-StarPU is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. GCC-StarPU is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with GCC-StarPU. If not, see <http://www.gnu.org/licenses/>. */
  13. /* Testing library, including stubs of StarPU functions. */
  14. #ifndef STARPU_GCC_PLUGIN
  15. # error barf!
  16. #endif
  17. #ifndef STARPU_USE_CPU
  18. # error damn it!
  19. #endif
  20. #undef NDEBUG
  21. #include <stdlib.h>
  22. #include <stdarg.h>
  23. #include <string.h>
  24. #include <assert.h>
  25. /* Stub used for testing purposes. */
  26. /* Number of tasks submitted. */
  27. static unsigned int tasks_submitted;
  28. struct insert_task_argument
  29. {
  30. /* `STARPU_VALUE', etc. */
  31. int type;
  32. /* Pointer to the expected value. */
  33. const void *pointer;
  34. /* Size in bytes of the data pointed to. */
  35. size_t size;
  36. };
  37. /* Pointer to a zero-terminated array listing the expected
  38. `starpu_insert_task' arguments. */
  39. const struct insert_task_argument *expected_insert_task_arguments;
  40. int
  41. starpu_insert_task (struct starpu_codelet *cl, ...)
  42. {
  43. assert (cl->where == (STARPU_CPU | STARPU_OPENCL));
  44. /* TODO: Call `cpu_func' & co. and check whether they do the right
  45. thing. */
  46. assert (cl->cpu_funcs[0] != NULL);
  47. assert (cl->opencl_funcs[0] != NULL);
  48. assert (cl->cuda_funcs[0] == NULL);
  49. va_list args;
  50. size_t pointer_arg;
  51. va_start (args, cl);
  52. const struct insert_task_argument *expected;
  53. for (expected = expected_insert_task_arguments, pointer_arg = 0;
  54. expected->type != 0;
  55. expected++)
  56. {
  57. int type;
  58. type = va_arg (args, int);
  59. assert (type == expected->type);
  60. switch (type)
  61. {
  62. case STARPU_VALUE:
  63. {
  64. void *arg;
  65. size_t size;
  66. arg = va_arg (args, void *);
  67. size = va_arg (args, size_t);
  68. assert (size == expected->size);
  69. assert (arg != NULL);
  70. assert (!memcmp (arg, expected->pointer, size));
  71. break;
  72. }
  73. case STARPU_RW:
  74. case STARPU_R:
  75. case STARPU_W:
  76. {
  77. starpu_data_handle_t handle;
  78. handle = starpu_data_lookup (expected->pointer);
  79. assert (type == cl->modes[pointer_arg++]);
  80. assert (va_arg (args, void *) == handle);
  81. break;
  82. }
  83. default:
  84. abort ();
  85. }
  86. }
  87. va_end (args);
  88. /* Make sure all the arguments were consumed. */
  89. assert (expected->type == 0);
  90. tasks_submitted++;
  91. return 0;
  92. }
  93. /* Our own implementation of `starpu_codelet_unpack_args', for debugging
  94. purposes. */
  95. void
  96. starpu_codelet_unpack_args (void *cl_raw_arg, ...)
  97. {
  98. va_list args;
  99. size_t nargs, arg, offset, size;
  100. unsigned char *cl_arg;
  101. cl_arg = (unsigned char *) cl_raw_arg;
  102. nargs = *cl_arg;
  103. va_start (args, cl_raw_arg);
  104. for (arg = 0, offset = 1, size = 0;
  105. arg < nargs;
  106. arg++, offset += sizeof (size_t) + size)
  107. {
  108. void *argp;
  109. argp = va_arg (args, void *);
  110. size = *(size_t *) &cl_arg[size];
  111. memcpy (argp, &cl_arg[offset], size);
  112. }
  113. va_end (args);
  114. }
  115. /* Data handles. For testing purposes, there's a dummy implementation of
  116. data handles below, which disguises the original pointer to form a pseudo
  117. handle. This allows us to test whether the task implementation is
  118. actually passed a pointer, not a handle. */
  119. #define pointer_as_int(p) ((uintptr_t) (p))
  120. #define int_as_pointer(i) ((void *) (i))
  121. #define dummy_pointer_to_handle(p) \
  122. ({ \
  123. assert ((pointer_as_int (p) & 1) == 0); \
  124. int_as_pointer (~pointer_as_int (p)); \
  125. })
  126. #define dummy_handle_to_pointer(h) \
  127. ({ \
  128. assert ((pointer_as_int (h) & 1) == 1); \
  129. int_as_pointer (~pointer_as_int (h)); \
  130. })
  131. starpu_data_handle_t
  132. starpu_data_lookup (const void *ptr)
  133. {
  134. return dummy_pointer_to_handle (ptr);
  135. }
  136. void *
  137. starpu_handle_get_local_ptr (starpu_data_handle_t handle)
  138. {
  139. return dummy_handle_to_pointer (handle);
  140. }
  141. /* Data registration. */
  142. struct data_register_arguments
  143. {
  144. /* A pointer to the vector being registered. */
  145. void *pointer;
  146. /* Number of elements in the vector. */
  147. size_t elements;
  148. /* Size of individual elements. */
  149. size_t element_size;
  150. };
  151. /* Number of `starpu_vector_data_register' calls. */
  152. static unsigned int data_register_calls;
  153. /* Variable describing the expected `starpu_vector_data_register'
  154. arguments. */
  155. struct data_register_arguments expected_register_arguments;
  156. void
  157. starpu_vector_data_register (starpu_data_handle_t *handle,
  158. uint32_t home_node, uintptr_t ptr,
  159. uint32_t count, size_t elemsize)
  160. {
  161. assert ((void *) ptr == expected_register_arguments.pointer);
  162. assert (count == expected_register_arguments.elements);
  163. assert (elemsize == expected_register_arguments.element_size);
  164. data_register_calls++;
  165. *handle = dummy_pointer_to_handle ((void *) ptr);
  166. }
  167. /* Data acquisition. */
  168. struct data_acquire_arguments
  169. {
  170. /* Pointer to the data being acquired. */
  171. void *pointer;
  172. };
  173. /* Number of `starpu_data_acquire' calls. */
  174. static unsigned int data_acquire_calls;
  175. /* Variable describing the expected `starpu_data_acquire' arguments. */
  176. struct data_acquire_arguments expected_acquire_arguments;
  177. int
  178. starpu_data_acquire (starpu_data_handle_t handle, enum starpu_access_mode mode)
  179. {
  180. /* XXX: Currently only `STARPU_RW'. */
  181. assert (mode == STARPU_RW);
  182. assert (dummy_handle_to_pointer (handle)
  183. == expected_acquire_arguments.pointer);
  184. data_acquire_calls++;
  185. return 0;
  186. }
  187. /* Data acquisition. */
  188. struct data_unregister_arguments
  189. {
  190. /* Pointer to the data being unregistered. */
  191. void *pointer;
  192. };
  193. /* Number of `starpu_data_unregister' calls. */
  194. static unsigned int data_unregister_calls;
  195. /* Variable describing the expected `starpu_data_unregister' arguments. */
  196. struct data_unregister_arguments expected_unregister_arguments;
  197. void
  198. starpu_data_unregister (starpu_data_handle_t handle)
  199. {
  200. assert (dummy_handle_to_pointer (handle)
  201. == expected_unregister_arguments.pointer);
  202. data_unregister_calls++;
  203. }
  204. /* Heap allocation. */
  205. /* Number of `starpu_malloc' and `starpu_free' calls. */
  206. static unsigned int malloc_calls, free_calls;
  207. static size_t expected_malloc_argument;
  208. static void *expected_free_argument;
  209. int
  210. starpu_malloc (void **ptr, size_t size)
  211. {
  212. assert (size == expected_malloc_argument);
  213. *ptr = malloc (size);
  214. malloc_calls++;
  215. return 0;
  216. }
  217. void
  218. _starpu_free_unref (void *ptr)
  219. {
  220. assert (* (void **) ptr == expected_free_argument);
  221. free_calls++;
  222. }
  223. /* Initialization. */
  224. static int initialized;
  225. int
  226. starpu_init (struct starpu_conf *config)
  227. {
  228. initialized++;
  229. return 0;
  230. }
  231. /* Shutdown. */
  232. void
  233. starpu_shutdown (void)
  234. {
  235. initialized--;
  236. }