test_interfaces.c 14 KB

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