commute.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2013 Université de Bordeaux 1
  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. void begin(void *descr[], void *_args STARPU_ATTRIBUTE_UNUSED)
  20. {
  21. int *x = (int *)STARPU_VARIABLE_GET_PTR(descr[0]);
  22. *x = 0;
  23. }
  24. static struct starpu_codelet codelet_begin =
  25. {
  26. .cpu_funcs = {begin, NULL},
  27. .cpu_funcs_name = {"begin", NULL},
  28. .nbuffers = 1,
  29. };
  30. void commute1(void *descr[], void *_args STARPU_ATTRIBUTE_UNUSED)
  31. {
  32. int *x = (int *)STARPU_VARIABLE_GET_PTR(descr[0]);
  33. *x = 1;
  34. }
  35. static struct starpu_codelet codelet_commute1 =
  36. {
  37. .cpu_funcs = {commute1, NULL},
  38. .cpu_funcs_name = {"commute1", NULL},
  39. .nbuffers = 1,
  40. .modes = {STARPU_RW | STARPU_COMMUTE}
  41. };
  42. void commute2(void *descr[], void *_args STARPU_ATTRIBUTE_UNUSED)
  43. {
  44. int *x = (int *)STARPU_VARIABLE_GET_PTR(descr[0]);
  45. *x = 2;
  46. }
  47. static struct starpu_codelet codelet_commute2 =
  48. {
  49. .cpu_funcs = {commute2, NULL},
  50. .cpu_funcs_name = {"commute2", NULL},
  51. .nbuffers = 1,
  52. .modes = {STARPU_W | STARPU_COMMUTE}
  53. };
  54. void commute3(void *descr[] STARPU_ATTRIBUTE_UNUSED, void *_args STARPU_ATTRIBUTE_UNUSED)
  55. {
  56. }
  57. static struct starpu_codelet codelet_commute3 =
  58. {
  59. .cpu_funcs = {commute3, NULL},
  60. .cpu_funcs_name = {"commute3", NULL},
  61. .nbuffers = 1,
  62. .modes = {STARPU_RW | STARPU_COMMUTE}
  63. };
  64. static struct starpu_codelet codelet_end;
  65. void end(void *descr[], void *_args STARPU_ATTRIBUTE_UNUSED)
  66. {
  67. int *x = (int *)STARPU_VARIABLE_GET_PTR(descr[0]);
  68. if (codelet_end.modes[0] & STARPU_W)
  69. (*x)++;
  70. }
  71. static struct starpu_codelet codelet_end =
  72. {
  73. .cpu_funcs = {end, NULL},
  74. .cpu_funcs_name = {"end", NULL},
  75. .nbuffers = 1,
  76. };
  77. static int x;
  78. static starpu_data_handle_t x_handle, f_handle;
  79. static void test(enum starpu_data_access_mode begin_mode, enum starpu_data_access_mode end_mode, int order)
  80. {
  81. struct starpu_task *begin_t, *commute1_t, *commute2_t, *end_t;
  82. codelet_begin.modes[0] = begin_mode;
  83. codelet_end.modes[0] = end_mode;
  84. begin_t = starpu_task_create();
  85. begin_t->cl = &codelet_begin;
  86. begin_t->handles[0] = x_handle;
  87. begin_t->use_tag = 1;
  88. begin_t->tag_id = 0;
  89. commute1_t = starpu_task_create();
  90. commute1_t->cl = &codelet_commute1;
  91. commute1_t->handles[0] = x_handle;
  92. commute2_t = starpu_task_create();
  93. commute2_t->cl = &codelet_commute2;
  94. commute2_t->handles[0] = x_handle;
  95. if (order)
  96. starpu_task_declare_deps_array(commute2_t, 1, &commute1_t);
  97. else
  98. starpu_task_declare_deps_array(commute1_t, 1, &commute2_t);
  99. end_t = starpu_task_create();
  100. end_t->cl = &codelet_end;
  101. end_t->handles[0] = x_handle;
  102. end_t->detach = 0;
  103. if (starpu_task_submit(begin_t) == -ENODEV)
  104. exit(STARPU_TEST_SKIPPED);
  105. if (starpu_task_submit(commute1_t) == -ENODEV)
  106. exit(STARPU_TEST_SKIPPED);
  107. if (starpu_task_submit(commute2_t) == -ENODEV)
  108. exit(STARPU_TEST_SKIPPED);
  109. starpu_insert_task(&codelet_commute3, STARPU_RW|STARPU_COMMUTE, x_handle, 0);
  110. if (starpu_task_submit(end_t) == -ENODEV)
  111. exit(STARPU_TEST_SKIPPED);
  112. starpu_task_wait(end_t);
  113. starpu_data_acquire(x_handle, STARPU_R);
  114. if (x != 1 + order + !!(end_mode & STARPU_W))
  115. exit(EXIT_FAILURE);
  116. starpu_data_release(x_handle);
  117. }
  118. int main(int argc, char **argv)
  119. {
  120. int i, ret;
  121. ret = starpu_initialize(NULL, &argc, &argv);
  122. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  123. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  124. /* Declare x */
  125. starpu_variable_data_register(&x_handle, STARPU_MAIN_RAM, (uintptr_t)&x, sizeof(x));
  126. for (i = 0; i <= 1; i++)
  127. {
  128. test(STARPU_R, STARPU_R, i);
  129. test(STARPU_W, STARPU_R, i);
  130. test(STARPU_W, STARPU_RW, i);
  131. test(STARPU_R, STARPU_RW, i);
  132. }
  133. starpu_shutdown();
  134. STARPU_RETURN(0);
  135. enodev:
  136. fprintf(stderr, "WARNING: No one can execute this task\n");
  137. /* yes, we do not perform the computation but we did detect that no one
  138. * could perform the kernel, so this is not an error from StarPU */
  139. starpu_shutdown();
  140. return STARPU_TEST_SKIPPED;
  141. }