acquire_release.c 3.9 KB

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