sync_and_notify_data.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010, 2012-2014 Université de Bordeaux
  4. * Copyright (C) 2010, 2011, 2012, 2013 Centre National de la Recherche Scientifique
  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. #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[], STARPU_ATTRIBUTE_UNUSED void *_args);
  44. void cuda_codelet_incC(void *descr[], STARPU_ATTRIBUTE_UNUSED void *_args);
  45. #endif
  46. #ifdef STARPU_USE_OPENCL
  47. void opencl_codelet_incA(void *descr[], STARPU_ATTRIBUTE_UNUSED void *_args);
  48. void opencl_codelet_incC(void *descr[], STARPU_ATTRIBUTE_UNUSED 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[], STARPU_ATTRIBUTE_UNUSED void *_args)
  55. {
  56. unsigned *val = (unsigned *)STARPU_VECTOR_GET_PTR(descr[0]);
  57. val[0]++;
  58. }
  59. void cpu_codelet_incC(void *descr[], STARPU_ATTRIBUTE_UNUSED void *_args)
  60. {
  61. unsigned *val = (unsigned *)STARPU_VECTOR_GET_PTR(descr[0]);
  62. val[2]++;
  63. }
  64. int main(int argc, char **argv)
  65. {
  66. int ret;
  67. #ifdef STARPU_QUICK_CHECK
  68. n /= 10;
  69. k /= 16;
  70. #endif
  71. ret = starpu_initialize(NULL, &argc, &argv);
  72. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  73. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  74. #ifdef STARPU_USE_OPENCL
  75. ret = starpu_opencl_load_opencl_from_file("tests/datawizard/sync_and_notify_data_opencl_codelet.cl", &opencl_code, NULL);
  76. STARPU_CHECK_RETURN_VALUE(ret, "starpu_opencl_load_opencl_from_file");
  77. #endif
  78. starpu_vector_data_register(&v_handle, STARPU_MAIN_RAM, (uintptr_t)v, VECTORSIZE, sizeof(unsigned));
  79. unsigned iter;
  80. for (iter = 0; iter < k; iter++)
  81. {
  82. unsigned ind;
  83. for (ind = 0; ind < n; ind++)
  84. {
  85. /* increment a = v[0] */
  86. struct starpu_codelet cl_inc_a =
  87. {
  88. .cpu_funcs = {cpu_codelet_incA, NULL},
  89. #ifdef STARPU_USE_CUDA
  90. .cuda_funcs = {cuda_codelet_incA, NULL},
  91. .cuda_flags = {STARPU_CUDA_ASYNC},
  92. #endif
  93. #ifdef STARPU_USE_OPENCL
  94. .opencl_funcs = {opencl_codelet_incA, NULL},
  95. .opencl_flags = {STARPU_OPENCL_ASYNC},
  96. #endif
  97. .cpu_funcs_name = {"cpu_codelet_incA", NULL},
  98. .nbuffers = 1,
  99. .modes = {STARPU_RW}
  100. };
  101. struct starpu_task *task = starpu_task_create();
  102. task->cl = &cl_inc_a;
  103. task->handles[0] = v_handle;
  104. task->synchronous = 1;
  105. ret = starpu_task_submit(task);
  106. if (ret == -ENODEV) goto enodev;
  107. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  108. }
  109. /* synchronize v in RAM */
  110. ret = starpu_data_acquire(v_handle, STARPU_RW);
  111. STARPU_CHECK_RETURN_VALUE(ret, "starpu_data_acquire");
  112. /* increment b */
  113. v[1]++;
  114. starpu_data_release(v_handle);
  115. for (ind = 0; ind < n; ind++)
  116. {
  117. /* increment c = v[2] */
  118. struct starpu_codelet cl_inc_c =
  119. {
  120. .cpu_funcs = {cpu_codelet_incC, NULL},
  121. #ifdef STARPU_USE_CUDA
  122. .cuda_funcs = {cuda_codelet_incC, NULL},
  123. .cuda_flags = {STARPU_CUDA_ASYNC},
  124. #endif
  125. #ifdef STARPU_USE_OPENCL
  126. .opencl_funcs = {opencl_codelet_incC, NULL},
  127. .opencl_flags = {STARPU_OPENCL_ASYNC},
  128. #endif
  129. .cpu_funcs_name = {"cpu_codelet_incC", NULL},
  130. .nbuffers = 1,
  131. .modes = {STARPU_RW}
  132. };
  133. struct starpu_task *task = starpu_task_create();
  134. task->cl = &cl_inc_c;
  135. task->handles[0] = v_handle;
  136. task->synchronous = 1;
  137. ret = starpu_task_submit(task);
  138. if (ret == -ENODEV) goto enodev;
  139. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  140. }
  141. }
  142. ret = starpu_data_acquire(v_handle, STARPU_RW);
  143. STARPU_CHECK_RETURN_VALUE(ret, "starpu_data_acquire");
  144. FPRINTF(stderr, "V = {%u, %u, %u}\n", v[0], v[1], v[2]);
  145. starpu_data_release(v_handle);
  146. starpu_data_unregister(v_handle);
  147. starpu_shutdown();
  148. if ((v[0] != n*k) || (v[1] != k) || (v[2] != n*k))
  149. {
  150. FPRINTF(stderr, "Incorrect result\n");
  151. return EXIT_FAILURE;
  152. }
  153. return EXIT_SUCCESS;
  154. enodev:
  155. starpu_data_unregister(v_handle);
  156. starpu_shutdown();
  157. fprintf(stderr, "WARNING: No one can execute this task\n");
  158. /* yes, we do not perform the computation but we did detect that no one
  159. * could perform the kernel, so this is not an error from StarPU */
  160. STARPU_RETURN(STARPU_TEST_SKIPPED);
  161. }