prologue.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009, 2010, 2013-2015 Université de Bordeaux
  4. * Copyright (C) 2010, 2011, 2012, 2013, 2015 CNRS
  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. /*
  18. * This is an example of using a prologue callback. We submit a task, whose
  19. * prologue callback (i.e. before task gets scheduled) prints a value, and
  20. * whose pop_prologue callback (i.e. after task gets scheduled, but before task
  21. * execution) prints another value.
  22. */
  23. #include <starpu.h>
  24. #define FPRINTF(ofile, fmt, ...) do { if (!getenv("STARPU_SSILENT")) {fprintf(ofile, fmt, ## __VA_ARGS__); }} while(0)
  25. starpu_data_handle_t handle;
  26. void cpu_codelet(void *descr[], STARPU_ATTRIBUTE_UNUSED void *_args)
  27. {
  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. .nbuffers = 1,
  37. .name = "callback"
  38. };
  39. void prologue_callback_func(void *callback_arg)
  40. {
  41. double *x = (double*)callback_arg;
  42. printf("x = %lf\n", *x);
  43. STARPU_ASSERT(*x == -999.0);
  44. }
  45. void pop_prologue_callback_func(void *args)
  46. {
  47. unsigned val = (uintptr_t) args;
  48. printf("pop_prologue_callback val %d \n", val);
  49. STARPU_ASSERT(val == 5);
  50. }
  51. int main(int argc, char **argv)
  52. {
  53. int v=40;
  54. int ret;
  55. ret = starpu_init(NULL);
  56. if (ret == -ENODEV)
  57. return 77;
  58. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  59. starpu_variable_data_register(&handle, STARPU_MAIN_RAM, (uintptr_t)&v, sizeof(int));
  60. double x = -999.0;
  61. struct starpu_task *task = starpu_task_create();
  62. task->cl = &cl;
  63. task->prologue_callback_func = prologue_callback_func;
  64. task->prologue_callback_arg = &x;
  65. task->prologue_callback_pop_func = pop_prologue_callback_func;
  66. task->prologue_callback_pop_arg = (void*) 5;
  67. task->handles[0] = handle;
  68. ret = starpu_task_submit(task);
  69. if (ret == -ENODEV) goto enodev;
  70. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  71. ret = starpu_task_insert(&cl,
  72. STARPU_RW, handle,
  73. STARPU_PROLOGUE_CALLBACK, prologue_callback_func,
  74. STARPU_PROLOGUE_CALLBACK_ARG, &x,
  75. STARPU_PROLOGUE_CALLBACK_POP, pop_prologue_callback_func,
  76. STARPU_PROLOGUE_CALLBACK_POP_ARG, 5,
  77. 0);
  78. if (ret == -ENODEV) goto enodev;
  79. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_insert");
  80. starpu_task_wait_for_all();
  81. enodev:
  82. starpu_data_unregister(handle);
  83. FPRINTF(stderr, "v -> %d\n", v);
  84. starpu_shutdown();
  85. return (ret == -ENODEV) ? 77 : 0;
  86. }