mocks.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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. #include <common/uthash.h>
  26. /* Stub used for testing purposes. */
  27. /* Number of tasks submitted. */
  28. static unsigned int tasks_submitted;
  29. struct insert_task_argument
  30. {
  31. /* `STARPU_VALUE', etc. */
  32. int type;
  33. /* Pointer to the expected value. */
  34. const void *pointer;
  35. /* Size in bytes of the data pointed to. */
  36. size_t size;
  37. };
  38. /* Pointer to a zero-terminated array listing the expected
  39. `starpu_insert_task' arguments. */
  40. const struct insert_task_argument *expected_insert_task_arguments;
  41. int
  42. starpu_insert_task (struct starpu_codelet *cl, ...)
  43. {
  44. assert (cl->where == (STARPU_CPU | STARPU_OPENCL));
  45. /* TODO: Call `cpu_func' & co. and check whether they do the right
  46. thing. */
  47. assert (cl->cpu_funcs[0] != NULL);
  48. assert (cl->opencl_funcs[0] != NULL);
  49. assert (cl->cuda_funcs[0] == NULL);
  50. va_list args;
  51. size_t pointer_arg;
  52. va_start (args, cl);
  53. const struct insert_task_argument *expected;
  54. for (expected = expected_insert_task_arguments, pointer_arg = 0;
  55. expected->type != 0;
  56. expected++)
  57. {
  58. int type;
  59. type = va_arg (args, int);
  60. assert (type == expected->type);
  61. switch (type)
  62. {
  63. case STARPU_VALUE:
  64. {
  65. void *arg;
  66. size_t size;
  67. arg = va_arg (args, void *);
  68. size = va_arg (args, size_t);
  69. assert (size == expected->size);
  70. assert (arg != NULL);
  71. assert (!memcmp (arg, expected->pointer, size));
  72. break;
  73. }
  74. case STARPU_RW:
  75. case STARPU_R:
  76. case STARPU_W:
  77. {
  78. starpu_data_handle_t handle;
  79. handle = starpu_data_lookup (expected->pointer);
  80. assert (type == cl->modes[pointer_arg++]);
  81. assert (va_arg (args, void *) == handle);
  82. break;
  83. }
  84. default:
  85. abort ();
  86. }
  87. }
  88. va_end (args);
  89. /* Make sure all the arguments were consumed. */
  90. assert (expected->type == 0);
  91. tasks_submitted++;
  92. return 0;
  93. }
  94. /* Our own implementation of `starpu_codelet_unpack_args', for debugging
  95. purposes. */
  96. void
  97. starpu_codelet_unpack_args (void *cl_raw_arg, ...)
  98. {
  99. va_list args;
  100. size_t nargs, arg, offset, size;
  101. unsigned char *cl_arg;
  102. cl_arg = (unsigned char *) cl_raw_arg;
  103. nargs = *cl_arg;
  104. va_start (args, cl_raw_arg);
  105. for (arg = 0, offset = 1, size = 0;
  106. arg < nargs;
  107. arg++, offset += sizeof (size_t) + size)
  108. {
  109. void *argp;
  110. argp = va_arg (args, void *);
  111. size = *(size_t *) &cl_arg[size];
  112. memcpy (argp, &cl_arg[offset], size);
  113. }
  114. va_end (args);
  115. }
  116. /* Data handles. A hash table mapping pointers to handles is maintained,
  117. which allows us to mimic the actual behavior of libstarpu. */
  118. /* Entry in the `registered_handles' hash table. `starpu_data_handle_t' is
  119. assumed to be a pointer to this structure. */
  120. struct handle_entry
  121. {
  122. UT_hash_handle hh;
  123. void *pointer;
  124. starpu_data_handle_t handle;
  125. };
  126. #define handle_to_entry(h) ((struct handle_entry *) (h))
  127. #define handle_to_pointer(h) \
  128. ({ \
  129. assert ((h) != NULL); \
  130. assert (handle_to_entry (h)->handle == (h)); \
  131. handle_to_entry (h)->pointer; \
  132. })
  133. static struct handle_entry *registered_handles;
  134. starpu_data_handle_t
  135. starpu_data_lookup (const void *ptr)
  136. {
  137. starpu_data_handle_t result;
  138. struct handle_entry *entry;
  139. HASH_FIND_PTR (registered_handles, &ptr, entry);
  140. if (STARPU_UNLIKELY (entry == NULL))
  141. result = NULL;
  142. else
  143. result = entry->handle;
  144. return result;
  145. }
  146. void *
  147. starpu_handle_get_local_ptr (starpu_data_handle_t handle)
  148. {
  149. return handle_to_pointer (handle);
  150. }
  151. /* Data registration. */
  152. struct data_register_arguments
  153. {
  154. /* A pointer to the vector being registered. */
  155. void *pointer;
  156. /* Number of elements in the vector. */
  157. size_t elements;
  158. /* Size of individual elements. */
  159. size_t element_size;
  160. };
  161. /* Number of `starpu_vector_data_register' calls. */
  162. static unsigned int data_register_calls;
  163. /* Variable describing the expected `starpu_vector_data_register'
  164. arguments. */
  165. struct data_register_arguments expected_register_arguments;
  166. void
  167. starpu_vector_data_register (starpu_data_handle_t *handle,
  168. uint32_t home_node, uintptr_t ptr,
  169. uint32_t count, size_t elemsize)
  170. {
  171. assert ((void *) ptr == expected_register_arguments.pointer);
  172. assert (count == expected_register_arguments.elements);
  173. assert (elemsize == expected_register_arguments.element_size);
  174. data_register_calls++;
  175. /* Add PTR to the REGISTERED_HANDLES hash table. */
  176. struct handle_entry *entry = malloc (sizeof (*entry));
  177. assert (entry != NULL);
  178. entry->pointer = (void *) ptr;
  179. entry->handle = (starpu_data_handle_t) entry;
  180. HASH_ADD_PTR(registered_handles, pointer, entry);
  181. *handle = (starpu_data_handle_t) entry;
  182. }
  183. /* Data acquisition. */
  184. struct data_acquire_arguments
  185. {
  186. /* Pointer to the data being acquired. */
  187. void *pointer;
  188. };
  189. /* Number of `starpu_data_acquire' calls. */
  190. static unsigned int data_acquire_calls;
  191. /* Variable describing the expected `starpu_data_acquire' arguments. */
  192. struct data_acquire_arguments expected_acquire_arguments;
  193. int
  194. starpu_data_acquire (starpu_data_handle_t handle, enum starpu_access_mode mode)
  195. {
  196. /* XXX: Currently only `STARPU_RW'. */
  197. assert (mode == STARPU_RW);
  198. assert (handle_to_pointer (handle) == expected_acquire_arguments.pointer);
  199. data_acquire_calls++;
  200. return 0;
  201. }
  202. /* Data acquisition. */
  203. struct data_unregister_arguments
  204. {
  205. /* Pointer to the data being unregistered. */
  206. void *pointer;
  207. };
  208. /* Number of `starpu_data_unregister' calls. */
  209. static unsigned int data_unregister_calls;
  210. /* Variable describing the expected `starpu_data_unregister' arguments. */
  211. struct data_unregister_arguments expected_unregister_arguments;
  212. void
  213. starpu_data_unregister (starpu_data_handle_t handle)
  214. {
  215. assert (handle != NULL);
  216. struct handle_entry *entry = handle_to_entry (handle);
  217. assert (entry->pointer != NULL);
  218. assert (entry->pointer == expected_unregister_arguments.pointer);
  219. /* Remove the PTR -> HANDLE mapping. If a mapping from PTR to another
  220. handle existed before (e.g., when using filters), it becomes visible
  221. again. */
  222. HASH_DEL (registered_handles, entry);
  223. entry->pointer = NULL;
  224. free (entry);
  225. data_unregister_calls++;
  226. }
  227. /* Heap allocation. */
  228. /* Number of `starpu_malloc' and `starpu_free' calls. */
  229. static unsigned int malloc_calls, free_calls;
  230. static size_t expected_malloc_argument;
  231. static void *expected_free_argument;
  232. int
  233. starpu_malloc (void **ptr, size_t size)
  234. {
  235. assert (size == expected_malloc_argument);
  236. *ptr = malloc (size);
  237. malloc_calls++;
  238. return 0;
  239. }
  240. int
  241. starpu_free (void *ptr)
  242. {
  243. assert (starpu_data_lookup (ptr) == NULL);
  244. assert (ptr == expected_free_argument);
  245. free_calls++;
  246. return 0;
  247. }
  248. /* Initialization. */
  249. static int initialized;
  250. int
  251. starpu_init (struct starpu_conf *config)
  252. {
  253. initialized++;
  254. return 0;
  255. }
  256. /* Shutdown. */
  257. void
  258. starpu_shutdown (void)
  259. {
  260. initialized--;
  261. }