test_interfaces.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2011-2021 Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
  4. *
  5. * StarPU is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation; either version 2.1 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * StarPU is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. *
  14. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  15. */
  16. #include <starpu.h>
  17. #include <datawizard/coherency.h>
  18. #include <assert.h>
  19. #include "test_interfaces.h"
  20. #include "../../helper.h"
  21. /*
  22. * This is definitely note thread-safe.
  23. */
  24. static struct test_config *current_config;
  25. /* TODO :
  26. - OpenCL to OpenCL support
  27. */
  28. static char *enum_to_string(int exit_code)
  29. {
  30. switch (exit_code)
  31. {
  32. case SUCCESS:
  33. return "Success";
  34. case FAILURE:
  35. return "Failure";
  36. case UNTESTED:
  37. return "Untested";
  38. case NO_DEVICE:
  39. return "No device available";
  40. case TASK_SUBMISSION_FAILURE:
  41. return "Task submission failed";
  42. default:
  43. assert(0);
  44. }
  45. }
  46. void data_interface_test_summary_print(FILE *f, struct data_interface_test_summary *s)
  47. {
  48. if (!f)
  49. f = stderr;
  50. FPRINTF(f, "%s : %s\n", current_config->name, enum_to_string(s->success));
  51. FPRINTF(f, "Asynchronous :\n");
  52. FPRINTF(f, "\tCPU -> CUDA : %s\n", enum_to_string(s->cpu_to_cuda_async));
  53. FPRINTF(f, "\tCUDA -> CUDA : %s\n", enum_to_string(s->cuda_to_cuda_async));
  54. FPRINTF(f, "\tCUDA -> CPU : %s\n", enum_to_string(s->cuda_to_cpu_async));
  55. FPRINTF(f, "\n");
  56. FPRINTF(f, "\tCPU -> OpenCL : %s\n", enum_to_string(s->cpu_to_opencl_async));
  57. FPRINTF(f, "\tOpenCL -> CPU : %s\n", enum_to_string(s->opencl_to_cpu_async));
  58. FPRINTF(f, "\n");
  59. FPRINTF(f, "Synchronous :\n");
  60. FPRINTF(f, "\tCPU -> CUDA : %s\n", enum_to_string(s->cpu_to_cuda));
  61. FPRINTF(f, "\tCUDA -> CUDA : %s\n", enum_to_string(s->cuda_to_cuda));
  62. FPRINTF(f, "\tCUDA -> CPU : %s\n", enum_to_string(s->cuda_to_cpu));
  63. FPRINTF(f, "\n");
  64. FPRINTF(f, "\tCPU -> OpenCL : %s\n", enum_to_string(s->cpu_to_opencl));
  65. FPRINTF(f, "\tOpenCL -> CPU : %s\n", enum_to_string(s->opencl_to_cpu));
  66. FPRINTF(f, "\n");
  67. FPRINTF(f, "\n");
  68. FPRINTF(f, "CPU -> CPU : %s\n", enum_to_string(s->cpu_to_cpu));
  69. FPRINTF(f, "to_pointer() : %s\n", enum_to_string(s->to_pointer));
  70. FPRINTF(f, "pointer_is_inside() : %s\n", enum_to_string(s->pointer_is_inside));
  71. FPRINTF(f, "compare() : %s\n", enum_to_string(s->compare));
  72. FPRINTF(f, "pack_unpack() : %s\n", enum_to_string(s->pack));
  73. }
  74. int data_interface_test_summary_success(struct data_interface_test_summary *s)
  75. {
  76. return s->success;
  77. }
  78. static void set_field(struct data_interface_test_summary *s, int *field, int ret)
  79. {
  80. *field = ret;
  81. if (ret == FAILURE) s->success = ret;
  82. }
  83. static void summary_init(struct data_interface_test_summary *s)
  84. {
  85. s->cpu_to_cpu = UNTESTED;
  86. s->compare = UNTESTED;
  87. s->cpu_to_cuda = UNTESTED;
  88. s->cuda_to_cuda = UNTESTED;
  89. s->cuda_to_cpu = UNTESTED;
  90. s->cpu_to_cuda_async = UNTESTED;
  91. s->cuda_to_cpu_async = UNTESTED;
  92. s->cuda_to_cuda_async = UNTESTED;
  93. s->cpu_to_opencl = UNTESTED;
  94. s->opencl_to_cpu = UNTESTED;
  95. s->cpu_to_opencl_async = UNTESTED;
  96. s->opencl_to_cpu_async = UNTESTED;
  97. s->to_pointer = UNTESTED;
  98. s->pointer_is_inside = UNTESTED;
  99. s->pack = UNTESTED;
  100. s->success = SUCCESS;
  101. };
  102. /*
  103. * This variable has to be either -1 or 1.
  104. * The kernels should check that the ith value stored in the data interface is
  105. * equal to i, if factor == 1, or -i, if factor == -1.
  106. */
  107. static int factor = -1;
  108. /*
  109. * Creates a complete task, only knowing on what device it should be executed.
  110. * Note that the global variable <current_config> is heavily used here.
  111. * Arguments :
  112. * - taskp : a pointer to a valid task
  113. * - type : STARPU_{CPU,CUDA,OPENCL}_WORKER.
  114. * - id: when positive, should be the worker id
  115. * Return values :
  116. * -ENODEV
  117. * 0 : success.
  118. */
  119. static int create_task(struct starpu_task **taskp, enum starpu_worker_archtype type, int id)
  120. {
  121. static int cpu_workers[STARPU_MAXCPUS];
  122. static int cuda_workers[STARPU_MAXCUDADEVS];
  123. static int opencl_workers[STARPU_MAXOPENCLDEVS];
  124. static int n_cpus = -1;
  125. static int n_cudas = -1;
  126. static int n_opencls = -1;
  127. if (n_cpus == -1) /* First time here */
  128. {
  129. /* We do not check the return values of the calls to
  130. * starpu_worker_get_ids_by_type now, because it is simpler to
  131. * detect a problem in the switch that comes right after this
  132. * block of code. */
  133. n_cpus = starpu_worker_get_ids_by_type(STARPU_CPU_WORKER, cpu_workers, STARPU_MAXCPUS);
  134. n_cudas = starpu_worker_get_ids_by_type(STARPU_CUDA_WORKER, cuda_workers, STARPU_MAXCUDADEVS);
  135. n_opencls = starpu_worker_get_ids_by_type(STARPU_OPENCL_WORKER, opencl_workers, STARPU_MAXOPENCLDEVS);
  136. }
  137. int *workers;
  138. static struct starpu_codelet cl;
  139. starpu_codelet_init(&cl);
  140. cl.nbuffers = 1;
  141. cl.modes[0] = STARPU_RW;
  142. if (type == STARPU_CPU_WORKER)
  143. {
  144. if (n_cpus == 0) return -ENODEV;
  145. if (id != -1 && id >= n_cpus)
  146. {
  147. FPRINTF(stderr, "Not enough CPU workers\n");
  148. return -ENODEV;
  149. }
  150. workers = cpu_workers;
  151. cl.cpu_funcs[0] = current_config->cpu_func;
  152. }
  153. else if (type == STARPU_CUDA_WORKER)
  154. {
  155. if (n_cudas == 0) return -ENODEV;
  156. if (id != -1 && id >= n_cudas)
  157. {
  158. FPRINTF(stderr, "Not enough CUDA workers\n");
  159. return -ENODEV;
  160. }
  161. workers = cuda_workers;
  162. cl.cuda_funcs[0] = current_config->cuda_func;
  163. }
  164. else if (type == STARPU_OPENCL_WORKER)
  165. {
  166. if (n_opencls == 0) return -ENODEV;
  167. if (id != -1 && id >= n_opencls)
  168. {
  169. FPRINTF(stderr, "Not enough OpenCL workers\n");
  170. return -ENODEV;
  171. }
  172. workers = opencl_workers;
  173. cl.opencl_funcs[0] = current_config->opencl_func;
  174. }
  175. else
  176. {
  177. return -ENODEV;
  178. }
  179. factor = -factor;
  180. struct starpu_task *task;
  181. task = starpu_task_build(&cl,
  182. STARPU_RW, *current_config->handle,
  183. STARPU_TASK_SYNCHRONOUS, 1,
  184. 0);
  185. task->cl_arg = &factor;
  186. task->cl_arg_size = sizeof(factor);
  187. if (id != -1)
  188. {
  189. task->execute_on_a_specific_worker = 1;
  190. task->workerid = workers[id];
  191. }
  192. *taskp = task;
  193. return 0;
  194. }
  195. /*
  196. * <device1>_to_<device2> functions.
  197. * They all create and submit a task that has to be executed on <device2>,
  198. * forcing a copy between <device1> and <device2>.
  199. */
  200. static enum exit_code ram_to_cuda(void)
  201. {
  202. int err;
  203. struct starpu_task *task;
  204. err = create_task(&task, STARPU_CUDA_WORKER, 0);
  205. if (err != 0)
  206. return NO_DEVICE;
  207. err = starpu_task_submit(task);
  208. if (err != 0)
  209. return TASK_SUBMISSION_FAILURE;
  210. FPRINTF(stderr, "[%s] : %d\n", __starpu_func__, current_config->copy_failed);
  211. return current_config->copy_failed;
  212. }
  213. static enum exit_code cuda_to_cuda(void)
  214. {
  215. int err;
  216. struct starpu_task *task;
  217. err = create_task(&task, STARPU_CUDA_WORKER, 1);
  218. if (err != 0)
  219. return NO_DEVICE;
  220. err = starpu_task_submit(task);
  221. if (err != 0)
  222. return TASK_SUBMISSION_FAILURE;
  223. FPRINTF(stderr, "[%s] : %d\n", __starpu_func__, current_config->copy_failed);
  224. return current_config->copy_failed;
  225. }
  226. static enum exit_code cuda_to_ram(void)
  227. {
  228. int err;
  229. struct starpu_task *task;
  230. err = create_task(&task, STARPU_CPU_WORKER, -1);
  231. if (err != 0)
  232. return NO_DEVICE;
  233. err = starpu_task_submit(task);
  234. if (err != 0)
  235. return TASK_SUBMISSION_FAILURE;
  236. FPRINTF(stderr, "[%s] : %d\n", __starpu_func__, current_config->copy_failed);
  237. return current_config->copy_failed;
  238. }
  239. static enum exit_code ram_to_opencl(void)
  240. {
  241. int err;
  242. struct starpu_task *task;
  243. err = create_task(&task, STARPU_OPENCL_WORKER, -1);
  244. if (err != 0)
  245. return NO_DEVICE;
  246. err = starpu_task_submit(task);
  247. if (err != 0)
  248. return TASK_SUBMISSION_FAILURE;
  249. FPRINTF(stderr, "[%s] : %d\n", __starpu_func__, current_config->copy_failed);
  250. return current_config->copy_failed;
  251. }
  252. static enum exit_code opencl_to_ram(void)
  253. {
  254. int err;
  255. struct starpu_task *task;
  256. err = create_task(&task, STARPU_CPU_WORKER, -1);
  257. if (err != 0)
  258. return NO_DEVICE;
  259. err = starpu_task_submit(task);
  260. if (err != 0)
  261. return TASK_SUBMISSION_FAILURE;
  262. FPRINTF(stderr, "[%s] : %d\n", __starpu_func__, current_config->copy_failed);
  263. return current_config->copy_failed;
  264. }
  265. /* End of the <device1>_to_<device2> functions. */
  266. static void run_cuda(int async, struct data_interface_test_summary *s)
  267. {
  268. /* RAM -> CUDA (-> CUDA) -> RAM */
  269. int err;
  270. err = ram_to_cuda();
  271. set_field(s, async==1?&s->cpu_to_cuda_async:&s->cpu_to_cuda, err);
  272. /* If this failed, there is no point in continuing. */
  273. if (err != SUCCESS)
  274. return;
  275. if (starpu_cuda_worker_get_count() >= 2)
  276. {
  277. err = cuda_to_cuda();
  278. }
  279. else
  280. {
  281. err = UNTESTED;
  282. }
  283. set_field(s, async==1?&s->cuda_to_cuda_async:&s->cuda_to_cuda, err);
  284. /* Even if cuda_to_cuda() failed, a valid copy is left on the first
  285. * cuda device, which means we can safely test cuda_to_ram() */
  286. err = cuda_to_ram();
  287. set_field(s, async==1?&s->cuda_to_cpu_async:&s->cuda_to_cpu, err);
  288. }
  289. static void run_opencl(int async, struct data_interface_test_summary *s)
  290. {
  291. /* RAM -> OpenCL -> RAM */
  292. int err;
  293. err = ram_to_opencl();
  294. set_field(s, async==1?&s->cpu_to_opencl_async:&s->cpu_to_opencl, err);
  295. if (err != SUCCESS)
  296. return;
  297. err = opencl_to_ram();
  298. set_field(s, async==1?&s->opencl_to_cpu_async:&s->opencl_to_cpu, err);
  299. }
  300. static void ram_to_ram(struct data_interface_test_summary *s)
  301. {
  302. int err;
  303. struct starpu_task *task;
  304. starpu_data_handle_t src, dst;
  305. void *src_interface, *dst_interface;
  306. src = *current_config->handle;
  307. dst = *current_config->dummy_handle;
  308. /* We do not care about the nodes */
  309. src_interface = starpu_data_get_interface_on_node(src, STARPU_MAIN_RAM);
  310. dst_interface = starpu_data_get_interface_on_node(dst, STARPU_MAIN_RAM);
  311. if (src->ops->copy_methods->ram_to_ram)
  312. src->ops->copy_methods->ram_to_ram(src_interface, STARPU_MAIN_RAM, dst_interface, STARPU_MAIN_RAM);
  313. else
  314. src->ops->copy_methods->any_to_any(src_interface, STARPU_MAIN_RAM, dst_interface, STARPU_MAIN_RAM, NULL);
  315. err = create_task(&task, STARPU_CPU_WORKER, -1);
  316. if (err != 0)
  317. goto out;
  318. task->handles[0] = dst;
  319. err = starpu_task_submit(task);
  320. if (err != 0)
  321. {
  322. err = TASK_SUBMISSION_FAILURE;
  323. goto out;
  324. }
  325. FPRINTF(stderr, "[%s] : %d\n", __starpu_func__, current_config->copy_failed);
  326. err = current_config->copy_failed;
  327. out:
  328. set_field(s, &s->cpu_to_cpu, err);
  329. }
  330. static void run_async(struct data_interface_test_summary *s)
  331. {
  332. int async = starpu_asynchronous_copy_disabled();
  333. if (async == 1)
  334. {
  335. FPRINTF(stderr, "Asynchronous copies have been disabled\n");
  336. return;
  337. }
  338. run_cuda(1, s);
  339. run_opencl(1, s);
  340. }
  341. static void run_sync(struct data_interface_test_summary *s)
  342. {
  343. starpu_data_handle_t handle = *current_config->handle;
  344. struct starpu_data_copy_methods new_copy_methods;
  345. struct starpu_data_copy_methods *old_copy_methods;
  346. old_copy_methods = (struct starpu_data_copy_methods *) handle->ops->copy_methods;
  347. memcpy(&new_copy_methods, old_copy_methods, sizeof(struct starpu_data_copy_methods));
  348. new_copy_methods.ram_to_cuda_async = NULL;
  349. new_copy_methods.cuda_to_cuda_async = NULL;
  350. new_copy_methods.cuda_to_ram_async = NULL;
  351. new_copy_methods.ram_to_opencl_async = NULL;
  352. new_copy_methods.opencl_to_ram_async = NULL;
  353. handle->ops->copy_methods = &new_copy_methods;
  354. run_cuda(0, s);
  355. run_opencl(0, s);
  356. handle->ops->copy_methods = old_copy_methods;
  357. }
  358. static void compare(struct data_interface_test_summary *s)
  359. {
  360. int err;
  361. void *interface_a, *interface_b;
  362. starpu_data_handle_t handle, dummy_handle;
  363. handle = *current_config->handle;
  364. dummy_handle = *current_config->dummy_handle;
  365. interface_a = starpu_data_get_interface_on_node(handle, STARPU_MAIN_RAM);
  366. interface_b = starpu_data_get_interface_on_node(dummy_handle, STARPU_MAIN_RAM);
  367. err = handle->ops->compare(interface_a, interface_b);
  368. s->compare = (err == 0) ? FAILURE : SUCCESS;
  369. set_field(s, &s->compare, s->compare);
  370. }
  371. static void to_pointer(struct data_interface_test_summary *s)
  372. {
  373. starpu_data_handle_t handle;
  374. s->to_pointer = UNTESTED;
  375. handle = *current_config->handle;
  376. if (handle->ops->to_pointer)
  377. {
  378. unsigned int node;
  379. unsigned int tests = 0;
  380. for (node = 0; node < STARPU_MAXNODES; node++)
  381. {
  382. if (starpu_node_get_kind(node) != STARPU_CPU_RAM)
  383. continue;
  384. if (!starpu_data_test_if_allocated_on_node(handle, node))
  385. continue;
  386. void *data_interface = starpu_data_get_interface_on_node(handle, node);
  387. void *ptr = handle->ops->to_pointer(data_interface, node);
  388. if (starpu_data_lookup(ptr) != handle)
  389. {
  390. s->to_pointer = FAILURE;
  391. break;
  392. }
  393. tests++;
  394. }
  395. if (tests > 0)
  396. s->to_pointer = SUCCESS;
  397. }
  398. set_field(s, &s->to_pointer, s->to_pointer);
  399. }
  400. static void pointer_is_inside(struct data_interface_test_summary *s)
  401. {
  402. starpu_data_handle_t handle;
  403. s->pointer_is_inside = UNTESTED;
  404. handle = *current_config->handle;
  405. if (handle->ops->pointer_is_inside && handle->ops->to_pointer)
  406. {
  407. unsigned int node;
  408. unsigned int tests = 0;
  409. for (node = 0; node < STARPU_MAXNODES; node++)
  410. {
  411. if (starpu_node_get_kind(node) != STARPU_CPU_RAM)
  412. continue;
  413. if (!starpu_data_test_if_allocated_on_node(handle, node))
  414. continue;
  415. void *data_interface = starpu_data_get_interface_on_node(handle, node);
  416. void *ptr = handle->ops->to_pointer(data_interface, node);
  417. if (starpu_data_lookup(ptr) != handle)
  418. {
  419. s->pointer_is_inside = FAILURE;
  420. break;
  421. }
  422. if (!starpu_data_pointer_is_inside(handle, node, ptr))
  423. {
  424. s->pointer_is_inside = FAILURE;
  425. break;
  426. }
  427. tests++;
  428. }
  429. if (tests > 0)
  430. s->pointer_is_inside = SUCCESS;
  431. }
  432. set_field(s, &s->pointer_is_inside, s->pointer_is_inside);
  433. }
  434. static void pack_unpack(struct data_interface_test_summary *s)
  435. {
  436. starpu_data_handle_t handle;
  437. starpu_data_handle_t dummy_handle;
  438. int err = UNTESTED;
  439. handle = *current_config->handle;
  440. dummy_handle = *current_config->dummy_handle;
  441. if (handle->ops->pack_data && handle->ops->unpack_data)
  442. {
  443. void *ptr = NULL;
  444. starpu_ssize_t size = 0;
  445. starpu_data_pack(handle, &ptr, &size);
  446. if (size != 0)
  447. {
  448. struct starpu_task *task;
  449. void *mem = (void *)starpu_malloc_on_node_flags(STARPU_MAIN_RAM, size, 0);
  450. starpu_data_acquire(dummy_handle, STARPU_W);
  451. starpu_data_unpack(dummy_handle, mem, size);
  452. starpu_data_unpack(dummy_handle, ptr, size);
  453. starpu_data_release(dummy_handle);
  454. factor = -factor;
  455. err = create_task(&task, STARPU_CPU_WORKER, -1);
  456. if (err != SUCCESS) goto out;
  457. task->handles[0] = dummy_handle;
  458. err = starpu_task_submit(task);
  459. if (err != 0)
  460. {
  461. err = TASK_SUBMISSION_FAILURE;
  462. goto out;
  463. }
  464. FPRINTF(stderr, "[%s] : %d\n", __starpu_func__, current_config->copy_failed);
  465. err = current_config->copy_failed;
  466. }
  467. }
  468. out:
  469. set_field(s, &s->pack, err);
  470. }
  471. static int load_conf(struct test_config *config)
  472. {
  473. if (!config ||
  474. #ifdef STARPU_USE_CPU
  475. !config->cpu_func ||
  476. !config->dummy_handle ||
  477. #endif
  478. #ifdef STARPU_USE_CUDA
  479. !config->cuda_func ||
  480. #endif
  481. #ifdef STARPU_USE_OPENCL
  482. !config->opencl_func ||
  483. #endif
  484. !config->handle)
  485. {
  486. return 1;
  487. }
  488. current_config = config;
  489. return 0;
  490. }
  491. void run_tests(struct test_config *conf, struct data_interface_test_summary *s)
  492. {
  493. summary_init(s);
  494. if (load_conf(conf) == 1)
  495. {
  496. FPRINTF(stderr, "Failed to load conf.\n");
  497. }
  498. run_async(s);
  499. run_sync(s);
  500. ram_to_ram(s);
  501. compare(s);
  502. to_pointer(s);
  503. pointer_is_inside(s);
  504. pack_unpack(s);
  505. }