acquire_release.c 2.4 KB

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