task_end_dep.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2018-2020 Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
  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. /* This shows how to defer termination of a task until the termination of
  17. * another task. */
  18. #include <starpu.h>
  19. #define FPRINTF(ofile, fmt, ...) do { if (!getenv("STARPU_SSILENT")) {fprintf(ofile, fmt, ## __VA_ARGS__); }} while(0)
  20. #define INIT 12
  21. void cpu_codelet2(void *descr[], void *args)
  22. {
  23. int *val = (int *)STARPU_VARIABLE_GET_PTR(descr[0]);
  24. (void)args;
  25. STARPU_ASSERT(*val == 2*INIT);
  26. starpu_sleep(0.1);
  27. STARPU_ASSERT(*val == 2*INIT);
  28. *val *= 2;
  29. }
  30. struct starpu_codelet cl2 =
  31. {
  32. .cpu_funcs = {cpu_codelet2},
  33. .cpu_funcs_name = {"cpu_codelet2"},
  34. .nbuffers = 1,
  35. .modes = {STARPU_RW},
  36. .name = "codelet2"
  37. };
  38. void cpu_codelet(void *descr[], void *args)
  39. {
  40. (void)args;
  41. int *val = (int *)STARPU_VARIABLE_GET_PTR(descr[0]);
  42. struct starpu_task *task = starpu_task_get_current();
  43. starpu_task_insert(&cl2,
  44. STARPU_RW, task->handles[0],
  45. STARPU_TASK_END_DEPS_ARRAY, 1, &task,
  46. 0);
  47. STARPU_ASSERT(*val == INIT);
  48. starpu_sleep(0.1);
  49. STARPU_ASSERT(*val == INIT);
  50. *val *= 2;
  51. }
  52. struct starpu_codelet cl =
  53. {
  54. .cpu_funcs = {cpu_codelet},
  55. .cpu_funcs_name = {"cpu_codelet"},
  56. .nbuffers = 1,
  57. .modes = {STARPU_RW},
  58. .name = "codelet"
  59. };
  60. int main(void)
  61. {
  62. int value=INIT;
  63. int ret;
  64. starpu_data_handle_t value_handle;
  65. struct starpu_conf conf;
  66. struct starpu_task *task;
  67. starpu_conf_init(&conf);
  68. conf.nmic = 0;
  69. conf.nmpi_ms = 0;
  70. ret = starpu_init(&conf);
  71. if (STARPU_UNLIKELY(ret == -ENODEV))
  72. {
  73. return 77;
  74. }
  75. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  76. if (starpu_cpu_worker_get_count() < 1)
  77. {
  78. FPRINTF(stderr, "This application requires at least 1 cpu worker\n");
  79. starpu_shutdown();
  80. return 77;
  81. }
  82. starpu_variable_data_register(&value_handle, STARPU_MAIN_RAM, (uintptr_t)&value, sizeof(value));
  83. task = starpu_task_build(&cl,
  84. STARPU_RW, value_handle,
  85. 0);
  86. STARPU_ASSERT(task);
  87. task->detach = 0;
  88. ret = starpu_task_submit(task);
  89. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  90. ret = starpu_task_wait(task);
  91. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_wait");
  92. starpu_data_set_sequential_consistency_flag(value_handle, 0);
  93. starpu_data_acquire_on_node(value_handle, STARPU_MAIN_RAM, STARPU_R);
  94. /* Waiting for the main task should have also waited for the subtask */
  95. STARPU_ASSERT(value == 2*2*INIT);
  96. starpu_data_release_on_node(value_handle, STARPU_MAIN_RAM);
  97. starpu_data_unregister(value_handle);
  98. STARPU_ASSERT(value == 2*2*INIT);
  99. starpu_shutdown();
  100. FPRINTF(stderr, "Value = %d\n", value);
  101. return ret;
  102. }