callback.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. #define FPRINTF(ofile, fmt, ...) do { if (!getenv("STARPU_SSILENT")) {fprintf(ofile, fmt, ## __VA_ARGS__); }} while(0)
  19. starpu_data_handle_t handle;
  20. void cpu_codelet(void *descr[], STARPU_ATTRIBUTE_UNUSED void *_args)
  21. {
  22. int *val = (int *)STARPU_VARIABLE_GET_PTR(descr[0]);
  23. *val += 1;
  24. }
  25. struct starpu_codelet cl =
  26. {
  27. .modes = { STARPU_RW },
  28. .cpu_funcs = {cpu_codelet, NULL},
  29. .nbuffers = 1,
  30. .name = "callback"
  31. };
  32. void callback_func(void *callback_arg)
  33. {
  34. int ret;
  35. struct starpu_task *task = starpu_task_create();
  36. task->cl = &cl;
  37. task->handles[0] = handle;
  38. ret = starpu_task_submit(task);
  39. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  40. }
  41. int main(int argc, char **argv)
  42. {
  43. int v=40;
  44. int ret;
  45. ret = starpu_init(NULL);
  46. if (ret == -ENODEV)
  47. return 77;
  48. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  49. starpu_variable_data_register(&handle, STARPU_MAIN_RAM, (uintptr_t)&v, sizeof(int));
  50. struct starpu_task *task = starpu_task_create();
  51. task->cl = &cl;
  52. task->callback_func = callback_func;
  53. task->callback_arg = NULL;
  54. task->handles[0] = handle;
  55. ret = starpu_task_submit(task);
  56. if (ret == -ENODEV) goto enodev;
  57. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  58. starpu_task_wait_for_all();
  59. starpu_data_unregister(handle);
  60. FPRINTF(stderr, "v -> %d\n", v);
  61. starpu_shutdown();
  62. return 0;
  63. enodev:
  64. starpu_shutdown();
  65. return 77;
  66. }