callback.c 2.3 KB

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