acquire_cb_insert.c 4.2 KB

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