complex.c 4.0 KB

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