insert_task.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. void func_cpu(void *descr[], void *_args)
  20. {
  21. /*
  22. * Do not use STARPU_SKIP_IF_VALGRIND here.
  23. * We need to call starpu_codelet_unpack_args() in order to make sure
  24. * there are no memory leaks in the program.
  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. /*
  32. * It is safe to use STARPU_SKIP_IF_VALGRIND here
  33. */
  34. STARPU_SKIP_IF_VALGRIND;
  35. *x0 = *x0 * ifactor;
  36. *x1 = *x1 * ffactor;
  37. }
  38. struct starpu_codelet mycodelet =
  39. {
  40. .modes = { STARPU_RW, STARPU_RW },
  41. .cpu_funcs = {func_cpu, NULL},
  42. .cpu_funcs_name = {"func_cpu", NULL},
  43. .nbuffers = 2
  44. };
  45. int main(int argc, char **argv)
  46. {
  47. int *x; float *f;
  48. int i, ret;
  49. int ifactor=12;
  50. float ffactor=10.0;
  51. starpu_data_handle_t data_handles[2];
  52. ret = starpu_initialize(NULL, &argc, &argv);
  53. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  54. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  55. starpu_malloc((void**)&x, sizeof(*x));
  56. *x = 1;
  57. starpu_variable_data_register(&data_handles[0], 0, (uintptr_t)x, sizeof(*x));
  58. starpu_malloc((void**)&f, sizeof(*f));
  59. *f = 2.0;
  60. starpu_variable_data_register(&data_handles[1], 0, (uintptr_t)f, sizeof(*f));
  61. FPRINTF(stderr, "VALUES: %d (%d) %f (%f)\n", *x, ifactor, *f, ffactor);
  62. ret = starpu_insert_task(&mycodelet,
  63. STARPU_VALUE, &ifactor, sizeof(ifactor),
  64. STARPU_VALUE, &ffactor, sizeof(ffactor),
  65. STARPU_RW, data_handles[0], STARPU_RW, data_handles[1],
  66. 0);
  67. if (ret == -ENODEV) goto enodev;
  68. STARPU_CHECK_RETURN_VALUE(ret, "starpu_insert_task");
  69. ret = starpu_task_wait_for_all();
  70. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_wait_for_all");
  71. for(i=0 ; i<2 ; i++)
  72. {
  73. ret = starpu_data_acquire(data_handles[i], STARPU_R);
  74. STARPU_CHECK_RETURN_VALUE(ret, "starpu_data_acquire");
  75. }
  76. FPRINTF(stderr, "VALUES: %d %f\n", *x, *f);
  77. for(i=0 ; i<2 ; i++)
  78. {
  79. starpu_data_release(data_handles[i]);
  80. }
  81. struct starpu_task *task = starpu_task_create();
  82. task->cl = &mycodelet;
  83. task->handles[0] = data_handles[0];
  84. task->handles[1] = data_handles[1];
  85. starpu_codelet_pack_args(&task->cl_arg, &task->cl_arg_size,
  86. STARPU_VALUE, &ifactor, sizeof(ifactor),
  87. STARPU_VALUE, &ffactor, sizeof(ffactor),
  88. 0);
  89. ret = starpu_task_submit(task);
  90. if (ret == -ENODEV) goto enodev;
  91. STARPU_CHECK_RETURN_VALUE(ret, "starpu_insert_task");
  92. ret = starpu_task_wait_for_all();
  93. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_wait_for_all");
  94. for(i=0 ; i<2 ; i++)
  95. {
  96. ret = starpu_data_acquire(data_handles[i], STARPU_R);
  97. STARPU_CHECK_RETURN_VALUE(ret, "starpu_data_acquire");
  98. }
  99. FPRINTF(stderr, "VALUES: %d %f\n", *x, *f);
  100. for(i=0 ; i<2 ; i++)
  101. {
  102. starpu_data_release(data_handles[i]);
  103. starpu_data_unregister(data_handles[i]);
  104. }
  105. starpu_free(x);
  106. starpu_free(f);
  107. starpu_shutdown();
  108. return EXIT_SUCCESS;
  109. enodev:
  110. starpu_shutdown();
  111. fprintf(stderr, "WARNING: No one can execute this task\n");
  112. /* yes, we do not perform the computation but we did detect that no one
  113. * could perform the kernel, so this is not an error from StarPU */
  114. return STARPU_TEST_SKIPPED;
  115. }