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