acquire_release2.c 4.0 KB

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