xref: /netbsd-src/external/gpl3/gcc.old/dist/libgomp/oacc-cuda.c (revision 8feb0f0b7eaff0608f8350bbfa3098827b4bb91b)
1 /* OpenACC Runtime Library: CUDA support glue.
2 
3    Copyright (C) 2014-2020 Free Software Foundation, Inc.
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"
30 #include "libgomp.h"
31 #include "oacc-int.h"
32 #include <assert.h>
33 
34 void *
acc_get_current_cuda_device(void)35 acc_get_current_cuda_device (void)
36 {
37   struct goacc_thread *thr = goacc_thread ();
38 
39   void *ret = NULL;
40   if (thr && thr->dev && thr->dev->openacc.cuda.get_current_device_func)
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     }
54 
55   return ret;
56 }
57 
58 void *
acc_get_current_cuda_context(void)59 acc_get_current_cuda_context (void)
60 {
61   struct goacc_thread *thr = goacc_thread ();
62 
63   void *ret = NULL;
64   if (thr && thr->dev && thr->dev->openacc.cuda.get_current_context_func)
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;
80 }
81 
82 void *
acc_get_cuda_stream(int async)83 acc_get_cuda_stream (int async)
84 {
85   struct goacc_thread *thr = goacc_thread ();
86 
87   if (!async_valid_p (async))
88     return NULL;
89 
90   void *ret = NULL;
91   if (thr && thr->dev && thr->dev->openacc.cuda.get_stream_func)
92     {
93       goacc_aq aq = lookup_goacc_asyncqueue (thr, false, async);
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 	}
113     }
114 
115   return ret;
116 }
117 
118 int
acc_set_cuda_stream(int async,void * stream)119 acc_set_cuda_stream (int async, void *stream)
120 {
121   struct goacc_thread *thr;
122 
123   if (!async_valid_p (async) || stream == NULL)
124     return 0;
125 
126   goacc_lazy_initialize ();
127 
128   thr = goacc_thread ();
129 
130   int ret = -1;
131   if (thr && thr->dev && thr->dev->openacc.cuda.set_stream_func)
132     {
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 
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");
150 	  ret = 0;
151 	  goto out_prof;
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);
156 
157     out_prof:
158       if (profiling_p)
159 	{
160 	  thr->prof_info = NULL;
161 	  thr->api_info = NULL;
162 	}
163     }
164 
165   return ret;
166 }
167