complex.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. #define FPRINTF(ofile, fmt, args ...) do { if (!getenv("STARPU_SSILENT")) {fprintf(ofile, fmt, ##args); }} while(0)
  20. static int can_execute(unsigned workerid, struct starpu_task *task, unsigned nimpl)
  21. {
  22. if (starpu_worker_get_type(workerid) == STARPU_OPENCL_WORKER)
  23. return 1;
  24. #ifdef STARPU_USE_CUDA
  25. /* Cuda device */
  26. const struct cudaDeviceProp *props;
  27. props = starpu_cuda_get_device_properties(workerid);
  28. if (props->major >= 2 || props->minor >= 3)
  29. {
  30. /* At least compute capability 1.3, supports doubles */
  31. return 1;
  32. }
  33. else
  34. {
  35. /* Old card does not support doubles */
  36. return 0;
  37. }
  38. #endif
  39. }
  40. #ifdef STARPU_USE_CUDA
  41. extern void copy_complex_codelet_cuda(void *descr[], __attribute__ ((unused)) void *_args);
  42. #endif
  43. #ifdef STARPU_USE_OPENCL
  44. extern void copy_complex_codelet_opencl(void *buffers[], void *args);
  45. #endif
  46. struct starpu_codelet cl_copy =
  47. {
  48. #ifdef STARPU_USE_CUDA
  49. .cuda_funcs = {copy_complex_codelet_cuda, NULL},
  50. #endif
  51. #ifdef STARPU_USE_OPENCL
  52. .opencl_funcs = {copy_complex_codelet_opencl, NULL},
  53. #endif
  54. .nbuffers = 2,
  55. .modes = {STARPU_R, STARPU_W},
  56. .can_execute = can_execute
  57. };
  58. #ifdef STARPU_USE_OPENCL
  59. struct starpu_opencl_program opencl_program;
  60. #endif
  61. int main(int argc, char **argv)
  62. {
  63. int ret = 0;
  64. starpu_data_handle_t handle1;
  65. starpu_data_handle_t handle2;
  66. double real = 45.0;
  67. double imaginary = 12.0;
  68. double copy_real = 78.0;
  69. double copy_imaginary = 78.0;
  70. int compare;
  71. int *compare_ptr = &compare;
  72. ret = starpu_init(NULL);
  73. if (ret == -ENODEV) return 77;
  74. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  75. #ifdef STARPU_USE_OPENCL
  76. ret = starpu_opencl_load_opencl_from_file("examples/interface/complex_kernels.cl",
  77. &opencl_program, NULL);
  78. STARPU_CHECK_RETURN_VALUE(ret, "starpu_opencl_load_opencl_from_file");
  79. #endif
  80. starpu_complex_data_register(&handle1, 0, &real, &imaginary, 1);
  81. starpu_complex_data_register(&handle2, 0, &copy_real, &copy_imaginary, 1);
  82. ret = starpu_insert_task(&cl_display, STARPU_R, handle1, 0);
  83. if (ret == -ENODEV) goto end;
  84. STARPU_CHECK_RETURN_VALUE(ret, "starpu_insert_task");
  85. ret = starpu_insert_task(&cl_display, STARPU_R, handle2, 0);
  86. if (ret == -ENODEV) goto end;
  87. STARPU_CHECK_RETURN_VALUE(ret, "starpu_insert_task");
  88. ret = starpu_insert_task(&cl_compare,
  89. STARPU_R, handle1,
  90. STARPU_R, handle2,
  91. STARPU_VALUE, &compare_ptr, sizeof(compare_ptr),
  92. 0);
  93. if (ret == -ENODEV) goto end;
  94. STARPU_CHECK_RETURN_VALUE(ret, "starpu_insert_task");
  95. starpu_task_wait_for_all();
  96. if (compare != 0)
  97. {
  98. FPRINTF(stderr, "Complex numbers should NOT be similar\n");
  99. goto end;
  100. }
  101. ret = starpu_insert_task(&cl_copy,
  102. STARPU_R, handle1,
  103. STARPU_W, handle2,
  104. 0);
  105. if (ret == -ENODEV) goto end;
  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 end;
  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 end;
  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. STARPU_VALUE, &compare_ptr, sizeof(compare_ptr),
  117. 0);
  118. if (ret == -ENODEV) goto end;
  119. STARPU_CHECK_RETURN_VALUE(ret, "starpu_insert_task");
  120. starpu_task_wait_for_all();
  121. if (compare != 1)
  122. {
  123. FPRINTF(stderr, "Complex numbers should be similar\n");
  124. }
  125. end:
  126. #ifdef STARPU_USE_OPENCL
  127. ret = starpu_opencl_unload_opencl(&opencl_program);
  128. STARPU_CHECK_RETURN_VALUE(ret, "starpu_opencl_unload_opencl");
  129. #endif
  130. starpu_data_unregister(handle1);
  131. starpu_data_unregister(handle2);
  132. starpu_shutdown();
  133. if (ret == -ENODEV) return 77; else return !compare;
  134. }