Skip to content

Commit

Permalink
Optimize zany-mapping matvec
Browse files Browse the repository at this point in the history
  • Loading branch information
pbrubeck committed Jan 21, 2025
1 parent 24c6952 commit e8ff24b
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
9 changes: 6 additions & 3 deletions finat/physically_mapped.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,15 +270,18 @@ def basis_evaluation(self, order, ps, entity=None, coordinate_mapping=None):
M = self.basis_transformation(coordinate_mapping)
# we expect M to be sparse with O(1) nonzeros per row
# for each row, get the column index of each nonzero entry
csr = [[j for j in range(M.shape[1]) if not isinstance(M.array[i, j], gem.Zero)]
csr = [[j for j in range(M.shape[1]) if not isinstance(M[i, j], gem.Zero)]
for i in range(M.shape[0])]

def matvec(table):
# basis recombination using hand-rolled sparse-dense matrix multiplication
table = [gem.partial_indexed(table, (j,)) for j in range(M.shape[1])]
ii = gem.indices(len(table.shape)-1)
phi = [gem.Indexed(table, (j, *ii)) for j in range(M.shape[1])]
# the sum approach is faster than calling numpy.dot or gem.IndexSum
expressions = [sum(M.array[i, j] * table[j] for j in js) for i, js in enumerate(csr)]
expressions = [gem.ComponentTensor(sum(M[i, j] * phi[j] for j in js), ii)
for i, js in enumerate(csr)]
val = gem.ListTensor(expressions)
# val = M @ table
return gem.optimise.aggressive_unroll(val)

result = super().basis_evaluation(order, ps, entity=entity)
Expand Down
2 changes: 1 addition & 1 deletion gem/gem.py
Original file line number Diff line number Diff line change
Expand Up @@ -689,7 +689,7 @@ def __new__(cls, aggregate, multiindex):
ll = tuple(rep.get(k, k) for k in kk)
return Indexed(C, ll)

# Simplify Constant and ListTensor
# Simplify Literal and ListTensor
if isinstance(aggregate, (Constant, ListTensor)):
if all(isinstance(i, int) for i in multiindex):
# All indices fixed
Expand Down

0 comments on commit e8ff24b

Please sign in to comment.