multiformat_cuda_opencl.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 = {cuda_func, NULL},
  46. .nbuffers = 1
  47. };
  48. task_cuda = starpu_task_create();
  49. task_cuda->cl = &cl_cuda;
  50. task_cuda->buffers[0].handle = handle;
  51. task_cuda->buffers[0].mode = STARPU_RW;
  52. ret = starpu_task_submit(task_cuda);
  53. if (ret != 0)
  54. return 1;
  55. static struct starpu_codelet cl_opencl =
  56. {
  57. .where = STARPU_OPENCL,
  58. .opencl_funcs = {opencl_func, NULL},
  59. .nbuffers = 1
  60. };
  61. task_opencl = starpu_task_create();
  62. task_opencl->cl = &cl_opencl;
  63. task_opencl->buffers[0].handle = handle;
  64. task_opencl->buffers[0].mode = STARPU_RW;
  65. ret = starpu_task_submit(task_opencl);
  66. if (ret != 0)
  67. return 1;
  68. task_cuda2 = starpu_task_create();
  69. task_cuda2->cl = &cl_cuda;
  70. task_cuda2->buffers[0].handle = handle;
  71. task_cuda2->buffers[0].mode = STARPU_RW;
  72. ret = starpu_task_submit(task_cuda2);
  73. if (ret != 0)
  74. return 1;
  75. return 0;
  76. }
  77. #endif /* !(STARPU_USE_CUDA && STARPU_USE_OPENCL) */
  78. static void
  79. register_handle(void)
  80. {
  81. int i;
  82. for (i = 0; i < NX; i++)
  83. vector[i] = i;
  84. starpu_multiformat_data_register(&handle, 0, vector, NX, &ops);
  85. }
  86. static void
  87. unregister_handle(void)
  88. {
  89. starpu_data_unregister(handle);
  90. }
  91. int
  92. main(void)
  93. {
  94. #if defined(STARPU_USE_CUDA) && defined(STARPU_USE_OPENCL)
  95. int ret;
  96. struct starpu_conf conf =
  97. {
  98. .ncpus = -1,
  99. .ncuda = 1,
  100. .nopencl = 1
  101. };
  102. ret = starpu_init(&conf);
  103. if (ret == -ENODEV)
  104. goto enodev;
  105. reset_stats(&global_stats);
  106. register_handle();
  107. ret = test();
  108. unregister_handle();
  109. starpu_shutdown();
  110. if (ret != 0)
  111. return STARPU_TEST_SKIPPED;
  112. struct stats expected_stats =
  113. {
  114. #ifdef STARPU_USE_CPU
  115. .cpu = 0,
  116. #endif
  117. #ifdef STARPU_USE_CUDA
  118. .cuda = 2,
  119. .cpu_to_cuda = 1,
  120. .cuda_to_cpu = 1,
  121. #endif
  122. #ifdef STARPU_USE_OPENCL
  123. .opencl = 1,
  124. .cpu_to_opencl = 0,
  125. .opencl_to_cpu = 0
  126. #endif
  127. };
  128. ret = compare_stats(&global_stats, &expected_stats);
  129. if (ret != 0)
  130. {
  131. print_stats(&global_stats);
  132. print_stats(&expected_stats);
  133. return EXIT_FAILURE;
  134. }
  135. return EXIT_SUCCESS;
  136. enodev:
  137. return STARPU_TEST_SKIPPED;
  138. #else
  139. return STARPU_TEST_SKIPPED;
  140. #endif
  141. }