starpu_task_bundle.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2012-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. #include <starpu.h>
  17. #include "../helper.h"
  18. /*
  19. * Test the bundle interface, putting tasks working on the same data in
  20. * the same bundle
  21. */
  22. #define NB_BUNDLE 10
  23. #define NB_ITERATION 5
  24. void func_cpu(void *descr[], void *args)
  25. {
  26. float *x = (float *) STARPU_VARIABLE_GET_PTR(descr[0]);
  27. float factor;
  28. factor = *(float *) args;
  29. *x *= factor;
  30. }
  31. struct starpu_codelet codelet =
  32. {
  33. .modes = {STARPU_RW},
  34. .cpu_funcs = {func_cpu},
  35. .cpu_funcs_name = {"func_cpu"},
  36. .nbuffers = 1
  37. };
  38. int main(int argc, char **argv)
  39. {
  40. int i, j, ret;
  41. ret = starpu_initialize(NULL, &argc, &argv);
  42. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  43. STARPU_CHECK_RETURN_VALUE(ret, "starpu_initialize");
  44. float *data;
  45. starpu_malloc((void**)&data, sizeof(*data) * NB_BUNDLE);
  46. float factors[NB_BUNDLE];
  47. starpu_data_handle_t handles[NB_BUNDLE];
  48. struct starpu_task *task[NB_ITERATION];
  49. starpu_task_bundle_t bundles[NB_BUNDLE];
  50. for (i = 0; i < NB_BUNDLE; i++)
  51. {
  52. data[i] = i + 1;
  53. factors[i] = NB_BUNDLE - i;
  54. }
  55. for (i = 0; i < NB_BUNDLE; i++)
  56. starpu_variable_data_register(&handles[i], STARPU_MAIN_RAM, (uintptr_t)&data[i], sizeof(float));
  57. FPRINTF(stderr, "VALUES:");
  58. for (i = 0; i < NB_BUNDLE; i++)
  59. FPRINTF(stderr, " %f (%f)", data[i], factors[i]);
  60. FPRINTF(stderr, "\n");
  61. for (i = 0; i < NB_BUNDLE; i++)
  62. {
  63. starpu_task_bundle_create(&bundles[i]);
  64. for (j = 0; j < NB_ITERATION; j++)
  65. {
  66. task[j] = starpu_task_create();
  67. task[j]->cl = &codelet;
  68. task[j]->cl_arg = &factors[i];
  69. task[j]->cl_arg_size = sizeof(float);
  70. task[j]->handles[0] = handles[i];
  71. ret = starpu_task_bundle_insert(bundles[i], task[j]);
  72. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_insert");
  73. }
  74. /* Put one aside, just for fun */
  75. ret = starpu_task_bundle_remove(bundles[i], task[NB_ITERATION / 2]);
  76. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_bundle_remove");
  77. for (j = 0; j < NB_ITERATION; j++)
  78. {
  79. ret = starpu_task_submit(task[j]);
  80. if (ret == -ENODEV) goto enodev;
  81. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  82. }
  83. starpu_task_bundle_close(bundles[i]);
  84. }
  85. ret = starpu_task_wait_for_all();
  86. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_wait_for_all");
  87. for(i = 0; i < NB_BUNDLE ; i++)
  88. {
  89. ret = starpu_data_acquire(handles[i], STARPU_R);
  90. STARPU_CHECK_RETURN_VALUE(ret, "starpu_data_acquire");
  91. }
  92. FPRINTF(stderr, "VALUES:");
  93. for (i = 0; i < NB_BUNDLE; i++)
  94. FPRINTF(stderr, " %f (%f)", data[i], factors[i]);
  95. FPRINTF(stderr, "\n");
  96. for(i = 0; i < NB_BUNDLE ; i++)
  97. {
  98. starpu_data_release(handles[i]);
  99. starpu_data_unregister(handles[i]);
  100. }
  101. starpu_free(data);
  102. starpu_shutdown();
  103. return EXIT_SUCCESS;
  104. enodev:
  105. starpu_shutdown();
  106. fprintf(stderr, "WARNING: No one can execute this task\n");
  107. /* yes, we do not perform the computation but we did detect that no one
  108. * could perform the kernel, so this is not an error from StarPU */
  109. return STARPU_TEST_SKIPPED;
  110. }