Skip to content

Commit

Permalink
Added sparse fill tests and improved reduction tests (dawn-ico#43)
Browse files Browse the repository at this point in the history
* Improved reduction tests

* Added simple sparse fill tests

* Addressed PR review

* Addressed review comments

* Added ambiguous sparse fill tests
  • Loading branch information
BenWeber42 authored Sep 21, 2020
1 parent 6cce9ce commit 7c494d5
Show file tree
Hide file tree
Showing 2 changed files with 155 additions and 5 deletions.
48 changes: 43 additions & 5 deletions tests/stencils/test_reduce.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,52 @@


def test_reduce():
transpile_and_validate(various_reductions)
transpile_and_validate(kw_args)


# TODO: Add tests for the following cases
# different expressions
# different location chains
# nested reductions
# expressions in weights (& init)
@stencil
def various_reductions(
vertex: Field[Vertex],
edge: Field[Edge, K],
cell: Field[Cell],
ve: Field[Vertex > Edge, K],
vc: Field[Vertex > Cell, K],
ev: Field[Edge > Vertex, K],
ec: Field[Edge > Cell, K],
cv: Field[Cell > Vertex, K],
ce: Field[Cell > Edge, K],
sparse1: Field[Vertex > Edge > Cell > Vertex > Edge > Cell],
sparse2: Field[Cell > Edge > Cell > Edge > Cell > Edge],
sparse3: Field[Cell > Vertex > Cell > Vertex > Cell > Edge],
):
with levels_upward:
edge = sum_over(Edge > Cell, cell * ec)

cell = max_over(Cell > Vertex, pow(vertex, cv / cell))

vertex = reduce_over(
Vertex > Edge,
log(ve)
/ max_over(
Edge > Cell > Vertex > Edge,
sin(edge[Edge]) / exp(edge[Edge > Cell > Vertex > Edge]) ** 5
- sum_over(
Edge > Cell,
sqrt(cell),
weights=[edge[Edge], arcsin(edge[Edge] * 100),],
),
),
mul,
init=sin(vertex) * floor(vertex / 8.765),
)

cell = reduce_over(
Cell > Vertex > Cell > Vertex > Cell > Edge,
sparse3 + sin(edge),
min,
init=-1,
)


@stencil
Expand Down
112 changes: 112 additions & 0 deletions tests/stencils/test_sparse_fill.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
from dusk.script import *
from test_util import transpile_and_validate


def test_sparse_fill():
transpile_and_validate(sparse_order_2_fill)
transpile_and_validate(longer_fills)
transpile_and_validate(fill_with_reduction)
transpile_and_validate(ambiguous_fill)
transpile_and_validate(ambiguous_sparse_fill_bug)


@stencil
def sparse_order_2_fill(
vertex: Field[Vertex],
edge: Field[Edge, K],
cell: Field[Cell],
ve1: Field[Vertex > Edge, K],
ve2: Field[Vertex > Edge, K],
vc: Field[Vertex > Cell, K],
ev1: Field[Edge > Vertex, K],
ev2: Field[Edge > Vertex, K],
ec: Field[Edge > Cell, K],
cv1: Field[Cell > Vertex, K],
cv2: Field[Cell > Vertex, K],
ce: Field[Cell > Edge, K],
):

with levels_upward:

with sparse[Vertex > Edge]:
ve1 = edge + 5 * ve2 + log(vertex)
with sparse[Vertex > Cell]:
vc = cell - 6
with sparse[Edge > Vertex]:
ev1 = vertex * 7 + ev2
with sparse[Edge > Cell]:
ec = cell / 8 / sin(edge)
with sparse[Cell > Vertex]:
cv1 = max(max(vertex ** 9, cv2), cell)
with sparse[Cell > Edge]:
ce = sqrt(edge ** 2 + cell ** 2)


@stencil
def longer_fills(
sparse1: Field[Vertex > Edge > Cell > Vertex > Edge > Cell],
sparse2: Field[Cell > Edge > Cell > Edge > Cell > Edge],
sparse3: Field[Edge > Cell > Vertex > Edge > Vertex > Edge],
):
with levels_upward:
with sparse[Vertex > Edge > Cell > Vertex > Edge > Cell]:
sparse1 = 50

with levels_upward:
with sparse[Cell > Edge > Cell > Edge > Cell > Edge]:
sparse2 = 50

with levels_upward:
with sparse[Edge > Cell > Vertex > Edge > Vertex > Edge]:
sparse3 = 50


@stencil
def fill_with_reduction(
sparse1: Field[Edge > Cell > Vertex],
sparse2: Field[Edge > Cell > Vertex],
vertex: Field[Vertex],
edge: Field[Edge, K],
cell: Field[Cell],
):
with levels_upward:
with sparse[Edge > Cell > Vertex]:
sparse1 = sum_over(Vertex > Cell, cell)
sparse2 = min_over(Vertex > Edge, edge)

with levels_upward:
with sparse[Edge > Cell > Vertex]:
sparse1 = sum_over(Edge > Vertex, vertex[Edge > Vertex])
sparse2 = sum_over(Edge > Cell, cell)

with levels_upward:
with sparse[Edge > Cell > Vertex]:
sparse1 = sum_over(Vertex > Cell > Vertex, vertex[Vertex])
sparse2 = min_over(Vertex > Edge > Vertex, vertex[Vertex > Edge > Vertex])


@stencil
def ambiguous_fill(
sparse1: Field[Edge > Cell > Edge],
sparse2: Field[Edge > Vertex > Edge],
edge1: Field[Edge, K],
edge2: Field[Edge, K],
):
with levels_downward:
with sparse[Edge > Cell > Edge]:
sparse1 = edge1[Edge] + 2 * edge2[Edge > Cell > Edge]

with levels_downward:
with sparse[Edge > Vertex > Edge]:
sparse2 = edge2[Edge] - 4 * edge1[Edge > Vertex > Edge]


@stencil
def ambiguous_sparse_fill_bug(
sparse_field: Field[Edge > Vertex > Edge, K], edge: Field[Edge, K],
):
with levels_downward:
# different location chain in sparse fill than `sparse_field`
with sparse[Edge > Cell > Edge]:
# invalid sparse write
sparse_field = edge[Edge > Cell > Edge]

0 comments on commit 7c494d5

Please sign in to comment.