insert_task.c 4.5 KB

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