sync_and_notify_data_implicit.c 5.0 KB

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