complex.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2012,2013 Inria
  4. * Copyright (C) 2012,2013,2015,2017 CNRS
  5. * Copyright (C) 2013,2014 Université de Bordeaux
  6. *
  7. * StarPU is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation; either version 2.1 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * StarPU is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. *
  16. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  17. */
  18. #include <starpu.h>
  19. #include "complex_interface.h"
  20. #include "complex_codelet.h"
  21. static int can_execute(unsigned workerid, struct starpu_task *task, unsigned nimpl)
  22. {
  23. (void) task;
  24. (void) nimpl;
  25. if (starpu_worker_get_type(workerid) == STARPU_OPENCL_WORKER)
  26. return 1;
  27. #ifdef STARPU_USE_CUDA
  28. #ifdef STARPU_SIMGRID
  29. /* We don't know, let's assume it can */
  30. return 1;
  31. #else
  32. /* Cuda device */
  33. const struct cudaDeviceProp *props;
  34. props = starpu_cuda_get_device_properties(workerid);
  35. if (props->major >= 2 || props->minor >= 3)
  36. {
  37. /* At least compute capability 1.3, supports doubles */
  38. return 1;
  39. }
  40. else
  41. {
  42. /* Old card does not support doubles */
  43. return 0;
  44. }
  45. #endif
  46. #else
  47. return 1;
  48. #endif
  49. }
  50. #ifdef STARPU_USE_CUDA
  51. extern void copy_complex_codelet_cuda(void *descr[], void *_args);
  52. #endif
  53. #ifdef STARPU_USE_OPENCL
  54. extern void copy_complex_codelet_opencl(void *buffers[], void *args);
  55. #endif
  56. struct starpu_codelet cl_copy =
  57. {
  58. #ifdef STARPU_USE_CUDA
  59. .cuda_funcs = {copy_complex_codelet_cuda},
  60. .cuda_flags = {STARPU_CUDA_ASYNC},
  61. #endif
  62. #ifdef STARPU_USE_OPENCL
  63. .opencl_funcs = {copy_complex_codelet_opencl},
  64. .opencl_flags = {STARPU_OPENCL_ASYNC},
  65. #endif
  66. .nbuffers = 2,
  67. .modes = {STARPU_R, STARPU_W},
  68. .can_execute = can_execute,
  69. .name = "cl_copy"
  70. };
  71. #ifdef STARPU_USE_OPENCL
  72. struct starpu_opencl_program opencl_program;
  73. #endif
  74. int main(void)
  75. {
  76. int ret = 0;
  77. starpu_data_handle_t handle1;
  78. starpu_data_handle_t handle2;
  79. double real = 45.0;
  80. double imaginary = 12.0;
  81. double copy_real = 78.0;
  82. double copy_imaginary = 78.0;
  83. int compare;
  84. int *compare_ptr = &compare;
  85. ret = starpu_init(NULL);
  86. if (ret == -ENODEV) return 77;
  87. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  88. #ifdef STARPU_USE_OPENCL
  89. ret = starpu_opencl_load_opencl_from_file("examples/interface/complex_kernels.cl",
  90. &opencl_program, NULL);
  91. STARPU_CHECK_RETURN_VALUE(ret, "starpu_opencl_load_opencl_from_file");
  92. #endif
  93. starpu_complex_data_register(&handle1, STARPU_MAIN_RAM, &real, &imaginary, 1);
  94. starpu_complex_data_register(&handle2, STARPU_MAIN_RAM, &copy_real, &copy_imaginary, 1);
  95. ret = starpu_task_insert(&cl_display, STARPU_VALUE, "handle1", strlen("handle1")+1, STARPU_R, handle1, 0);
  96. if (ret == -ENODEV) goto end;
  97. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_insert");
  98. ret = starpu_task_insert(&cl_display, STARPU_VALUE, "handle2", strlen("handle2")+1, STARPU_R, handle2, 0);
  99. if (ret == -ENODEV) goto end;
  100. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_insert");
  101. ret = starpu_task_insert(&cl_compare,
  102. STARPU_R, handle1,
  103. STARPU_R, handle2,
  104. STARPU_VALUE, &compare_ptr, sizeof(compare_ptr),
  105. 0);
  106. if (ret == -ENODEV) goto end;
  107. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_insert");
  108. starpu_task_wait_for_all();
  109. if (compare != 0)
  110. {
  111. FPRINTF(stderr, "Complex numbers should NOT be similar\n");
  112. goto end;
  113. }
  114. ret = starpu_task_insert(&cl_copy,
  115. STARPU_R, handle1,
  116. STARPU_W, handle2,
  117. 0);
  118. if (ret == -ENODEV) goto end;
  119. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_insert");
  120. ret = starpu_task_insert(&cl_display, STARPU_VALUE, "handle1", strlen("handle1")+1, STARPU_R, handle1, 0);
  121. if (ret == -ENODEV) goto end;
  122. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_insert");
  123. ret = starpu_task_insert(&cl_display, STARPU_VALUE, "handle2", strlen("handle2")+1, STARPU_R, handle2, 0);
  124. if (ret == -ENODEV) goto end;
  125. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_insert");
  126. ret = starpu_task_insert(&cl_compare,
  127. STARPU_R, handle1,
  128. STARPU_R, handle2,
  129. STARPU_VALUE, &compare_ptr, sizeof(compare_ptr),
  130. 0);
  131. if (ret == -ENODEV) goto end;
  132. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_insert");
  133. starpu_task_wait_for_all();
  134. if (compare != 1)
  135. {
  136. FPRINTF(stderr, "Complex numbers should be similar\n");
  137. }
  138. end:
  139. #ifdef STARPU_USE_OPENCL
  140. {
  141. int ret2 = starpu_opencl_unload_opencl(&opencl_program);
  142. STARPU_CHECK_RETURN_VALUE(ret2, "starpu_opencl_unload_opencl");
  143. }
  144. #endif
  145. starpu_data_unregister(handle1);
  146. starpu_data_unregister(handle2);
  147. starpu_shutdown();
  148. if (ret == -ENODEV) return 77; else return !compare;
  149. }