callback.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009, 2010, 2013 Université de Bordeaux 1
  4. * Copyright (C) 2010, 2011, 2012, 2013 Centre National de la Recherche Scientifique
  5. *
  6. * StarPU is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation; either version 2.1 of the License, or (at
  9. * your option) any later version.
  10. *
  11. * StarPU is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. *
  15. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  16. */
  17. #include <starpu.h>
  18. #include <sys/time.h>
  19. #define FPRINTF(ofile, fmt, args ...) do { if (!getenv("STARPU_SSILENT")) {fprintf(ofile, fmt, ##args); }} while(0)
  20. starpu_data_handle_t handle;
  21. void cpu_codelet(void *descr[], __attribute__ ((unused)) void *_args)
  22. {
  23. int *val = (int *)STARPU_VARIABLE_GET_PTR(descr[0]);
  24. *val += 1;
  25. }
  26. struct starpu_codelet cl =
  27. {
  28. .modes = { STARPU_RW },
  29. .cpu_funcs = {cpu_codelet, NULL},
  30. .nbuffers = 1,
  31. .name = "callback"
  32. };
  33. void callback_func(void *callback_arg)
  34. {
  35. int ret;
  36. struct starpu_task *task = starpu_task_create();
  37. task->cl = &cl;
  38. task->handles[0] = handle;
  39. ret = starpu_task_submit(task);
  40. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  41. }
  42. int main(int argc, char **argv)
  43. {
  44. int v=40;
  45. int ret;
  46. ret = starpu_init(NULL);
  47. if (ret == -ENODEV)
  48. return 77;
  49. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  50. starpu_variable_data_register(&handle, 0, (uintptr_t)&v, sizeof(int));
  51. struct starpu_task *task = starpu_task_create();
  52. task->cl = &cl;
  53. task->callback_func = callback_func;
  54. task->callback_arg = NULL;
  55. task->handles[0] = handle;
  56. ret = starpu_task_submit(task);
  57. if (ret == -ENODEV) goto enodev;
  58. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  59. starpu_task_wait_for_all();
  60. starpu_data_unregister(handle);
  61. FPRINTF(stderr, "v -> %d\n", v);
  62. starpu_shutdown();
  63. return 0;
  64. enodev:
  65. starpu_shutdown();
  66. return 77;
  67. }