acquire_release.c 3.9 KB

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