Debian Codegraph @

loki/transformations/transpile/tests/test_sdfg.py

raw
1 # (C) Copyright 2018- ECMWF.
2 # This software is licensed under the terms of the Apache Licence Version 2.0
3 # which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
4 # In applying this licence, ECMWF does not waive the privileges and immunities
5 # granted to it by virtue of its status as an intergovernmental organisation
6 # nor does it submit to any jurisdiction.
7
8 import importlib
9 import itertools
10 from pathlib import Path
11 import numpy as np
12 import pytest
13
14 from loki import Subroutine
15 from loki.jit_build import jit_compile_and_run
16 from loki.frontend import available_frontends
17
18 from loki.transformations.transpile import FortranPythonTransformation
19
20
21 pytestmark = [
22 # Skip tests if dace is not installed
23 pytest.mark.skipif(
24 importlib.util.find_spec('dace') is None,
25 reason='DaCe is not installed'
26 ),
27 # Disable warnings from Dace about np.bool being deprecated
28 pytest.mark.filterwarnings(
29 "ignore:`np.bool` is a deprecated alias:DeprecationWarning"
30 )
31 ]
32
33
34 def load_module(path):
35 path = Path(path)
36
37 # Trigger the actual module import
38 try:
39 return importlib.import_module(path.stem)
40 except ModuleNotFoundError:
41 # If module caching interferes, try again with clean caches
42 importlib.invalidate_caches()
43 return importlib.import_module(path.stem)
44
45
46 def create_sdfg(routine, tmp_path):
47 trafo = FortranPythonTransformation(with_dace=True, suffix='_py')
48 routine.apply(trafo, path=tmp_path)
49
50 mod = load_module(trafo.py_path)
51 function = getattr(mod, routine.name)
52 return function.to_sdfg()
53
54
55 @pytest.mark.parametrize('frontend', available_frontends())
56 def test_sdfg_routine_copy(tmp_path, frontend):
57
58 fcode = """
59 subroutine routine_copy(n, x, y)
60 ! A simple routine that copies the values of x to y
61 use iso_fortran_env, only: real64
62 implicit none
63 real(kind=real64), intent(in) :: x(n)
64 real(kind=real64), intent(out) :: y(n)
65 integer, intent(in) :: n
66 integer :: i
67
68 do i=1,n
69 y(i) = x(i)
70 enddo
71 end subroutine routine_copy
72 """
73 routine = Subroutine.from_source(fcode, frontend=frontend)
74
75 # Test the reference solution
76 filepath = tmp_path/(f'routine_copy_{frontend}.f90')
77 n = 64
78 x_ref = np.array(range(n), dtype=np.float64)
79 x = np.zeros(n, dtype=np.float64)
80 x[:] = x_ref[:]
81 y = np.zeros(n, dtype=np.float64)
82 jit_compile_and_run(routine, n=n, x=x, y=y, filepath=filepath)
83 assert all(x_ref == y)
84
85 # Create and compile the SDFG
86 sdfg = create_sdfg(routine, tmp_path)
87 assert sdfg.validate() is None
88
89 csdfg = sdfg.compile()
90 assert csdfg
91
92 # Run the SDFG
93 x[:] = x_ref[:]
94 csdfg(n=np.int32(n), x=x, y=y)
95 assert all(x_ref == y)
96
97
98 @pytest.mark.xfail(reason='Scalar inout arguments do not work in dace')
99 @pytest.mark.filterwarnings('ignore:The value of the smallest subnormal.*class \'numpy.float64\':UserWarning')
100 @pytest.mark.parametrize('frontend', available_frontends())
101 def test_sdfg_routine_axpy_scalar(tmp_path, frontend):
102
103 fcode = """
104 subroutine routine_axpy_scalar(a, x, y)
105 ! A simple standard routine that computes x = a * x + y for
106 ! scalar arguments
107 use iso_fortran_env, only: real64
108 implicit none
109 real(kind=real64), intent(in) :: a, y
110 real(kind=real64), intent(inout) :: x
111
112 x = a * x + y
113 end subroutine routine_axpy_scalar
114 """
115 routine = Subroutine.from_source(fcode, frontend=frontend)
116
117 # Test the reference solution
118 filepath = tmp_path/(f'sdfg_routine_axpy_scalar_{frontend}.f90')
119 a = np.float64(23)
120 x = np.float64(42)
121 x_out = np.array([x], dtype=np.float64)
122 y = np.float64(5)
123 jit_compile_and_run(routine, a=a, x=x_out, y=y, filepath=filepath)
124 assert x_out == a * x + y
125
126 # Create and compile the SDFG
127 sdfg = create_sdfg(routine, tmp_path)
128 assert sdfg.validate() is None
129
130 csdfg = sdfg.compile()
131 assert csdfg
132
133 # Run the SDFG
134 x_out = np.array([x], dtype=np.float64)
135 csdfg(a=a, x=x_out, y=y)
136 assert x_out == a * x + y
137
138
139 @pytest.mark.parametrize('frontend', available_frontends())
140 def test_sdfg_routine_copy_stream(tmp_path, frontend):
141
142 fcode = """
143 subroutine routine_copy_stream(length, alpha, vector_in, vector_out)
144 implicit none
145 ! A simple standard looking routine to test argument declarations
146 ! and generator toolchain
147 integer, intent(in) :: length, alpha(1), vector_in(length)
148 integer, intent(out) :: vector_out(length)
149 integer :: i
150
151 !$loki dataflow
152 do i=1, length
153 vector_out(i) = vector_in(i) + alpha(1)
154 end do
155 end subroutine routine_copy_stream
156 """
157 routine = Subroutine.from_source(fcode, frontend=frontend)
158 # TODO: make alpha a true scalar, which doesn't seem to work with SDFG at the moment???
159
160 # Test the reference solution
161 filepath = tmp_path/(f'sdfg_routine_copy_stream_{frontend}.f90')
162 length = 32
163 alpha = np.array([7], dtype=np.int32)
164 vector_in = np.array(range(length), order='F', dtype=np.int32)
165 vector_out = np.zeros(length, order='F', dtype=np.int32)
166 jit_compile_and_run(
167 routine, length=length, alpha=alpha, vector_in=vector_in, vector_out=vector_out, filepath=filepath
168 )
169 assert np.all(vector_out == np.array(range(length)) + alpha)
170
171 # Create and compile the SDFG
172 sdfg = create_sdfg(routine, tmp_path)
173 assert sdfg.validate() is None
174
175 csdfg = sdfg.compile()
176 assert csdfg
177
178 # Run the SDFG
179 vec_in = np.array(range(length), order='F', dtype=np.intc)
180 vec_out = np.zeros(length, order='F', dtype=np.intc)
181 csdfg(length=length, alpha=alpha, vector_in=vec_in, vector_out=vec_out)
182 assert np.all(vec_out == np.array(range(length)) + alpha)
183
184
185 @pytest.mark.parametrize('frontend', available_frontends())
186 def test_sdfg_routine_fixed_loop(tmp_path, frontend):
187
188 fcode = """
189 subroutine routine_fixed_loop(scalar, vector, vector_out, tensor, tensor_out)
190 use iso_fortran_env, only: real64
191 implicit none
192 ! integer :: n=6, m=4
193 real(kind=real64), intent(in) :: scalar(1)
194 real(kind=real64), intent(in) :: tensor(6, 4), vector(6)
195 real(kind=real64), intent(out) :: tensor_out(4, 6), vector_out(6)
196 integer :: i, j
197
198 ! For testing, the operation is:
199 !$loki dataflow
200 do j=1, 6
201 vector_out(j) = vector(j) + tensor(j, 1) + 1.0
202 !$loki dataflow
203 do i=1, 4
204 tensor_out(i, j) = tensor(j, i)
205 end do
206 end do
207 end subroutine routine_fixed_loop
208 """
209 routine = Subroutine.from_source(fcode, frontend=frontend)
210 filepath = tmp_path/(f'sdfg_routine_fixed_loop_{frontend}.f90')
211 # Test the reference solution
212 n, m = 6, 4
213 scalar = np.array([2.0], dtype=np.float64)
214 vector = np.zeros(shape=(n,), order='F') + 3.
215 tensor = np.array([list(range(i, i+m)) for i in range(n)], order='F', dtype=np.float64)
216 tensor_out = np.zeros(shape=(m, n), order='F')
217 ref_vector = vector + np.array(list(range(n)), dtype=np.float64) + 1.
218 ref_tensor = np.transpose(tensor)
219 jit_compile_and_run(
220 routine, scalar=scalar, vector=vector, vector_out=vector, tensor=tensor, tensor_out=tensor_out,
221 filepath=filepath
222 )
223 assert np.all(vector == ref_vector)
224 assert np.all(tensor_out == ref_tensor)
225
226 # Create and compile the SDFG
227 sdfg = create_sdfg(routine, tmp_path)
228 assert sdfg.validate() is None
229
230 csdfg = sdfg.compile()
231 assert csdfg
232
233 # Test the transpiled kernel
234 n, m = 6, 4
235 scalar = np.array([2.0], dtype=np.float64)
236 vector = np.zeros(shape=(n,), order='F') + 3.
237 tensor = np.zeros(shape=(n, m), order='F') + 4.
238 tensor = np.array([list(range(i, i+m)) for i in range(n)], order='F', dtype=np.float64)
239 tensor_out = np.zeros(shape=(m, n), order='F')
240 csdfg(scalar=scalar, vector=vector, vector_out=vector, tensor=tensor, tensor_out=tensor_out)
241 assert np.all(vector == ref_vector)
242 assert np.all(tensor_out == ref_tensor)
243
244
245 @pytest.mark.skip(reason=('This translates successfully but the generated OpenMP code does not '
246 'honour the loop-carried dependency, thus creating data races for more '
247 'than 1 thread.'))
248 @pytest.mark.parametrize('frontend', available_frontends())
249 def test_sdfg_routine_loop_carried_dependency(tmp_path, frontend):
250
251 fcode = """
252 subroutine routine_loop_carried_dependency(vector)
253 use iso_fortran_env, only: real64
254 implicit none
255 real(kind=real64), intent(inout) :: vector(32)
256 integer :: i
257
258 !$loki dataflow
259 do i=2, 32
260 vector(i) = vector(i) + vector(i-1)
261 end do
262 end subroutine routine_loop_carried_dependency
263 """
264 routine = Subroutine.from_source(fcode, frontend=frontend)
265 filepath = tmp_path/(f'sdfg_routine_loop_carried_dependency_{frontend}.f90')
266 # Test the reference solution
267 n = 32
268 vector = np.zeros(shape=(n,), order='F') + 3.
269 ref_vector = np.array(list(itertools.accumulate(vector)))
270 jit_compile_and_run(routine, vector=vector, filepath=filepath)
271 assert np.all(vector == ref_vector)
272
273 # Create and compile the SDFG
274 sdfg = create_sdfg(routine, tmp_path)
275 assert sdfg.validate() is None
276
277 csdfg = sdfg.compile()
278 assert csdfg
279
280 # Test the transpiled kernel
281 n = 32
282 vector = np.zeros(shape=(n,), order='F') + 3.
283 ref_vector = np.array(list(itertools.accumulate(vector)))
284 csdfg(vector=vector)
285 assert np.all(vector == ref_vector)
286
287
288 @pytest.mark.parametrize('frontend', available_frontends())
289 def test_sdfg_routine_moving_average(tmp_path, frontend):
290 # TODO: This needs more work to properly handle boundary values.
291 # In the current form, these values seem to be handled in a way
292 # that causes race conditions. Either this is a DaCe bug or we are
293 # using DaCe wrong tmp_path.
294
295 fcode = """
296 subroutine routine_moving_average(length, data_in, data_out)
297 use iso_fortran_env, only: real64
298 implicit none
299 integer, intent(in) :: length
300 real(kind=real64), intent(in) :: data_in(length)
301 real(kind=real64), intent(out) :: data_out(length)
302 integer :: i
303 real(kind=real64) :: prev, next, divisor, incr
304
305 data_out(1) = (data_in(1) + data_in(2)) / 2.0
306
307 !$loki dataflow
308 do i=2, length-1
309 ! TODO: range check prohibits this for some reason
310 incr = 1.0
311 divisor = 2.0
312 if (i > 1) then
313 prev = data_in(i-1)
314 ! divisor = 2.0
315 else
316 divisor = divisor - incr
317 prev = 0
318 ! divisor = 1.0
319 end if
320 if (i < length) then
321 next = data_in(i+1)
322 divisor = divisor + incr
323 else
324 next = 0
325 end if
326 data_out(i) = (prev + data_in(i) + next) / divisor
327 end do
328
329 data_out(length) = (data_in(length-1) + data_in(length)) / 2.0
330 end subroutine routine_moving_average
331 """
332 routine = Subroutine.from_source(fcode, frontend=frontend)
333 filepath = tmp_path/(f'sdfg_routine_moving_average_{frontend}.f90')
334 # Create random input data
335 n = 32
336 data_in = np.array(np.random.rand(n), order='F')
337
338 # Compute reference solution
339 expected = np.zeros(shape=(n,), order='F')
340 expected[0] = (data_in[0] + data_in[1]) / 2.
341 expected[1:-1] = (data_in[:-2] + data_in[1:-1] + data_in[2:]) / 3.
342 expected[-1] = (data_in[-2] + data_in[-1]) / 2.
343
344 # Test the Fortran kernel
345 data_out = np.zeros(shape=(n,), order='F')
346 jit_compile_and_run(routine, length=n, data_in=data_in, data_out=data_out, filepath=filepath)
347 assert np.all(data_out[1:-1] == expected[1:-1])
348
349 # Create and compile the SDFG
350 sdfg = create_sdfg(routine, tmp_path)
351 assert sdfg.validate() is None
352
353 csdfg = sdfg.compile()
354 assert csdfg
355
356 # Test the transpiled kernel
357 data_out = np.zeros(shape=(n,), order='F')
358 csdfg(length=n, data_in=data_in, data_out=data_out)
359 assert np.all(data_out[1:-1] == expected[1:-1])