sync_and_notify_data_implicit.c 4.9 KB

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