acquire_release.c 4.0 KB

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