test_interfaces.c 13 KB

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