Skip to content

Commit 17c5470

Browse files
JDBetteridgeFabioLuporini
authored andcommitted
compiler: Fix transfers for OpenACC backend
1 parent 6d5f693 commit 17c5470

4 files changed

Lines changed: 55 additions & 27 deletions

File tree

‎devito/passes/iet/languages/openacc.py‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
from devito.arch import AMDGPUX, NVIDIAX
44
from devito.ir import (
5-
Block, BusyWaitCall, Call, DeviceCall, DummyExpr, EntryFunction, FindSymbols, List,
6-
ParallelTree, Pragma, Return, make_callable
5+
Block, BusyWaitCall, Call, Conditional, DeviceCall, DummyExpr, EntryFunction,
6+
FindSymbols, List, ParallelTree, Pragma, Return, make_callable
77
)
88
from devito.passes import is_on_device, needs_transfer
99
from devito.passes.iet.definitions import DeviceAwareDataManager
@@ -226,7 +226,8 @@ def place_devptr(self, iet, **kwargs):
226226
return iet, {}
227227

228228
symbols = FindSymbols('basics').visit(iet)
229-
functions = [f for f in symbols if needs_transfer(f, self.gpu_fit)]
229+
functions = [f for f in FindSymbols().visit(iet)
230+
if needs_transfer(f, self.gpu_fit)]
230231

231232
efuncs = []
232233
calls = []
@@ -248,7 +249,9 @@ def place_devptr(self, iet, **kwargs):
248249

249250
ret = Return(ctdp)
250251

251-
body = List(body=[init, dpf, castf, ret])
252+
# Empty allocations have no present device address to discover.
253+
nbytes = FieldFromPointer(f._C_field_nbytes, f._C_symbol)
254+
body = List(body=[init, Conditional(nbytes > 0, dpf), castf, ret])
252255

253256
name = self.sregistry.make_name(prefix='map_device_ptr')
254257
efuncs.append(make_callable(name, body, retval=hp))

‎devito/passes/iet/linearization.py‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,11 @@ def linearize_transfers(iet, sregistry=None, **kwargs):
391391

392392
imask = [(wildcard, size)]
393393

394+
# Preserve snapshot metadata even when the flattened mask is empty:
395+
# `_rebuild()` without positional arguments restores the original items.
396+
if isinstance(imask0, IMask):
397+
imask = IMask(*imask, **imask0.__dict__)
398+
394399
rebuilt = n._rebuild(imask=imask)
395400

396401
if exprs:

‎tests/test_gpu_common.py‎

Lines changed: 42 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from devito.arch import Cpu64, Device, get_cpu_info, get_gpu_info
1515
from devito.exceptions import InvalidArgument
1616
from devito.ir import (
17-
Call, Conditional, Expression, FindNodes, FindSymbols, Section,
17+
Call, Conditional, Expression, FindNodes, FindSymbols, Section, Transfer,
1818
retrieve_iteration_tree
1919
)
2020
from devito.passes.iet.languages.openmp import OmpIteration
@@ -285,7 +285,9 @@ def test_linearize_transfer_no_overflow(self):
285285

286286
# The transfer bound is a product of the four `size[i]`, each cast to
287287
# `long`; the multiplication is thus carried out in 64-bit arithmetic
288-
for transfer in op.body.maps + op.body.unmaps:
288+
transfers = FindNodes(Transfer).visit(op.body.maps + op.body.unmaps)
289+
assert len(transfers) == 3
290+
for transfer in transfers:
289291
code = transfer.ccode.value
290292
for i in range(4):
291293
assert f'(long)(u_vec->size[{i}])' in code
@@ -453,7 +455,10 @@ def test_tasking_fused(self):
453455
assert len(retrieve_iteration_tree(op)) == 3
454456
locks = [i for i in FindSymbols().visit(op) if isinstance(i, Lock)]
455457
assert len(locks) == 1 # Only 1 because it's only `tmp` that needs protection
456-
assert len(op._func_table) == 5 + int(configuration['language'] == 'openacc')
458+
if configuration['language'] == 'openacc':
459+
assert len(op._func_table) == 7
460+
else:
461+
assert len(op._func_table) == 5
457462
exprs = FindNodes(Expression).visit(op._func_table['copy_to_host0'].root)
458463
b = 17 if configuration['language'] == 'openacc' else 16 # No `qid` w/ OMP
459464
assert str(exprs[b]) == 'const int deviceid = sdata0->deviceid;'
@@ -508,7 +513,10 @@ def test_tasking_unfused_two_locks(self):
508513
body = sections[2].body[0].body[0]
509514
assert str(body.body[0]) == 'release_lock0(lock0);'
510515
assert str(body.body[1]) == 'activate0(time,sdata0);'
511-
assert len(op._func_table) == 5 + int(configuration['language'] == 'openacc')
516+
if configuration['language'] == 'openacc':
517+
assert len(op._func_table) == 7
518+
else:
519+
assert len(op._func_table) == 5
512520
calls = FindNodes(Call).visit(op._func_table['copy_to_host0'].root)
513521
stores = [str(i) for i in calls if i.name == '__atomic_store_n']
514522
assert stores[0] == '__atomic_store_n(&lock0[0],1,__ATOMIC_RELEASE);'
@@ -563,7 +571,10 @@ def test_tasking_forcefuse(self):
563571
assert str(body.body[1]) == 'sdata0[0].time = time;'
564572
assert str(body.body[2]) == \
565573
'__atomic_store_n(&sdata0[0].flag,2,__ATOMIC_RELEASE);'
566-
assert len(op._func_table) == 5 + int(configuration['language'] == 'openacc')
574+
if configuration['language'] == 'openacc':
575+
assert len(op._func_table) == 7
576+
else:
577+
assert len(op._func_table) == 5
567578
exprs = FindNodes(Expression).visit(op._func_table['copy_to_host0'].root)
568579
b = 21 if configuration['language'] == 'openacc' else 20 # No `qid` w/ OMP
569580
assert exprs[b].write is u
@@ -636,7 +647,10 @@ def test_tasking_multi_output(self):
636647
body = op1._func_table['activate0'].root.body
637648
assert str(body.body[-1]) == \
638649
'__atomic_store_n(&sdata0[wi0].flag,2,__ATOMIC_RELEASE);'
639-
assert len(op1._func_table) == 5 + int(configuration['language'] == 'openacc')
650+
if configuration['language'] == 'openacc':
651+
assert len(op1._func_table) == 7
652+
else:
653+
assert len(op1._func_table) == 5
640654
exprs = FindNodes(Expression).visit(op1._func_table['copy_to_host0'].root)
641655
b = 21 if configuration['language'] == 'openacc' else 20 # No `qid` w/ OMP
642656
assert exprs[b].write is usave
@@ -695,8 +709,9 @@ def test_streaming_basic(self, opt, ntmps):
695709
op = Operator(eqn, opt=opt)
696710

697711
# Check generated code
712+
# OpenACC maps both the Function and its staging array.
698713
if configuration['language'] == 'openacc':
699-
assert len(op._func_table) == 10
714+
assert len(op._func_table) == 11
700715
else:
701716
assert len(op._func_table) == 8
702717
assert len([i for i in FindSymbols().visit(op) if i.is_Array]) == ntmps
@@ -771,7 +786,7 @@ def test_streaming_two_buffers(self, opt, ntmps):
771786
# Check generated code
772787
arrays = [i for i in FindSymbols().visit(op) if i.is_Array]
773788
if configuration['language'] == 'openacc':
774-
assert len(op._func_table) == 10
789+
assert len(op._func_table) == 11
775790
assert len(arrays) == ntmps
776791
else:
777792
assert len(op._func_table) == 8
@@ -982,7 +997,7 @@ def test_streaming_multi_input(self, opt, ntmps):
982997

983998
# Check generated code
984999
if configuration['language'] == 'openacc':
985-
assert len(op1._func_table) == 10
1000+
assert len(op1._func_table) == 11
9861001
else:
9871002
assert len(op1._func_table) == 8
9881003
assert len([i for i in FindSymbols().visit(op1) if i.is_Array]) == ntmps
@@ -1074,7 +1089,7 @@ def test_streaming_postponed_deletion(self, opt, ntmps):
10741089

10751090
# Check generated code
10761091
if configuration['language'] == 'openacc':
1077-
assert len(op1._func_table) == 10
1092+
assert len(op1._func_table) == 11
10781093
else:
10791094
assert len(op1._func_table) == 8
10801095
assert len([i for i in FindSymbols().visit(op1) if i.is_Array]) == ntmps
@@ -1160,7 +1175,7 @@ def test_composite_buffering_tasking_multi_output(self):
11601175
# loop nests, but they eventually get mapped to the same pair of efuncs,
11611176
# since devito attempts to maximize code reuse
11621177
if configuration['language'] == 'openacc':
1163-
assert len(op1._func_table) == 9
1178+
assert len(op1._func_table) == 10
11641179
else:
11651180
assert len(op1._func_table) == 7
11661181

@@ -1383,7 +1398,7 @@ def test_save_multi_output(self):
13831398
# are identical, so they get mapped to the same efuncs (init + copy)
13841399
# There also are two extra functions to allocate and free arrays
13851400
if configuration['language'] == 'openacc':
1386-
assert len(op._func_table) == 9
1401+
assert len(op._func_table) == 10
13871402
else:
13881403
assert len(op._func_table) == 7
13891404

@@ -1442,7 +1457,7 @@ def test_save_w_nonaffine_time(self):
14421457
# We just check the generated code here
14431458
assert len([i for i in FindSymbols().visit(op) if isinstance(i, Lock)]) == 1
14441459
if configuration['language'] == 'openacc':
1445-
assert len(op._func_table) == 9
1460+
assert len(op._func_table) == 11
14461461
else:
14471462
assert len(op._func_table) == 7
14481463

@@ -1502,7 +1517,7 @@ def test_streaming_w_shifting(self, opt, ntmps):
15021517

15031518
# Check generated code
15041519
if configuration['language'] == 'openacc':
1505-
assert len(op._func_table) == 10
1520+
assert len(op._func_table) == 11
15061521
else:
15071522
assert len(op._func_table) == 8
15081523
assert len([i for i in FindSymbols().visit(op) if i.is_Array]) == ntmps
@@ -1560,16 +1575,21 @@ def test_streaming_complete(self):
15601575
{'npthreads': 1}))
15611576

15621577
# Check generated code
1563-
diff = int(configuration['language'] == 'openmp')
1564-
extra = int(configuration['language'] == 'openacc')
1565-
assert len(op1._func_table) == 14 - diff + extra
1566-
assert len([i for i in FindSymbols().visit(op1) if i.is_Array]) == 9 - diff
1567-
assert len(op2._func_table) == 14 - diff + extra
1568-
assert len([i for i in FindSymbols().visit(op2) if i.is_Array]) == 9 - diff
15691578
# The streaming/buffering cannot be fused since the guards of
15701579
# vas and vasb/vasf are not compatible.
1571-
assert len(op3._func_table) == 12 - diff + extra
1572-
assert len([i for i in FindSymbols().visit(op3) if i.is_Array]) == 9 - diff
1580+
if configuration['language'] == 'openacc':
1581+
expected_funcs = (16, 16, 14)
1582+
expected_arrays = 9
1583+
elif configuration['language'] == 'openmp':
1584+
expected_funcs = (13, 13, 11)
1585+
expected_arrays = 8
1586+
else:
1587+
expected_funcs = (14, 14, 12)
1588+
expected_arrays = 9
1589+
for op, nfuncs in zip((op1, op2, op3), expected_funcs, strict=True):
1590+
assert len(op._func_table) == nfuncs
1591+
arrays = [i for i in FindSymbols().visit(op) if i.is_Array]
1592+
assert len(arrays) == expected_arrays
15731593

15741594
op0.apply(time_m=15, time_M=35, save_shift=0)
15751595
op1.apply(time_m=15, time_M=36, save_shift=0, u=u1)

‎tests/test_gpu_openacc.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ def test_basic(self, mode):
423423

424424
# Make sure we've indeed generated OpenACC+MPI code
425425
assert 'acc parallel' in str(op)
426-
assert len(op._func_table) == 4
426+
assert len(op._func_table) == 5
427427

428428
op(time_M=1)
429429

0 commit comments

Comments
 (0)