mocks.h 7.8 KB

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