prologue.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. #define FPRINTF(ofile, fmt, ...) do { if (!getenv("STARPU_SSILENT")) {fprintf(ofile, fmt, ## __VA_ARGS__); }} while(0)
  19. starpu_data_handle_t handle;
  20. void cpu_codelet(void *descr[], STARPU_ATTRIBUTE_UNUSED void *_args)
  21. {
  22. int *val = (int *)STARPU_VARIABLE_GET_PTR(descr[0]);
  23. *val += 1;
  24. printf("task executing \n");
  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. void pop_prologue_callback_func(void *args)
  48. {
  49. unsigned val = (unsigned) args;
  50. printf("pop_prologue_callback val %d \n", val);
  51. }
  52. int main(int argc, char **argv)
  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. struct starpu_task *task = starpu_task_create();
  62. task->cl = &cl;
  63. task->prologue_callback_func = callback_func;
  64. task->prologue_callback_arg = NULL;
  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. double *x = (double*)malloc(sizeof(double));
  72. *x = -999.0;
  73. int ret2 = starpu_task_insert(&cl,
  74. STARPU_RW, handle,
  75. STARPU_PROLOGUE_CALLBACK, prologue_callback_func,
  76. STARPU_PROLOGUE_CALLBACK_ARG, x,
  77. STARPU_PROLOGUE_CALLBACK_POP, pop_prologue_callback_func,
  78. STARPU_PROLOGUE_CALLBACK_POP_ARG, 5,
  79. 0);
  80. starpu_task_wait_for_all();
  81. starpu_data_unregister(handle);
  82. FPRINTF(stderr, "v -> %d\n", v);
  83. free(x);
  84. starpu_shutdown();
  85. return 0;
  86. enodev:
  87. starpu_shutdown();
  88. return 77;
  89. }