insert_task.c 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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;
  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. 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. starpu_task_wait_for_all();
  52. for(i=0 ; i<2 ; i++) {
  53. starpu_data_acquire(data_handles[i], STARPU_R);
  54. }
  55. FPRINTF(stderr, "VALUES: %d %f\n", x, f);
  56. for(i=0 ; i<2 ; i++) {
  57. starpu_data_release(data_handles[i]);
  58. }
  59. struct starpu_task *task = starpu_task_create();
  60. task->cl = &mycodelet;
  61. task->buffers[0].handle = data_handles[0];
  62. task->buffers[0].mode = STARPU_RW;
  63. task->buffers[1].handle = data_handles[1];
  64. task->buffers[1].mode = STARPU_RW;
  65. char *arg_buffer;
  66. size_t arg_buffer_size;
  67. starpu_pack_cl_args(&arg_buffer, &arg_buffer_size,
  68. STARPU_VALUE, &ifactor, sizeof(ifactor),
  69. STARPU_VALUE, &ffactor, sizeof(ffactor),
  70. 0);
  71. task->cl_arg = arg_buffer;
  72. task->cl_arg_size = arg_buffer_size;
  73. starpu_task_submit(task);
  74. starpu_task_wait_for_all();
  75. for(i=0 ; i<2 ; i++) {
  76. starpu_data_acquire(data_handles[i], STARPU_R);
  77. }
  78. FPRINTF(stderr, "VALUES: %d %f\n", x, f);
  79. starpu_shutdown();
  80. return 0;
  81. }