acquire_release2.c 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 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. for(i=0; i<ntasks; i++)
  58. {
  59. starpu_data_acquire_cb(token_handle, STARPU_W, callback, NULL); // recv
  60. increment_token(0);
  61. starpu_data_acquire_cb(token_handle, STARPU_R, callback, NULL); // send
  62. }
  63. starpu_data_unregister(token_handle);
  64. FPRINTF(stderr, "Token: %u\n", token);
  65. assert(token==ntasks);
  66. starpu_shutdown();
  67. return 0;
  68. }