callback.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009, 2010, 2013, 2015 Université de Bordeaux
  4. * Copyright (C) 2010, 2011, 2012, 2013 CNRS
  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. /*
  18. * This is an example of using a callback. We submit a task, whose callback
  19. * submits another task (without any callback).
  20. */
  21. #include <starpu.h>
  22. #define FPRINTF(ofile, fmt, ...) do { if (!getenv("STARPU_SSILENT")) {fprintf(ofile, fmt, ## __VA_ARGS__); }} while(0)
  23. starpu_data_handle_t handle;
  24. void cpu_codelet(void *descr[], STARPU_ATTRIBUTE_UNUSED void *_args)
  25. {
  26. int *val = (int *)STARPU_VARIABLE_GET_PTR(descr[0]);
  27. *val += 1;
  28. }
  29. struct starpu_codelet cl =
  30. {
  31. .modes = { STARPU_RW },
  32. .cpu_funcs = {cpu_codelet},
  33. .nbuffers = 1,
  34. .name = "callback"
  35. };
  36. void callback_func(void *callback_arg)
  37. {
  38. int ret;
  39. struct starpu_task *task = starpu_task_create();
  40. task->cl = &cl;
  41. task->handles[0] = handle;
  42. ret = starpu_task_submit(task);
  43. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  44. }
  45. int main(int argc, char **argv)
  46. {
  47. int v=40;
  48. int ret;
  49. ret = starpu_init(NULL);
  50. if (ret == -ENODEV)
  51. return 77;
  52. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  53. starpu_variable_data_register(&handle, STARPU_MAIN_RAM, (uintptr_t)&v, sizeof(int));
  54. struct starpu_task *task = starpu_task_create();
  55. task->cl = &cl;
  56. task->callback_func = callback_func;
  57. task->callback_arg = NULL;
  58. task->handles[0] = handle;
  59. ret = starpu_task_submit(task);
  60. if (ret == -ENODEV) goto enodev;
  61. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  62. starpu_task_wait_for_all();
  63. starpu_data_unregister(handle);
  64. FPRINTF(stderr, "v -> %d\n", v);
  65. starpu_shutdown();
  66. return (v == 42) ? 0 : 1;
  67. enodev:
  68. starpu_shutdown();
  69. return 77;
  70. }