commute.c 4.5 KB

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