sequential_consistency.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2018 CNRS
  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 <starpu.h>
  17. #define FPRINTF(ofile, fmt, ...) do { if (!getenv("STARPU_SSILENT")) {fprintf(ofile, fmt, ## __VA_ARGS__); }} while(0)
  18. void cpu_codeletA(void *descr[], void *args);
  19. void cpu_codeletB(void *descr[], void *args);
  20. void cpu_codeletC(void *descr[], void *args);
  21. struct starpu_codelet clA =
  22. {
  23. .cpu_funcs = {cpu_codeletA},
  24. .cpu_funcs_name = {"cpu_codeletA"},
  25. .nbuffers = 1,
  26. .modes = {STARPU_RW},
  27. .name = "codeletA"
  28. };
  29. struct starpu_codelet clB =
  30. {
  31. .cpu_funcs = {cpu_codeletB},
  32. .cpu_funcs_name = {"cpu_codeletB"},
  33. .nbuffers = 1,
  34. .modes = {STARPU_RW},
  35. .name = "codeletB"
  36. };
  37. struct starpu_codelet clC =
  38. {
  39. .cpu_funcs = {cpu_codeletC},
  40. .cpu_funcs_name = {"cpu_codeletC"},
  41. .nbuffers = 1,
  42. .modes = {STARPU_RW},
  43. .name = "codeletC"
  44. };
  45. void cpu_codeletA(void *descr[], void *args)
  46. {
  47. int *val = (int *)STARPU_VARIABLE_GET_PTR(descr[0]);
  48. starpu_data_handle_t value_handle;
  49. starpu_tag_t tagHoldC;
  50. int ret;
  51. unsigned char handle_sequential_consistency[] = {0};
  52. FPRINTF(stderr, "[Task A] Value = %d\n", *val);
  53. starpu_codelet_unpack_args(args, &value_handle, &tagHoldC);
  54. // With several data, one would need to use a dynamically
  55. // allocated array for the sequential consistency,
  56. // the array could be freed immediately after calling
  57. // starpu_task_insert()
  58. ret = starpu_task_insert(&clB,
  59. STARPU_RW, value_handle,
  60. STARPU_CALLBACK_WITH_ARG, starpu_tag_notify_from_apps, tagHoldC,
  61. STARPU_HANDLES_SEQUENTIAL_CONSISTENCY, handle_sequential_consistency,
  62. STARPU_NAME, "taskB",
  63. 0);
  64. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_insert");
  65. *val *= 2;
  66. }
  67. void cpu_codeletB(void *descr[], void *args)
  68. {
  69. (void)args;
  70. int *val = (int *)STARPU_VARIABLE_GET_PTR(descr[0]);
  71. FPRINTF(stderr, "[Task B] Value = %d\n", *val);
  72. STARPU_ASSERT_MSG(*val == 24, "Incorrect value %d (expected 24)\n", *val);
  73. *val += 1;
  74. }
  75. void cpu_codeletC(void *descr[], void *args)
  76. {
  77. (void)args;
  78. int *val = (int *)STARPU_VARIABLE_GET_PTR(descr[0]);
  79. FPRINTF(stderr, "[Task C] Value = %d\n", *val);
  80. STARPU_ASSERT_MSG(*val == 25, "Incorrect value %d (expected 25)\n", *val);
  81. *val *= 2;
  82. }
  83. /*
  84. * Submit taskA and hold it
  85. * Submit taskC and hold it
  86. * Release taskA
  87. * Execute taskA --> submit taskB
  88. * Execute taskB --> callback: release taskC
  89. *
  90. * All three tasks use the same data in RW, taskB is submitted after
  91. * taskC, so taskB should normally only execute after taskC but as the
  92. * sequential consistency for (taskB, data) is unset, taskB can
  93. * execute straightaway
  94. */
  95. int main(void)
  96. {
  97. int value=12;
  98. int ret;
  99. starpu_data_handle_t value_handle;
  100. starpu_tag_t tagHoldA = 42;
  101. starpu_tag_t tagHoldC = 84;
  102. starpu_tag_t tagA = 421;
  103. starpu_tag_t tagC = 842;
  104. struct starpu_conf conf;
  105. if (sizeof(starpu_tag_t) > sizeof(void*))
  106. {
  107. // Can't pass a tag_t through callback arg :/
  108. return 77;
  109. }
  110. starpu_conf_init(&conf);
  111. conf.nmic = 0;
  112. conf.nscc = 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. }