multiformat_handle_conversion.c 4.9 KB

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