complex.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2012, 2013 Centre National de la Recherche Scientifique
  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 "complex_interface.h"
  18. #include "complex_codelet.h"
  19. static int can_execute(unsigned workerid, struct starpu_task *task, unsigned nimpl)
  20. {
  21. if (starpu_worker_get_type(workerid) == STARPU_OPENCL_WORKER)
  22. return 1;
  23. #ifdef STARPU_USE_CUDA
  24. /* Cuda device */
  25. const struct cudaDeviceProp *props;
  26. props = starpu_cuda_get_device_properties(workerid);
  27. if (props->major >= 2 || props->minor >= 3)
  28. {
  29. /* At least compute capability 1.3, supports doubles */
  30. return 1;
  31. }
  32. else
  33. {
  34. /* Old card does not support doubles */
  35. return 0;
  36. }
  37. #else
  38. return 1;
  39. #endif
  40. }
  41. #ifdef STARPU_USE_CUDA
  42. extern void copy_complex_codelet_cuda(void *descr[], STARPU_ATTRIBUTE_UNUSED void *_args);
  43. #endif
  44. #ifdef STARPU_USE_OPENCL
  45. extern void copy_complex_codelet_opencl(void *buffers[], void *args);
  46. #endif
  47. struct starpu_codelet cl_copy =
  48. {
  49. #ifdef STARPU_USE_CUDA
  50. .cuda_funcs = {copy_complex_codelet_cuda, NULL},
  51. .cuda_flags = {STARPU_CUDA_ASYNC},
  52. #endif
  53. #ifdef STARPU_USE_OPENCL
  54. .opencl_funcs = {copy_complex_codelet_opencl, NULL},
  55. .opencl_flags = {STARPU_OPENCL_ASYNC},
  56. #endif
  57. .nbuffers = 2,
  58. .modes = {STARPU_R, STARPU_W},
  59. .can_execute = can_execute,
  60. .name = "cl_copy"
  61. };
  62. #ifdef STARPU_USE_OPENCL
  63. struct starpu_opencl_program opencl_program;
  64. #endif
  65. int main(int argc, char **argv)
  66. {
  67. int ret = 0;
  68. starpu_data_handle_t handle1;
  69. starpu_data_handle_t handle2;
  70. double real = 45.0;
  71. double imaginary = 12.0;
  72. double copy_real = 78.0;
  73. double copy_imaginary = 78.0;
  74. int compare;
  75. int *compare_ptr = &compare;
  76. ret = starpu_init(NULL);
  77. if (ret == -ENODEV) return 77;
  78. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  79. #ifdef STARPU_USE_OPENCL
  80. ret = starpu_opencl_load_opencl_from_file("examples/interface/complex_kernels.cl",
  81. &opencl_program, NULL);
  82. STARPU_CHECK_RETURN_VALUE(ret, "starpu_opencl_load_opencl_from_file");
  83. #endif
  84. starpu_complex_data_register(&handle1, STARPU_MAIN_RAM, &real, &imaginary, 1);
  85. starpu_complex_data_register(&handle2, STARPU_MAIN_RAM, &copy_real, &copy_imaginary, 1);
  86. ret = starpu_task_insert(&cl_display, STARPU_VALUE, "handle1", strlen("handle1")+1, STARPU_R, handle1, 0);
  87. if (ret == -ENODEV) goto end;
  88. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_insert");
  89. ret = starpu_task_insert(&cl_display, STARPU_VALUE, "handle2", strlen("handle2")+1, STARPU_R, handle2, 0);
  90. if (ret == -ENODEV) goto end;
  91. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_insert");
  92. ret = starpu_task_insert(&cl_compare,
  93. STARPU_R, handle1,
  94. STARPU_R, handle2,
  95. STARPU_VALUE, &compare_ptr, sizeof(compare_ptr),
  96. 0);
  97. if (ret == -ENODEV) goto end;
  98. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_insert");
  99. starpu_task_wait_for_all();
  100. if (compare != 0)
  101. {
  102. FPRINTF(stderr, "Complex numbers should NOT be similar\n");
  103. goto end;
  104. }
  105. ret = starpu_task_insert(&cl_copy,
  106. STARPU_R, handle1,
  107. STARPU_W, handle2,
  108. 0);
  109. if (ret == -ENODEV) goto end;
  110. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_insert");
  111. ret = starpu_task_insert(&cl_display, STARPU_VALUE, "handle1", strlen("handle1")+1, STARPU_R, handle1, 0);
  112. if (ret == -ENODEV) goto end;
  113. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_insert");
  114. ret = starpu_task_insert(&cl_display, STARPU_VALUE, "handle2", strlen("handle2")+1, STARPU_R, handle2, 0);
  115. if (ret == -ENODEV) goto end;
  116. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_insert");
  117. ret = starpu_task_insert(&cl_compare,
  118. STARPU_R, handle1,
  119. STARPU_R, handle2,
  120. STARPU_VALUE, &compare_ptr, sizeof(compare_ptr),
  121. 0);
  122. if (ret == -ENODEV) goto end;
  123. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_insert");
  124. starpu_task_wait_for_all();
  125. if (compare != 1)
  126. {
  127. FPRINTF(stderr, "Complex numbers should be similar\n");
  128. }
  129. end:
  130. #ifdef STARPU_USE_OPENCL
  131. {
  132. int ret2 = starpu_opencl_unload_opencl(&opencl_program);
  133. STARPU_CHECK_RETURN_VALUE(ret2, "starpu_opencl_unload_opencl");
  134. }
  135. #endif
  136. starpu_data_unregister(handle1);
  137. starpu_data_unregister(handle2);
  138. starpu_shutdown();
  139. if (ret == -ENODEV) return 77; else return !compare;
  140. }