insert_task.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2011-2021 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. * Try the starpu_task_insert interface in various ways
  20. */
  21. static int _ifactor = 12;
  22. static float _ffactor = 10.0;
  23. void func_cpu_args(void *descr[], void *_args)
  24. {
  25. int *x0 = (int *)STARPU_VARIABLE_GET_PTR(descr[0]);
  26. float *x1 = (float *)STARPU_VARIABLE_GET_PTR(descr[1]);
  27. int ifactor;
  28. float ffactor;
  29. starpu_codelet_unpack_args(_args, &ifactor, &ffactor);
  30. *x0 = *x0 * ifactor;
  31. *x1 = *x1 * ffactor;
  32. }
  33. void func_cpu_noargs(void *descr[], void *_args)
  34. {
  35. int *x0 = (int *)STARPU_VARIABLE_GET_PTR(descr[0]);
  36. float *x1 = (float *)STARPU_VARIABLE_GET_PTR(descr[1]);
  37. (void)_args;
  38. *x0 = *x0 * _ifactor;
  39. *x1 = *x1 * _ffactor;
  40. }
  41. struct starpu_codelet mycodelet_args =
  42. {
  43. .modes = { STARPU_RW, STARPU_RW },
  44. .cpu_funcs = {func_cpu_args},
  45. .cpu_funcs_name = {"func_cpu_args"},
  46. .nbuffers = 2
  47. };
  48. struct starpu_codelet mycodelet_noargs =
  49. {
  50. .modes = { STARPU_RW, STARPU_RW },
  51. .cpu_funcs = {func_cpu_noargs},
  52. .nbuffers = 2
  53. };
  54. static
  55. int test_codelet(struct starpu_codelet *codelet, int task_insert, int args, int x, float f)
  56. {
  57. starpu_data_handle_t data_handles[2];
  58. int xx = x;
  59. float ff = f;
  60. int i, ret;
  61. starpu_variable_data_register(&data_handles[0], STARPU_MAIN_RAM, (uintptr_t)&xx, sizeof(xx));
  62. starpu_variable_data_register(&data_handles[1], STARPU_MAIN_RAM, (uintptr_t)&ff, sizeof(ff));
  63. FPRINTF(stderr, "values: %d (%d) %f (%f)\n", xx, _ifactor, ff, _ffactor);
  64. if (task_insert)
  65. {
  66. if (args)
  67. ret = starpu_task_insert(codelet,
  68. STARPU_VALUE, &_ifactor, sizeof(_ifactor),
  69. STARPU_VALUE, &_ffactor, sizeof(_ffactor),
  70. STARPU_RW, data_handles[0], STARPU_RW, data_handles[1],
  71. 0);
  72. else
  73. ret = starpu_task_insert(codelet,
  74. STARPU_RW, data_handles[0], STARPU_RW, data_handles[1],
  75. 0);
  76. if (ret == -ENODEV) goto enodev;
  77. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_insert");
  78. }
  79. else
  80. {
  81. struct starpu_task *task;
  82. if (args)
  83. task = starpu_task_build(codelet,
  84. STARPU_VALUE, &_ifactor, sizeof(_ifactor),
  85. STARPU_VALUE, &_ffactor, sizeof(_ffactor),
  86. STARPU_RW, data_handles[0], STARPU_RW, data_handles[1],
  87. 0);
  88. else
  89. task = starpu_task_build(codelet,
  90. STARPU_RW, data_handles[0], STARPU_RW, data_handles[1],
  91. 0);
  92. ret = starpu_task_submit(task);
  93. if (ret == -ENODEV) goto enodev;
  94. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  95. }
  96. enodev:
  97. for(i=0 ; i<2 ; i++)
  98. {
  99. starpu_data_unregister(data_handles[i]);
  100. }
  101. FPRINTF(stderr, "values: %d (should be %d) %f (should be %f)\n\n", xx, x*_ifactor, ff, f*_ffactor);
  102. return ret == -ENODEV ? ret : xx == x*_ifactor && ff == f*_ffactor;
  103. }
  104. int main(void)
  105. {
  106. int ret;
  107. ret = starpu_init(NULL);
  108. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  109. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  110. FPRINTF(stderr, "Testing codelet with task_insert and with arguments\n");
  111. ret = test_codelet(&mycodelet_args, 1, 1, 4, 2.0);
  112. if (ret == -ENODEV) goto enodev;
  113. if (ret)
  114. {
  115. FPRINTF(stderr, "Testing codelet with task_insert and without arguments\n");
  116. ret = test_codelet(&mycodelet_noargs, 1, 0, 9, 7.0);
  117. }
  118. if (ret == -ENODEV) goto enodev;
  119. if (ret)
  120. {
  121. FPRINTF(stderr, "Testing codelet with task_build and with arguments\n");
  122. ret = test_codelet(&mycodelet_args, 0, 1, 5, 3.0);
  123. }
  124. if (ret == -ENODEV) goto enodev;
  125. if (ret)
  126. {
  127. FPRINTF(stderr, "Testing codelet with task_build and without arguments\n");
  128. ret = test_codelet(&mycodelet_noargs, 0, 0, 7, 5.0);
  129. }
  130. if (ret == -ENODEV) goto enodev;
  131. starpu_shutdown();
  132. STARPU_RETURN(ret?0:1);
  133. enodev:
  134. starpu_shutdown();
  135. fprintf(stderr, "WARNING: No one can execute this task\n");
  136. /* yes, we do not perform the computation but we did detect that no one
  137. * could perform the kernel, so this is not an error from StarPU */
  138. return STARPU_TEST_SKIPPED;
  139. }