acquire_release2.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010, 2011, 2012 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 <config.h>
  17. #include <starpu.h>
  18. #include "../helper.h"
  19. static unsigned ntasks = 40000;
  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. STARPU_SKIP_IF_VALGRIND;
  26. unsigned *tokenptr = (unsigned *)STARPU_VARIABLE_GET_PTR(descr[0]);
  27. (*tokenptr)++;
  28. }
  29. static struct starpu_codelet increment_cl =
  30. {
  31. .modes = { STARPU_RW },
  32. .where = STARPU_CPU|STARPU_CUDA,
  33. .cpu_funcs = {increment_cpu, NULL},
  34. #ifdef STARPU_USE_CUDA
  35. .cuda_funcs = {increment_cuda, NULL},
  36. #endif
  37. .nbuffers = 1
  38. };
  39. unsigned token = 0;
  40. starpu_data_handle_t token_handle;
  41. int increment_token(int synchronous)
  42. {
  43. struct starpu_task *task = starpu_task_create();
  44. task->synchronous = synchronous;
  45. task->cl = &increment_cl;
  46. task->handles[0] = token_handle;
  47. return starpu_task_submit(task);
  48. }
  49. void callback(void *arg __attribute__ ((unused)))
  50. {
  51. starpu_data_release(token_handle);
  52. }
  53. #warning TODO add threads
  54. int main(int argc, char **argv)
  55. {
  56. int i;
  57. int ret;
  58. ret = starpu_init(NULL);
  59. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  60. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  61. starpu_variable_data_register(&token_handle, 0, (uintptr_t)&token, sizeof(unsigned));
  62. FPRINTF(stderr, "Token: %u\n", token);
  63. #ifdef STARPU_SLOW_MACHINE
  64. ntasks /= 10;
  65. #endif
  66. for(i=0; i<ntasks; i++)
  67. {
  68. ret = starpu_data_acquire_cb(token_handle, STARPU_W, callback, NULL); // recv
  69. STARPU_CHECK_RETURN_VALUE(ret, "starpu_data_acquire_cb");
  70. ret = increment_token(0);
  71. if (ret == -ENODEV) goto enodev;
  72. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  73. starpu_data_acquire_cb(token_handle, STARPU_R, callback, NULL); // send
  74. STARPU_CHECK_RETURN_VALUE(ret, "starpu_data_acquire_cb");
  75. }
  76. starpu_data_unregister(token_handle);
  77. starpu_shutdown();
  78. FPRINTF(stderr, "Token: %u\n", token);
  79. if (token == ntasks)
  80. return EXIT_SUCCESS;
  81. else
  82. return EXIT_FAILURE;
  83. enodev:
  84. starpu_data_unregister(token_handle);
  85. fprintf(stderr, "WARNING: No one can execute this task\n");
  86. /* yes, we do not perform the computation but we did detect that no one
  87. * could perform the kernel, so this is not an error from StarPU */
  88. starpu_shutdown();
  89. return STARPU_TEST_SKIPPED;
  90. }