callback.c 2.1 KB

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