sync_and_notify_data_implicit.c 5.0 KB

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