prologue.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2013-2014 Inria
  4. * Copyright (C) 2010-2017 CNRS
  5. * Copyright (C) 2009-2010,2013-2015 Université de Bordeaux
  6. *
  7. * StarPU is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation; either version 2.1 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * StarPU is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. *
  16. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  17. */
  18. /*
  19. * This is an example of using a prologue callback. We submit a task, whose
  20. * prologue callback (i.e. before task gets scheduled) prints a value, and
  21. * whose pop_prologue callback (i.e. after task gets scheduled, but before task
  22. * execution) prints another value.
  23. */
  24. #include <starpu.h>
  25. #define FPRINTF(ofile, fmt, ...) do { if (!getenv("STARPU_SSILENT")) {fprintf(ofile, fmt, ## __VA_ARGS__); }} while(0)
  26. starpu_data_handle_t handle;
  27. void cpu_codelet(void *descr[], void *_args)
  28. {
  29. (void)_args;
  30. int *val = (int *)STARPU_VARIABLE_GET_PTR(descr[0]);
  31. *val += 1;
  32. printf("task executing \n");
  33. }
  34. struct starpu_codelet cl =
  35. {
  36. .modes = { STARPU_RW },
  37. .cpu_funcs = {cpu_codelet},
  38. .cpu_funcs_name = {"cpu_codelet"},
  39. .nbuffers = 1,
  40. .name = "callback"
  41. };
  42. void prologue_callback_func(void *callback_arg)
  43. {
  44. double *x = (double*)callback_arg;
  45. printf("x = %lf\n", *x);
  46. STARPU_ASSERT(*x == -999.0);
  47. }
  48. void pop_prologue_callback_func(void *args)
  49. {
  50. unsigned val = (uintptr_t) args;
  51. printf("pop_prologue_callback val %u \n", val);
  52. STARPU_ASSERT(val == 5);
  53. }
  54. int main(void)
  55. {
  56. int v=40;
  57. int ret;
  58. ret = starpu_init(NULL);
  59. if (ret == -ENODEV)
  60. return 77;
  61. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  62. starpu_variable_data_register(&handle, STARPU_MAIN_RAM, (uintptr_t)&v, sizeof(int));
  63. double x = -999.0;
  64. struct starpu_task *task = starpu_task_create();
  65. task->cl = &cl;
  66. task->prologue_callback_func = prologue_callback_func;
  67. task->prologue_callback_arg = &x;
  68. task->prologue_callback_pop_func = pop_prologue_callback_func;
  69. task->prologue_callback_pop_arg = (void*) 5;
  70. task->handles[0] = handle;
  71. ret = starpu_task_submit(task);
  72. if (ret == -ENODEV) goto enodev;
  73. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  74. ret = starpu_task_insert(&cl,
  75. STARPU_RW, handle,
  76. STARPU_PROLOGUE_CALLBACK, prologue_callback_func,
  77. STARPU_PROLOGUE_CALLBACK_ARG, &x,
  78. STARPU_PROLOGUE_CALLBACK_POP, pop_prologue_callback_func,
  79. STARPU_PROLOGUE_CALLBACK_POP_ARG, 5,
  80. 0);
  81. if (ret == -ENODEV) goto enodev;
  82. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_insert");
  83. starpu_task_wait_for_all();
  84. enodev:
  85. starpu_data_unregister(handle);
  86. FPRINTF(stderr, "v -> %d\n", v);
  87. starpu_shutdown();
  88. return (ret == -ENODEV) ? 77 : 0;
  89. }