8000 Merge pull request #20 from opesci/memory_align · devitocodes/devito@7022239 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7022239

Browse files
committed
Merge pull request #20 from opesci/memory_align
Force alignment of arrays in memory
2 parents 6d57c6e + ede51f4 commit 7022239

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

devito/interfaces.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import numpy as np
22
from sympy import IndexedBase
3+
from tools import aligned
34

45

56
class DenseData(IndexedBase):
@@ -19,7 +20,7 @@ def set_initializer(self, lambda_initializer):
1920
self.initializer = lambda_initializer
2021

2122
def _allocate_memory(self):
22-
self.pointer = np.zeros(self.var_shape, self.dtype, order='C')
23+
self.pointer = aligned(np.zeros(self.var_shape, self.dtype, order='C'), alignment=64)
2324

2425
@property
2526
def data(self):

devito/tools.py

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import numpy as np
2+
3+
4+
def aligned(a, alignment=16):
5+
if (a.ctypes.data % alignment) == 0:
6+
return a
7+
8+
extra = alignment / a.itemsize
9+
buf = np.empty(a.size + extra, dtype=a.dtype)
10+
ofs = (-buf.ctypes.data % alignment) / a.itemsize
11+
aa = buf[ofs:ofs+a.size].reshape(a.shape)
12+
np.copyto(aa, a)
13+
assert (aa.ctypes.data % alignment) == 0
14+
return aa