insert_task.c 4.9 KB

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