complex.c 4.7 KB

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