acquire_release2.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010, 2011 Centre National de la Recherche Scientifique
  4. *
  5. * StarPU 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. * StarPU 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. #define FPRINTF(ofile, fmt, args ...) do { if (!getenv("STARPU_SSILENT")) {fprintf(ofile, fmt, ##args); }} while(0)
  18. static unsigned ntasks = 40000;
  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(int synchronous)
  38. {
  39. struct starpu_task *task = starpu_task_create();
  40. task->synchronous = synchronous;
  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. #warning TODO add threads
  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. #ifdef STARPU_SLOW_MACHINE
  58. ntasks /= 10;
  59. #endif
  60. for(i=0; i<ntasks; i++)
  61. {
  62. starpu_data_acquire_cb(token_handle, STARPU_W, callback, NULL); // recv
  63. increment_token(0);
  64. starpu_data_acquire_cb(token_handle, STARPU_R, callback, NULL); // send
  65. }
  66. starpu_data_unregister(token_handle);
  67. FPRINTF(stderr, "Token: %u\n", token);
  68. assert(token==ntasks);
  69. starpu_shutdown();
  70. return 0;
  71. }