acquire_release2.c 3.8 KB

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