multiformat_cuda_opencl.c 3.5 KB

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