insert_task.c 4.6 KB

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