prologue.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. #include <sys/time.h>
  19. #define FPRINTF(ofile, fmt, ...) do { if (!getenv("STARPU_SSILENT")) {fprintf(ofile, fmt, ## __VA_ARGS__); }} while(0)
  20. starpu_data_handle_t handle;
  21. void cpu_codelet(void *descr[], STARPU_ATTRIBUTE_UNUSED void *_args)
  22. {
  23. int *val = (int *)STARPU_VARIABLE_GET_PTR(descr[0]);
  24. *val += 1;
  25. }
  26. struct starpu_codelet cl =
  27. {
  28. .modes = { STARPU_RW },
  29. .cpu_funcs = {cpu_codelet, NULL},
  30. .nbuffers = 1,
  31. .name = "callback"
  32. };
  33. void callback_func(void *callback_arg)
  34. {
  35. int ret;
  36. struct starpu_task *task = starpu_task_create();
  37. task->cl = &cl;
  38. task->handles[0] = handle;
  39. ret = starpu_task_submit(task);
  40. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  41. }
  42. void prologue_callback_func(void *callback_arg)
  43. {
  44. double *x = (double*)callback_arg;
  45. printf("x = %lf\n", *x);
  46. }
  47. int main(int argc, char **argv)
  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->prologue_callback_func = callback_func;
  59. task->prologue_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. double *x = (double*)malloc(sizeof(double));
  65. *x = -999.0;
  66. int ret2 = starpu_task_insert(&cl,
  67. STARPU_RW, handle,
  68. STARPU_PROLOGUE_CALLBACK, prologue_callback_func,
  69. STARPU_PROLOGUE_CALLBACK_ARG, x,
  70. 0);
  71. starpu_task_wait_for_all();
  72. starpu_data_unregister(handle);
  73. FPRINTF(stderr, "v -> %d\n", v);
  74. free(x);
  75. starpu_shutdown();
  76. return 0;
  77. enodev:
  78. starpu_shutdown();
  79. return 77;
  80. }