Skip to content

Commit 8192c0f

Browse files
Merge pull request #3042 from devitocodes/temp_alloc_cast
compiler: Cast operands to long when calculating sizes for array allocation
2 parents 66d6755 + e100bb6 commit 8192c0f

3 files changed

Lines changed: 27 additions & 2 deletions

File tree

‎devito/passes/iet/definitions.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -601,7 +601,7 @@ def _alloc_local_array_on_high_bw_mem(self, site, obj, storage):
601601
doalloc = self.langbb['device-alloc']
602602
dofree = self.langbb['device-free']
603603

604-
nbytes = SizeOf(obj._C_typedata)*obj.size
604+
nbytes = SizeOf(obj._C_typedata)*as_long(obj.size)
605605

606606
zeroing, efuncs = self._zero_init(obj, storage)
607607
allocs = [doalloc(nbytes, deviceid, retobj=obj), *zeroing]

‎tests/test_dse.py‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2976,6 +2976,30 @@ def test_subdims_wo_block_temps(self):
29762976

29772977
op.apply()
29782978

2979+
@pytest.mark.parametrize('lang', ['C', 'CXX'])
2980+
def test_temp_array_allocation_uses_long(self, lang):
2981+
"""
2982+
Ensure that operands are cast to long when calculating the size of a
2983+
temporary array to prevent int32 wraparound.
2984+
"""
2985+
grid = Grid(shape=(31, 17, 25))
2986+
2987+
a = Function(name='a', grid=grid)
2988+
f = TimeFunction(name='f', grid=grid)
2989+
g = TimeFunction(name='g', grid=grid)
2990+
2991+
eq0 = Eq(f.forward, g + sin(a).dx)
2992+
eq1 = Eq(g.forward, f + sin(a).dx)
2993+
2994+
opt = ('advanced', {'cire-minmem': True, 'index-mode': 'int64'})
2995+
with switchconfig(language=lang):
2996+
op = Operator([eq0, eq1], opt=opt)
2997+
2998+
cast = '(long)' if lang == 'C' else 'static_cast<long>'
2999+
3000+
# 3 casts in memalign, 3 in memset
3001+
assert str(op).count(cast) == 6
3002+
29793003

29803004
class TestIsoAcoustic:
29813005

‎tests/test_gpu_openmp.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,8 @@ def test_array_rw(self):
217217
assert len(op.body.allocs) == 2
218218
assert str(op.body.allocs[0]) ==\
219219
('float * r0_vec = (float *)'
220-
'omp_target_alloc(x_size*y_size*z_size*sizeof(float),'
220+
'omp_target_alloc('
221+
'sizeof(float)*(long)z_size*(long)y_size*(long)x_size,'
221222
'omp_get_default_device());')
222223
assert str(op.body.allocs[1]) ==\
223224
'init0(x_M,x_m,y_M,y_m,z_M,z_m,r0_vec,y_size,z_size);'

0 commit comments

Comments
 (0)