acquire_release.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*
  2. * StarPU
  3. * Copyright (C) Université Bordeaux 1, CNRS 2008-2010 (see AUTHORS file)
  4. *
  5. * This program 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. * This program 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. #include <starpu.h>
  17. static unsigned ntasks = 400;
  18. void increment_cpu(void *descr[], __attribute__ ((unused)) void *_args)
  19. {
  20. unsigned *tokenptr = (unsigned *)STARPU_VARIABLE_GET_PTR(descr[0]);
  21. (*tokenptr)++;
  22. }
  23. static starpu_codelet increment_cl = {
  24. .where = STARPU_CPU,
  25. .cpu_func = increment_cpu,
  26. .nbuffers = 1
  27. };
  28. unsigned token = 0;
  29. starpu_data_handle token_handle;
  30. void increment_token()
  31. {
  32. struct starpu_task *task = starpu_task_create();
  33. task->cl = &increment_cl;
  34. task->buffers[0].handle = token_handle;
  35. task->buffers[0].mode = STARPU_RW;
  36. starpu_task_submit(task);
  37. }
  38. void callback(void *arg __attribute__ ((unused)))
  39. {
  40. starpu_data_release(token_handle);
  41. }
  42. int main(int argc, char **argv)
  43. {
  44. int i;
  45. starpu_init(NULL);
  46. starpu_variable_data_register(&token_handle, 0, (uintptr_t)&token, sizeof(unsigned));
  47. fprintf(stderr, "Token: %d\n", token);
  48. for(i=0; i<ntasks; i++)
  49. {
  50. starpu_data_acquire(token_handle, STARPU_RW);
  51. increment_token();
  52. starpu_data_release(token_handle);
  53. starpu_data_acquire_cb(token_handle, STARPU_RW, callback, NULL);
  54. }
  55. starpu_task_wait_for_all();
  56. starpu_shutdown();
  57. fprintf(stderr, "Token: %d\n", token);
  58. STARPU_ASSERT(token==ntasks);
  59. return 0;
  60. }