complex.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2012 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. #ifdef STARPU_USE_OPENCL
  19. #include <starpu_opencl.h>
  20. #endif
  21. #ifdef STARPU_USE_CUDA
  22. extern void copy_complex_codelet_cuda(void *descr[], __attribute__ ((unused)) void *_args);
  23. #endif
  24. #ifdef STARPU_USE_OPENCL
  25. extern void copy_complex_codelet_opencl(void *buffers[], void *args);
  26. #endif
  27. void compare_complex_codelet(void *descr[], __attribute__ ((unused)) void *_args)
  28. {
  29. int nx1 = STARPU_COMPLEX_GET_NX(descr[0]);
  30. double *real1 = STARPU_COMPLEX_GET_REAL(descr[0]);
  31. double *imaginary1 = STARPU_COMPLEX_GET_IMAGINARY(descr[0]);
  32. int nx2 = STARPU_COMPLEX_GET_NX(descr[1]);
  33. double *real2 = STARPU_COMPLEX_GET_REAL(descr[1]);
  34. double *imaginary2 = STARPU_COMPLEX_GET_IMAGINARY(descr[1]);
  35. int compare = (nx1 == nx2);
  36. if (nx1 == nx2)
  37. {
  38. int i;
  39. for(i=0 ; i<nx1 ; i++)
  40. {
  41. if (real1[i] != real2[i] || imaginary1[i] != imaginary2[i])
  42. {
  43. compare = 0;
  44. break;
  45. }
  46. }
  47. }
  48. fprintf(stderr, "Complex numbers are%s similar\n", compare==0 ? " NOT" : "");
  49. }
  50. void display_complex_codelet(void *descr[], __attribute__ ((unused)) void *_args)
  51. {
  52. int nx = STARPU_COMPLEX_GET_NX(descr[0]);
  53. double *real = STARPU_COMPLEX_GET_REAL(descr[0]);
  54. double *imaginary = STARPU_COMPLEX_GET_IMAGINARY(descr[0]);
  55. int i;
  56. for(i=0 ; i<nx ; i++)
  57. {
  58. fprintf(stderr, "Complex[%d] = %3.2f + %3.2f i\n", i, real[i], imaginary[i]);
  59. }
  60. }
  61. struct starpu_codelet cl_display =
  62. {
  63. .cpu_funcs = {display_complex_codelet, NULL},
  64. .nbuffers = 1,
  65. .modes = {STARPU_R}
  66. };
  67. struct starpu_codelet cl_copy =
  68. {
  69. #ifdef STARPU_USE_CUDA
  70. .cuda_funcs = {copy_complex_codelet_cuda, NULL},
  71. #endif
  72. #ifdef STARPU_USE_OPENCL
  73. .opencl_funcs = {copy_complex_codelet_opencl, NULL},
  74. #endif
  75. .nbuffers = 2,
  76. .modes = {STARPU_R, STARPU_W}
  77. };
  78. struct starpu_codelet cl_compare =
  79. {
  80. .cpu_funcs = {compare_complex_codelet, NULL},
  81. .nbuffers = 2,
  82. .modes = {STARPU_R, STARPU_R}
  83. };
  84. #ifdef STARPU_USE_OPENCL
  85. struct starpu_opencl_program opencl_program;
  86. #endif
  87. int main(int argc, char **argv)
  88. {
  89. int ret = 0;
  90. starpu_data_handle_t handle1;
  91. starpu_data_handle_t handle2;
  92. double real = 45.0;
  93. double imaginary = 12.0;
  94. double copy_real = 78.0;
  95. double copy_imaginary = 78.0;
  96. ret = starpu_init(NULL);
  97. if (ret == -ENODEV) return 77;
  98. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  99. #ifdef STARPU_USE_OPENCL
  100. ret = starpu_opencl_load_opencl_from_file("examples/interface/complex_kernels.cl",
  101. &opencl_program, NULL);
  102. STARPU_CHECK_RETURN_VALUE(ret, "starpu_opencl_load_opencl_from_file");
  103. #endif
  104. starpu_complex_data_register(&handle1, 0, &real, &imaginary, 1);
  105. starpu_complex_data_register(&handle2, 0, &copy_real, &copy_imaginary, 1);
  106. ret = starpu_insert_task(&cl_display, STARPU_R, handle1, 0);
  107. if (ret == -ENODEV) goto enodev;
  108. STARPU_CHECK_RETURN_VALUE(ret, "starpu_insert_task");
  109. ret = starpu_insert_task(&cl_display, STARPU_R, handle2, 0);
  110. if (ret == -ENODEV) goto enodev;
  111. STARPU_CHECK_RETURN_VALUE(ret, "starpu_insert_task");
  112. ret = starpu_insert_task(&cl_compare,
  113. STARPU_R, handle1,
  114. STARPU_R, handle2,
  115. 0);
  116. if (ret == -ENODEV) goto enodev;
  117. STARPU_CHECK_RETURN_VALUE(ret, "starpu_insert_task");
  118. ret = starpu_insert_task(&cl_copy,
  119. STARPU_R, handle1,
  120. STARPU_W, handle2,
  121. 0);
  122. if (ret == -ENODEV) goto enodev;
  123. STARPU_CHECK_RETURN_VALUE(ret, "starpu_insert_task");
  124. ret = starpu_insert_task(&cl_display, STARPU_R, handle1, 0);
  125. if (ret == -ENODEV) goto enodev;
  126. STARPU_CHECK_RETURN_VALUE(ret, "starpu_insert_task");
  127. ret = starpu_insert_task(&cl_display, STARPU_R, handle2, 0);
  128. if (ret == -ENODEV) goto enodev;
  129. STARPU_CHECK_RETURN_VALUE(ret, "starpu_insert_task");
  130. ret = starpu_insert_task(&cl_compare,
  131. STARPU_R, handle1,
  132. STARPU_R, handle2,
  133. 0);
  134. if (ret == -ENODEV) goto enodev;
  135. STARPU_CHECK_RETURN_VALUE(ret, "starpu_insert_task");
  136. starpu_task_wait_for_all();
  137. #ifdef STARPU_USE_OPENCL
  138. ret = starpu_opencl_unload_opencl(&opencl_program);
  139. STARPU_CHECK_RETURN_VALUE(ret, "starpu_opencl_unload_opencl");
  140. #endif
  141. starpu_shutdown();
  142. return 0;
  143. enodev:
  144. #ifdef STARPU_USE_OPENCL
  145. ret = starpu_opencl_unload_opencl(&opencl_program);
  146. STARPU_CHECK_RETURN_VALUE(ret, "starpu_opencl_unload_opencl");
  147. #endif
  148. starpu_data_unregister(handle1);
  149. starpu_data_unregister(handle2);
  150. starpu_shutdown();
  151. return 77;
  152. }