multiformat_handle_conversion.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2011-2012 INRIA
  4. * Copyright (C) 2011, 2012 Centre National de la Recherche Scientifique
  5. *
  6. * StarPU is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation; either version 2.1 of the License, or (at
  9. * your option) any later version.
  10. *
  11. * StarPU is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. *
  15. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  16. */
  17. #include <starpu.h>
  18. #include "generic.h"
  19. #include "../../../../helper.h"
  20. #define DEBUG 0
  21. #if DEBUG
  22. #define SYNCHRONOUS 1 /* Easier to debug with synchronous tasks */
  23. #define ENTER() do { FPRINTF(stderr, "Entering %s\n", __func__); } while (0)
  24. #else
  25. #define SYNCHRONOUS 0
  26. #define ENTER()
  27. #endif
  28. extern struct stats global_stats;
  29. #if defined(STARPU_USE_CUDA) || defined(STARPU_USE_OPENCL)
  30. static void
  31. create_and_submit_tasks(int where, starpu_data_handle_t handles[])
  32. {
  33. FPRINTF(stderr, "***** Starting Task 1\n");
  34. struct starpu_codelet cl =
  35. {
  36. .modes = { STARPU_RW },
  37. .nbuffers = 1,
  38. .where = where
  39. };
  40. #ifdef STARPU_USE_CUDA
  41. if (where & STARPU_CUDA)
  42. cl.cuda_funcs[0] = cuda_func;
  43. #endif
  44. #ifdef STARPU_USE_OPENCL
  45. if (where & STARPU_OPENCL)
  46. cl.opencl_funcs[0] = opencl_func;
  47. #endif
  48. struct starpu_task *task = starpu_task_create();
  49. task->synchronous = SYNCHRONOUS;
  50. task->cl = &cl;
  51. task->handles[0] = handles[0];
  52. assert(starpu_task_submit(task) == 0);
  53. #ifdef STARPU_USE_CPU
  54. FPRINTF(stderr, "***** Starting Task 2\n");
  55. struct starpu_codelet cl2 =
  56. {
  57. .modes = { STARPU_RW },
  58. .cpu_funcs = {cpu_func, NULL},
  59. .nbuffers = 1,
  60. .where = STARPU_CPU,
  61. };
  62. struct starpu_task *task2 = starpu_task_create();
  63. task2->synchronous = SYNCHRONOUS;
  64. task2->cl = &cl2;
  65. task2->handles[0] = handles[1];
  66. assert(starpu_task_submit(task2) == 0);
  67. #endif /* !STARPU_USE_CPU */
  68. FPRINTF(stderr, "***** Starting Task 3\n");
  69. struct starpu_codelet cl3 =
  70. {
  71. .modes = { STARPU_RW, STARPU_RW },
  72. .nbuffers = 2,
  73. .where = where
  74. };
  75. #ifdef STARPU_USE_CUDA
  76. if (where & STARPU_CUDA)
  77. cl3.cuda_funcs[0] = cuda_func;
  78. #endif
  79. #ifdef STARPU_USE_OPENCL
  80. if (where & STARPU_OPENCL)
  81. cl3.opencl_funcs[0] = opencl_func;
  82. #endif
  83. struct starpu_task *task3 = starpu_task_create();
  84. task3->synchronous = SYNCHRONOUS;
  85. task3->cl = &cl3;
  86. task3->handles[0] = handles[0];
  87. task3->handles[1] = handles[1];
  88. assert(starpu_task_submit(task3) == 0);
  89. assert(starpu_task_wait_for_all() == 0);
  90. FPRINTF(stderr, "***** End of all tasks\n");
  91. return;
  92. }
  93. #endif
  94. /* XXX Just a little bit of copy/pasta here... */
  95. #ifdef STARPU_USE_CUDA
  96. static int
  97. test_cuda(void)
  98. {
  99. int i;
  100. int vector1[NX];
  101. int vector2[NX];
  102. starpu_data_handle_t handles[2];
  103. for (i = 0; i < NX; i++)
  104. {
  105. vector1[i] = i;
  106. vector2[i] = i;
  107. }
  108. starpu_multiformat_data_register(&handles[0], 0, vector1, NX, &ops);
  109. starpu_multiformat_data_register(&handles[1], 0, vector2, NX, &ops);
  110. memset(&global_stats, 0, sizeof(global_stats));
  111. create_and_submit_tasks(STARPU_CUDA, handles);
  112. starpu_data_unregister(handles[0]);
  113. starpu_data_unregister(handles[1]);
  114. #if DEBUG
  115. print_stats(&global_stats);
  116. #endif
  117. struct stats expected_stats;
  118. #ifdef STARPU_USE_CPU
  119. expected_stats.cpu = 1;
  120. #endif /* !STARPU_USE_CPU */
  121. #ifdef STARPU_USE_OPENCL
  122. expected_stats.opencl = 0;
  123. expected_stats.cpu_to_opencl = 0;
  124. expected_stats.opencl_to_cpu = 0;
  125. #endif /* !STARPU_USE_OPENCL */
  126. expected_stats.cuda = 2;
  127. expected_stats.cpu_to_cuda = 2;
  128. expected_stats.cuda_to_cpu = 2;
  129. return compare_stats(&expected_stats, &global_stats);
  130. }
  131. #endif /* !STARPU_USE_CUDA */
  132. #ifdef STARPU_USE_OPENCL
  133. static int
  134. test_opencl(void)
  135. {
  136. int i;
  137. int vector1[NX];
  138. int vector2[NX];
  139. starpu_data_handle_t handles[2];
  140. for (i = 0; i < NX; i++)
  141. {
  142. vector1[i] = i;
  143. vector2[i] = i;
  144. }
  145. starpu_multiformat_data_register(&handles[0], 0, vector1, NX, &ops);
  146. starpu_multiformat_data_register(&handles[1], 0, vector2, NX, &ops);
  147. memset(&global_stats, 0, sizeof(global_stats));
  148. create_and_submit_tasks(STARPU_OPENCL, handles);
  149. starpu_data_unregister(handles[0]);
  150. starpu_data_unregister(handles[1]);
  151. #if DEBUG
  152. print_stats(&global_stats);
  153. #endif
  154. struct stats expected_stats;
  155. #ifdef STARPU_USE_CPU
  156. expected_stats.cpu = 1;
  157. #endif /* !STARPU_USE_CPU */
  158. #ifdef STARPU_USE_CUDA
  159. expected_stats.cuda = 0;
  160. expected_stats.cpu_to_cuda = 0;
  161. expected_stats.cuda_to_cpu = 0;
  162. #endif /* !STARPU_USE_CUDA */
  163. expected_stats.opencl = 2;
  164. expected_stats.cpu_to_opencl = 2;
  165. expected_stats.opencl_to_cpu = 2;
  166. return compare_stats(&expected_stats, &global_stats);
  167. }
  168. #endif /* !STARPU_USE_OPENCL */
  169. int
  170. main(void)
  171. {
  172. #ifdef STARPU_USE_CPU
  173. int ret;
  174. struct starpu_conf conf;
  175. starpu_conf_init(&conf);
  176. conf.ncuda = 2;
  177. conf.nopencl = 1;
  178. ret = starpu_init(&conf);
  179. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  180. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  181. unsigned int ncpu = starpu_cpu_worker_get_count();
  182. if (ncpu == 0)
  183. {
  184. FPRINTF(stderr, "No CPUS, cannot run this test.\n");
  185. return STARPU_TEST_SKIPPED;
  186. }
  187. unsigned int ncuda = starpu_cuda_worker_get_count();
  188. unsigned int nopencl = starpu_opencl_worker_get_count();
  189. #ifdef STARPU_USE_OPENCL
  190. if (nopencl > 0 && test_opencl() != 0)
  191. {
  192. FPRINTF(stderr, "OPENCL FAILED\n");
  193. return EXIT_FAILURE;
  194. }
  195. #endif
  196. #ifdef STARPU_USE_CUDA
  197. if (ncuda > 0 && test_cuda() != 0)
  198. {
  199. FPRINTF(stderr, "CUDA FAILED \n");
  200. return EXIT_FAILURE;
  201. }
  202. #endif
  203. starpu_shutdown();
  204. if (ncuda == 0 && nopencl == 0)
  205. return STARPU_TEST_SKIPPED;
  206. else
  207. return EXIT_SUCCESS;
  208. #else /* !STARPU_USE_CPU */
  209. /* Without the CPU, there is no point in using the multiformat
  210. * interface, so this test is pointless. */
  211. return STARPU_TEST_SKIPPED;
  212. #endif
  213. }