acquire_release2.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010, 2011, 2012, 2013 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. #include "../helper.h"
  19. #ifdef STARPU_QUICK_CHECK
  20. static unsigned ntasks = 40;
  21. #else
  22. static unsigned ntasks = 40000;
  23. #endif
  24. #ifdef STARPU_USE_CUDA
  25. extern void increment_cuda(void *descr[], STARPU_ATTRIBUTE_UNUSED void *_args);
  26. #endif
  27. #ifdef STARPU_USE_OPENCL
  28. extern void increment_opencl(void *buffers[], void *args);
  29. #endif
  30. void increment_cpu(void *descr[], STARPU_ATTRIBUTE_UNUSED void *_args)
  31. {
  32. STARPU_SKIP_IF_VALGRIND;
  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. #endif
  43. #ifdef STARPU_USE_OPENCL
  44. .opencl_funcs = {increment_opencl, NULL},
  45. #endif
  46. .cpu_funcs_name = {"increment_cpu", NULL},
  47. .nbuffers = 1
  48. };
  49. unsigned token = 0;
  50. starpu_data_handle_t token_handle;
  51. int increment_token(int synchronous)
  52. {
  53. struct starpu_task *task = starpu_task_create();
  54. task->synchronous = synchronous;
  55. task->cl = &increment_cl;
  56. task->handles[0] = token_handle;
  57. return starpu_task_submit(task);
  58. }
  59. void callback(void *arg STARPU_ATTRIBUTE_UNUSED)
  60. {
  61. starpu_data_release(token_handle);
  62. }
  63. #ifdef STARPU_DEVEL
  64. # warning TODO add threads
  65. #endif
  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, 0, (uintptr_t)&token, sizeof(unsigned));
  82. FPRINTF(stderr, "Token: %u\n", token);
  83. for(i=0; i<ntasks; i++)
  84. {
  85. ret = starpu_data_acquire_cb(token_handle, STARPU_W, callback, NULL); // recv
  86. STARPU_CHECK_RETURN_VALUE(ret, "starpu_data_acquire_cb");
  87. ret = increment_token(0);
  88. if (ret == -ENODEV) goto enodev;
  89. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  90. starpu_data_acquire_cb(token_handle, STARPU_R, callback, NULL); // send
  91. STARPU_CHECK_RETURN_VALUE(ret, "starpu_data_acquire_cb");
  92. }
  93. starpu_data_unregister(token_handle);
  94. #ifdef STARPU_USE_OPENCL
  95. ret = starpu_opencl_unload_opencl(&opencl_program);
  96. STARPU_CHECK_RETURN_VALUE(ret, "starpu_opencl_unload_opencl");
  97. #endif
  98. starpu_shutdown();
  99. FPRINTF(stderr, "Token: %u\n", token);
  100. if (token == ntasks)
  101. ret = EXIT_SUCCESS;
  102. else
  103. ret = EXIT_FAILURE;
  104. STARPU_RETURN(ret);
  105. enodev:
  106. starpu_data_unregister(token_handle);
  107. fprintf(stderr, "WARNING: No one can execute this task\n");
  108. /* yes, we do not perform the computation but we did detect that no one
  109. * could perform the kernel, so this is not an error from StarPU */
  110. #ifdef STARPU_USE_OPENCL
  111. ret = starpu_opencl_unload_opencl(&opencl_program);
  112. STARPU_CHECK_RETURN_VALUE(ret, "starpu_opencl_unload_opencl");
  113. #endif
  114. starpu_shutdown();
  115. return STARPU_TEST_SKIPPED;
  116. }