test_interfaces.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  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. #ifdef STARPU_USE_CUDA
  125. CPU_TO_CUDA,
  126. CUDA_TO_CUDA,
  127. CUDA_TO_CPU,
  128. #endif /* !STARPU_USE_CUDA */
  129. #ifdef STARPU_USE_OPENCL
  130. CPU_TO_OPENCL,
  131. OPENCL_TO_CPU
  132. #endif /* !STARPU_USE_OPENCL */
  133. };
  134. static int*
  135. get_field(struct data_interface_test_summary *s, int async, enum operation op)
  136. {
  137. switch (op)
  138. {
  139. #ifdef STARPU_USE_CUDA
  140. case CPU_TO_CUDA:
  141. return async?&s->cpu_to_cuda_async:&s->cpu_to_cuda;
  142. case CUDA_TO_CUDA:
  143. return async?&s->cuda_to_cuda_async:&s->cuda_to_cuda;
  144. case CUDA_TO_CPU:
  145. return async?&s->cuda_to_cpu_async:&s->cuda_to_cpu;
  146. #endif /* !STARPU_USE_CUDA */
  147. #ifdef STARPU_USE_OPENCL
  148. case CPU_TO_OPENCL:
  149. return async?&s->cpu_to_opencl_async:&s->cpu_to_opencl;
  150. case OPENCL_TO_CPU:
  151. return async?&s->opencl_to_cpu_async:&s->opencl_to_cpu;
  152. #endif /* !STARPU_USE_OPENCL */
  153. default:
  154. assert(0);
  155. }
  156. }
  157. static void
  158. set_field(struct data_interface_test_summary *s, int async,
  159. enum operation op, int ret)
  160. {
  161. int *field = get_field(s, async, op);
  162. switch (ret)
  163. {
  164. case SUCCESS:
  165. *field = SUCCESS;
  166. break;
  167. case FAILURE:
  168. *field = FAILURE;
  169. s->success = FAILURE;
  170. break;
  171. case UNTESTED:
  172. *field = UNTESTED;
  173. break;
  174. case TASK_CREATION_FAILURE:
  175. *field = TASK_CREATION_FAILURE;
  176. break;
  177. case TASK_SUBMISSION_FAILURE:
  178. *field = TASK_SUBMISSION_FAILURE;
  179. break;
  180. default:
  181. assert(0);
  182. }
  183. }
  184. static struct data_interface_test_summary summary = {
  185. #ifdef STARPU_USE_CUDA
  186. .cpu_to_cuda = UNTESTED,
  187. .cuda_to_cuda = UNTESTED,
  188. .cuda_to_cpu = UNTESTED,
  189. .cpu_to_cuda_async = UNTESTED,
  190. .cuda_to_cpu_async = UNTESTED,
  191. .cuda_to_cuda_async = UNTESTED,
  192. #endif
  193. #ifdef STARPU_USE_OPENCL
  194. .cpu_to_opencl = UNTESTED,
  195. .opencl_to_cpu = UNTESTED,
  196. .cpu_to_opencl_async = UNTESTED,
  197. .opencl_to_cpu_async = UNTESTED,
  198. #endif
  199. .success = SUCCESS
  200. };
  201. /*
  202. * This variable has to be either -1 or 1.
  203. * The kernels should check that the ith value stored in the data interface is
  204. * equal to i, if factor == 1, or -i, if factor == -1.
  205. */
  206. static int factor = -1;
  207. /*
  208. * Creates a complete task, only knowing on what device it should be executed.
  209. * Note that the global variable <current_config> is heavily used here.
  210. * Arguments :
  211. * - taskp : a pointer to a valid task
  212. * - type : STARPU_{CPU,CUDA,OPENCL}_WORKER. Gordon is not supported.
  213. * - id : -1 if you dont care about the device where the task will be
  214. * executed, as long as it has the right type.
  215. * >= 0 if you want to make sure the task will be executed on the
  216. * idth device that has the specified type.
  217. * Return values :
  218. * -ENODEV
  219. * 0 : success.
  220. */
  221. static int
  222. create_task(struct starpu_task **taskp, enum starpu_archtype type, int id)
  223. {
  224. static int cpu_workers[STARPU_MAXCPUS];
  225. static int cuda_workers[STARPU_MAXCUDADEVS];
  226. static int opencl_workers[STARPU_MAXOPENCLDEVS];
  227. static int n_cpus = -1;
  228. static int n_cudas = -1;
  229. static int n_opencls = -1;
  230. if (n_cpus == -1) /* First time here */
  231. {
  232. /* XXX Dont check them all at once. */
  233. /* XXX Error checking */
  234. n_cpus = starpu_worker_get_ids_by_type(STARPU_CPU_WORKER,
  235. cpu_workers,
  236. STARPU_MAXCPUS);
  237. n_cudas = starpu_worker_get_ids_by_type(STARPU_CUDA_WORKER,
  238. cuda_workers,
  239. STARPU_MAXCUDADEVS);
  240. n_opencls = starpu_worker_get_ids_by_type(STARPU_OPENCL_WORKER,
  241. opencl_workers,
  242. STARPU_MAXOPENCLDEVS);
  243. }
  244. int workerid;
  245. static struct starpu_codelet_t cl;
  246. cl.nbuffers = 1;
  247. switch (type)
  248. {
  249. case STARPU_CPU_WORKER:
  250. if (id != -1)
  251. {
  252. if (id >= n_cpus)
  253. {
  254. fprintf(stderr, "Not enough CPU workers\n");
  255. return -ENODEV;
  256. }
  257. workerid = *(cpu_workers + id);
  258. }
  259. cl.where = STARPU_CPU;
  260. cl.cpu_func = current_config->cpu_func;
  261. break;
  262. #ifdef STARPU_USE_CUDA
  263. case STARPU_CUDA_WORKER:
  264. if (id != -1)
  265. {
  266. if (id >= n_cudas)
  267. {
  268. fprintf(stderr, "Not enough CUDA workers\n");
  269. return -ENODEV;
  270. }
  271. workerid = cuda_workers[id];
  272. }
  273. cl.where = STARPU_CUDA;
  274. cl.cuda_func = current_config->cuda_func;
  275. break;
  276. #endif /* !STARPU_USE_CUDA */
  277. #ifdef STARPU_USE_OPENCL
  278. case STARPU_OPENCL_WORKER:
  279. if (id != -1)
  280. {
  281. if (id >= n_opencls)
  282. {
  283. fprintf(stderr, "Not enough OpenCL workers\n");
  284. return -ENODEV;
  285. }
  286. workerid = *(opencl_workers + id);
  287. }
  288. cl.where = STARPU_OPENCL;
  289. cl.opencl_func = current_config->opencl_func;
  290. break;
  291. #endif /* ! STARPU_USE_OPENCL */
  292. case STARPU_GORDON_WORKER: /* Not supported */
  293. default:
  294. return -ENODEV;
  295. }
  296. struct starpu_task *task = starpu_task_create();
  297. task->synchronous = 1;
  298. task->cl = &cl;
  299. task->buffers[0].handle = *current_config->handle;
  300. task->buffers[0].mode = STARPU_RW;
  301. if (id != -1)
  302. {
  303. task->execute_on_a_specific_worker = 1;
  304. task->workerid = workerid;
  305. }
  306. factor = -factor;
  307. task->cl_arg = &factor;
  308. task->cl_arg_size = sizeof(&factor);
  309. *taskp = task;
  310. return 0;
  311. }
  312. /*
  313. * <device1>_to_<device2> functions.
  314. * They all create and submit a task that has to be executed on <device2>,
  315. * forcing a copy between <device1> and <device2>.
  316. * XXX : could we sometimes use starp_insert_task() ? It seems hars because we
  317. * need to set the execute_on_a_specific_worker field...
  318. */
  319. #ifdef STARPU_USE_CUDA
  320. static enum exit_code
  321. ram_to_cuda(void)
  322. {
  323. int err;
  324. struct starpu_task *task;
  325. err = create_task(&task, STARPU_CUDA_WORKER, 0);
  326. if (err != 0)
  327. return TASK_CREATION_FAILURE;
  328. err = starpu_task_submit(task);
  329. if (err != 0)
  330. return TASK_SUBMISSION_FAILURE;
  331. fprintf(stderr, "[%s] : %d\n", __func__, current_config->copy_failed);
  332. return current_config->copy_failed;
  333. }
  334. #if HAVE_CUDA_MEMCPY_PEER
  335. static enum exit_code
  336. cuda_to_cuda(void)
  337. {
  338. int err;
  339. struct starpu_task *task;
  340. err = create_task(&task, STARPU_CUDA_WORKER, 1);
  341. if (err != 0)
  342. return TASK_CREATION_FAILURE;
  343. err = starpu_task_submit(task);
  344. if (err != 0)
  345. return TASK_SUBMISSION_FAILURE;
  346. fprintf(stderr, "[%s] : %d\n", __func__, current_config->copy_failed);
  347. return current_config->copy_failed;
  348. }
  349. #endif
  350. static enum exit_code
  351. cuda_to_ram(void)
  352. {
  353. int err;
  354. struct starpu_task *task;
  355. err = create_task(&task, STARPU_CPU_WORKER, -1);
  356. if (err != 0)
  357. return TASK_CREATION_FAILURE;
  358. err = starpu_task_submit(task);
  359. if (err != 0)
  360. return TASK_SUBMISSION_FAILURE;
  361. fprintf(stderr, "[%s] : %d\n", __func__, current_config->copy_failed);
  362. return current_config->copy_failed;
  363. }
  364. #endif /* !STARPU_USE_CUDA */
  365. #ifdef STARPU_USE_OPENCL
  366. static enum exit_code
  367. ram_to_opencl()
  368. {
  369. int err;
  370. struct starpu_task *task;
  371. err = create_task(&task, STARPU_OPENCL_WORKER, 0);
  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. static enum exit_code
  381. opencl_to_ram()
  382. {
  383. int err;
  384. struct starpu_task *task;
  385. err = create_task(&task, STARPU_CPU_WORKER, -1);
  386. if (err != 0)
  387. return TASK_CREATION_FAILURE;
  388. err = starpu_task_submit(task);
  389. if (err != 0)
  390. return TASK_SUBMISSION_FAILURE;
  391. fprintf(stderr, "[%s] : %d\n", __func__, current_config->copy_failed);
  392. return current_config->copy_failed;
  393. }
  394. #endif /* !STARPU_USE_OPENCL */
  395. /* End of the <device1>_to_<device2> functions. */
  396. static void
  397. run_cuda(int async)
  398. {
  399. /* RAM -> CUDA (-> CUDA) -> RAM */
  400. int err;
  401. #ifdef STARPU_USE_CUDA
  402. err = ram_to_cuda();
  403. set_field(&summary, async, CPU_TO_CUDA, err);
  404. /* If this failed, there is no point in continuing. */
  405. if (err != SUCCESS)
  406. return;
  407. #ifdef HAVE_CUDA_MEMCPY_PEER
  408. err = cuda_to_cuda();
  409. set_field(&summary, async, CUDA_TO_CUDA, err);
  410. /* Even if cuda_to_cuda() failed, a valid copy is left on the first
  411. * cuda device, which means we can safely test cuda_to_ram() */
  412. #else
  413. summary.cuda_to_cuda_async = UNTESTED;
  414. #endif /* !HAVE_CUDA_MEMCPY_PEER */
  415. err = cuda_to_ram();
  416. set_field(&summary, async, CUDA_TO_CPU, err);
  417. #endif /* !STARPU_USE_CUDA */
  418. }
  419. static void
  420. run_opencl(int async)
  421. {
  422. /* RAM -> OpenCL -> RAM */
  423. int err;
  424. #if STARPU_USE_OPENCL
  425. err = ram_to_opencl();
  426. set_field(&summary, async, CPU_TO_OPENCL, err);
  427. if (err != SUCCESS)
  428. return;
  429. err = opencl_to_ram();
  430. set_field(&summary, async, OPENCL_TO_CPU, err);
  431. #endif /* !STARPU_USE_OPENCL */
  432. }
  433. static void
  434. run_async(void)
  435. {
  436. run_cuda(1);
  437. run_opencl(1);
  438. }
  439. static void
  440. run_sync(void)
  441. {
  442. starpu_data_handle handle = *current_config->handle;
  443. struct starpu_data_interface_ops *ops = handle->ops;
  444. //struct starpu_data_copy_methods *copy_methods = ops->copy_methods;
  445. // copy_methods->ram_to_cuda_async = NULL;
  446. struct starpu_data_interface_ops *new_ops;
  447. struct starpu_data_copy_methods new_copy_methods;
  448. memcpy(&new_copy_methods,
  449. handle->ops->copy_methods,
  450. sizeof(struct starpu_data_copy_methods));
  451. #ifdef STARPU_USE_CUDA
  452. new_copy_methods.ram_to_cuda_async = NULL;
  453. new_copy_methods.cuda_to_cuda_async = NULL;
  454. new_copy_methods.cuda_to_ram_async = NULL;
  455. #endif /* !STARPU_USE_CUDA */
  456. #ifdef STARPU_USE_OPENCL
  457. new_copy_methods.ram_to_opencl_async = NULL;
  458. new_copy_methods.opencl_to_ram_async = NULL;
  459. #endif /* !STARPU_USE_OPENCL */
  460. handle->ops->copy_methods = &new_copy_methods;
  461. run_cuda(0);
  462. run_opencl(0);
  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. }