|
22 | 22 | ) |
23 | 23 | from devito.tools import as_list, as_mapper, as_tuple, filter_sorted, flatten |
24 | 24 | from devito.types import ( |
25 | | - Array, ComponentAccess, CustomDimension, DeviceMap, DeviceRM, Dimension, Eq, Symbol, |
26 | | - size_t |
| 25 | + Array, ComponentAccess, CustomDimension, DeviceMap, DeviceRM, Dimension, Eq, Pointer, |
| 26 | + Symbol, size_t |
27 | 27 | ) |
28 | 28 |
|
29 | 29 | __all__ = ['DataManager', 'DeviceAwareDataManager', 'Storage'] |
@@ -172,11 +172,14 @@ def _alloc_host_array_on_high_bw_mem(self, site, obj, storage, *args): |
172 | 172 | memptr = VOID(Byref(obj._C_symbol), '**') |
173 | 173 | alignment = obj._data_alignment |
174 | 174 | nbytes = SizeOf(obj._C_typedata)*as_long(obj.size) |
175 | | - alloc = self.langbb['host-alloc'](memptr, alignment, nbytes) |
| 175 | + allocs = [decl, self.langbb['host-alloc'](memptr, alignment, nbytes)] |
| 176 | + if obj._is_zero_init: |
| 177 | + allocs.append(self.langbb['host-memset'](obj._C_symbol, 0, nbytes)) |
| 178 | + storage.include(self.langbb['header-memcpy']) |
176 | 179 |
|
177 | 180 | free = self.langbb['host-free'](obj._C_symbol) |
178 | 181 |
|
179 | | - storage.update(obj, site, allocs=(decl, alloc), frees=free) |
| 182 | + storage.update(obj, site, allocs=tuple(allocs), frees=free) |
180 | 183 |
|
181 | 184 | def _alloc_local_array_on_high_bw_mem(self, site, obj, storage, *args): |
182 | 185 | """ |
@@ -579,11 +582,23 @@ def _alloc_local_array_on_high_bw_mem(self, site, obj, storage): |
579 | 582 | dofree = self.langbb['device-free'] |
580 | 583 |
|
581 | 584 | nbytes = SizeOf(obj._C_typedata)*obj.size |
582 | | - init = doalloc(nbytes, deviceid, retobj=obj) |
| 585 | + allocs = [doalloc(nbytes, deviceid, retobj=obj)] |
| 586 | + |
| 587 | + if obj._is_zero_init: |
| 588 | + # No language here has a device-side memset, so stage the zeros |
| 589 | + # through a scratch host block; it's a one-off, at setup time |
| 590 | + scratch = Pointer(name=self.sregistry.make_name(prefix='zeros')) |
| 591 | + allocs.extend([ |
| 592 | + Call('calloc', (obj.size, SizeOf(obj._C_typedata)), |
| 593 | + retobj=scratch, cast=True), |
| 594 | + self.langbb['memcpy-to-device'](obj._C_symbol, scratch, nbytes), |
| 595 | + self.langbb['host-free'](scratch), |
| 596 | + ]) |
| 597 | + storage.include(self.langbb['header-memcpy']) |
583 | 598 |
|
584 | 599 | free = dofree(obj._C_name, deviceid) |
585 | 600 |
|
586 | | - storage.update(obj, site, allocs=init, frees=free) |
| 601 | + storage.update(obj, site, allocs=tuple(allocs), frees=free) |
587 | 602 |
|
588 | 603 | def _map_array_on_high_bw_mem(self, site, obj, storage): |
589 | 604 | """ |
@@ -704,16 +719,19 @@ def process(self, graph): |
704 | 719 |
|
705 | 720 | def make_zero_init(obj, rcompile, sregistry): |
706 | 721 | cdims = [] |
707 | | - for d, (h0, h1), s in zip( |
708 | | - obj.dimensions, obj._size_halo, obj.symbolic_shape, strict=True |
| 722 | + for d, (h0, h1), (p0, p1), s in zip( |
| 723 | + obj.dimensions, obj._size_halo, obj._size_padding, obj.symbolic_shape, |
| 724 | + strict=True |
709 | 725 | ): |
710 | 726 | if d.is_NonlinearDerived: |
711 | | - assert h0 == h1 == 0 |
| 727 | + assert h0 == h1 == p0 == p1 == 0 |
712 | 728 | m = 0 |
713 | 729 | M = s - 1 |
714 | 730 | else: |
715 | | - m = d.symbolic_min - h0 |
716 | | - M = d.symbolic_max + h1 |
| 731 | + # Spans the whole allocation, padding included, or the untouched |
| 732 | + # entries would be left holding junk |
| 733 | + m = d.symbolic_min - h0 - p0 |
| 734 | + M = d.symbolic_max + h1 + p1 |
717 | 735 | cdims.append(CustomDimension(name=d.name, parent=d, |
718 | 736 | symbolic_min=m, symbolic_max=M)) |
719 | 737 |
|
|
0 commit comments