sequential_consistency.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2018, 2019 CNRS
  4. * Copyright (C) 2018 Université de Bordeaux
  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 <starpu.h>
  18. #define FPRINTF(ofile, fmt, ...) do { if (!getenv("STARPU_SSILENT")) {fprintf(ofile, fmt, ## __VA_ARGS__); }} while(0)
  19. void cpu_codeletA(void *descr[], void *args);
  20. void cpu_codeletB(void *descr[], void *args);
  21. void cpu_codeletC(void *descr[], void *args);
  22. struct starpu_codelet clA =
  23. {
  24. .cpu_funcs = {cpu_codeletA},
  25. .cpu_funcs_name = {"cpu_codeletA"},
  26. .nbuffers = 1,
  27. .modes = {STARPU_RW},
  28. .name = "codeletA"
  29. };
  30. struct starpu_codelet clB =
  31. {
  32. .cpu_funcs = {cpu_codeletB},
  33. .cpu_funcs_name = {"cpu_codeletB"},
  34. .nbuffers = 1,
  35. .modes = {STARPU_RW},
  36. .name = "codeletB"
  37. };
  38. struct starpu_codelet clC =
  39. {
  40. .cpu_funcs = {cpu_codeletC},
  41. .cpu_funcs_name = {"cpu_codeletC"},
  42. .nbuffers = 1,
  43. .modes = {STARPU_RW},
  44. .name = "codeletC"
  45. };
  46. void cpu_codeletA(void *descr[], void *args)
  47. {
  48. int *val = (int *)STARPU_VARIABLE_GET_PTR(descr[0]);
  49. starpu_data_handle_t value_handle;
  50. starpu_tag_t tagHoldC;
  51. int ret;
  52. unsigned char handle_sequential_consistency[] = {0};
  53. FPRINTF(stderr, "[Task A] Value = %d\n", *val);
  54. starpu_codelet_unpack_args(args, &value_handle, &tagHoldC);
  55. // With several data, one would need to use a dynamically
  56. // allocated array for the sequential consistency,
  57. // the array could be freed immediately after calling
  58. // starpu_task_insert()
  59. ret = starpu_task_insert(&clB,
  60. STARPU_RW, value_handle,
  61. STARPU_CALLBACK_WITH_ARG, starpu_tag_notify_from_apps, tagHoldC,
  62. STARPU_HANDLES_SEQUENTIAL_CONSISTENCY, handle_sequential_consistency,
  63. STARPU_NAME, "taskB",
  64. 0);
  65. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_insert");
  66. *val *= 2;
  67. }
  68. void cpu_codeletB(void *descr[], void *args)
  69. {
  70. (void)args;
  71. int *val = (int *)STARPU_VARIABLE_GET_PTR(descr[0]);
  72. FPRINTF(stderr, "[Task B] Value = %d\n", *val);
  73. STARPU_ASSERT_MSG(*val == 24, "Incorrect value %d (expected 24)\n", *val);
  74. *val += 1;
  75. }
  76. void cpu_codeletC(void *descr[], void *args)
  77. {
  78. (void)args;
  79. int *val = (int *)STARPU_VARIABLE_GET_PTR(descr[0]);
  80. FPRINTF(stderr, "[Task C] Value = %d\n", *val);
  81. STARPU_ASSERT_MSG(*val == 25, "Incorrect value %d (expected 25)\n", *val);
  82. *val *= 2;
  83. }
  84. /*
  85. * Submit taskA and hold it
  86. * Submit taskC and hold it
  87. * Release taskA
  88. * Execute taskA --> submit taskB
  89. * Execute taskB --> callback: release taskC
  90. *
  91. * All three tasks use the same data in RW, taskB is submitted after
  92. * taskC, so taskB should normally only execute after taskC but as the
  93. * sequential consistency for (taskB, data) is unset, taskB can
  94. * execute straightaway
  95. */
  96. int main(void)
  97. {
  98. int value=12;
  99. int ret;
  100. starpu_data_handle_t value_handle;
  101. starpu_tag_t tagHoldA = 42;
  102. starpu_tag_t tagHoldC = 84;
  103. starpu_tag_t tagA = 421;
  104. starpu_tag_t tagC = 842;
  105. struct starpu_conf conf;
  106. if (sizeof(starpu_tag_t) > sizeof(void*))
  107. {
  108. // Can't pass a tag_t through callback arg :/
  109. return 77;
  110. }
  111. starpu_conf_init(&conf);
  112. conf.nmic = 0;
  113. conf.nmpi_ms = 0;
  114. ret = starpu_init(&conf);
  115. if (STARPU_UNLIKELY(ret == -ENODEV))
  116. {
  117. return 77;
  118. }
  119. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  120. if (starpu_cpu_worker_get_count() < 1)
  121. {
  122. FPRINTF(stderr, "This application requires at least 1 cpu worker\n");
  123. starpu_shutdown();
  124. return 77;
  125. }
  126. starpu_variable_data_register(&value_handle, STARPU_MAIN_RAM, (uintptr_t)&value, sizeof(value));
  127. starpu_tag_declare_deps_array(tagA, 1, &tagHoldA);
  128. starpu_tag_declare_deps_array(tagC, 1, &tagHoldC);
  129. ret = starpu_task_insert(&clA,
  130. STARPU_TAG, tagA,
  131. STARPU_RW, value_handle,
  132. STARPU_VALUE, &value_handle, sizeof(starpu_data_handle_t),
  133. STARPU_VALUE, &tagHoldC, sizeof(starpu_tag_t),
  134. STARPU_NAME, "taskA",
  135. 0);
  136. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_insert");
  137. ret = starpu_task_insert(&clC,
  138. STARPU_TAG, tagC,
  139. STARPU_RW, value_handle,
  140. STARPU_NAME, "taskC",
  141. 0);
  142. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_insert");
  143. // Release taskA (we want to make sure it will execute after taskC has been submitted)
  144. starpu_tag_notify_from_apps(tagHoldA);
  145. starpu_data_unregister(value_handle);
  146. STARPU_ASSERT_MSG(value == 50, "Incorrect value %d (expected 50)\n", value);
  147. starpu_shutdown();
  148. FPRINTF(stderr, "Value = %d\n", value);
  149. return ret;
  150. }