sync_and_notify_data_implicit.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2021 Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
  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 <stdio.h>
  17. #include <unistd.h>
  18. #include <errno.h>
  19. #include <starpu.h>
  20. #include "../helper.h"
  21. /*
  22. * Mix tasks with implicit dependencies and data acquisitions
  23. */
  24. #define N_DEF 100
  25. #define K_DEF 256
  26. static unsigned n=N_DEF;
  27. static unsigned k=K_DEF;
  28. /*
  29. * In this test, we maintain a vector v = (a,b,c).
  30. *
  31. * Each iteration consists of:
  32. * - increment a n times
  33. * - sync v in ram
  34. * - incrementer b
  35. * - notify the modification of v
  36. * - incrementer c n times
  37. * - sync v
  38. *
  39. * At the end, we have to make sure that if we did k iterations,
  40. * v == (kn, k, kn)
  41. */
  42. #ifdef STARPU_USE_CUDA
  43. void cuda_codelet_incA(void *descr[], void *_args);
  44. void cuda_codelet_incC(void *descr[], void *_args);
  45. #endif
  46. #ifdef STARPU_USE_OPENCL
  47. void opencl_codelet_incA(void *descr[], void *_args);
  48. void opencl_codelet_incC(void *descr[], void *_args);
  49. struct starpu_opencl_program opencl_code;
  50. #endif
  51. #define VECTORSIZE 16
  52. starpu_data_handle_t v_handle;
  53. static unsigned v[VECTORSIZE] STARPU_ATTRIBUTE_ALIGNED(128) = {0, 0, 0, 0};
  54. void cpu_codelet_incA(void *descr[], void *arg)
  55. {
  56. (void)arg;
  57. unsigned *val = (unsigned *)STARPU_VECTOR_GET_PTR(descr[0]);
  58. val[0]++;
  59. }
  60. void cpu_codelet_incC(void *descr[], void *arg)
  61. {
  62. (void)arg;
  63. unsigned *val = (unsigned *)STARPU_VECTOR_GET_PTR(descr[0]);
  64. val[2]++;
  65. }
  66. /* increment a = v[0] */
  67. static struct starpu_codelet cl_inc_a =
  68. {
  69. .cpu_funcs = {cpu_codelet_incA},
  70. #ifdef STARPU_USE_CUDA
  71. .cuda_funcs = {cuda_codelet_incA},
  72. .cuda_flags = {STARPU_CUDA_ASYNC},
  73. #endif
  74. #ifdef STARPU_USE_OPENCL
  75. .opencl_funcs = {opencl_codelet_incA},
  76. .opencl_flags = {STARPU_OPENCL_ASYNC},
  77. #endif
  78. .cpu_funcs_name = {"cpu_codelet_incA"},
  79. .nbuffers = 1,
  80. .modes = {STARPU_RW}
  81. };
  82. /* increment c = v[2] */
  83. struct starpu_codelet cl_inc_c =
  84. {
  85. .cpu_funcs = {cpu_codelet_incC},
  86. #ifdef STARPU_USE_CUDA
  87. .cuda_funcs = {cuda_codelet_incC},
  88. .cuda_flags = {STARPU_CUDA_ASYNC},
  89. #endif
  90. #ifdef STARPU_USE_OPENCL
  91. .opencl_funcs = {opencl_codelet_incC},
  92. .opencl_flags = {STARPU_OPENCL_ASYNC},
  93. #endif
  94. .cpu_funcs_name = {"cpu_codelet_incC"},
  95. .nbuffers = 1,
  96. .modes = {STARPU_RW}
  97. };
  98. int main(int argc, char **argv)
  99. {
  100. int ret;
  101. #ifdef STARPU_QUICK_CHECK
  102. n /= 10;
  103. #endif
  104. #ifndef STARPU_LONG_CHECK
  105. k /= 8;
  106. #endif
  107. ret = starpu_initialize(NULL, &argc, &argv);
  108. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  109. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  110. #ifdef STARPU_USE_OPENCL
  111. ret = starpu_opencl_load_opencl_from_file("tests/datawizard/sync_and_notify_data_opencl_codelet.cl", &opencl_code, NULL);
  112. STARPU_CHECK_RETURN_VALUE(ret, "starpu_opencl_load_opencl_from_file");
  113. #endif
  114. starpu_vector_data_register(&v_handle, STARPU_MAIN_RAM, (uintptr_t)v, VECTORSIZE, sizeof(unsigned));
  115. unsigned iter;
  116. for (iter = 0; iter < k; iter++)
  117. {
  118. unsigned ind;
  119. for (ind = 0; ind < n; ind++)
  120. {
  121. struct starpu_task *task = starpu_task_create();
  122. task->cl = &cl_inc_a;
  123. task->handles[0] = v_handle;
  124. ret = starpu_task_submit(task);
  125. if (ret == -ENODEV) goto enodev;
  126. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  127. }
  128. /* synchronize v in RAM */
  129. ret = starpu_data_acquire(v_handle, STARPU_RW);
  130. STARPU_CHECK_RETURN_VALUE(ret, "starpu_data_acquire");
  131. /* increment b */
  132. v[1]++;
  133. starpu_data_release(v_handle);
  134. for (ind = 0; ind < n; ind++)
  135. {
  136. struct starpu_task *task = starpu_task_create();
  137. task->cl = &cl_inc_c;
  138. task->handles[0] = v_handle;
  139. ret = starpu_task_submit(task);
  140. if (ret == -ENODEV) goto enodev;
  141. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  142. }
  143. }
  144. ret = starpu_data_acquire(v_handle, STARPU_RW);
  145. STARPU_CHECK_RETURN_VALUE(ret, "starpu_data_acquire");
  146. FPRINTF(stderr, "V = {%u, %u, %u}\n", v[0], v[1], v[2]);
  147. starpu_data_release(v_handle);
  148. starpu_data_unregister(v_handle);
  149. starpu_shutdown();
  150. ret = EXIT_SUCCESS;
  151. if ((v[0] != n*k) || (v[1] != k) || (v[2] != n*k))
  152. {
  153. FPRINTF(stderr, "Incorrect result\n");
  154. ret = EXIT_FAILURE;
  155. }
  156. return ret;
  157. enodev:
  158. starpu_data_unregister(v_handle);
  159. starpu_shutdown();
  160. fprintf(stderr, "WARNING: No one can execute this task\n");
  161. /* yes, we do not perform the computation but we did detect that no one
  162. * could perform the kernel, so this is not an error from StarPU */
  163. return STARPU_TEST_SKIPPED;
  164. }