sync_and_notify_data.c 5.0 KB

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