test_interfaces.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2011 Institut National de Recherche en Informatique et Automatique
  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. #ifdef STARPU_USE_OPENCL
  18. #include <starpu_opencl.h>
  19. #endif
  20. /* XXX Why cant we dereference a handle without this one ? */
  21. #include <core/sched_policy.h>
  22. #include <assert.h>
  23. #include "test_interfaces.h"
  24. #include "../../common/helper.h"
  25. /*
  26. * This is definitely note thrad-safe.
  27. */
  28. static struct test_config *current_config;
  29. /* TODO :
  30. - OpenCL to OpenCL support
  31. */
  32. /*
  33. * Users do not know about this enum. They only know that SUCCESS is 0, and
  34. * FAILURE is 1. Therefore, the values of SUCCESS and FAILURE shall not be
  35. * changed.
  36. */
  37. enum exit_code {
  38. SUCCESS = 0,
  39. FAILURE = 1,
  40. UNTESTED = 2,
  41. TASK_CREATION_FAILURE = 3,
  42. TASK_SUBMISSION_FAILURE = 4
  43. };
  44. static char *
  45. enum_to_string(exit_code)
  46. {
  47. switch (exit_code)
  48. {
  49. case SUCCESS:
  50. return "Success";
  51. case FAILURE:
  52. return "Failure";
  53. case UNTESTED:
  54. return "Untested";
  55. case TASK_CREATION_FAILURE:
  56. return "Task creation failed";
  57. case TASK_SUBMISSION_FAILURE:
  58. return "Task submission failed";
  59. default:
  60. assert(0);
  61. }
  62. }
  63. struct data_interface_test_summary {
  64. int success;
  65. /* Copy methods */
  66. #ifdef STARPU_USE_CPU
  67. int cpu_to_cpu;
  68. #endif
  69. #ifdef STARPU_USE_CUDA
  70. int cpu_to_cuda;
  71. int cuda_to_cuda;
  72. int cuda_to_cpu;
  73. int cpu_to_cuda_async;
  74. int cuda_to_cpu_async;
  75. int cuda_to_cuda_async;
  76. #endif
  77. #ifdef STARPU_USE_OPENCL
  78. int cpu_to_opencl;
  79. int opencl_to_cpu;
  80. int cpu_to_opencl_async;
  81. int opencl_to_cpu_async;
  82. #endif
  83. /* Other stuff */
  84. int compare;
  85. };
  86. void data_interface_test_summary_print(FILE *f,
  87. struct data_interface_test_summary *s)
  88. {
  89. if (!f)
  90. f = stderr;
  91. FPRINTF(f, "%s : %s\n",
  92. current_config->name, enum_to_string(s->success));
  93. FPRINTF(f, "Asynchronous :\n");
  94. #ifdef STARPU_USE_CUDA
  95. FPRINTF(f, "\tCPU -> CUDA : %s\n",
  96. enum_to_string(s->cpu_to_cuda_async));
  97. FPRINTF(f, "\tCUDA -> CUDA : %s\n",
  98. enum_to_string(s->cuda_to_cuda_async));
  99. FPRINTF(f, "\tCUDA -> CPU : %s\n",
  100. enum_to_string(s->cuda_to_cpu_async));
  101. #endif /* !STARPU_USE_CUDA */
  102. #ifdef STARPU_USE_OPENCL
  103. FPRINTF(f, "\tCPU -> OpenCl : %s\n",
  104. enum_to_string(s->cpu_to_opencl_async));
  105. FPRINTF(f, "\tOpenCl -> CPU : %s\n",
  106. enum_to_string(s->opencl_to_cpu_async));
  107. #endif /* !STARPU_USE_OPENCL */
  108. FPRINTF(f, "Synchronous :\n");
  109. #ifdef STARPU_USE_CUDA
  110. FPRINTF(f, "\tCPU -> CUDA ; %s\n",
  111. enum_to_string(s->cpu_to_cuda));
  112. FPRINTF(f, "\tCUDA -> CUDA : %s\n",
  113. enum_to_string(s->cuda_to_cuda));
  114. FPRINTF(f, "\tCUDA -> CPU : %s\n",
  115. enum_to_string(s->cuda_to_cpu));
  116. #endif /* !STARPU_USE_CUDA */
  117. #ifdef STARPU_USE_OPENCL
  118. FPRINTF(f, "\tCPU -> OpenCl : %s\n",
  119. enum_to_string(s->cpu_to_opencl));
  120. FPRINTF(f, "\tOpenCl -> CPU : %s\n",
  121. enum_to_string(s->opencl_to_cpu));
  122. #endif /* !STARPU_USE_OPENCL */
  123. #ifdef STARPU_USE_CPU
  124. (void) fprintf(f, "CPU -> CPU : %s\n",
  125. enum_to_string(s->cpu_to_cpu));
  126. (void) fprintf(f, "compare() : %s\n",
  127. enum_to_string(s->compare));
  128. #endif /* !STARPU_USE_CPU */
  129. }
  130. int
  131. data_interface_test_summary_success(data_interface_test_summary *s)
  132. {
  133. return s->success;
  134. }
  135. enum operation {
  136. CPU_TO_CPU,
  137. #ifdef STARPU_USE_CUDA
  138. CPU_TO_CUDA,
  139. CUDA_TO_CUDA,
  140. CUDA_TO_CPU,
  141. #endif /* !STARPU_USE_CUDA */
  142. #ifdef STARPU_USE_OPENCL
  143. CPU_TO_OPENCL,
  144. OPENCL_TO_CPU
  145. #endif /* !STARPU_USE_OPENCL */
  146. };
  147. static int*
  148. get_field(struct data_interface_test_summary *s, int async, enum operation op)
  149. {
  150. switch (op)
  151. {
  152. #ifdef STARPU_USE_CPU
  153. case CPU_TO_CPU:
  154. return &s->cpu_to_cpu;
  155. #endif
  156. #ifdef STARPU_USE_CUDA
  157. case CPU_TO_CUDA:
  158. return async?&s->cpu_to_cuda_async:&s->cpu_to_cuda;
  159. case CUDA_TO_CUDA:
  160. return async?&s->cuda_to_cuda_async:&s->cuda_to_cuda;
  161. case CUDA_TO_CPU:
  162. return async?&s->cuda_to_cpu_async:&s->cuda_to_cpu;
  163. #endif /* !STARPU_USE_CUDA */
  164. #ifdef STARPU_USE_OPENCL
  165. case CPU_TO_OPENCL:
  166. return async?&s->cpu_to_opencl_async:&s->cpu_to_opencl;
  167. case OPENCL_TO_CPU:
  168. return async?&s->opencl_to_cpu_async:&s->opencl_to_cpu;
  169. #endif /* !STARPU_USE_OPENCL */
  170. default:
  171. assert(0);
  172. }
  173. }
  174. static void
  175. set_field(struct data_interface_test_summary *s, int async,
  176. enum operation op, int ret)
  177. {
  178. int *field = get_field(s, async, op);
  179. switch (ret)
  180. {
  181. case SUCCESS:
  182. *field = SUCCESS;
  183. break;
  184. case FAILURE:
  185. *field = FAILURE;
  186. s->success = FAILURE;
  187. break;
  188. case UNTESTED:
  189. *field = UNTESTED;
  190. break;
  191. case TASK_CREATION_FAILURE:
  192. *field = TASK_CREATION_FAILURE;
  193. break;
  194. case TASK_SUBMISSION_FAILURE:
  195. *field = TASK_SUBMISSION_FAILURE;
  196. break;
  197. default:
  198. assert(0);
  199. }
  200. }
  201. static struct data_interface_test_summary summary = {
  202. #ifdef STARPU_USE_CPU
  203. .cpu_to_cpu = UNTESTED,
  204. #endif
  205. #ifdef STARPU_USE_CUDA
  206. .cpu_to_cuda = UNTESTED,
  207. .cuda_to_cuda = UNTESTED,
  208. .cuda_to_cpu = UNTESTED,
  209. .cpu_to_cuda_async = UNTESTED,
  210. .cuda_to_cpu_async = UNTESTED,
  211. .cuda_to_cuda_async = UNTESTED,
  212. #endif
  213. #ifdef STARPU_USE_OPENCL
  214. .cpu_to_opencl = UNTESTED,
  215. .opencl_to_cpu = UNTESTED,
  216. .cpu_to_opencl_async = UNTESTED,
  217. .opencl_to_cpu_async = UNTESTED,
  218. #endif
  219. .success = SUCCESS
  220. };
  221. /*
  222. * This variable has to be either -1 or 1.
  223. * The kernels should check that the ith value stored in the data interface is
  224. * equal to i, if factor == 1, or -i, if factor == -1.
  225. */
  226. static int factor = -1;
  227. /*
  228. * Creates a complete task, only knowing on what device it should be executed.
  229. * Note that the global variable <current_config> is heavily used here.
  230. * Arguments :
  231. * - taskp : a pointer to a valid task
  232. * - type : STARPU_{CPU,CUDA,OPENCL}_WORKER. Gordon is not supported.
  233. * - id : -1 if you dont care about the device where the task will be
  234. * executed, as long as it has the right type.
  235. * >= 0 if you want to make sure the task will be executed on the
  236. * idth device that has the specified type.
  237. * Return values :
  238. * -ENODEV
  239. * 0 : success.
  240. */
  241. static int
  242. create_task(struct starpu_task **taskp, enum starpu_archtype type, int id)
  243. {
  244. static int cpu_workers[STARPU_MAXCPUS];
  245. static int cuda_workers[STARPU_MAXCUDADEVS];
  246. static int opencl_workers[STARPU_MAXOPENCLDEVS];
  247. static int n_cpus = -1;
  248. static int n_cudas = -1;
  249. static int n_opencls = -1;
  250. if (n_cpus == -1) /* First time here */
  251. {
  252. /* XXX Dont check them all at once. */
  253. /* XXX Error checking */
  254. n_cpus = starpu_worker_get_ids_by_type(STARPU_CPU_WORKER,
  255. cpu_workers,
  256. STARPU_MAXCPUS);
  257. n_cudas = starpu_worker_get_ids_by_type(STARPU_CUDA_WORKER,
  258. cuda_workers,
  259. STARPU_MAXCUDADEVS);
  260. n_opencls = starpu_worker_get_ids_by_type(STARPU_OPENCL_WORKER,
  261. opencl_workers,
  262. STARPU_MAXOPENCLDEVS);
  263. }
  264. int workerid;
  265. static struct starpu_codelet cl;
  266. cl.nbuffers = 1;
  267. switch (type)
  268. {
  269. case STARPU_CPU_WORKER:
  270. if (id != -1)
  271. {
  272. if (id >= n_cpus)
  273. {
  274. FPRINTF(stderr, "Not enough CPU workers\n");
  275. return -ENODEV;
  276. }
  277. workerid = *(cpu_workers + id);
  278. }
  279. cl.where = STARPU_CPU;
  280. cl.cpu_funcs[0] = current_config->cpu_func;
  281. break;
  282. #ifdef STARPU_USE_CUDA
  283. case STARPU_CUDA_WORKER:
  284. if (id != -1)
  285. {
  286. if (id >= n_cudas)
  287. {
  288. FPRINTF(stderr, "Not enough CUDA workers\n");
  289. return -ENODEV;
  290. }
  291. workerid = cuda_workers[id];
  292. }
  293. cl.where = STARPU_CUDA;
  294. cl.cuda_funcs[0] = current_config->cuda_func;
  295. break;
  296. #endif /* !STARPU_USE_CUDA */
  297. #ifdef STARPU_USE_OPENCL
  298. case STARPU_OPENCL_WORKER:
  299. if (id != -1)
  300. {
  301. if (id >= n_opencls)
  302. {
  303. FPRINTF(stderr, "Not enough OpenCL workers\n");
  304. return -ENODEV;
  305. }
  306. workerid = *(opencl_workers + id);
  307. }
  308. cl.where = STARPU_OPENCL;
  309. cl.opencl_funcs[0] = current_config->opencl_func;
  310. break;
  311. #endif /* ! STARPU_USE_OPENCL */
  312. case STARPU_GORDON_WORKER: /* Not supported */
  313. default:
  314. return -ENODEV;
  315. }
  316. struct starpu_task *task = starpu_task_create();
  317. task->synchronous = 1;
  318. task->cl = &cl;
  319. task->buffers[0].handle = *current_config->handle;
  320. task->buffers[0].mode = STARPU_RW;
  321. if (id != -1)
  322. {
  323. task->execute_on_a_specific_worker = 1;
  324. task->workerid = workerid;
  325. }
  326. factor = -factor;
  327. task->cl_arg = &factor;
  328. task->cl_arg_size = sizeof(&factor);
  329. *taskp = task;
  330. return 0;
  331. }
  332. /*
  333. * <device1>_to_<device2> functions.
  334. * They all create and submit a task that has to be executed on <device2>,
  335. * forcing a copy between <device1> and <device2>.
  336. * XXX : could we sometimes use starp_insert_task() ? It seems hars because we
  337. * need to set the execute_on_a_specific_worker field...
  338. */
  339. #ifdef STARPU_USE_CUDA
  340. static enum exit_code
  341. ram_to_cuda(void)
  342. {
  343. int err;
  344. struct starpu_task *task;
  345. err = create_task(&task, STARPU_CUDA_WORKER, 0);
  346. if (err != 0)
  347. return TASK_CREATION_FAILURE;
  348. err = starpu_task_submit(task);
  349. if (err != 0)
  350. return TASK_SUBMISSION_FAILURE;
  351. FPRINTF(stderr, "[%s] : %d\n", __func__, current_config->copy_failed);
  352. return current_config->copy_failed;
  353. }
  354. #ifdef HAVE_CUDA_MEMCPY_PEER
  355. static enum exit_code
  356. cuda_to_cuda(void)
  357. {
  358. int err;
  359. struct starpu_task *task;
  360. err = create_task(&task, STARPU_CUDA_WORKER, 1);
  361. if (err != 0)
  362. return TASK_CREATION_FAILURE;
  363. err = starpu_task_submit(task);
  364. if (err != 0)
  365. return TASK_SUBMISSION_FAILURE;
  366. FPRINTF(stderr, "[%s] : %d\n", __func__, current_config->copy_failed);
  367. return current_config->copy_failed;
  368. }
  369. #endif
  370. static enum exit_code
  371. cuda_to_ram(void)
  372. {
  373. int err;
  374. struct starpu_task *task;
  375. err = create_task(&task, STARPU_CPU_WORKER, -1);
  376. if (err != 0)
  377. return TASK_CREATION_FAILURE;
  378. err = starpu_task_submit(task);
  379. if (err != 0)
  380. return TASK_SUBMISSION_FAILURE;
  381. FPRINTF(stderr, "[%s] : %d\n", __func__, current_config->copy_failed);
  382. return current_config->copy_failed;
  383. }
  384. #endif /* !STARPU_USE_CUDA */
  385. #ifdef STARPU_USE_OPENCL
  386. static enum exit_code
  387. ram_to_opencl()
  388. {
  389. int err;
  390. struct starpu_task *task;
  391. err = create_task(&task, STARPU_OPENCL_WORKER, 0);
  392. if (err != 0)
  393. return TASK_CREATION_FAILURE;
  394. err = starpu_task_submit(task);
  395. if (err != 0)
  396. return TASK_SUBMISSION_FAILURE;
  397. FPRINTF(stderr, "[%s] : %d\n", __func__, current_config->copy_failed);
  398. return current_config->copy_failed;
  399. }
  400. static enum exit_code
  401. opencl_to_ram()
  402. {
  403. int err;
  404. struct starpu_task *task;
  405. err = create_task(&task, STARPU_CPU_WORKER, -1);
  406. if (err != 0)
  407. return TASK_CREATION_FAILURE;
  408. err = starpu_task_submit(task);
  409. if (err != 0)
  410. return TASK_SUBMISSION_FAILURE;
  411. FPRINTF(stderr, "[%s] : %d\n", __func__, current_config->copy_failed);
  412. return current_config->copy_failed;
  413. }
  414. #endif /* !STARPU_USE_OPENCL */
  415. /* End of the <device1>_to_<device2> functions. */
  416. #ifdef STARPU_USE_CUDA
  417. static void
  418. run_cuda(int async)
  419. {
  420. /* RAM -> CUDA (-> CUDA) -> RAM */
  421. int err;
  422. err = ram_to_cuda();
  423. set_field(&summary, async, CPU_TO_CUDA, err);
  424. /* If this failed, there is no point in continuing. */
  425. if (err != SUCCESS)
  426. return;
  427. #ifdef HAVE_CUDA_MEMCPY_PEER
  428. err = cuda_to_cuda();
  429. set_field(&summary, async, CUDA_TO_CUDA, err);
  430. /* Even if cuda_to_cuda() failed, a valid copy is left on the first
  431. * cuda device, which means we can safely test cuda_to_ram() */
  432. #else
  433. summary.cuda_to_cuda_async = UNTESTED;
  434. #endif /* !HAVE_CUDA_MEMCPY_PEER */
  435. #ifdef STARPU_USE_CPU
  436. err = cuda_to_ram();
  437. set_field(&summary, async, CUDA_TO_CPU, err);
  438. #endif /* !STARPU_USE_CPU */
  439. }
  440. #endif /* !STARPU_USE_CUDA */
  441. #if STARPU_USE_OPENCL
  442. static void
  443. run_opencl(int async)
  444. {
  445. /* RAM -> OpenCL -> RAM */
  446. int err;
  447. err = ram_to_opencl();
  448. set_field(&summary, async, CPU_TO_OPENCL, err);
  449. if (err != SUCCESS)
  450. return;
  451. #if STARPU_USE_CPU
  452. err = opencl_to_ram();
  453. set_field(&summary, async, OPENCL_TO_CPU, err);
  454. #endif /*!STARPU_USE_CPU */
  455. }
  456. #endif /* !STARPU_USE_OPENCL */
  457. #ifdef STARPU_USE_CPU
  458. /* Valid data should be in RAM before calling this function */
  459. static void
  460. ram_to_ram(void)
  461. {
  462. int err;
  463. struct starpu_task *task;
  464. starpu_data_handle_t src, dst;
  465. void *src_interface, *dst_interface;
  466. src = *current_config->handle;
  467. dst = *current_config->dummy_handle;
  468. /* We do not care about the nodes */
  469. src_interface = starpu_data_get_interface_on_node(src, 0);
  470. dst_interface = starpu_data_get_interface_on_node(dst, 0);
  471. src->ops->copy_methods->ram_to_ram(src_interface, 0, dst_interface, 0);
  472. err = create_task(&task, STARPU_CPU_WORKER, -1);
  473. if (err != 0)
  474. goto out;
  475. task->buffers[0].handle = dst;
  476. err = starpu_task_submit(task);
  477. if (err != 0)
  478. {
  479. err = TASK_SUBMISSION_FAILURE;
  480. goto out;
  481. }
  482. fprintf(stderr, "[%s] : %d\n", __func__, current_config->copy_failed);
  483. err = current_config->copy_failed;
  484. out:
  485. set_field(&summary, 0, CPU_TO_CPU, err);
  486. }
  487. #endif /* !STARPU_USE_CPU */
  488. static void
  489. run_async(void)
  490. {
  491. #ifdef STARPU_USE_CUDA
  492. run_cuda(1);
  493. #endif /* !STARPU_USE_CUDA */
  494. #ifdef STARPU_USE_OPENCL
  495. run_opencl(1);
  496. #endif /* !STARPU_USE_OPENCL */
  497. }
  498. static void
  499. run_sync(void)
  500. {
  501. starpu_data_handle_t handle = *current_config->handle;
  502. struct starpu_data_copy_methods new_copy_methods;
  503. struct starpu_data_copy_methods *old_copy_methods;
  504. old_copy_methods = handle->ops->copy_methods;
  505. memcpy(&new_copy_methods,
  506. old_copy_methods,
  507. sizeof(struct starpu_data_copy_methods));
  508. #ifdef STARPU_USE_CUDA
  509. new_copy_methods.ram_to_cuda_async = NULL;
  510. new_copy_methods.cuda_to_cuda_async = NULL;
  511. new_copy_methods.cuda_to_ram_async = NULL;
  512. #endif /* !STARPU_USE_CUDA */
  513. #ifdef STARPU_USE_OPENCL
  514. new_copy_methods.ram_to_opencl_async = NULL;
  515. new_copy_methods.opencl_to_ram_async = NULL;
  516. #endif /* !STARPU_USE_OPENCL */
  517. handle->ops->copy_methods = &new_copy_methods;
  518. #ifdef STARPU_USE_CUDA
  519. run_cuda(0);
  520. #endif /* !STARPU_USE_CUDA */
  521. #ifdef STARPU_USE_OPENCL
  522. run_opencl(0);
  523. #endif /* !STARPU_USE_OPENCL */
  524. handle->ops->copy_methods = old_copy_methods;
  525. }
  526. static void
  527. compare(void)
  528. {
  529. int err;
  530. void *interface_a, *interface_b;
  531. starpu_data_handle_t handle, dummy_handle;
  532. handle = *current_config->handle;
  533. dummy_handle = *current_config->dummy_handle;
  534. interface_a = starpu_data_get_interface_on_node(handle, 0);
  535. interface_b = starpu_data_get_interface_on_node(dummy_handle, 0);
  536. err = handle->ops->compare(interface_a, interface_b);
  537. if (err == 0)
  538. {
  539. summary.compare = FAILURE;
  540. summary.success = FAILURE;
  541. }
  542. else
  543. {
  544. summary.compare = SUCCESS;
  545. }
  546. }
  547. static int
  548. load_conf(struct test_config *config)
  549. {
  550. if (!config ||
  551. #ifdef STARPU_USE_CPU
  552. !config->cpu_func ||
  553. !config->dummy_handle ||
  554. #endif
  555. #ifdef STARPU_USE_CUDA
  556. !config->cuda_func ||
  557. #endif
  558. #ifdef STARPU_USE_OPENCL
  559. !config->opencl_func ||
  560. #endif
  561. !config->handle)
  562. {
  563. return 1;
  564. }
  565. current_config = config;
  566. return 0;
  567. }
  568. data_interface_test_summary*
  569. run_tests(struct test_config *conf)
  570. {
  571. if (load_conf(conf) == 1)
  572. {
  573. FPRINTF(stderr, "Failed to load conf.\n");
  574. return NULL;
  575. }
  576. run_async();
  577. run_sync();
  578. #ifdef STARPU_USE_CPU
  579. ram_to_ram();
  580. compare();
  581. #endif
  582. return &summary;
  583. }