prologue.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 prologue callback. We submit a task, whose
  18. * prologue callback (i.e. before task gets scheduled) prints a value, and
  19. * whose pop_prologue callback (i.e. after task gets scheduled, but before task
  20. * execution) prints another value.
  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. printf("task executing \n");
  31. }
  32. struct starpu_codelet cl =
  33. {
  34. .modes = { STARPU_RW },
  35. .cpu_funcs = {cpu_codelet},
  36. .cpu_funcs_name = {"cpu_codelet"},
  37. .nbuffers = 1,
  38. .name = "callback"
  39. };
  40. void prologue_callback_func(void *callback_arg)
  41. {
  42. double *x = (double*)callback_arg;
  43. printf("x = %lf\n", *x);
  44. STARPU_ASSERT(*x == -999.0);
  45. }
  46. void pop_prologue_callback_func(void *args)
  47. {
  48. unsigned val = (uintptr_t) args;
  49. printf("pop_prologue_callback val %u \n", val);
  50. STARPU_ASSERT(val == 5);
  51. }
  52. int main(void)
  53. {
  54. int v=40;
  55. int ret;
  56. ret = starpu_init(NULL);
  57. if (ret == -ENODEV)
  58. return 77;
  59. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  60. starpu_variable_data_register(&handle, STARPU_MAIN_RAM, (uintptr_t)&v, sizeof(int));
  61. double x = -999.0;
  62. struct starpu_task *task = starpu_task_create();
  63. task->cl = &cl;
  64. task->prologue_callback_func = prologue_callback_func;
  65. task->prologue_callback_arg = &x;
  66. task->prologue_callback_pop_func = pop_prologue_callback_func;
  67. task->prologue_callback_pop_arg = (void*) 5;
  68. task->handles[0] = handle;
  69. ret = starpu_task_submit(task);
  70. if (ret == -ENODEV) goto enodev;
  71. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  72. ret = starpu_task_insert(&cl,
  73. STARPU_RW, handle,
  74. STARPU_PROLOGUE_CALLBACK, prologue_callback_func,
  75. STARPU_PROLOGUE_CALLBACK_ARG_NFREE, &x,
  76. STARPU_PROLOGUE_CALLBACK_POP, pop_prologue_callback_func,
  77. STARPU_PROLOGUE_CALLBACK_POP_ARG_NFREE, 5,
  78. 0);
  79. if (ret == -ENODEV) goto enodev;
  80. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_insert");
  81. starpu_task_wait_for_all();
  82. enodev:
  83. starpu_data_unregister(handle);
  84. FPRINTF(stderr, "v -> %d\n", v);
  85. starpu_shutdown();
  86. return (ret == -ENODEV) ? 77 : 0;
  87. }