insert_task.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2011 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 <starpu.h>
  17. #include "../common/helper.h"
  18. #define FPRINTF(ofile, fmt, args ...) do { if (!getenv("STARPU_SSILENT")) {fprintf(ofile, fmt, ##args); }} while(0)
  19. void func_cpu(void *descr[], void *_args)
  20. {
  21. int *x0 = (int *)STARPU_VARIABLE_GET_PTR(descr[0]);
  22. float *x1 = (float *)STARPU_VARIABLE_GET_PTR(descr[1]);
  23. int ifactor;
  24. float ffactor;
  25. starpu_unpack_cl_args(_args, &ifactor, &ffactor);
  26. *x0 = *x0 * ifactor;
  27. *x1 = *x1 * ffactor;
  28. }
  29. starpu_codelet mycodelet = {
  30. .where = STARPU_CPU,
  31. .cpu_func = func_cpu,
  32. .nbuffers = 2
  33. };
  34. int main(int argc, char **argv)
  35. {
  36. int x; float f;
  37. int i, ret;
  38. int ifactor=12;
  39. float ffactor=10.0;
  40. starpu_data_handle data_handles[2];
  41. ret = starpu_init(NULL);
  42. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  43. x = 1;
  44. starpu_variable_data_register(&data_handles[0], 0, (uintptr_t)&x, sizeof(x));
  45. f = 2.0;
  46. starpu_variable_data_register(&data_handles[1], 0, (uintptr_t)&f, sizeof(f));
  47. FPRINTF(stderr, "VALUES: %d (%d) %f (%f)\n", x, ifactor, f, ffactor);
  48. ret = starpu_insert_task(&mycodelet,
  49. STARPU_VALUE, &ifactor, sizeof(ifactor),
  50. STARPU_VALUE, &ffactor, sizeof(ffactor),
  51. STARPU_RW, data_handles[0], STARPU_RW, data_handles[1],
  52. 0);
  53. if (ret == -ENODEV) goto enodev;
  54. STARPU_CHECK_RETURN_VALUE(ret, "starpu_insert_task");
  55. ret = starpu_task_wait_for_all();
  56. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_wait_for_all");
  57. for(i=0 ; i<2 ; i++) {
  58. ret = starpu_data_acquire(data_handles[i], STARPU_R);
  59. STARPU_CHECK_RETURN_VALUE(ret, "starpu_data_acquire");
  60. }
  61. FPRINTF(stderr, "VALUES: %d %f\n", x, f);
  62. for(i=0 ; i<2 ; i++) {
  63. starpu_data_release(data_handles[i]);
  64. }
  65. struct starpu_task *task = starpu_task_create();
  66. task->cl = &mycodelet;
  67. task->buffers[0].handle = data_handles[0];
  68. task->buffers[0].mode = STARPU_RW;
  69. task->buffers[1].handle = data_handles[1];
  70. task->buffers[1].mode = STARPU_RW;
  71. char *arg_buffer;
  72. size_t arg_buffer_size;
  73. starpu_pack_cl_args(&arg_buffer, &arg_buffer_size,
  74. STARPU_VALUE, &ifactor, sizeof(ifactor),
  75. STARPU_VALUE, &ffactor, sizeof(ffactor),
  76. 0);
  77. task->cl_arg = arg_buffer;
  78. task->cl_arg_size = arg_buffer_size;
  79. starpu_task_submit(task);
  80. starpu_task_wait_for_all();
  81. for(i=0 ; i<2 ; i++) {
  82. ret = starpu_data_acquire(data_handles[i], STARPU_R);
  83. STARPU_CHECK_RETURN_VALUE(ret, "starpu_data_acquire");
  84. }
  85. FPRINTF(stderr, "VALUES: %d %f\n", x, f);
  86. starpu_shutdown();
  87. return 0;
  88. enodev:
  89. starpu_shutdown();
  90. fprintf(stderr, "WARNING: No one can execute this task\n");
  91. /* yes, we do not perform the computation but we did detect that no one
  92. * could perform the kernel, so this is not an error from StarPU */
  93. return 77;
  94. }