complex.c 4.8 KB

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