commute.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2013,2015 Inria
  4. * Copyright (C) 2013,2014,2017 CNRS
  5. * Copyright (C) 2013,2014,2016 Université de Bordeaux
  6. * Copyright (C) 2013 Thibaut Lambert
  7. *
  8. * StarPU is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU Lesser General Public License as published by
  10. * the Free Software Foundation; either version 2.1 of the License, or (at
  11. * your option) any later version.
  12. *
  13. * StarPU is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  16. *
  17. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  18. */
  19. #include <starpu.h>
  20. #include "../helper.h"
  21. /*
  22. * Trigger various STARPU_R / STARPU_RW / STARPU_RW|COMMUTE patterns
  23. */
  24. void begin(void *descr[], void *arg)
  25. {
  26. (void)arg;
  27. int *x = (int *)STARPU_VARIABLE_GET_PTR(descr[0]);
  28. *x = 0;
  29. }
  30. static struct starpu_codelet codelet_begin =
  31. {
  32. .cpu_funcs = {begin},
  33. .cpu_funcs_name = {"begin"},
  34. .nbuffers = 1,
  35. .name = "begin",
  36. };
  37. void commute1(void *descr[], void *arg)
  38. {
  39. (void)arg;
  40. int *x = (int *)STARPU_VARIABLE_GET_PTR(descr[0]);
  41. *x = 1;
  42. }
  43. static struct starpu_codelet codelet_commute1 =
  44. {
  45. .cpu_funcs = {commute1},
  46. .cpu_funcs_name = {"commute1"},
  47. .nbuffers = 1,
  48. .modes = {STARPU_RW | STARPU_COMMUTE},
  49. .name = "commute1",
  50. };
  51. void commute2(void *descr[], void *arg)
  52. {
  53. (void)arg;
  54. int *x = (int *)STARPU_VARIABLE_GET_PTR(descr[0]);
  55. *x = 2;
  56. }
  57. static struct starpu_codelet codelet_commute2 =
  58. {
  59. .cpu_funcs = {commute2},
  60. .cpu_funcs_name = {"commute2"},
  61. .nbuffers = 1,
  62. .modes = {STARPU_W | STARPU_COMMUTE},
  63. .name = "commute2",
  64. };
  65. void commute3(void *descr[], void *arg)
  66. {
  67. (void)descr;
  68. (void)arg;
  69. }
  70. static struct starpu_codelet codelet_commute3 =
  71. {
  72. .cpu_funcs = {commute3},
  73. .cpu_funcs_name = {"commute3"},
  74. .nbuffers = 1,
  75. .modes = {STARPU_RW | STARPU_COMMUTE},
  76. .name = "commute3",
  77. };
  78. static struct starpu_codelet codelet_end;
  79. void end(void *descr[], void *_args)
  80. {
  81. int *x = (int *)STARPU_VARIABLE_GET_PTR(descr[0]);
  82. enum starpu_data_access_mode end_mode = *(enum starpu_data_access_mode*) _args;
  83. if (end_mode & STARPU_W)
  84. (*x)++;
  85. }
  86. static struct starpu_codelet codelet_end =
  87. {
  88. .cpu_funcs = {end},
  89. .cpu_funcs_name = {"end"},
  90. .nbuffers = 1,
  91. .name = "end",
  92. };
  93. static int x;
  94. static starpu_data_handle_t x_handle, f_handle;
  95. static void test(enum starpu_data_access_mode begin_mode, enum starpu_data_access_mode end_mode, int order)
  96. {
  97. struct starpu_task *begin_t, *commute1_t, *commute2_t, *end_t;
  98. int ret;
  99. codelet_begin.modes[0] = begin_mode;
  100. codelet_end.modes[0] = end_mode;
  101. begin_t = starpu_task_create();
  102. begin_t->cl = &codelet_begin;
  103. begin_t->handles[0] = x_handle;
  104. begin_t->use_tag = 1;
  105. begin_t->tag_id = (order<<20) + (begin_mode<<10) + end_mode;
  106. commute1_t = starpu_task_create();
  107. commute1_t->cl = &codelet_commute1;
  108. commute1_t->handles[0] = x_handle;
  109. commute2_t = starpu_task_create();
  110. commute2_t->cl = &codelet_commute2;
  111. commute2_t->handles[0] = x_handle;
  112. if (order)
  113. starpu_task_declare_deps_array(commute2_t, 1, &commute1_t);
  114. else
  115. starpu_task_declare_deps_array(commute1_t, 1, &commute2_t);
  116. end_t = starpu_task_create();
  117. end_t->cl = &codelet_end;
  118. end_t->handles[0] = x_handle;
  119. end_t->detach = 0;
  120. end_t->cl_arg = &end_mode;
  121. end_t->cl_arg_size = sizeof(end_mode);
  122. if (starpu_task_submit(begin_t) == -ENODEV)
  123. exit(STARPU_TEST_SKIPPED);
  124. if (starpu_task_submit(commute1_t) == -ENODEV)
  125. exit(STARPU_TEST_SKIPPED);
  126. if (starpu_task_submit(commute2_t) == -ENODEV)
  127. exit(STARPU_TEST_SKIPPED);
  128. starpu_task_insert(&codelet_commute3, STARPU_RW|STARPU_COMMUTE, x_handle, 0);
  129. if (starpu_task_submit(end_t) == -ENODEV)
  130. exit(STARPU_TEST_SKIPPED);
  131. ret = starpu_task_wait(end_t);
  132. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_wait");
  133. starpu_data_acquire(x_handle, STARPU_R);
  134. if (x != 1 + order + !!(end_mode & STARPU_W))
  135. exit(EXIT_FAILURE);
  136. starpu_data_release(x_handle);
  137. }
  138. int main(int argc, char **argv)
  139. {
  140. int i, ret;
  141. ret = starpu_initialize(NULL, &argc, &argv);
  142. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  143. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  144. /* Declare x */
  145. starpu_variable_data_register(&x_handle, STARPU_MAIN_RAM, (uintptr_t)&x, sizeof(x));
  146. for (i = 0; i <= 1; i++)
  147. {
  148. test(STARPU_R, STARPU_R, i);
  149. test(STARPU_W, STARPU_R, i);
  150. test(STARPU_W, STARPU_RW, i);
  151. test(STARPU_R, STARPU_RW, i);
  152. }
  153. starpu_data_unregister(x_handle);
  154. starpu_shutdown();
  155. STARPU_RETURN(0);
  156. }