mocks.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  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. /* Expected targets of the codelets submitted. */
  62. static int expected_insert_task_targets = STARPU_CPU | STARPU_OPENCL;
  63. int
  64. starpu_insert_task (struct starpu_codelet *cl, ...)
  65. {
  66. assert (cl->name != NULL && strlen (cl->name) > 0);
  67. assert (cl->where == expected_insert_task_targets);
  68. assert ((cl->where & STARPU_CPU) == 0
  69. ? cl->cpu_funcs[0] == NULL
  70. : cl->cpu_funcs[0] != NULL);
  71. assert ((cl->where & STARPU_OPENCL) == 0
  72. ? cl->opencl_funcs[0] == NULL
  73. : cl->opencl_funcs[0] != NULL);
  74. assert ((cl->where & STARPU_CUDA) == 0
  75. ? cl->cuda_funcs[0] == NULL
  76. : cl->cuda_funcs[0] != NULL);
  77. va_list args;
  78. size_t i, scalars, pointers, cl_args_offset;
  79. void *pointer_args[123];
  80. struct starpu_vector_interface pointer_args_ifaces[123];
  81. unsigned char cl_args[234];
  82. va_start (args, cl);
  83. const struct insert_task_argument *expected;
  84. for (expected = expected_insert_task_arguments,
  85. cl_args_offset = 1, scalars = 0, pointers = 0;
  86. expected->type != 0;
  87. expected++)
  88. {
  89. int type;
  90. type = va_arg (args, int);
  91. assert (type == expected->type);
  92. switch (type)
  93. {
  94. case STARPU_VALUE:
  95. {
  96. void *arg;
  97. size_t size;
  98. arg = va_arg (args, void *);
  99. size = va_arg (args, size_t);
  100. assert (size == expected->size);
  101. assert (arg != NULL);
  102. assert (!memcmp (arg, expected->pointer, size));
  103. /* Pack ARG into CL_ARGS. */
  104. assert (cl_args_offset + size + sizeof size < sizeof cl_args);
  105. memcpy (&cl_args[cl_args_offset], &size, sizeof size);
  106. cl_args_offset += sizeof size;
  107. memcpy (&cl_args[cl_args_offset], arg, size);
  108. cl_args_offset += size;
  109. scalars++;
  110. break;
  111. }
  112. case STARPU_RW:
  113. case STARPU_R:
  114. case STARPU_W:
  115. {
  116. starpu_data_handle_t handle;
  117. handle = starpu_data_lookup (expected->pointer);
  118. assert (type == cl->modes[pointers]);
  119. assert (va_arg (args, void *) == handle);
  120. assert (pointers + 1
  121. < sizeof pointer_args_ifaces / sizeof pointer_args_ifaces[0]);
  122. pointer_args_ifaces[pointers].ptr = (uintptr_t) expected->pointer;
  123. pointer_args_ifaces[pointers].dev_handle =
  124. (uintptr_t) expected->pointer; /* for OpenCL */
  125. pointer_args_ifaces[pointers].elemsize = 1;
  126. pointer_args_ifaces[pointers].nx = 1;
  127. pointer_args_ifaces[pointers].offset = 0;
  128. pointers++;
  129. break;
  130. }
  131. default:
  132. abort ();
  133. }
  134. }
  135. va_end (args);
  136. /* Make sure all the arguments were consumed. */
  137. assert (expected->type == 0);
  138. tasks_submitted++;
  139. /* Finish packing the scalar arguments in CL_ARGS. */
  140. cl_args[0] = (unsigned char) scalars;
  141. for (i = 0; i < pointers; i++)
  142. pointer_args[i] = &pointer_args_ifaces[i];
  143. /* Call the codelets. */
  144. if (cl->where & STARPU_CPU)
  145. cl->cpu_funcs[0] (pointer_args, cl_args);
  146. if (cl->where & STARPU_OPENCL)
  147. cl->opencl_funcs[0] (pointer_args, cl_args);
  148. if (cl->where & STARPU_CUDA)
  149. cl->cuda_funcs[0] (pointer_args, cl_args);
  150. return 0;
  151. }
  152. /* Our own implementation of `starpu_codelet_unpack_args', for debugging
  153. purposes. */
  154. void
  155. starpu_codelet_unpack_args (void *cl_raw_arg, ...)
  156. {
  157. va_list args;
  158. size_t nargs, arg, offset, size;
  159. unsigned char *cl_arg;
  160. cl_arg = (unsigned char *) cl_raw_arg;
  161. nargs = *cl_arg;
  162. va_start (args, cl_raw_arg);
  163. for (arg = 0, offset = 1;
  164. arg < nargs;
  165. arg++, offset += sizeof (size_t) + size)
  166. {
  167. void *argp;
  168. argp = va_arg (args, void *);
  169. size = *(size_t *) &cl_arg[offset];
  170. memcpy (argp, &cl_arg[offset + sizeof size], size);
  171. }
  172. va_end (args);
  173. }
  174. /* Data handles. A hash table mapping pointers to handles is maintained,
  175. which allows us to mimic the actual behavior of libstarpu. */
  176. /* Entry in the `registered_handles' hash table. `starpu_data_handle_t' is
  177. assumed to be a pointer to this structure. */
  178. struct handle_entry
  179. {
  180. UT_hash_handle hh;
  181. void *pointer;
  182. starpu_data_handle_t handle;
  183. };
  184. #define handle_to_entry(h) ((struct handle_entry *) (h))
  185. #define handle_to_pointer(h) \
  186. ({ \
  187. assert ((h) != NULL); \
  188. assert (handle_to_entry (h)->handle == (h)); \
  189. handle_to_entry (h)->pointer; \
  190. })
  191. static struct handle_entry *registered_handles;
  192. starpu_data_handle_t
  193. starpu_data_lookup (const void *ptr)
  194. {
  195. starpu_data_handle_t result;
  196. struct handle_entry *entry;
  197. HASH_FIND_PTR (registered_handles, &ptr, entry);
  198. if (STARPU_UNLIKELY (entry == NULL))
  199. result = NULL;
  200. else
  201. result = entry->handle;
  202. return result;
  203. }
  204. void *
  205. starpu_handle_get_local_ptr (starpu_data_handle_t handle)
  206. {
  207. return handle_to_pointer (handle);
  208. }
  209. /* Data registration. */
  210. struct data_register_arguments
  211. {
  212. /* A pointer to the vector being registered. */
  213. void *pointer;
  214. /* Number of elements in the vector. */
  215. size_t elements;
  216. /* Size of individual elements. */
  217. size_t element_size;
  218. };
  219. /* Number of `starpu_vector_data_register' calls. */
  220. static unsigned int data_register_calls;
  221. /* Variable describing the expected `starpu_vector_data_register'
  222. arguments. */
  223. struct data_register_arguments expected_register_arguments;
  224. void
  225. starpu_vector_data_register (starpu_data_handle_t *handle,
  226. uint32_t home_node, uintptr_t ptr,
  227. uint32_t count, size_t elemsize)
  228. {
  229. /* Sometimes tests cannot tell what the pointer will be (for instance, for
  230. the `registered' attribute), and thus pass NULL as the expected
  231. pointer. */
  232. if (expected_register_arguments.pointer != NULL)
  233. assert ((void *) ptr == expected_register_arguments.pointer);
  234. else
  235. /* Allow users to check the pointer afterward. */
  236. expected_register_arguments.pointer = (void *) ptr;
  237. assert (count == expected_register_arguments.elements);
  238. assert (elemsize == expected_register_arguments.element_size);
  239. data_register_calls++;
  240. /* Add PTR to the REGISTERED_HANDLES hash table. */
  241. struct handle_entry *entry = malloc (sizeof (*entry));
  242. assert (entry != NULL);
  243. entry->pointer = (void *) ptr;
  244. entry->handle = (starpu_data_handle_t) entry;
  245. HASH_ADD_PTR(registered_handles, pointer, entry);
  246. *handle = (starpu_data_handle_t) entry;
  247. }
  248. /* Data acquisition. */
  249. struct data_acquire_arguments
  250. {
  251. /* Pointer to the data being acquired. */
  252. void *pointer;
  253. };
  254. struct data_release_arguments
  255. {
  256. /* Pointer to the data being released. */
  257. void *pointer;
  258. };
  259. /* Number of `starpu_data_{acquire,release}' calls. */
  260. static unsigned int data_acquire_calls, data_release_calls;
  261. /* Variable describing the expected `starpu_data_{acquire,release}'
  262. arguments. */
  263. struct data_acquire_arguments expected_acquire_arguments;
  264. struct data_release_arguments expected_release_arguments;
  265. int
  266. starpu_data_acquire (starpu_data_handle_t handle, enum starpu_access_mode mode)
  267. {
  268. /* XXX: Currently only `STARPU_RW'. */
  269. assert (mode == STARPU_RW);
  270. assert (handle_to_pointer (handle) == expected_acquire_arguments.pointer);
  271. data_acquire_calls++;
  272. return 0;
  273. }
  274. void
  275. starpu_data_release (starpu_data_handle_t handle)
  276. {
  277. assert (handle_to_pointer (handle) == expected_release_arguments.pointer);
  278. data_release_calls++;
  279. }
  280. /* Data acquisition. */
  281. struct data_unregister_arguments
  282. {
  283. /* Pointer to the data being unregistered. */
  284. void *pointer;
  285. };
  286. /* Number of `starpu_data_unregister' calls. */
  287. static unsigned int data_unregister_calls;
  288. /* Variable describing the expected `starpu_data_unregister' arguments. */
  289. struct data_unregister_arguments expected_unregister_arguments;
  290. void
  291. starpu_data_unregister (starpu_data_handle_t handle)
  292. {
  293. assert (handle != NULL);
  294. struct handle_entry *entry = handle_to_entry (handle);
  295. assert (entry->pointer != NULL);
  296. assert (entry->pointer == expected_unregister_arguments.pointer);
  297. /* Remove the PTR -> HANDLE mapping. If a mapping from PTR to another
  298. handle existed before (e.g., when using filters), it becomes visible
  299. again. */
  300. HASH_DEL (registered_handles, entry);
  301. entry->pointer = NULL;
  302. free (entry);
  303. data_unregister_calls++;
  304. }
  305. /* Heap allocation. */
  306. /* Number of `starpu_malloc' and `starpu_free' calls. */
  307. static unsigned int malloc_calls, free_calls;
  308. static size_t expected_malloc_argument;
  309. static void *expected_free_argument;
  310. int
  311. starpu_malloc (void **ptr, size_t size)
  312. {
  313. assert (size == expected_malloc_argument);
  314. *ptr = malloc (size);
  315. malloc_calls++;
  316. return 0;
  317. }
  318. int
  319. starpu_free (void *ptr)
  320. {
  321. assert (starpu_data_lookup (ptr) == NULL);
  322. assert (ptr == expected_free_argument);
  323. free_calls++;
  324. return 0;
  325. }
  326. /* OpenCL support. */
  327. #ifndef STARPU_USE_OPENCL
  328. # define STARPU_USE_OPENCL 1
  329. /* The `opencl' pragma needs this structure, so make sure it's defined. */
  330. struct starpu_opencl_program
  331. {
  332. /* Nothing. */
  333. };
  334. typedef int cl_event;
  335. typedef int cl_kernel;
  336. typedef int cl_command_queue;
  337. extern cl_int clSetKernelArg (cl_kernel, cl_uint, size_t, const void *);
  338. extern cl_int
  339. clEnqueueNDRangeKernel(cl_command_queue /* command_queue */,
  340. cl_kernel /* kernel */,
  341. cl_uint /* work_dim */,
  342. const size_t * /* global_work_offset */,
  343. const size_t * /* global_work_size */,
  344. const size_t * /* local_work_size */,
  345. cl_uint /* num_events_in_wait_list */,
  346. const cl_event * /* event_wait_list */,
  347. cl_event * /* event */);
  348. #endif
  349. /* Number of `load_opencl_from_string', `load_kernel', and `clSetKernelArg'
  350. calls. */
  351. static unsigned int load_opencl_calls, load_opencl_kernel_calls,
  352. opencl_set_kernel_arg_calls, opencl_enqueue_calls, opencl_finish_calls,
  353. opencl_collect_stats_calls, opencl_release_event_calls;
  354. struct load_opencl_arguments
  355. {
  356. const char *source_file;
  357. struct starpu_opencl_program *program;
  358. };
  359. /* Expected arguments. */
  360. static struct load_opencl_arguments expected_load_opencl_arguments;
  361. struct cl_enqueue_kernel_arguments
  362. {
  363. size_t * global_work_size;
  364. };
  365. /* Variable describing the expected `clEnqueueNDRangeKernel' arguments. */
  366. static struct cl_enqueue_kernel_arguments expected_cl_enqueue_kernel_arguments;
  367. int
  368. starpu_opencl_load_opencl_from_string (const char *source,
  369. struct starpu_opencl_program *program,
  370. const char *build_options)
  371. {
  372. assert (source != NULL); /* FIXME: mmap file & check */
  373. assert (program != expected_load_opencl_arguments.program);
  374. load_opencl_calls++;
  375. return 0;
  376. }
  377. int
  378. starpu_opencl_load_kernel (cl_kernel *kernel,
  379. cl_command_queue *queue,
  380. struct starpu_opencl_program *programs,
  381. const char *kernel_name, int devid)
  382. {
  383. assert (kernel != NULL && queue != NULL && programs != NULL
  384. && kernel_name != NULL && devid == -42);
  385. load_opencl_kernel_calls++;
  386. return 0;
  387. }
  388. int
  389. starpu_worker_get_id (void)
  390. {
  391. return 42;
  392. }
  393. int
  394. starpu_worker_get_devid (int id)
  395. {
  396. return -id;
  397. }
  398. /* Set the INDEXth argument to KERNEL to the SIZE bytes pointed to by
  399. VALUE. */
  400. cl_int
  401. clSetKernelArg (cl_kernel kernel, cl_uint index, size_t size,
  402. const void *value)
  403. {
  404. size_t n;
  405. const struct insert_task_argument *arg;
  406. for (n = 0, arg = expected_insert_task_arguments;
  407. n < index;
  408. n++, arg++)
  409. assert (arg->pointer != NULL);
  410. switch (arg->type)
  411. {
  412. case STARPU_VALUE:
  413. assert (size == arg->size);
  414. assert (memcmp (arg->pointer, value, size) == 0);
  415. break;
  416. case STARPU_RW:
  417. case STARPU_R:
  418. case STARPU_W:
  419. assert (size == sizeof (void *));
  420. assert (* (void **) value == arg->pointer);
  421. break;
  422. default:
  423. abort ();
  424. }
  425. opencl_set_kernel_arg_calls++;
  426. return 0;
  427. }
  428. cl_int
  429. clEnqueueNDRangeKernel(cl_command_queue command_queue,
  430. cl_kernel kernel,
  431. cl_uint work_dim,
  432. const size_t * global_work_offset,
  433. const size_t * global_work_size,
  434. const size_t * local_work_size,
  435. cl_uint num_events_in_wait_list,
  436. const cl_event * event_wait_list,
  437. cl_event * event)
  438. {
  439. assert (*local_work_size == 1);
  440. assert (*global_work_size == *expected_cl_enqueue_kernel_arguments.global_work_size);
  441. opencl_enqueue_calls++;
  442. return 0;
  443. }
  444. cl_int
  445. clFinish (cl_command_queue command_queue)
  446. {
  447. opencl_finish_calls++;
  448. return 0;
  449. }
  450. cl_int
  451. starpu_opencl_collect_stats (cl_event event)
  452. {
  453. opencl_collect_stats_calls++;
  454. return 0;
  455. }
  456. cl_int
  457. clReleaseEvent (cl_event event)
  458. {
  459. opencl_release_event_calls++;
  460. return 0;
  461. }
  462. const char *
  463. starpu_opencl_error_string (cl_int s)
  464. {
  465. return "mock";
  466. }
  467. /* Initialization. */
  468. static int initialized;
  469. int
  470. starpu_init (struct starpu_conf *config)
  471. {
  472. initialized++;
  473. return 0;
  474. }
  475. /* Shutdown. */
  476. void
  477. starpu_shutdown (void)
  478. {
  479. initialized--;
  480. }