multiformat_cuda_opencl.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2011-2013 Inria
  4. * Copyright (C) 2011-2013,2015,2017 CNRS
  5. * Copyright (C) 2011,2012,2014,2016 Université de Bordeaux
  6. *
  7. * StarPU is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation; either version 2.1 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * StarPU is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. *
  16. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  17. */
  18. #include <starpu.h>
  19. #include "generic.h"
  20. #include "../../../../helper.h"
  21. #if defined(STARPU_USE_CUDA) && defined(STARPU_USE_OPENCL)
  22. extern struct stats global_stats;
  23. static int vector[NX];
  24. static starpu_data_handle_t handle;
  25. /*
  26. * Initially, our vector should be in RAM. It is then used on a CUDA device,
  27. * then on an OpenCL device, and finally, on a CUDA device again.
  28. * The following operations should be performed, in this specific order :
  29. * - CPU -> CUDA conversion
  30. * - CUDA kernel execution
  31. * - OpenCL kernel execution
  32. * - CUDA kernel execution
  33. * - CUDA -> CPU conversion
  34. *
  35. * Note that we will not run any conversion between CUDA and OpenCL, because
  36. * StarPU assumes that the data structures used on CUDA and OpenCL devices are
  37. * the same.
  38. */
  39. static int
  40. test(void)
  41. {
  42. int ret;
  43. struct starpu_task *task_cuda, *task_cuda2, *task_opencl;
  44. static struct starpu_codelet cl_cuda =
  45. {
  46. .cuda_funcs = {cuda_func},
  47. .nbuffers = 1,
  48. .modes = {STARPU_RW}
  49. };
  50. task_cuda = starpu_task_create();
  51. task_cuda->cl = &cl_cuda;
  52. task_cuda->handles[0] = handle;
  53. ret = starpu_task_submit(task_cuda);
  54. if (ret != 0)
  55. {
  56. task_cuda->destroy = 0;
  57. starpu_task_destroy(task_cuda);
  58. return 1;
  59. }
  60. static struct starpu_codelet cl_opencl =
  61. {
  62. .opencl_funcs = {opencl_func},
  63. .nbuffers = 1,
  64. .modes = {STARPU_RW}
  65. };
  66. task_opencl = starpu_task_create();
  67. task_opencl->cl = &cl_opencl;
  68. task_opencl->handles[0] = handle;
  69. ret = starpu_task_submit(task_opencl);
  70. if (ret != 0)
  71. {
  72. task_opencl->destroy = 0;
  73. starpu_task_destroy(task_opencl);
  74. return 1;
  75. }
  76. task_cuda2 = starpu_task_create();
  77. task_cuda2->cl = &cl_cuda;
  78. task_cuda2->handles[0] = handle;
  79. ret = starpu_task_submit(task_cuda2);
  80. if (ret != 0)
  81. {
  82. task_cuda2->destroy = 0;
  83. starpu_task_destroy(task_cuda2);
  84. return 1;
  85. }
  86. return 0;
  87. }
  88. static void
  89. register_handle(void)
  90. {
  91. int i;
  92. for (i = 0; i < NX; i++)
  93. vector[i] = i;
  94. starpu_multiformat_data_register(&handle, STARPU_MAIN_RAM, vector, NX, &ops);
  95. }
  96. static void
  97. unregister_handle(void)
  98. {
  99. starpu_data_unregister(handle);
  100. }
  101. #endif /* !(STARPU_USE_CUDA && STARPU_USE_OPENCL) */
  102. int
  103. main(void)
  104. {
  105. #if defined(STARPU_USE_CUDA) && defined(STARPU_USE_OPENCL)
  106. int ret;
  107. struct starpu_conf conf;
  108. starpu_conf_init(&conf);
  109. conf.ncuda = 1;
  110. conf.nopencl = 1;
  111. ret = starpu_init(&conf);
  112. if (ret == -ENODEV)
  113. goto enodev;
  114. reset_stats(&global_stats);
  115. register_handle();
  116. ret = test();
  117. unregister_handle();
  118. starpu_shutdown();
  119. if (ret != 0)
  120. return STARPU_TEST_SKIPPED;
  121. struct stats expected_stats =
  122. {
  123. #ifdef STARPU_USE_CPU
  124. .cpu = 0,
  125. #endif
  126. #ifdef STARPU_USE_CUDA
  127. .cuda = 2,
  128. .cpu_to_cuda = 1,
  129. .cuda_to_cpu = 1,
  130. #endif
  131. #ifdef STARPU_USE_OPENCL
  132. .opencl = 1,
  133. .cpu_to_opencl = 0,
  134. .opencl_to_cpu = 0
  135. #endif
  136. };
  137. ret = compare_stats(&global_stats, &expected_stats);
  138. if (ret != 0)
  139. {
  140. print_stats(&global_stats);
  141. print_stats(&expected_stats);
  142. return EXIT_FAILURE;
  143. }
  144. return EXIT_SUCCESS;
  145. enodev:
  146. return STARPU_TEST_SKIPPED;
  147. #else
  148. return STARPU_TEST_SKIPPED;
  149. #endif
  150. }