multiformat_handle_conversion.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. static void
  30. create_and_submit_tasks(int where, starpu_data_handle_t handles[])
  31. {
  32. FPRINTF(stderr, "***** Starting Task 1\n");
  33. struct starpu_codelet cl =
  34. {
  35. .modes = { STARPU_RW },
  36. .nbuffers = 1,
  37. .where = where
  38. };
  39. #ifdef STARPU_USE_CUDA
  40. if (where & STARPU_CUDA)
  41. cl.cuda_funcs[0] = cuda_func;
  42. #endif
  43. #ifdef STARPU_USE_OPENCL
  44. if (where & STARPU_OPENCL)
  45. cl.opencl_funcs[0] = opencl_func;
  46. #endif
  47. struct starpu_task *task = starpu_task_create();
  48. task->synchronous = SYNCHRONOUS;
  49. task->cl = &cl;
  50. task->handles[0] = handles[0];
  51. assert(starpu_task_submit(task) == 0);
  52. #ifdef STARPU_USE_CPU
  53. FPRINTF(stderr, "***** Starting Task 2\n");
  54. struct starpu_codelet cl2 =
  55. {
  56. .modes = { STARPU_RW },
  57. .cpu_funcs = {cpu_func, NULL},
  58. .nbuffers = 1,
  59. .where = STARPU_CPU,
  60. };
  61. struct starpu_task *task2 = starpu_task_create();
  62. task2->synchronous = SYNCHRONOUS;
  63. task2->cl = &cl2;
  64. task2->handles[0] = handles[1];
  65. assert(starpu_task_submit(task2) == 0);
  66. #endif /* !STARPU_USE_CPU */
  67. FPRINTF(stderr, "***** Starting Task 3\n");
  68. struct starpu_codelet cl3 =
  69. {
  70. .modes = { STARPU_RW, STARPU_RW },
  71. .nbuffers = 2,
  72. .where = where
  73. };
  74. #ifdef STARPU_USE_CUDA
  75. if (where & STARPU_CUDA)
  76. cl3.cuda_funcs[0] = cuda_func;
  77. #endif
  78. #ifdef STARPU_USE_OPENCL
  79. if (where & STARPU_OPENCL)
  80. cl3.opencl_funcs[0] = opencl_func;
  81. #endif
  82. struct starpu_task *task3 = starpu_task_create();
  83. task3->synchronous = SYNCHRONOUS;
  84. task3->cl = &cl3;
  85. task3->handles[0] = handles[0];
  86. task3->handles[1] = handles[1];
  87. assert(starpu_task_submit(task3) == 0);
  88. assert(starpu_task_wait_for_all() == 0);
  89. FPRINTF(stderr, "***** End of all tasks\n");
  90. return;
  91. }
  92. /* XXX Just a little bit of copy/pasta here... */
  93. #ifdef STARPU_USE_CUDA
  94. static int
  95. test_cuda(void)
  96. {
  97. int i;
  98. int vector1[NX];
  99. int vector2[NX];
  100. starpu_data_handle_t handles[2];
  101. for (i = 0; i < NX; i++)
  102. {
  103. vector1[i] = i;
  104. vector2[i] = i;
  105. }
  106. starpu_multiformat_data_register(handles, 0, vector1, NX, &ops);
  107. starpu_multiformat_data_register(handles+1, 0, vector2, NX, &ops);
  108. memset(&global_stats, 0, sizeof(global_stats));
  109. create_and_submit_tasks(STARPU_CUDA, handles);
  110. starpu_data_unregister(handles[0]);
  111. starpu_data_unregister(handles[1]);
  112. #if DEBUG
  113. print_stats(&global_stats);
  114. #endif
  115. struct stats expected_stats;
  116. #ifdef STARPU_USE_CPU
  117. expected_stats.cpu = 1;
  118. #endif /* !STARPU_USE_CPU */
  119. #ifdef STARPU_USE_CUDA
  120. expected_stats.cpu_to_cuda = 2;
  121. expected_stats.cuda_to_cpu = 2;
  122. expected_stats.cpu_to_cuda = 2;
  123. #endif /* !STARPU_USE_CUDA */
  124. return compare_stats(&expected_stats, &global_stats);
  125. }
  126. #endif /* !STARPU_USE_CUDA */
  127. #ifdef STARPU_USE_OPENCL
  128. static int
  129. test_opencl(void)
  130. {
  131. int i;
  132. int vector1[NX];
  133. int vector2[NX];
  134. starpu_data_handle_t handles[2];
  135. for (i = 0; i < NX; i++)
  136. {
  137. vector1[i] = i;
  138. vector2[i] = i;
  139. }
  140. starpu_multiformat_data_register(handles, 0, vector1, NX, &ops);
  141. starpu_multiformat_data_register(handles+1, 0, vector2, NX, &ops);
  142. memset(&global_stats, 0, sizeof(global_stats));
  143. create_and_submit_tasks(STARPU_OPENCL, handles);
  144. starpu_data_unregister(handles[0]);
  145. starpu_data_unregister(handles[1]);
  146. #if DEBUG
  147. print_stats(&global_stats);
  148. #endif
  149. struct stats expected_stats;
  150. #ifdef STARPU_USE_CPU
  151. expected_stats.cpu = 1;
  152. #endif /* !STARPU_USE_CPU */
  153. #ifdef STARPU_USE_OPENCl
  154. expected_stats.cpu_to_opencl = 2;
  155. expected_stats.opencl_to_cpu = 2;
  156. expected_stats.cpu_to_opencl = 2;
  157. #endif /* !STARPU_USE_OPENCL */
  158. return compare_stats(&expected_stats, &global_stats);
  159. }
  160. #endif /* !STARPU_USE_OPENCL */
  161. int
  162. main(void)
  163. {
  164. #ifdef STARPU_USE_CPU
  165. int ret;
  166. struct starpu_conf conf =
  167. {
  168. .ncpus = -1,
  169. .ncuda = 2,
  170. .nopencl = 1
  171. };
  172. ret = starpu_init(&conf);
  173. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  174. unsigned int ncpu = starpu_cpu_worker_get_count();
  175. if (ncpu == 0)
  176. {
  177. FPRINTF(stderr, "No CPUS, cannot run this test.\n");
  178. return STARPU_TEST_SKIPPED;
  179. }
  180. unsigned int ncuda = starpu_cuda_worker_get_count();
  181. unsigned int nopencl = starpu_opencl_worker_get_count();
  182. #ifdef STARPU_USE_OPENCL
  183. if (nopencl > 0 && test_opencl() != 0)
  184. {
  185. FPRINTF(stderr, "OPENCL FAILED\n");
  186. return EXIT_FAILURE;
  187. }
  188. #endif
  189. #ifdef STARPU_USE_CUDA
  190. if (ncuda > 0 && test_cuda() != 0)
  191. {
  192. FPRINTF(stderr, "CUDA FAILED \n");
  193. return EXIT_FAILURE;
  194. }
  195. #endif
  196. starpu_shutdown();
  197. if (ncuda == 0 && nopencl == 0)
  198. return STARPU_TEST_SKIPPED;
  199. else
  200. return EXIT_SUCCESS;
  201. #else /* !STARPU_USE_CPU */
  202. /* Without the CPU, there is no point in using the multiformat
  203. * interface, so this test is pointless. */
  204. return STARPU_TEST_SKIPPED;
  205. #endif
  206. }