Debian Codegraph @

libgomp/testsuite/libgomp.fortran/interop-hip.h

raw
1 ! Minimal check whether HIP works - by checking whether the API routines
2 ! seem to work. This includes a fallback if hipfort is not available
3
4 #ifndef HAVE_HIPFORT
5 #ifndef USE_HIP_FALLBACK_MODULE
6 #if USE_CUDA_NAMES
7 #warning "Using fallback implementation for module hipfort as HAVE_HIPFORT is undefined (for NVIDA/CUDA)"
8 #else
9 #warning "Using fallback implementation for module hipfort as HAVE_HIPFORT is undefined - assume AMD as USE_CUDA_NAMES is unset"
10 #endif
11 #endif
12 module hipfort ! Minimal implementation for the testsuite
13 implicit none
14
15 enum, bind(c)
16 enumerator :: hipSuccess = 0
17 enumerator :: hipErrorNotSupported = 801
18 end enum
19
20 enum, bind(c)
21 enumerator :: hipDeviceAttributeClockRate = 5
22 enumerator :: hipDeviceAttributeMaxGridDimX = 29
23 end enum
24
25 interface
26 integer(kind(hipSuccess)) function hipDeviceGetAttribute (ip, attr, dev) &
27 #if USE_CUDA_NAMES
28 bind(c, name="cudaDeviceGetAttribute")
29 #else
30 bind(c, name="hipDeviceGetAttribute")
31 #endif
32 use iso_c_binding, only: c_ptr, c_int
33 import
34 implicit none
35 type(c_ptr), value :: ip
36 integer(kind(hipDeviceAttributeClockRate)), value :: attr
37 integer(c_int), value :: dev
38 end
39
40 integer(kind(hipSuccess)) function hipCtxGetApiVersion (ctx, ip) &
41 #if USE_CUDA_NAMES
42 bind(c, name="cudaCtxGetApiVersion")
43 #else
44 bind(c, name="hipCtxGetApiVersion")
45 #endif
46 use iso_c_binding, only: c_ptr
47 import
48 implicit none
49 type(c_ptr), value :: ctx, ip
50 end
51
52 integer(kind(hipSuccess)) function hipStreamQuery (stream) &
53 #if USE_CUDA_NAMES
54 bind(c, name="cudaStreamQuery")
55 #else
56 bind(c, name="hipStreamQuery")
57 #endif
58 use iso_c_binding, only: c_ptr
59 import
60 implicit none
61 type(c_ptr), value :: stream
62 end
63
64 integer(kind(hipSuccess)) function hipStreamGetFlags (stream, flags) &
65 #if USE_CUDA_NAMES
66 bind(c, name="cudaStreamGetFlags")
67 #else
68 bind(c, name="hipStreamGetFlags")
69 #endif
70 use iso_c_binding, only: c_ptr
71 import
72 implicit none
73 type(c_ptr), value :: stream
74 type(c_ptr), value :: flags
75 end
76 end interface
77 end module
78 #endif
79
80 program main
81 use iso_c_binding, only: c_ptr, c_int, c_loc
82 use omp_lib
83 use hipfort
84 implicit none (type, external)
85
86 ! Only supported since CUDA 12.8 - skip for better compatibility
87 ! ! Manally implement hipStreamGetDevice as hipfort misses it
88 ! ! -> https://github.com/ROCm/hipfort/issues/238
89 ! interface
90 ! integer(kind(hipSuccess)) function my_hipStreamGetDevice(stream, dev) &
91 !#if USE_CUDA_NAMES
92 ! bind(c, name="cudaStreamGetDevice")
93 !#else
94 ! bind(c, name="hipStreamGetDevice")
95 !#endif
96 ! use iso_c_binding, only: c_ptr, c_int
97 ! import
98 ! implicit none
99 ! type(c_ptr), value :: stream
100 ! integer(c_int) :: dev
101 ! end
102 ! end interface
103
104 integer(c_int), target :: ivar
105 integer(omp_interop_rc_kind) :: res
106 integer(omp_interop_kind) :: obj
107 integer(omp_interop_fr_kind) :: fr
108 integer(kind(hipSuccess)) :: hip_err
109 integer(c_int) :: hip_dev, dev_stream
110 type(c_ptr) :: hip_ctx, hip_sm
111
112 logical :: vendor_is_amd
113
114 obj = omp_interop_none
115
116 !$omp interop init(target, targetsync, prefer_type("hip") : obj)
117
118 fr = omp_get_interop_int (obj, omp_ipr_fr_id, res)
119 if (res /= omp_irc_success) error stop 1
120 if (fr /= omp_ifr_hip) error stop 1
121
122 ivar = omp_get_interop_int (obj, omp_ipr_vendor, res)
123 if (ivar == 1) then ! AMD
124 vendor_is_amd = .true.
125 else if (ivar == 11) then ! Nvidia
126 vendor_is_amd = .false.
127 else
128 error stop 1 ! Unknown
129 endif
130 #if USE_CUDA_NAMES
131 if (vendor_is_amd) error stop 1
132 #else
133 if (.not. vendor_is_amd) error stop 1
134 #endif
135
136 ! Check whether the omp_ipr_device -> hipDevice_t yields a valid device.
137
138 hip_dev = omp_get_interop_int (obj, omp_ipr_device, res)
139 if (res /= omp_irc_success) error stop 1
140
141 ! AMD messed up in Fortran with the attribute handling, missing the
142 ! translation table it has for C.
143 block
144 enum, bind(c)
145 enumerator :: cudaDevAttrClockRate = 13
146 enumerator :: cudaDevAttrMaxGridDimX = 5
147 end enum
148
149 ! Assume a clock size is available and > 1 GHz; value is in kHz.
150 ! c_loc is completely bogus, but as AMD messed up the interface ...
151 ! Cf. https://github.com/ROCm/hipfort/issues/239
152 if (vendor_is_amd) then
153 hip_err = hipDeviceGetAttribute (c_loc(ivar), hipDeviceAttributeClockRate, hip_dev)
154 else
155 hip_err = hipDeviceGetAttribute (c_loc(ivar), cudaDevAttrClockRate, hip_dev)
156 endif
157 if (hip_err /= hipSuccess) error stop 1
158 if (ivar <= 1000000) error stop 1 ! in kHz
159
160 ! Assume that the MaxGridDimX is available and > 1024
161 ! c_loc is completely bogus, but as AMD messed up the interface ...
162 ! Cf. https://github.com/ROCm/hipfort/issues/239
163 if (vendor_is_amd) then
164 hip_err = hipDeviceGetAttribute (c_loc(ivar), hipDeviceAttributeMaxGridDimX, hip_dev)
165 else
166 hip_err = hipDeviceGetAttribute (c_loc(ivar), cudaDevAttrMaxGridDimX, hip_dev)
167 endif
168 if (hip_err /= hipSuccess) error stop 1
169 if (ivar <= 1024) error stop 1
170 end block
171
172
173 ! Check whether the omp_ipr_device_context -> hipCtx_t yields a context.
174
175 hip_ctx = omp_get_interop_ptr (obj, omp_ipr_device_context, res)
176 if (res /= omp_irc_success) error stop 1
177
178 ! ! Assume API Version > 0 for Nvidia, hipErrorNotSupported for AMD. */
179 ! ivar = -99
180 ! ! AMD deprectated hipCtxGetApiVersion (in C/C++)
181 ! hip_err = hipCtxGetApiVersion (hip_ctx, c_loc(ivar))
182 !
183 ! if (vendor_is_amd) then
184 ! if (hip_err /= hipErrorNotSupported .or. ivar /= -99) error stop 1
185 ! else
186 ! if (hip_err /= hipSuccess) error stop 1
187 ! if (ivar <= 0) error stop 1
188 ! end if
189
190
191 ! Check whether the omp_ipr_targetsync -> hipStream_t yields a stream.
192
193 hip_sm = omp_get_interop_ptr (obj, omp_ipr_targetsync, res)
194 if (res /= omp_irc_success) error stop 1
195
196 ! Skip as this is only in CUDA 12.8
197 ! dev_stream = 99
198 ! ! Not (yet) implemented: https://github.com/ROCm/hipfort/issues/238
199 ! ! hip_err = hipStreamGetDevice (hip_sm, dev_stream)
200 ! hip_err = my_hipStreamGetDevice (hip_sm, dev_stream)
201 ! if (hip_err /= hipSuccess) error stop 1
202 ! if (dev_stream /= hip_dev) error stop 1
203
204 ! Get flags of the stream
205 hip_err = hipStreamGetFlags (hip_sm, c_loc (ivar))
206 if (hip_err /= hipSuccess) error stop 1
207 ! Accept any value
208
209 ! All jobs should have been completed (as there were none none)
210 hip_err = hipStreamQuery (hip_sm)
211 if (hip_err /= hipSuccess) error stop 1
212
213 !$omp interop destroy(obj)
214 end