acquire_release2.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2011-2013 Inria
  4. * Copyright (C) 2010-2013,2015,2017 CNRS
  5. * Copyright (C) 2011,2013-2016 Université de Bordeaux
  6. *
  7. * StarPU is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation; either version 2.1 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * StarPU is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. *
  16. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  17. */
  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 = 40;
  25. #elif !defined(STARPU_LONG_CHECK)
  26. static unsigned ntasks = 4000;
  27. #else
  28. static unsigned ntasks = 40000;
  29. #endif
  30. #ifdef STARPU_USE_CUDA
  31. extern void increment_cuda(void *descr[], void *_args);
  32. #endif
  33. #ifdef STARPU_USE_OPENCL
  34. extern void increment_opencl(void *buffers[], void *args);
  35. #endif
  36. void increment_cpu(void *descr[], void *arg)
  37. {
  38. (void)arg;
  39. unsigned *tokenptr = (unsigned *)STARPU_VARIABLE_GET_PTR(descr[0]);
  40. (*tokenptr)++;
  41. }
  42. static struct starpu_codelet increment_cl =
  43. {
  44. .modes = { STARPU_RW },
  45. .cpu_funcs = {increment_cpu},
  46. #ifdef STARPU_USE_CUDA
  47. .cuda_funcs = {increment_cuda},
  48. .cuda_flags = {STARPU_CUDA_ASYNC},
  49. #endif
  50. #ifdef STARPU_USE_OPENCL
  51. .opencl_funcs = {increment_opencl},
  52. .opencl_flags = {STARPU_OPENCL_ASYNC},
  53. #endif
  54. .cpu_funcs_name = {"increment_cpu"},
  55. .nbuffers = 1
  56. };
  57. unsigned token = 0;
  58. starpu_data_handle_t token_handle;
  59. static
  60. int increment_token(int synchronous)
  61. {
  62. struct starpu_task *task = starpu_task_create();
  63. task->synchronous = synchronous;
  64. task->cl = &increment_cl;
  65. task->handles[0] = token_handle;
  66. return starpu_task_submit(task);
  67. }
  68. static
  69. void callback(void *arg)
  70. {
  71. (void)arg;
  72. starpu_data_release(token_handle);
  73. }
  74. #ifdef STARPU_DEVEL
  75. # warning TODO add threads
  76. #endif
  77. #ifdef STARPU_USE_OPENCL
  78. struct starpu_opencl_program opencl_program;
  79. #endif
  80. int main(int argc, char **argv)
  81. {
  82. unsigned i;
  83. int ret;
  84. ret = starpu_initialize(NULL, &argc, &argv);
  85. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  86. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  87. #ifdef STARPU_USE_OPENCL
  88. ret = starpu_opencl_load_opencl_from_file("tests/datawizard/acquire_release_opencl_kernel.cl",
  89. &opencl_program, NULL);
  90. STARPU_CHECK_RETURN_VALUE(ret, "starpu_opencl_load_opencl_from_file");
  91. #endif
  92. starpu_variable_data_register(&token_handle, STARPU_MAIN_RAM, (uintptr_t)&token, sizeof(unsigned));
  93. FPRINTF(stderr, "Token: %u\n", token);
  94. for(i=0; i<ntasks; i++)
  95. {
  96. ret = starpu_data_acquire_cb(token_handle, STARPU_W, callback, NULL); // recv
  97. STARPU_CHECK_RETURN_VALUE(ret, "starpu_data_acquire_cb");
  98. ret = increment_token(0);
  99. if (ret == -ENODEV) goto enodev;
  100. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  101. ret = starpu_data_acquire_cb(token_handle, STARPU_R, callback, NULL); // send
  102. STARPU_CHECK_RETURN_VALUE(ret, "starpu_data_acquire_cb");
  103. }
  104. starpu_data_unregister(token_handle);
  105. #ifdef STARPU_USE_OPENCL
  106. ret = starpu_opencl_unload_opencl(&opencl_program);
  107. STARPU_CHECK_RETURN_VALUE(ret, "starpu_opencl_unload_opencl");
  108. #endif
  109. starpu_shutdown();
  110. FPRINTF(stderr, "Token: %u\n", token);
  111. if (token == ntasks)
  112. ret = EXIT_SUCCESS;
  113. else
  114. ret = EXIT_FAILURE;
  115. return ret;
  116. enodev:
  117. starpu_data_unregister(token_handle);
  118. fprintf(stderr, "WARNING: No one can execute this task\n");
  119. /* yes, we do not perform the computation but we did detect that no one
  120. * could perform the kernel, so this is not an error from StarPU */
  121. #ifdef STARPU_USE_OPENCL
  122. ret = starpu_opencl_unload_opencl(&opencl_program);
  123. STARPU_CHECK_RETURN_VALUE(ret, "starpu_opencl_unload_opencl");
  124. #endif
  125. starpu_shutdown();
  126. return STARPU_TEST_SKIPPED;
  127. }