multiformat_cuda_opencl.c 3.5 KB

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