complex.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. #endif
  52. #ifdef STARPU_USE_OPENCL
  53. .opencl_funcs = {copy_complex_codelet_opencl, NULL},
  54. #endif
  55. .nbuffers = 2,
  56. .modes = {STARPU_R, STARPU_W},
  57. .can_execute = can_execute,
  58. .name = "cl_copy"
  59. };
  60. #ifdef STARPU_USE_OPENCL
  61. struct starpu_opencl_program opencl_program;
  62. #endif
  63. int main(int argc, char **argv)
  64. {
  65. int ret = 0;
  66. starpu_data_handle_t handle1;
  67. starpu_data_handle_t handle2;
  68. double real = 45.0;
  69. double imaginary = 12.0;
  70. double copy_real = 78.0;
  71. double copy_imaginary = 78.0;
  72. int compare;
  73. int *compare_ptr = &compare;
  74. ret = starpu_init(NULL);
  75. if (ret == -ENODEV) return 77;
  76. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  77. #ifdef STARPU_USE_OPENCL
  78. ret = starpu_opencl_load_opencl_from_file("examples/interface/complex_kernels.cl",
  79. &opencl_program, NULL);
  80. STARPU_CHECK_RETURN_VALUE(ret, "starpu_opencl_load_opencl_from_file");
  81. #endif
  82. starpu_complex_data_register(&handle1, STARPU_MAIN_RAM, &real, &imaginary, 1);
  83. starpu_complex_data_register(&handle2, STARPU_MAIN_RAM, &copy_real, &copy_imaginary, 1);
  84. ret = starpu_task_insert(&cl_display, STARPU_VALUE, "handle1", strlen("handle1"), STARPU_R, handle1, 0);
  85. if (ret == -ENODEV) goto end;
  86. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_insert");
  87. ret = starpu_task_insert(&cl_display, STARPU_VALUE, "handle2", strlen("handle2"), STARPU_R, handle2, 0);
  88. if (ret == -ENODEV) goto end;
  89. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_insert");
  90. ret = starpu_task_insert(&cl_compare,
  91. STARPU_R, handle1,
  92. STARPU_R, handle2,
  93. STARPU_VALUE, &compare_ptr, sizeof(compare_ptr),
  94. 0);
  95. if (ret == -ENODEV) goto end;
  96. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_insert");
  97. starpu_task_wait_for_all();
  98. if (compare != 0)
  99. {
  100. FPRINTF(stderr, "Complex numbers should NOT be similar\n");
  101. goto end;
  102. }
  103. ret = starpu_task_insert(&cl_copy,
  104. STARPU_R, handle1,
  105. STARPU_W, handle2,
  106. 0);
  107. if (ret == -ENODEV) goto end;
  108. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_insert");
  109. ret = starpu_task_insert(&cl_display, STARPU_VALUE, "handle1", strlen("handle1"), STARPU_R, handle1, 0);
  110. if (ret == -ENODEV) goto end;
  111. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_insert");
  112. ret = starpu_task_insert(&cl_display, STARPU_VALUE, "handle2", strlen("handle2"), STARPU_R, handle2, 0);
  113. if (ret == -ENODEV) goto end;
  114. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_insert");
  115. ret = starpu_task_insert(&cl_compare,
  116. STARPU_R, handle1,
  117. STARPU_R, handle2,
  118. STARPU_VALUE, &compare_ptr, sizeof(compare_ptr),
  119. 0);
  120. if (ret == -ENODEV) goto end;
  121. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_insert");
  122. starpu_task_wait_for_all();
  123. if (compare != 1)
  124. {
  125. FPRINTF(stderr, "Complex numbers should be similar\n");
  126. }
  127. end:
  128. #ifdef STARPU_USE_OPENCL
  129. {
  130. int ret2 = starpu_opencl_unload_opencl(&opencl_program);
  131. STARPU_CHECK_RETURN_VALUE(ret2, "starpu_opencl_unload_opencl");
  132. }
  133. #endif
  134. starpu_data_unregister(handle1);
  135. starpu_data_unregister(handle2);
  136. starpu_shutdown();
  137. if (ret == -ENODEV) return 77; else return !compare;
  138. }