acquire_cb_insert.c 3.5 KB

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