complex.c 4.4 KB

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