mocks.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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 (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_func != NULL);
  47. assert (cl->opencl_func != NULL);
  48. assert (cl->cuda_func == NULL);
  49. va_list args;
  50. va_start (args, cl);
  51. const struct insert_task_argument *expected;
  52. for (expected = expected_insert_task_arguments;
  53. expected->type != 0;
  54. expected++)
  55. {
  56. int type;
  57. type = va_arg (args, int);
  58. assert (type == expected->type);
  59. switch (type)
  60. {
  61. case STARPU_VALUE:
  62. {
  63. void *arg;
  64. size_t size;
  65. arg = va_arg (args, void *);
  66. size = va_arg (args, size_t);
  67. assert (size == expected->size);
  68. assert (arg != NULL);
  69. assert (!memcmp (arg, expected->pointer, size));
  70. break;
  71. }
  72. case STARPU_RW:
  73. case STARPU_R:
  74. case STARPU_W:
  75. {
  76. starpu_data_handle handle;
  77. handle = starpu_data_lookup (expected->pointer);
  78. assert (va_arg (args, void *) == handle);
  79. break;
  80. }
  81. default:
  82. abort ();
  83. }
  84. }
  85. va_end (args);
  86. /* Make sure all the arguments were consumed. */
  87. assert (expected->type == 0);
  88. tasks_submitted++;
  89. return 0;
  90. }
  91. /* Our own implementation of `starpu_unpack_cl_args', for debugging
  92. purposes. */
  93. void
  94. starpu_unpack_cl_args (void *cl_raw_arg, ...)
  95. {
  96. va_list args;
  97. size_t nargs, arg, offset, size;
  98. unsigned char *cl_arg;
  99. cl_arg = (unsigned char *) cl_raw_arg;
  100. nargs = *cl_arg;
  101. va_start (args, cl_raw_arg);
  102. for (arg = 0, offset = 1, size = 0;
  103. arg < nargs;
  104. arg++, offset += sizeof (size_t) + size)
  105. {
  106. void *argp;
  107. argp = va_arg (args, void *);
  108. size = *(size_t *) &cl_arg[size];
  109. memcpy (argp, &cl_arg[offset], size);
  110. }
  111. va_end (args);
  112. }
  113. /* Data handles. For testing purposes, there's a dummy implementation of
  114. data handles below, which disguises the original pointer to form a pseudo
  115. handle. This allows us to test whether the task implementation is
  116. actually passed a pointer, not a handle. */
  117. #define pointer_as_int(p) ((uintptr_t) (p))
  118. #define int_as_pointer(i) ((void *) (i))
  119. #define dummy_pointer_to_handle(p) \
  120. ({ \
  121. assert ((pointer_as_int (p) & 1) == 0); \
  122. int_as_pointer (~pointer_as_int (p)); \
  123. })
  124. #define dummy_handle_to_pointer(h) \
  125. ({ \
  126. assert ((pointer_as_int (h) & 1) == 1); \
  127. int_as_pointer (~pointer_as_int (h)); \
  128. })
  129. starpu_data_handle
  130. starpu_data_lookup (const void *ptr)
  131. {
  132. return dummy_pointer_to_handle (ptr);
  133. }
  134. void *
  135. starpu_handle_get_local_ptr (starpu_data_handle handle)
  136. {
  137. return dummy_handle_to_pointer (handle);
  138. }
  139. /* Data registration. */
  140. struct data_register_arguments
  141. {
  142. /* A pointer to the vector being registered. */
  143. void *pointer;
  144. /* Number of elements in the vector. */
  145. size_t elements;
  146. /* Size of individual elements. */
  147. size_t element_size;
  148. };
  149. /* Number of `starpu_vector_data_register' calls. */
  150. static unsigned int data_register_calls;
  151. /* Variable describing the expected `starpu_vector_data_register'
  152. arguments. */
  153. struct data_register_arguments expected_register_arguments;
  154. void
  155. starpu_vector_data_register (starpu_data_handle *handle,
  156. uint32_t home_node, uintptr_t ptr,
  157. uint32_t count, size_t elemsize)
  158. {
  159. assert ((void *) ptr == expected_register_arguments.pointer);
  160. assert (count == expected_register_arguments.elements);
  161. assert (elemsize == expected_register_arguments.element_size);
  162. data_register_calls++;
  163. *handle = dummy_pointer_to_handle ((void *) ptr);
  164. }
  165. /* Data acquisition. */
  166. struct data_acquire_arguments
  167. {
  168. /* Pointer to the data being acquired. */
  169. void *pointer;
  170. };
  171. /* Number of `starpu_data_acquire' calls. */
  172. static unsigned int data_acquire_calls;
  173. /* Variable describing the expected `starpu_data_acquire' arguments. */
  174. struct data_acquire_arguments expected_acquire_arguments;
  175. int
  176. starpu_data_acquire (starpu_data_handle handle, starpu_access_mode mode)
  177. {
  178. /* XXX: Currently only `STARPU_RW'. */
  179. assert (mode == STARPU_RW);
  180. assert (dummy_handle_to_pointer (handle)
  181. == expected_acquire_arguments.pointer);
  182. data_acquire_calls++;
  183. return 0;
  184. }
  185. /* Data acquisition. */
  186. struct data_unregister_arguments
  187. {
  188. /* Pointer to the data being unregistered. */
  189. void *pointer;
  190. };
  191. /* Number of `starpu_data_unregister' calls. */
  192. static unsigned int data_unregister_calls;
  193. /* Variable describing the expected `starpu_data_unregister' arguments. */
  194. struct data_unregister_arguments expected_unregister_arguments;
  195. void
  196. starpu_data_unregister (starpu_data_handle handle)
  197. {
  198. assert (dummy_handle_to_pointer (handle)
  199. == expected_unregister_arguments.pointer);
  200. data_unregister_calls++;
  201. }
  202. /* Initialization. */
  203. static int initialized;
  204. int
  205. starpu_init (struct starpu_conf *config)
  206. {
  207. initialized++;
  208. return 0;
  209. }
  210. /* Shutdown. */
  211. void
  212. starpu_shutdown (void)
  213. {
  214. initialized--;
  215. }