test_interfaces.c 15 KB

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