insert_task.c 3.1 KB

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