acquire_cb_insert.c 4.0 KB

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