insert_task.c 4.6 KB

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