acquire_release.c 2.3 KB

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