acquire_release.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010, 2014, 2016 Université de Bordeaux
  4. * Copyright (C) 2010, 2011, 2012, 2013 CNRS
  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. /*
  21. * Call acquire/release in competition with inserting task working on the same data
  22. */
  23. #ifdef STARPU_QUICK_CHECK
  24. static unsigned ntasks = 10;
  25. #else
  26. static unsigned ntasks = 10000;
  27. #endif
  28. #ifdef STARPU_USE_CUDA
  29. extern void increment_cuda(void *descr[], STARPU_ATTRIBUTE_UNUSED void *_args);
  30. #endif
  31. #ifdef STARPU_USE_OPENCL
  32. extern void increment_opencl(void *buffers[], void *args);
  33. #endif
  34. void increment_cpu(void *descr[], STARPU_ATTRIBUTE_UNUSED void *_args)
  35. {
  36. unsigned *tokenptr = (unsigned *)STARPU_VARIABLE_GET_PTR(descr[0]);
  37. (*tokenptr)++;
  38. }
  39. static struct starpu_codelet increment_cl =
  40. {
  41. .modes = { STARPU_RW },
  42. .cpu_funcs = {increment_cpu},
  43. #ifdef STARPU_USE_CUDA
  44. .cuda_funcs = {increment_cuda},
  45. .cuda_flags = {STARPU_CUDA_ASYNC},
  46. #endif
  47. #ifdef STARPU_USE_OPENCL
  48. .opencl_funcs = {increment_opencl},
  49. .opencl_flags = {STARPU_OPENCL_ASYNC},
  50. #endif
  51. .cpu_funcs_name = {"increment_cpu"},
  52. .nbuffers = 1
  53. };
  54. unsigned token = 0;
  55. starpu_data_handle_t token_handle;
  56. static
  57. int increment_token(void)
  58. {
  59. int ret;
  60. struct starpu_task *task = starpu_task_create();
  61. task->synchronous = 1;
  62. task->cl = &increment_cl;
  63. task->handles[0] = token_handle;
  64. ret = starpu_task_submit(task);
  65. return ret;
  66. }
  67. static
  68. void callback(void *arg STARPU_ATTRIBUTE_UNUSED)
  69. {
  70. starpu_data_release(token_handle);
  71. }
  72. #ifdef STARPU_USE_OPENCL
  73. struct starpu_opencl_program opencl_program;
  74. #endif
  75. int main(int argc, char **argv)
  76. {
  77. unsigned i;
  78. int ret;
  79. ret = starpu_initialize(NULL, &argc, &argv);
  80. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  81. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  82. #ifdef STARPU_USE_OPENCL
  83. ret = starpu_opencl_load_opencl_from_file("tests/datawizard/acquire_release_opencl_kernel.cl",
  84. &opencl_program, NULL);
  85. STARPU_CHECK_RETURN_VALUE(ret, "starpu_opencl_load_opencl_from_file");
  86. #endif
  87. starpu_variable_data_register(&token_handle, STARPU_MAIN_RAM, (uintptr_t)&token, sizeof(unsigned));
  88. FPRINTF(stderr, "Token: %u\n", token);
  89. for(i=0; i<ntasks; i++)
  90. {
  91. /* synchronize data in RAM */
  92. ret = starpu_data_acquire(token_handle, STARPU_R);
  93. STARPU_CHECK_RETURN_VALUE(ret, "starpu_data_acquire");
  94. token ++;
  95. starpu_data_release(token_handle);
  96. ret = increment_token();
  97. if (ret == -ENODEV) goto enodev;
  98. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  99. ret = starpu_data_acquire_cb(token_handle, STARPU_RW, callback, NULL);
  100. STARPU_CHECK_RETURN_VALUE(ret, "starpu_data_acquire_cb");
  101. }
  102. starpu_data_unregister(token_handle);
  103. #ifdef STARPU_USE_OPENCL
  104. ret = starpu_opencl_unload_opencl(&opencl_program);
  105. STARPU_CHECK_RETURN_VALUE(ret, "starpu_opencl_unload_opencl");
  106. #endif
  107. starpu_shutdown();
  108. FPRINTF(stderr, "Token: %u\n", token);
  109. if (token == ntasks * 2)
  110. ret = EXIT_SUCCESS;
  111. else
  112. ret = EXIT_FAILURE;
  113. return ret;
  114. enodev:
  115. starpu_data_unregister(token_handle);
  116. fprintf(stderr, "WARNING: No one can execute this task\n");
  117. /* yes, we do not perform the computation but we did detect that no one
  118. * could perform the kernel, so this is not an error from StarPU */
  119. #ifdef STARPU_USE_OPENCL
  120. ret = starpu_opencl_unload_opencl(&opencl_program);
  121. STARPU_CHECK_RETURN_VALUE(ret, "starpu_opencl_unload_opencl");
  122. #endif
  123. starpu_shutdown();
  124. return STARPU_TEST_SKIPPED;
  125. }