multiformat_cuda_opencl.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2011 INRIA
  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. #include "generic.h"
  18. #include "../../../../helper.h"
  19. #if defined(STARPU_USE_CUDA) && defined(STARPU_USE_OPENCL)
  20. extern struct stats global_stats;
  21. static int vector[NX];
  22. static starpu_data_handle_t handle;
  23. /*
  24. * Initially, our vector should be in RAM. It is then used on a CUDA device,
  25. * then on an OpenCL device, and finally, on a CUDA device again.
  26. * The following operations should be performed, in this specific order :
  27. * - CPU -> CUDA conversion
  28. * - CUDA kernel execution
  29. * - OpenCL kernel execution
  30. * - CUDA kernel execution
  31. * - CUDA -> CPU conversion
  32. *
  33. * Note that we will not run any conversion between CUDA and OpenCL, because
  34. * StarPU assumes that the data structures used on CUDA and OpenCL devices are
  35. * the same.
  36. */
  37. static int
  38. test(void)
  39. {
  40. int ret;
  41. struct starpu_task *task_cuda, *task_cuda2, *task_opencl;
  42. static struct starpu_codelet cl_cuda =
  43. {
  44. .cuda_funcs = {cuda_func},
  45. .nbuffers = 1,
  46. .modes = {STARPU_RW}
  47. };
  48. task_cuda = starpu_task_create();
  49. task_cuda->cl = &cl_cuda;
  50. task_cuda->handles[0] = handle;
  51. ret = starpu_task_submit(task_cuda);
  52. if (ret != 0)
  53. {
  54. task_cuda->destroy = 0;
  55. starpu_task_destroy(task_cuda);
  56. return 1;
  57. }
  58. static struct starpu_codelet cl_opencl =
  59. {
  60. .opencl_funcs = {opencl_func},
  61. .nbuffers = 1,
  62. .modes = {STARPU_RW}
  63. };
  64. task_opencl = starpu_task_create();
  65. task_opencl->cl = &cl_opencl;
  66. task_opencl->handles[0] = handle;
  67. ret = starpu_task_submit(task_opencl);
  68. if (ret != 0)
  69. {
  70. task_opencl->destroy = 0;
  71. starpu_task_destroy(task_opencl);
  72. return 1;
  73. }
  74. task_cuda2 = starpu_task_create();
  75. task_cuda2->cl = &cl_cuda;
  76. task_cuda2->handles[0] = handle;
  77. ret = starpu_task_submit(task_cuda2);
  78. if (ret != 0)
  79. {
  80. task_cuda2->destroy = 0;
  81. starpu_task_destroy(task_cuda2);
  82. return 1;
  83. }
  84. return 0;
  85. }
  86. static void
  87. register_handle(void)
  88. {
  89. int i;
  90. for (i = 0; i < NX; i++)
  91. vector[i] = i;
  92. starpu_multiformat_data_register(&handle, STARPU_MAIN_RAM, vector, NX, &ops);
  93. }
  94. static void
  95. unregister_handle(void)
  96. {
  97. starpu_data_unregister(handle);
  98. }
  99. #endif /* !(STARPU_USE_CUDA && STARPU_USE_OPENCL) */
  100. int
  101. main(void)
  102. {
  103. #if defined(STARPU_USE_CUDA) && defined(STARPU_USE_OPENCL)
  104. int ret;
  105. struct starpu_conf conf;
  106. starpu_conf_init(&conf);
  107. conf.ncuda = 1;
  108. conf.nopencl = 1;
  109. ret = starpu_init(&conf);
  110. if (ret == -ENODEV)
  111. goto enodev;
  112. reset_stats(&global_stats);
  113. register_handle();
  114. ret = test();
  115. unregister_handle();
  116. starpu_shutdown();
  117. if (ret != 0)
  118. return STARPU_TEST_SKIPPED;
  119. struct stats expected_stats =
  120. {
  121. #ifdef STARPU_USE_CPU
  122. .cpu = 0,
  123. #endif
  124. #ifdef STARPU_USE_CUDA
  125. .cuda = 2,
  126. .cpu_to_cuda = 1,
  127. .cuda_to_cpu = 1,
  128. #endif
  129. #ifdef STARPU_USE_OPENCL
  130. .opencl = 1,
  131. .cpu_to_opencl = 0,
  132. .opencl_to_cpu = 0
  133. #endif
  134. };
  135. ret = compare_stats(&global_stats, &expected_stats);
  136. if (ret != 0)
  137. {
  138. print_stats(&global_stats);
  139. print_stats(&expected_stats);
  140. return EXIT_FAILURE;
  141. }
  142. return EXIT_SUCCESS;
  143. enodev:
  144. return STARPU_TEST_SKIPPED;
  145. #else
  146. return STARPU_TEST_SKIPPED;
  147. #endif
  148. }