acquire_release2.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. #include "../common/helper.h"
  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. int 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. return 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. int ret;
  55. ret = starpu_init(NULL);
  56. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  57. starpu_variable_data_register(&token_handle, 0, (uintptr_t)&token, sizeof(unsigned));
  58. FPRINTF(stderr, "Token: %u\n", token);
  59. #ifdef STARPU_SLOW_MACHINE
  60. ntasks /= 10;
  61. #endif
  62. for(i=0; i<ntasks; i++)
  63. {
  64. ret = starpu_data_acquire_cb(token_handle, STARPU_W, callback, NULL); // recv
  65. STARPU_CHECK_RETURN_VALUE(ret, "starpu_data_acquire_cb");
  66. ret = increment_token(0);
  67. if (ret == -ENODEV) goto enodev;
  68. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  69. starpu_data_acquire_cb(token_handle, STARPU_R, callback, NULL); // send
  70. STARPU_CHECK_RETURN_VALUE(ret, "starpu_data_acquire_cb");
  71. }
  72. starpu_data_unregister(token_handle);
  73. FPRINTF(stderr, "Token: %u\n", token);
  74. assert(token==ntasks);
  75. starpu_shutdown();
  76. return 0;
  77. enodev:
  78. starpu_data_unregister(token_handle);
  79. fprintf(stderr, "WARNING: No one can execute this task\n");
  80. /* yes, we do not perform the computation but we did detect that no one
  81. * could perform the kernel, so this is not an error from StarPU */
  82. starpu_shutdown();
  83. return 77;
  84. }