acquire_cb_insert.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2011-2020 Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
  4. * Copyright (C) 2013 Thibaut Lambert
  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. #include "../helper.h"
  19. /*
  20. * Test that inserting a task from the callback of a starpu_data_acquire_cb
  21. * call, with proper dependency with an already-submitted task
  22. */
  23. #define N 16
  24. #define M 4
  25. #define X 2
  26. void which_index_cpu(void *descr[], void *_args)
  27. {
  28. (void)_args;
  29. int *x0 = (int *)STARPU_VARIABLE_GET_PTR(descr[0]);
  30. /* A real case would actually compute something */
  31. *x0 = X;
  32. }
  33. struct starpu_codelet which_index =
  34. {
  35. .cpu_funcs = {which_index_cpu},
  36. .cpu_funcs_name = {"which_index_cpu"},
  37. .nbuffers = 1,
  38. .modes = {STARPU_W}
  39. };
  40. void work_cpu(void *descr[], void *_args)
  41. {
  42. int i, n = STARPU_VECTOR_GET_NX(descr[0]);
  43. float *x0 = (float *)STARPU_VECTOR_GET_PTR(descr[0]);
  44. (void)_args;
  45. for (i = 0; i < n; i++)
  46. x0[i] = i + 1;
  47. }
  48. struct starpu_codelet work =
  49. {
  50. .cpu_funcs = {work_cpu},
  51. .cpu_funcs_name = {"work_cpu"},
  52. .nbuffers = 1,
  53. .modes = {STARPU_W}
  54. };
  55. static int x;
  56. static starpu_data_handle_t x_handle, f_handle;
  57. static
  58. void callback(void *arg)
  59. {
  60. (void)arg;
  61. starpu_task_insert(&work, STARPU_W, starpu_data_get_sub_data(f_handle, 1, x), 0);
  62. starpu_data_release(x_handle);
  63. }
  64. int main(int argc, char **argv)
  65. {
  66. int i, ret;
  67. float *f;
  68. ret = starpu_initialize(NULL, &argc, &argv);
  69. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  70. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  71. if(starpu_cpu_worker_get_count() == 0) return STARPU_TEST_SKIPPED;
  72. /* Declare x */
  73. starpu_variable_data_register(&x_handle, STARPU_MAIN_RAM, (uintptr_t)&x, sizeof(x));
  74. /* Allocate and Declare f */
  75. ret = starpu_malloc((void**)&f, N * sizeof(*f));
  76. STARPU_CHECK_RETURN_VALUE(ret, "starpu_malloc");
  77. memset(f, 0, N * sizeof(*f));
  78. starpu_vector_data_register(&f_handle, STARPU_MAIN_RAM, (uintptr_t)f, N, sizeof(*f));
  79. /* Partition f */
  80. struct starpu_data_filter filter =
  81. {
  82. .filter_func = starpu_vector_filter_block,
  83. .nchildren = M,
  84. };
  85. starpu_data_partition(f_handle, &filter);
  86. /* Compute which portion we will work on */
  87. ret = starpu_task_insert(&which_index, STARPU_W, x_handle, 0);
  88. if (ret == -ENODEV) goto enodev;
  89. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_insert");
  90. /* And submit the corresponding task */
  91. #ifdef __GCC__
  92. STARPU_DATA_ACQUIRE_CB(
  93. x_handle,
  94. STARPU_R,
  95. starpu_task_insert(&work, STARPU_W, starpu_data_get_sub_data(f_handle, 1, x), 0)
  96. );
  97. #else
  98. starpu_data_acquire_cb(x_handle, STARPU_R, callback, NULL);
  99. #endif
  100. /* Wait for acquisition (and thus insertion) */
  101. starpu_data_acquire(x_handle, STARPU_W);
  102. starpu_data_release(x_handle);
  103. /* Now wait for the inserted task */
  104. ret = starpu_task_wait_for_all();
  105. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_wait_for_all");
  106. /* Can now clean */
  107. starpu_data_unpartition(f_handle, STARPU_MAIN_RAM);
  108. starpu_data_unregister(f_handle);
  109. starpu_data_unregister(x_handle);
  110. FPRINTF(stderr, "VALUES: %d", x);
  111. for(i=0 ; i<N ; i++)
  112. {
  113. FPRINTF(stderr, " %f", f[i]);
  114. }
  115. FPRINTF(stderr, "\n");
  116. ret = EXIT_SUCCESS;
  117. if (f[X*(N/M)] != 1 || f[X*(N/M)+1] != 2 ||
  118. f[X*(N/M)+2] != 3 || f[X*(N/M)+3] != 4)
  119. ret = EXIT_FAILURE;
  120. starpu_free(f);
  121. starpu_shutdown();
  122. return ret;
  123. enodev:
  124. fprintf(stderr, "WARNING: No one can execute this task\n");
  125. /* yes, we do not perform the computation but we did detect that no one
  126. * could perform the kernel, so this is not an error from StarPU */
  127. starpu_shutdown();
  128. return STARPU_TEST_SKIPPED;
  129. }