mocks.h 8.4 KB

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