Debian Codegraph @

loki/transformations/transpile/tests/test_scc_cuda.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 from pathlib import Path
9 import pytest
10
11 from loki import Scheduler, Dimension, read_file
12 from loki.frontend import available_frontends
13 from loki.ir import nodes as ir, FindNodes
14
15 from loki.transformations.transpile import (
16 FortranCTransformation, FortranISOCWrapperTransformation
17 )
18 from loki.transformations.single_column import (
19 SCCLowLevelHoist, SCCLowLevelParametrise
20 )
21
22
23 @pytest.fixture(scope='module', name='horizontal')
24 def fixture_horizontal():
25 return Dimension(name='horizontal', size='nlon', index='jl', bounds=('start', 'iend'))
26
27
28 @pytest.fixture(scope='module', name='vertical')
29 def fixture_vertical():
30 return Dimension(name='vertical', size='nz', index='jk')
31
32
33 @pytest.fixture(scope='module', name='blocking')
34 def fixture_blocking():
35 return Dimension(name='blocking', size='nb', index='b')
36
37
38 @pytest.fixture(scope='module', name='here')
39 def fixture_here():
40 return Path(__file__).parent
41
42
43 @pytest.fixture(name='config')
44 def fixture_config():
45 """
46 Default configuration dict with basic options.
47 """
48 return {
49 'default': {
50 'mode': 'idem',
51 'role': 'kernel',
52 'expand': True,
53 'strict': False, # cudafor import
54 },
55 'routines': {
56 'driver': {'role': 'driver'}
57 }
58 }
59
60 def remove_whitespace_linebreaks(text):
61 return text.replace(' ', '').replace('\n', ' ').replace('\r', '').replace('\t', '').lower()
62
63 @pytest.mark.parametrize('frontend', available_frontends())
64 def test_scc_cuda_parametrise(tmp_path, here, frontend, config, horizontal, vertical, blocking):
65 """
66 Test SCC-CUF transformation type 0, thus including parametrising (array dimension(s))
67 """
68
69 proj = here / '../../tests/sources/projSccCuf/module'
70
71 scheduler = Scheduler(
72 paths=[proj], config=config, seed_routines=['driver'],
73 output_dir=tmp_path, frontend=frontend, xmods=[tmp_path]
74 )
75
76 dic2p = {'nz': 137}
77 cuda_transform = SCCLowLevelParametrise(
78 horizontal=horizontal, vertical=vertical, block_dim=blocking,
79 transformation_type='parametrise',
80 dim_vars=(vertical.size,), as_kwarguments=True, remove_vector_section=True,
81 use_c_ptr=True, dic2p=dic2p, path=here, mode='cuda'
82 )
83 scheduler.process(transformation=cuda_transform)
84 f2c_transformation = FortranCTransformation(language='cuda')
85 scheduler.process(transformation=f2c_transformation)
86 f2cwrap = FortranISOCWrapperTransformation(language='cuda', use_c_ptr=True)
87 scheduler.process(transformation=f2cwrap)
88
89 kernel = scheduler['kernel_mod#kernel'].ir
90 kernel_variable_map = kernel.variable_map
91 assert kernel_variable_map[horizontal.index].type.intent is None
92 assert kernel_variable_map[horizontal.index].scope == kernel
93 device = scheduler['kernel_mod#device'].ir
94 device_variable_map = device.variable_map
95 assert device_variable_map[horizontal.index].type.intent.lower() == 'in'
96 assert device_variable_map[horizontal.index].scope == device
97
98 fc_kernel = remove_whitespace_linebreaks(read_file(tmp_path/'kernel_fc.F90'))
99 c_kernel = remove_whitespace_linebreaks(read_file(tmp_path/'kernel_c.c'))
100 c_kernel_header = remove_whitespace_linebreaks(read_file(tmp_path/'kernel_c.h'))
101 c_kernel_launch = remove_whitespace_linebreaks(read_file(tmp_path/'kernel_c_launch.h'))
102 c_device = remove_whitespace_linebreaks(read_file(tmp_path/'device_c.c'))
103 c_elemental_device = remove_whitespace_linebreaks(read_file(tmp_path/'elemental_device_c.c'))
104 c_some_func = remove_whitespace_linebreaks(read_file(tmp_path/'some_func_c.c'))
105 c_some_func_header = remove_whitespace_linebreaks(read_file(tmp_path/'some_func_c.h'))
106
107 calls = FindNodes(ir.CallStatement).visit(scheduler["driver_mod#driver"].ir.body)
108 assert len(calls) == 3
109 for call in calls:
110 assert str(call.name).lower() == 'kernel'
111 assert call.pragma[0].keyword == 'loki'
112 assert 'removed_loop' in call.pragma[0].content
113 # kernel_fc.F90
114 assert '!$acchost_datause_device(q,t,z)' in fc_kernel
115 assert 'kernel_iso_c(start,nlon,c_loc(q),c_loc(t),c_loc(z),nb,tot,iend)' in fc_kernel
116 assert 'bind(c,name="kernel_c_launch")' in fc_kernel
117 assert 'useiso_c_binding' in fc_kernel
118 # kernel_c.c
119 assert '#include<cuda.h>' in c_kernel
120 assert '#include<cuda_runtime.h>' in c_kernel
121 assert '#include"kernel_c.h"' in c_kernel
122 assert '#include"kernel_c_launch.h"' in c_kernel
123 assert 'include"elemental_device_c.h"' in c_kernel
124 assert 'include"device_c.h"' in c_kernel
125 assert 'include"some_func_c.h"' in c_kernel
126 assert '__global__voidkernel_c' in c_kernel
127 assert 'jl=threadidx.x;' in c_kernel
128 assert 'b=blockidx.x;' in c_kernel
129 assert 'device_c(' in c_kernel
130 assert 'elemental_device_c(' in c_kernel
131 assert '=some_func_c(' in c_kernel
132 # kernel_c.h
133 assert '__global__voidkernel_c' in c_kernel_header
134 assert 'jl=threadidx.x;' not in c_kernel_header
135 assert 'b=blockidx.x;' not in c_kernel_header
136 # kernel_c_launch.h
137 assert 'extern"c"' in c_kernel_launch
138 assert 'voidkernel_c_launch(' in c_kernel_launch
139 assert 'structdim3blockdim;' in c_kernel_launch
140 assert 'structdim3griddim;' in c_kernel_launch
141 assert 'griddim=dim3(' in c_kernel_launch
142 assert 'blockdim=dim3(' in c_kernel_launch
143 assert 'kernel_c<<<griddim,blockdim>>>(' in c_kernel_launch
144 assert 'cudadevicesynchronize();' in c_kernel_launch
145 # device_c.c
146 assert '#include<cuda.h>' in c_device
147 assert '#include<cuda_runtime.h>' in c_device
148 assert '#include"device_c.h"' in c_device
149 # elemental_device_c.c
150 assert '__device__voiddevice_c(' in c_device
151 assert '#include<cuda.h>' in c_elemental_device
152 assert '#include<cuda_runtime.h>' in c_elemental_device
153 assert '#include"elemental_device_c.h"' in c_elemental_device
154 # some_func_c.c
155 assert 'doublesome_func_c(doublea)' in c_some_func
156 assert 'returnsome_func' in c_some_func
157 # some_func_c.h
158 assert 'doublesome_func_c(doublea);' in c_some_func_header
159
160
161 @pytest.mark.parametrize('frontend', available_frontends())
162 def test_scc_cuda_hoist(tmp_path, here, frontend, config, horizontal, vertical, blocking):
163 """
164 Test SCC-CUF transformation type 0, thus including parametrising (array dimension(s))
165 """
166
167 proj = here / '../../tests/sources/projSccCuf/module'
168
169 scheduler = Scheduler(
170 paths=[proj], config=config, seed_routines=['driver'],
171 output_dir=tmp_path, frontend=frontend, xmods=[tmp_path]
172 )
173
174 cuda_transform = SCCLowLevelHoist(
175 horizontal=horizontal, vertical=vertical, block_dim=blocking,
176 transformation_type='parametrise',
177 dim_vars=(vertical.size,), as_kwarguments=True, remove_vector_section=True,
178 use_c_ptr=True, path=here, mode='cuda'
179 )
180 scheduler.process(transformation=cuda_transform)
181 f2c_transformation = FortranCTransformation(language='cuda')
182 scheduler.process(transformation=f2c_transformation)
183 f2cwrap = FortranISOCWrapperTransformation(language='cuda', use_c_ptr=True)
184 scheduler.process(transformation=f2cwrap)
185
186 kernel = scheduler['kernel_mod#kernel'].ir
187 kernel_variable_map = kernel.variable_map
188 assert kernel_variable_map[horizontal.index].type.intent is None
189 assert kernel_variable_map[horizontal.index].scope == kernel
190 device = scheduler['kernel_mod#device'].ir
191 device_variable_map = device.variable_map
192 assert device_variable_map[horizontal.index].type.intent.lower() == 'in'
193 assert device_variable_map[horizontal.index].scope == device
194
195 fc_kernel = remove_whitespace_linebreaks(read_file(tmp_path/'kernel_fc.F90'))
196 c_kernel = remove_whitespace_linebreaks(read_file(tmp_path/'kernel_c.c'))
197 c_kernel_header = remove_whitespace_linebreaks(read_file(tmp_path/'kernel_c.h'))
198 c_kernel_launch = remove_whitespace_linebreaks(read_file(tmp_path/'kernel_c_launch.h'))
199 c_device = remove_whitespace_linebreaks(read_file(tmp_path/'device_c.c'))
200 c_elemental_device = remove_whitespace_linebreaks(read_file(tmp_path/'elemental_device_c.c'))
201 c_some_func = remove_whitespace_linebreaks(read_file(tmp_path/'some_func_c.c'))
202 c_some_func_header = remove_whitespace_linebreaks(read_file(tmp_path/'some_func_c.h'))
203
204 calls = FindNodes(ir.CallStatement).visit(scheduler["driver_mod#driver"].ir.body)
205 assert len(calls) == 3
206 for call in calls:
207 assert str(call.name).lower() == 'kernel'
208 assert call.pragma[0].keyword == 'loki'
209 assert 'removed_loop' in call.pragma[0].content
210 # kernel_fc.F90
211 assert '!$acchost_datause_device(q,t,z,local_z,device_local_x)' in fc_kernel
212 assert 'kernel_iso_c(start,nlon,nz,c_loc(q),c_loc(t),c_loc(z)' in fc_kernel
213 assert 'c_loc(z),nb,tot,iend,c_loc(local_z),c_loc(device_local_x))' in fc_kernel
214 assert 'bind(c,name="kernel_c_launch")' in fc_kernel
215 assert 'useiso_c_binding' in fc_kernel
216 # kernel_c.c
217 assert '#include<cuda.h>' in c_kernel
218 assert '#include<cuda_runtime.h>' in c_kernel
219 assert '#include"kernel_c.h"' in c_kernel
220 assert '#include"kernel_c_launch.h"' in c_kernel
221 assert '#include"elemental_device_c.h"' in c_kernel
222 assert '#include"device_c.h"' in c_kernel
223 assert 'include"some_func_c.h"' in c_kernel
224 assert '__global__voidkernel_c' in c_kernel
225 assert 'jl=threadidx.x;' in c_kernel
226 assert 'b=blockidx.x;' in c_kernel
227 assert 'device_c(' in c_kernel
228 assert 'elemental_device_c(' in c_kernel
229 assert '=some_func_c(' in c_kernel
230 # kernel_c.h
231 assert '__global__voidkernel_c' in c_kernel_header
232 assert 'jl=threadidx.x;' not in c_kernel_header
233 assert 'b=blockidx.x;' not in c_kernel_header
234 # kernel_c_launch.h
235 assert 'extern"c"' in c_kernel_launch
236 assert 'voidkernel_c_launch(' in c_kernel_launch
237 assert 'structdim3blockdim;' in c_kernel_launch
238 assert 'structdim3griddim;' in c_kernel_launch
239 assert 'griddim=dim3(' in c_kernel_launch
240 assert 'blockdim=dim3(' in c_kernel_launch
241 assert 'kernel_c<<<griddim,blockdim>>>(' in c_kernel_launch
242 assert 'cudadevicesynchronize();' in c_kernel_launch
243 # device_c.c
244 assert '#include<cuda.h>' in c_device
245 assert '#include<cuda_runtime.h>' in c_device
246 assert '#include"device_c.h"' in c_device
247 assert '__device__voiddevice_c(' in c_device
248 # elemental_device_c.c
249 assert '#include<cuda.h>' in c_elemental_device
250 assert '#include<cuda_runtime.h>' in c_elemental_device
251 assert '#include"elemental_device_c.h"' in c_elemental_device
252 # some_func_c.c
253 assert 'doublesome_func_c(doublea)' in c_some_func
254 assert 'returnsome_func' in c_some_func
255 # some_func_c.h
256 assert 'doublesome_func_c(doublea);' in c_some_func_header