acquire_release.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010 Université de Bordeaux 1
  4. * Copyright (C) 2010, 2011, 2012 Centre National de la Recherche Scientifique
  5. *
  6. * StarPU is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation; either version 2.1 of the License, or (at
  9. * your option) any later version.
  10. *
  11. * StarPU is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. *
  15. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  16. */
  17. #include <config.h>
  18. #include <starpu.h>
  19. #include "../helper.h"
  20. #ifdef STARPU_SLOW_MACHINE
  21. static unsigned ntasks = 10;
  22. #else
  23. static unsigned ntasks = 10000;
  24. #endif
  25. #ifdef STARPU_USE_CUDA
  26. extern void increment_cuda(void *descr[], __attribute__ ((unused)) void *_args);
  27. #endif
  28. void increment_cpu(void *descr[], __attribute__ ((unused)) void *_args)
  29. {
  30. STARPU_SKIP_IF_VALGRIND;
  31. unsigned *tokenptr = (unsigned *)STARPU_VARIABLE_GET_PTR(descr[0]);
  32. (*tokenptr)++;
  33. }
  34. static struct starpu_codelet increment_cl =
  35. {
  36. .modes = { STARPU_RW },
  37. .where = STARPU_CPU|STARPU_CUDA,
  38. .cpu_funcs = {increment_cpu, NULL},
  39. #ifdef STARPU_USE_CUDA
  40. .cuda_funcs = {increment_cuda, NULL},
  41. #endif
  42. .nbuffers = 1
  43. };
  44. unsigned token = 0;
  45. starpu_data_handle_t token_handle;
  46. int increment_token()
  47. {
  48. int ret;
  49. struct starpu_task *task = starpu_task_create();
  50. task->synchronous = 1;
  51. task->cl = &increment_cl;
  52. task->handles[0] = token_handle;
  53. ret = starpu_task_submit(task);
  54. starpu_task_destroy(task);
  55. return ret;
  56. }
  57. void callback(void *arg __attribute__ ((unused)))
  58. {
  59. starpu_data_release(token_handle);
  60. }
  61. int main(int argc, char **argv)
  62. {
  63. int i;
  64. int ret;
  65. ret = starpu_init(NULL);
  66. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  67. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  68. starpu_variable_data_register(&token_handle, 0, (uintptr_t)&token, sizeof(unsigned));
  69. FPRINTF(stderr, "Token: %u\n", token);
  70. for(i=0; i<ntasks; i++)
  71. {
  72. /* synchronize data in RAM */
  73. ret = starpu_data_acquire(token_handle, STARPU_R);
  74. STARPU_CHECK_RETURN_VALUE(ret, "starpu_data_acquire");
  75. token ++;
  76. starpu_data_release(token_handle);
  77. ret = increment_token();
  78. if (ret == -ENODEV) goto enodev;
  79. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  80. ret = starpu_data_acquire_cb(token_handle, STARPU_RW, callback, NULL);
  81. STARPU_CHECK_RETURN_VALUE(ret, "starpu_data_acquire_cb");
  82. }
  83. starpu_data_unregister(token_handle);
  84. starpu_shutdown();
  85. FPRINTF(stderr, "Token: %u\n", token);
  86. if (token == ntasks * 2)
  87. return EXIT_SUCCESS;
  88. else
  89. return EXIT_FAILURE;
  90. enodev:
  91. starpu_data_unregister(token_handle);
  92. fprintf(stderr, "WARNING: No one can execute this task\n");
  93. /* yes, we do not perform the computation but we did detect that no one
  94. * could perform the kernel, so this is not an error from StarPU */
  95. starpu_shutdown();
  96. return STARPU_TEST_SKIPPED;
  97. }