test_interfaces.c 15 KB

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