mocks.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. /* GCC-StarPU
  2. Copyright (C) 2011 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. assert (cl->cpu_func == STARPU_MULTIPLE_CPU_IMPLEMENTATIONS);
  50. assert (cl->opencl_func == STARPU_MULTIPLE_OPENCL_IMPLEMENTATIONS);
  51. assert (cl->cuda_func == STARPU_MULTIPLE_CUDA_IMPLEMENTATIONS);
  52. va_list args;
  53. va_start (args, cl);
  54. const struct insert_task_argument *expected;
  55. for (expected = expected_insert_task_arguments;
  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 (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_unpack_cl_args', for debugging
  95. purposes. */
  96. void
  97. starpu_unpack_cl_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. For testing purposes, there's a dummy implementation of
  117. data handles below, which disguises the original pointer to form a pseudo
  118. handle. This allows us to test whether the task implementation is
  119. actually passed a pointer, not a handle. */
  120. #define pointer_as_int(p) ((uintptr_t) (p))
  121. #define int_as_pointer(i) ((void *) (i))
  122. #define dummy_pointer_to_handle(p) \
  123. ({ \
  124. assert ((pointer_as_int (p) & 1) == 0); \
  125. int_as_pointer (~pointer_as_int (p)); \
  126. })
  127. #define dummy_handle_to_pointer(h) \
  128. ({ \
  129. assert ((pointer_as_int (h) & 1) == 1); \
  130. int_as_pointer (~pointer_as_int (h)); \
  131. })
  132. starpu_data_handle_t
  133. starpu_data_lookup (const void *ptr)
  134. {
  135. return dummy_pointer_to_handle (ptr);
  136. }
  137. void *
  138. starpu_handle_get_local_ptr (starpu_data_handle_t handle)
  139. {
  140. return dummy_handle_to_pointer (handle);
  141. }
  142. /* Data registration. */
  143. struct data_register_arguments
  144. {
  145. /* A pointer to the vector being registered. */
  146. void *pointer;
  147. /* Number of elements in the vector. */
  148. size_t elements;
  149. /* Size of individual elements. */
  150. size_t element_size;
  151. };
  152. /* Number of `starpu_vector_data_register' calls. */
  153. static unsigned int data_register_calls;
  154. /* Variable describing the expected `starpu_vector_data_register'
  155. arguments. */
  156. struct data_register_arguments expected_register_arguments;
  157. void
  158. starpu_vector_data_register (starpu_data_handle_t *handle,
  159. uint32_t home_node, uintptr_t ptr,
  160. uint32_t count, size_t elemsize)
  161. {
  162. assert ((void *) ptr == expected_register_arguments.pointer);
  163. assert (count == expected_register_arguments.elements);
  164. assert (elemsize == expected_register_arguments.element_size);
  165. data_register_calls++;
  166. *handle = dummy_pointer_to_handle ((void *) ptr);
  167. }
  168. /* Data acquisition. */
  169. struct data_acquire_arguments
  170. {
  171. /* Pointer to the data being acquired. */
  172. void *pointer;
  173. };
  174. /* Number of `starpu_data_acquire' calls. */
  175. static unsigned int data_acquire_calls;
  176. /* Variable describing the expected `starpu_data_acquire' arguments. */
  177. struct data_acquire_arguments expected_acquire_arguments;
  178. int
  179. starpu_data_acquire (starpu_data_handle_t handle, enum starpu_access_mode mode)
  180. {
  181. /* XXX: Currently only `STARPU_RW'. */
  182. assert (mode == STARPU_RW);
  183. assert (dummy_handle_to_pointer (handle)
  184. == expected_acquire_arguments.pointer);
  185. data_acquire_calls++;
  186. return 0;
  187. }
  188. /* Data acquisition. */
  189. struct data_unregister_arguments
  190. {
  191. /* Pointer to the data being unregistered. */
  192. void *pointer;
  193. };
  194. /* Number of `starpu_data_unregister' calls. */
  195. static unsigned int data_unregister_calls;
  196. /* Variable describing the expected `starpu_data_unregister' arguments. */
  197. struct data_unregister_arguments expected_unregister_arguments;
  198. void
  199. starpu_data_unregister (starpu_data_handle_t handle)
  200. {
  201. assert (dummy_handle_to_pointer (handle)
  202. == expected_unregister_arguments.pointer);
  203. data_unregister_calls++;
  204. }
  205. /* Heap allocation. */
  206. /* Number of `starpu_malloc' and `starpu_free' calls. */
  207. static unsigned int malloc_calls, free_calls;
  208. static size_t expected_malloc_argument;
  209. static void *expected_free_argument;
  210. int
  211. starpu_malloc (void **ptr, size_t size)
  212. {
  213. assert (size == expected_malloc_argument);
  214. *ptr = malloc (size);
  215. malloc_calls++;
  216. return 0;
  217. }
  218. int
  219. starpu_free (void *ptr)
  220. {
  221. assert (ptr == expected_free_argument);
  222. free_calls++;
  223. return 0;
  224. }
  225. /* Initialization. */
  226. static int initialized;
  227. int
  228. starpu_init (struct starpu_conf *config)
  229. {
  230. initialized++;
  231. return 0;
  232. }
  233. /* Shutdown. */
  234. void
  235. starpu_shutdown (void)
  236. {
  237. initialized--;
  238. }