acquire_release.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. #ifdef STARPU_USE_CUDA
  19. extern void increment_cuda(void *descr[], __attribute__ ((unused)) void *_args);
  20. #endif
  21. void increment_cpu(void *descr[], __attribute__ ((unused)) void *_args)
  22. {
  23. unsigned *tokenptr = (unsigned *)STARPU_VARIABLE_GET_PTR(descr[0]);
  24. (*tokenptr)++;
  25. }
  26. static starpu_codelet increment_cl = {
  27. .where = STARPU_CPU|STARPU_CUDA,
  28. .cpu_func = increment_cpu,
  29. #ifdef STARPU_USE_CUDA
  30. .cuda_func = increment_cuda,
  31. #endif
  32. .nbuffers = 1
  33. };
  34. unsigned token = 0;
  35. starpu_data_handle token_handle;
  36. void increment_token()
  37. {
  38. struct starpu_task *task = starpu_task_create();
  39. task->synchronous = 1;
  40. task->cl = &increment_cl;
  41. task->buffers[0].handle = token_handle;
  42. task->buffers[0].mode = STARPU_RW;
  43. starpu_task_submit(task);
  44. }
  45. void callback(void *arg __attribute__ ((unused)))
  46. {
  47. starpu_data_release(token_handle);
  48. }
  49. int main(int argc, char **argv)
  50. {
  51. int i;
  52. starpu_init(NULL);
  53. starpu_variable_data_register(&token_handle, 0, (uintptr_t)&token, sizeof(unsigned));
  54. fprintf(stderr, "Token: %d\n", token);
  55. for(i=0; i<ntasks; i++)
  56. {
  57. /* synchronize data in RAM */
  58. starpu_data_acquire(token_handle, STARPU_R);
  59. token ++;
  60. starpu_data_release(token_handle);
  61. increment_token();
  62. starpu_data_acquire_cb(token_handle, STARPU_RW, callback, NULL);
  63. }
  64. starpu_data_unregister(token_handle);
  65. fprintf(stderr, "Token: %d\n", token);
  66. STARPU_ASSERT(token==ntasks*2);
  67. starpu_shutdown();
  68. return 0;
  69. }