oacc-cuda.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /* OpenACC Runtime Library: CUDA support glue.
  2. Copyright (C) 2014-2022 Free Software Foundation, Inc.
  3. Contributed by Mentor Embedded.
  4. This file is part of the GNU Offloading and Multi Processing Library
  5. (libgomp).
  6. Libgomp is free software; you can redistribute it and/or modify it
  7. under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 3, or (at your option)
  9. any later version.
  10. Libgomp is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  12. FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. more details.
  14. Under Section 7 of GPL version 3, you are granted additional
  15. permissions described in the GCC Runtime Library Exception, version
  16. 3.1, as published by the Free Software Foundation.
  17. You should have received a copy of the GNU General Public License and
  18. a copy of the GCC Runtime Library Exception along with this program;
  19. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  20. <http://www.gnu.org/licenses/>. */
  21. #include "openacc.h"
  22. #include "libgomp.h"
  23. #include "oacc-int.h"
  24. #include <assert.h>
  25. void *
  26. acc_get_current_cuda_device (void)
  27. {
  28. struct goacc_thread *thr = goacc_thread ();
  29. void *ret = NULL;
  30. if (thr && thr->dev && thr->dev->openacc.cuda.get_current_device_func)
  31. {
  32. acc_prof_info prof_info;
  33. acc_api_info api_info;
  34. bool profiling_p = GOACC_PROFILING_SETUP_P (thr, &prof_info, &api_info);
  35. ret = thr->dev->openacc.cuda.get_current_device_func ();
  36. if (profiling_p)
  37. {
  38. thr->prof_info = NULL;
  39. thr->api_info = NULL;
  40. }
  41. }
  42. return ret;
  43. }
  44. void *
  45. acc_get_current_cuda_context (void)
  46. {
  47. struct goacc_thread *thr = goacc_thread ();
  48. void *ret = NULL;
  49. if (thr && thr->dev && thr->dev->openacc.cuda.get_current_context_func)
  50. {
  51. acc_prof_info prof_info;
  52. acc_api_info api_info;
  53. bool profiling_p = GOACC_PROFILING_SETUP_P (thr, &prof_info, &api_info);
  54. ret = thr->dev->openacc.cuda.get_current_context_func ();
  55. if (profiling_p)
  56. {
  57. thr->prof_info = NULL;
  58. thr->api_info = NULL;
  59. }
  60. }
  61. return ret;
  62. }
  63. void *
  64. acc_get_cuda_stream (int async)
  65. {
  66. struct goacc_thread *thr = goacc_thread ();
  67. if (!async_valid_p (async))
  68. return NULL;
  69. void *ret = NULL;
  70. if (thr && thr->dev && thr->dev->openacc.cuda.get_stream_func)
  71. {
  72. goacc_aq aq = lookup_goacc_asyncqueue (thr, false, async);
  73. if (!aq)
  74. return ret;
  75. acc_prof_info prof_info;
  76. acc_api_info api_info;
  77. bool profiling_p = GOACC_PROFILING_SETUP_P (thr, &prof_info, &api_info);
  78. if (profiling_p)
  79. {
  80. prof_info.async = async;
  81. prof_info.async_queue = prof_info.async;
  82. }
  83. ret = thr->dev->openacc.cuda.get_stream_func (aq);
  84. if (profiling_p)
  85. {
  86. thr->prof_info = NULL;
  87. thr->api_info = NULL;
  88. }
  89. }
  90. return ret;
  91. }
  92. int
  93. acc_set_cuda_stream (int async, void *stream)
  94. {
  95. struct goacc_thread *thr;
  96. if (!async_valid_p (async) || stream == NULL)
  97. return 0;
  98. goacc_lazy_initialize ();
  99. thr = goacc_thread ();
  100. int ret = -1;
  101. if (thr && thr->dev && thr->dev->openacc.cuda.set_stream_func)
  102. {
  103. acc_prof_info prof_info;
  104. acc_api_info api_info;
  105. bool profiling_p = GOACC_PROFILING_SETUP_P (thr, &prof_info, &api_info);
  106. if (profiling_p)
  107. {
  108. prof_info.async = async;
  109. prof_info.async_queue = prof_info.async;
  110. }
  111. goacc_aq aq = get_goacc_asyncqueue (async);
  112. /* Due to not using an asyncqueue for "acc_async_sync", this cannot be
  113. used to change the CUDA stream associated with "acc_async_sync". */
  114. if (!aq)
  115. {
  116. assert (async == acc_async_sync);
  117. gomp_debug (0, "Refusing request to set CUDA stream associated"
  118. " with \"acc_async_sync\"\n");
  119. ret = 0;
  120. goto out_prof;
  121. }
  122. gomp_mutex_lock (&thr->dev->openacc.async.lock);
  123. ret = thr->dev->openacc.cuda.set_stream_func (aq, stream);
  124. gomp_mutex_unlock (&thr->dev->openacc.async.lock);
  125. out_prof:
  126. if (profiling_p)
  127. {
  128. thr->prof_info = NULL;
  129. thr->api_info = NULL;
  130. }
  131. }
  132. return ret;
  133. }