acquire_release2.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 = 40000;
  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(int synchronous)
  37. {
  38. struct starpu_task *task = starpu_task_create();
  39. task->synchronous = synchronous;
  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. #warning add threads
  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. starpu_data_acquire_cb(token_handle, STARPU_W, callback, NULL); // recv
  59. increment_token(0);
  60. starpu_data_acquire_cb(token_handle, STARPU_R, callback, NULL); // send
  61. }
  62. starpu_data_unregister(token_handle);
  63. fprintf(stderr, "Token: %d\n", token);
  64. assert(token==ntasks);
  65. starpu_shutdown();
  66. return 0;
  67. }