acquire_release.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 = 10000;
  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->synchronous = 1;
  34. task->cl = &increment_cl;
  35. task->buffers[0].handle = token_handle;
  36. task->buffers[0].mode = STARPU_RW;
  37. starpu_task_submit(task);
  38. }
  39. void callback(void *arg __attribute__ ((unused)))
  40. {
  41. starpu_data_release(token_handle);
  42. }
  43. int main(int argc, char **argv)
  44. {
  45. int i;
  46. starpu_init(NULL);
  47. starpu_variable_data_register(&token_handle, 0, (uintptr_t)&token, sizeof(unsigned));
  48. fprintf(stderr, "Token: %d\n", token);
  49. for(i=0; i<ntasks; i++)
  50. {
  51. /* synchronize data in RAM */
  52. starpu_data_acquire(token_handle, STARPU_R);
  53. token ++;
  54. starpu_data_release(token_handle);
  55. increment_token();
  56. starpu_data_acquire_cb(token_handle, STARPU_R, callback, NULL);
  57. }
  58. starpu_data_unregister(token_handle);
  59. fprintf(stderr, "Token: %d\n", token);
  60. STARPU_ASSERT(token==ntasks*2);
  61. starpu_shutdown();
  62. return 0;
  63. }