multiformat_cuda_opencl.c 3.6 KB

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