insert_task.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2012-2013 Inria
  4. * Copyright (C) 2011-2013,2015,2017 CNRS
  5. * Copyright (C) 2013-2016 Université de Bordeaux
  6. *
  7. * StarPU is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation; either version 2.1 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * StarPU is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. *
  16. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  17. */
  18. #include <starpu.h>
  19. #include "../helper.h"
  20. /*
  21. * Try the starpu_task_insert interface in various ways
  22. */
  23. static int _ifactor = 12;
  24. static float _ffactor = 10.0;
  25. void func_cpu_args(void *descr[], void *_args)
  26. {
  27. int *x0 = (int *)STARPU_VARIABLE_GET_PTR(descr[0]);
  28. float *x1 = (float *)STARPU_VARIABLE_GET_PTR(descr[1]);
  29. int ifactor;
  30. float ffactor;
  31. starpu_codelet_unpack_args(_args, &ifactor, &ffactor);
  32. *x0 = *x0 * ifactor;
  33. *x1 = *x1 * ffactor;
  34. }
  35. void func_cpu_noargs(void *descr[], void *_args)
  36. {
  37. int *x0 = (int *)STARPU_VARIABLE_GET_PTR(descr[0]);
  38. float *x1 = (float *)STARPU_VARIABLE_GET_PTR(descr[1]);
  39. (void)_args;
  40. *x0 = *x0 * _ifactor;
  41. *x1 = *x1 * _ffactor;
  42. }
  43. struct starpu_codelet mycodelet_args =
  44. {
  45. .modes = { STARPU_RW, STARPU_RW },
  46. .cpu_funcs = {func_cpu_args},
  47. .cpu_funcs_name = {"func_cpu_args"},
  48. .nbuffers = 2
  49. };
  50. struct starpu_codelet mycodelet_noargs =
  51. {
  52. .modes = { STARPU_RW, STARPU_RW },
  53. .cpu_funcs = {func_cpu_noargs},
  54. .nbuffers = 2
  55. };
  56. static
  57. int test_codelet(struct starpu_codelet *codelet, int task_insert, int args, int x, float f)
  58. {
  59. starpu_data_handle_t data_handles[2];
  60. int xx = x;
  61. float ff = f;
  62. int i, ret;
  63. starpu_variable_data_register(&data_handles[0], STARPU_MAIN_RAM, (uintptr_t)&xx, sizeof(xx));
  64. starpu_variable_data_register(&data_handles[1], STARPU_MAIN_RAM, (uintptr_t)&ff, sizeof(ff));
  65. FPRINTF(stderr, "values: %d (%d) %f (%f)\n", xx, _ifactor, ff, _ffactor);
  66. if (task_insert)
  67. {
  68. if (args)
  69. ret = starpu_task_insert(codelet,
  70. STARPU_VALUE, &_ifactor, sizeof(_ifactor),
  71. STARPU_VALUE, &_ffactor, sizeof(_ffactor),
  72. STARPU_RW, data_handles[0], STARPU_RW, data_handles[1],
  73. 0);
  74. else
  75. ret = starpu_task_insert(codelet,
  76. STARPU_RW, data_handles[0], STARPU_RW, data_handles[1],
  77. 0);
  78. if (ret == -ENODEV) goto enodev;
  79. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_insert");
  80. }
  81. else
  82. {
  83. struct starpu_task *task;
  84. if (args)
  85. task = starpu_task_build(codelet,
  86. STARPU_VALUE, &_ifactor, sizeof(_ifactor),
  87. STARPU_VALUE, &_ffactor, sizeof(_ffactor),
  88. STARPU_RW, data_handles[0], STARPU_RW, data_handles[1],
  89. 0);
  90. else
  91. task = starpu_task_build(codelet,
  92. STARPU_RW, data_handles[0], STARPU_RW, data_handles[1],
  93. 0);
  94. ret = starpu_task_submit(task);
  95. if (ret == -ENODEV) goto enodev;
  96. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  97. }
  98. enodev:
  99. for(i=0 ; i<2 ; i++)
  100. {
  101. starpu_data_unregister(data_handles[i]);
  102. }
  103. FPRINTF(stderr, "values: %d (should be %d) %f (should be %f)\n\n", xx, x*_ifactor, ff, f*_ffactor);
  104. return ret == -ENODEV ? ret : xx == x*_ifactor && ff == f*_ffactor;
  105. }
  106. int main(void)
  107. {
  108. int x; float f;
  109. int i, ret;
  110. int ifactor=12;
  111. float ffactor=10.0;
  112. starpu_data_handle_t data_handles[2];
  113. ret = starpu_init(NULL);
  114. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  115. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  116. FPRINTF(stderr, "Testing codelet with task_insert and with arguments\n");
  117. ret = test_codelet(&mycodelet_args, 1, 1, 4, 2.0);
  118. if (ret == -ENODEV) goto enodev;
  119. if (ret)
  120. {
  121. FPRINTF(stderr, "Testing codelet with task_insert and without arguments\n");
  122. ret = test_codelet(&mycodelet_noargs, 1, 0, 9, 7.0);
  123. }
  124. if (ret == -ENODEV) goto enodev;
  125. if (ret)
  126. {
  127. FPRINTF(stderr, "Testing codelet with task_build and with arguments\n");
  128. ret = test_codelet(&mycodelet_args, 0, 1, 5, 3.0);
  129. }
  130. if (ret == -ENODEV) goto enodev;
  131. if (ret)
  132. {
  133. FPRINTF(stderr, "Testing codelet with task_build and without arguments\n");
  134. ret = test_codelet(&mycodelet_noargs, 0, 0, 7, 5.0);
  135. }
  136. if (ret == -ENODEV) goto enodev;
  137. starpu_shutdown();
  138. STARPU_RETURN(ret?0:1);
  139. enodev:
  140. starpu_shutdown();
  141. fprintf(stderr, "WARNING: No one can execute this task\n");
  142. /* yes, we do not perform the computation but we did detect that no one
  143. * could perform the kernel, so this is not an error from StarPU */
  144. return STARPU_TEST_SKIPPED;
  145. }