callback.c 2.2 KB

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