Skip to content

Commit

Permalink
Transformations: Appeasing linter gods through goat sacrifices!
Browse files Browse the repository at this point in the history
The "system" works!
  • Loading branch information
mlange05 committed Nov 22, 2024
1 parent 2c88983 commit 655d74a
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 74 deletions.
7 changes: 3 additions & 4 deletions loki/transformations/parallel/tests/test_field_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@

import pytest

from loki import Subroutine, Module, Dimension
from loki import Subroutine, Dimension
from loki.frontend import available_frontends, OMNI
from loki.ir import nodes as ir, FindNodes
from loki.expression import symbols as sym
from loki.scope import Scope
from loki.transformations.parallel import (
remove_field_api_view_updates, add_field_api_view_updates, get_field_type,
field_get_device_data, FieldAPITransferType
remove_field_api_view_updates, add_field_api_view_updates,
get_field_type, field_get_device_data, FieldAPITransferType
)
from loki.types import BasicType, SymbolAttributes
from loki.logging import WARNING
Expand Down Expand Up @@ -198,4 +198,3 @@ def test_field_get_device_data():
assert get_dev_data_call.name.parent == fptr
with pytest.raises(TypeError):
_ = field_get_device_data(fptr, dev_ptr, "none_transfer_type", scope)

6 changes: 3 additions & 3 deletions loki/transformations/parallel/tests/test_openmp_region.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,11 +262,11 @@ def test_remove_firstprivate_copies(frontend):
@pytest.mark.parametrize('frontend', available_frontends(
skip=[(OMNI, 'OMNI needs full type definitions for derived types')]
))
def test_add_firstprivate_copies(tmp_path, frontend):
def test_add_firstprivate_copies(frontend):
"""
A simple test for :any:`add_firstprivate_copies`
"""

fcode = """
subroutine test_add_openmp_loop(ydgeom, state, arr)
use geom_mod, only: geom_type
Expand Down Expand Up @@ -312,7 +312,7 @@ def test_add_firstprivate_copies(tmp_path, frontend):
assert str(calls[0].name).startswith('state%')
assert calls[1].arguments[0].parent == 'state'
assert len(FindNodes(ir.Loop).visit(routine.body)) == 2

# Put the explicit firstprivate copies back in
add_firstprivate_copies(
routine=routine, fprivate_map=fprivate_map
Expand Down
4 changes: 2 additions & 2 deletions loki/transformations/sanitise/tests/test_associates.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import pytest

from loki import BasicType, Subroutine, Module
from loki import BasicType, Subroutine
from loki.expression import symbols as sym
from loki.frontend import available_frontends, OMNI
from loki.ir import nodes as ir, FindNodes
Expand Down Expand Up @@ -447,7 +447,7 @@ def test_resolve_associates_stmt_func(frontend):
Test scope management for stmt funcs, either as
:any:`ProcedureSymbol` or :any:`DeferredTypeSymbol`.
"""
fcode = f"""
fcode = """
subroutine test_associates_stmt_func(ydcst, a, b)
use yomcst, only: tcst
implicit none
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -624,7 +624,7 @@ def test_scc_devector_section_special_case(frontend, horizontal, vertical, block

# Check that all else-bodies have been wrapped
else_bodies = conditional.else_bodies
assert(len(else_bodies) == 3)
assert len(else_bodies) == 3
for body in else_bodies:
assert isinstance(body[0], ir.Comment)
assert isinstance(body[1], ir.Loop)
Expand Down
135 changes: 75 additions & 60 deletions loki/transformations/tests/test_loop_blocking.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,15 @@
import pytest
import numpy as np

from loki import available_frontends, Subroutine, pragmas_attached, find_driver_loops, Loop, fgen, \
ir, FindNodes, jit_compile, clean_test, FindVariables, Array
from loki import Subroutine
from loki.build import jit_compile, clean_test
from loki.expression import Array
from loki.frontend import available_frontends
from loki.ir import (
nodes as ir, FindNodes, FindVariables, pragmas_attached
)

from loki.transformations.utilities import find_driver_loops
from loki.transformations.loop_blocking import split_loop, block_loop_arrays


Expand Down Expand Up @@ -43,16 +50,17 @@ def test_1d_splitting(tmp_path, frontend, block_size, n):
loops = FindNodes(ir.Loop).visit(routine.ir)
num_loops = len(loops)
num_vars = len(routine.variable_map)
with pragmas_attached(routine, Loop):
loops = find_driver_loops(routine.body,
targets=None)
splitting_vars, inner_loop, outer_loop = split_loop(routine, loops[0], block_size)
with pragmas_attached(routine, ir.Loop):
loops = find_driver_loops(routine.body, targets=None)
split_loop(routine, loops[0], block_size)
loops = FindNodes(ir.Loop).visit(routine.ir)

assert len(
loops) == num_loops + 1, f"Total number of loops transformation is: {len(loops)} but expected {num_loops + 1}"
assert len(
routine.variable_map) == num_vars + LOKI_LOOP_SLIT_VAR_ADDITION, f"Total number of variables after loop splitting is: {len(routine.variable_map)} but expected {num_vars + LOKI_LOOP_SLIT_VAR_ADDITION}"
assert len(loops) == num_loops + 1, \
f"Total number of loops transformation is: {len(loops)} but expected {num_loops + 1}"
assert len(routine.variable_map) == num_vars + LOKI_LOOP_SLIT_VAR_ADDITION, (
f"Total number of variables after loop splitting is: {len(routine.variable_map)} "
f"but expected {num_vars + LOKI_LOOP_SLIT_VAR_ADDITION}"
)

filepath = tmp_path / (f'{routine.name}_{frontend}.f90')
function = jit_compile(routine, filepath=filepath, objname=routine.name)
Expand Down Expand Up @@ -93,16 +101,17 @@ def test_1d_splitting_multi_var(tmp_path, frontend, block_size, n):
loops = FindNodes(ir.Loop).visit(routine.ir)
num_loops = len(loops)
num_vars = len(routine.variable_map)
with pragmas_attached(routine, Loop):
loops = find_driver_loops(routine.body,
targets=None)
splitting_vars, inner_loop, outer_loop = split_loop(routine, loops[0], block_size)
with pragmas_attached(routine, ir.Loop):
loops = find_driver_loops(routine.body, targets=None)
split_loop(routine, loops[0], block_size)
loops = FindNodes(ir.Loop).visit(routine.ir)

assert len(
loops) == num_loops + 1, f"Total number of loops transformation is: {len(loops)} but expected {num_loops + 1}"
assert len(
routine.variable_map) == num_vars + LOKI_LOOP_SLIT_VAR_ADDITION, f"Total number of variables after loop splitting is: {len(routine.variable_map)} but expected {num_vars + LOKI_LOOP_SLIT_VAR_ADDITION}"
assert len(loops) == num_loops + 1, \
f"Total number of loops transformation is: {len(loops)} but expected {num_loops + 1}"
assert len(routine.variable_map) == num_vars + LOKI_LOOP_SLIT_VAR_ADDITION, (
f"Total number of variables after loop splitting is: {len(routine.variable_map)} "
f"but expected {num_vars + LOKI_LOOP_SLIT_VAR_ADDITION}"
)

filepath = tmp_path / (f'{routine.name}_{frontend}.f90')
function = jit_compile(routine, filepath=filepath, objname=routine.name)
Expand Down Expand Up @@ -141,16 +150,17 @@ def test_2d_splitting(tmp_path, frontend, block_size, n):
loops = FindNodes(ir.Loop).visit(routine.ir)
num_loops = len(loops)
num_vars = len(routine.variable_map)
with pragmas_attached(routine, Loop):
loops = find_driver_loops(routine.body,
targets=None)
splitting_vars, inner_loop, outer_loop = split_loop(routine, loops[0], block_size)
with pragmas_attached(routine, ir.Loop):
loops = find_driver_loops(routine.body, targets=None)
split_loop(routine, loops[0], block_size)
loops = FindNodes(ir.Loop).visit(routine.ir)

assert len(
loops) == num_loops + 1, f"Total number of loops transformation is: {len(loops)} but expected {num_loops + 1}"
assert len(
routine.variable_map) == num_vars + LOKI_LOOP_SLIT_VAR_ADDITION, f"Total number of variables after loop splitting is: {len(routine.variable_map)} but expected {num_vars + LOKI_LOOP_SLIT_VAR_ADDITION}"
assert len(loops) == num_loops + 1, \
f"Total number of loops transformation is: {len(loops)} but expected {num_loops + 1}"
assert len(routine.variable_map) == num_vars + LOKI_LOOP_SLIT_VAR_ADDITION, (
f"Total number of variables after loop splitting is: {len(routine.variable_map)} "
f"but expected {num_vars + LOKI_LOOP_SLIT_VAR_ADDITION}"
)

filepath = tmp_path / (f'{routine.name}_{frontend}.f90')
function = jit_compile(routine, filepath=filepath, objname=routine.name)
Expand Down Expand Up @@ -191,16 +201,17 @@ def test_3d_splitting(tmp_path, frontend, block_size, n):
loops = FindNodes(ir.Loop).visit(routine.ir)
num_loops = len(loops)
num_vars = len(routine.variable_map)
with pragmas_attached(routine, Loop):
loops = find_driver_loops(routine.body,
targets=None)
splitting_vars, inner_loop, outer_loop = split_loop(routine, loops[0], block_size)
with pragmas_attached(routine, ir.Loop):
loops = find_driver_loops(routine.body, targets=None)
split_loop(routine, loops[0], block_size)
loops = FindNodes(ir.Loop).visit(routine.ir)

assert len(
loops) == num_loops + 1, f"Total number of loops transformation is: {len(loops)} but expected {num_loops + 1}"
assert len(
routine.variable_map) == num_vars + LOKI_LOOP_SLIT_VAR_ADDITION, f"Total number of variables after loop splitting is: {len(routine.variable_map)} but expected {num_vars + LOKI_LOOP_SLIT_VAR_ADDITION}"
assert len(loops) == num_loops + 1, \
f"Total number of loops transformation is: {len(loops)} but expected {num_loops + 1}"
assert len(routine.variable_map) == num_vars + LOKI_LOOP_SLIT_VAR_ADDITION, (
f"Total number of variables after loop splitting is: {len(routine.variable_map)} "
f"but expected {num_vars + LOKI_LOOP_SLIT_VAR_ADDITION}"
)

filepath = tmp_path / (f'{routine.name}_{frontend}.f90')
function = jit_compile(routine, filepath=filepath, objname=routine.name)
Expand Down Expand Up @@ -249,7 +260,7 @@ def test_1d_blocking(tmp_path, frontend, block_size, n):
"""
routine = Subroutine.from_source(fcode, frontend=frontend)
loops = FindNodes(ir.Loop).visit(routine.ir)
with pragmas_attached(routine, Loop):
with pragmas_attached(routine, ir.Loop):
loops = find_driver_loops(routine.body,
targets=None)

Expand All @@ -258,10 +269,12 @@ def test_1d_blocking(tmp_path, frontend, block_size, n):

splitting_vars, inner_loop, outer_loop = split_loop(routine, loops[0], block_size)
loops = FindNodes(ir.Loop).visit(routine.ir)
assert len(
loops) == num_loops + 1, f"Total number of loops transformation is: {len(loops)} but expected {num_loops + 1}"
assert len(
routine.variable_map) == num_vars + LOKI_LOOP_SLIT_VAR_ADDITION, f"Total number of variables after loop splitting is: {len(routine.variable_map)} but expected {num_vars + LOKI_LOOP_SLIT_VAR_ADDITION}"
assert len(loops) == num_loops + 1, \
f"Total number of loops transformation is: {len(loops)} but expected {num_loops + 1}"
assert len(routine.variable_map) == num_vars + LOKI_LOOP_SLIT_VAR_ADDITION, (
f"Total number of variables after loop splitting is: {len(routine.variable_map)} "
f"but expected {num_vars + LOKI_LOOP_SLIT_VAR_ADDITION}"
)
num_vars = len(routine.variable_map)

blocking_indices = ['i']
Expand Down Expand Up @@ -308,19 +321,20 @@ def test_1d_blocking_multi_intent(tmp_path, frontend, block_size, n):
"""
routine = Subroutine.from_source(fcode, frontend=frontend)
loops = FindNodes(ir.Loop).visit(routine.ir)
with pragmas_attached(routine, Loop):
with pragmas_attached(routine, ir.Loop):
loops = find_driver_loops(routine.body,
targets=None)

num_loops = len(loops)
num_vars = len(routine.variable_map)
splitting_vars, inner_loop, outer_loop = split_loop(routine, loops[0], block_size)
loops = FindNodes(ir.Loop).visit(routine.ir)
assert len(
loops) == num_loops + 1, f"Total number of loops transformation is: {len(loops)} but expected {num_loops + 1}"
assert len(
routine.variable_map) == num_vars + LOKI_LOOP_SLIT_VAR_ADDITION, f"Total number of variables after loop splitting is: {len(routine.variable_map)} but expected {num_vars + LOKI_LOOP_SLIT_VAR_ADDITION}"

assert len(loops) == num_loops + 1, \
f"Total number of loops transformation is: {len(loops)} but expected {num_loops + 1}"
assert len(routine.variable_map) == num_vars + LOKI_LOOP_SLIT_VAR_ADDITION, (
f"Total number of variables after loop splitting is: {len(routine.variable_map)} "
f"but expected {num_vars + LOKI_LOOP_SLIT_VAR_ADDITION}"
)

num_vars = len(routine.variable_map)
blocking_indices = ['i']
Expand Down Expand Up @@ -371,16 +385,17 @@ def test_2d_blocking(tmp_path, frontend, block_size, n):
loops = FindNodes(ir.Loop).visit(routine.ir)
num_loops = len(loops)
num_vars = len(routine.variable_map)
with pragmas_attached(routine, Loop):
loops = find_driver_loops(routine.body,
targets=None)
with pragmas_attached(routine, ir.Loop):
loops = find_driver_loops(routine.body, targets=None)
splitting_vars, inner_loop, outer_loop = split_loop(routine, loops[0], block_size)
loops = FindNodes(ir.Loop).visit(routine.ir)

assert len(
loops) == num_loops + 1, f"Total number of loops transformation is: {len(loops)} but expected {num_loops + 1}"
assert len(
routine.variable_map) == num_vars + LOKI_LOOP_SLIT_VAR_ADDITION, f"Total number of variables after loop splitting is: {len(routine.variable_map)} but expected {num_vars + LOKI_LOOP_SLIT_VAR_ADDITION}"
assert len(loops) == num_loops + 1, \
f"Total number of loops transformation is: {len(loops)} but expected {num_loops + 1}"
assert len(routine.variable_map) == num_vars + LOKI_LOOP_SLIT_VAR_ADDITION, (
f"Total number of variables after loop splitting is: {len(routine.variable_map)} "
f"but expected {num_vars + LOKI_LOOP_SLIT_VAR_ADDITION}"
)

num_vars = len(routine.variable_map)
blocking_indices = ['i']
Expand All @@ -406,7 +421,6 @@ def test_2d_blocking(tmp_path, frontend, block_size, n):
clean_test(filepath)



@pytest.mark.parametrize('frontend', available_frontends())
@pytest.mark.parametrize('block_size', [117])
@pytest.mark.parametrize('n', [500])
Expand All @@ -431,16 +445,17 @@ def test_3d_blocking(tmp_path, frontend, block_size, n):
loops = FindNodes(ir.Loop).visit(routine.ir)
num_loops = len(loops)
num_vars = len(routine.variable_map)
with pragmas_attached(routine, Loop):
loops = find_driver_loops(routine.body,
targets=None)
with pragmas_attached(routine, ir.Loop):
loops = find_driver_loops(routine.body, targets=None)
splitting_vars, inner_loop, outer_loop = split_loop(routine, loops[0], block_size)
loops = FindNodes(ir.Loop).visit(routine.ir)

assert len(
loops) == num_loops + 1, f"Total number of loops transformation is: {len(loops)} but expected {num_loops + 1}"
assert len(
routine.variable_map) == num_vars + LOKI_LOOP_SLIT_VAR_ADDITION, f"Total number of variables after loop splitting is: {len(routine.variable_map)} but expected {num_vars + LOKI_LOOP_SLIT_VAR_ADDITION}"
assert len(loops) == num_loops + 1, \
f"Total number of loops transformation is: {len(loops)} but expected {num_loops + 1}"
assert len(routine.variable_map) == num_vars + LOKI_LOOP_SLIT_VAR_ADDITION, (
f"Total number of variables after loop splitting is: {len(routine.variable_map)} "
f"but expected {num_vars + LOKI_LOOP_SLIT_VAR_ADDITION}"
)

num_vars = len(routine.variable_map)
blocking_indices = ['i']
Expand Down
6 changes: 3 additions & 3 deletions loki/transformations/tests/test_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ def test_transform_utilites_get_loop_bounds(frontend, tmp_path):
y = Dimension(name='y', size='n', index='i', bounds=('a', 'b'))
z = Dimension(name='y', size='n', index='i', bounds=('dim%a', 'dim%b'))

start, end = get_loop_bounds(routine, x)
start, end = get_loop_bounds(routine, x) # pylint: disable=unbalanced-tuple-unpacking
assert isinstance(start, sym.Scalar)
assert start.type.dtype == BasicType.INTEGER
assert start.type.intent == 'in'
Expand All @@ -403,10 +403,10 @@ def test_transform_utilites_get_loop_bounds(frontend, tmp_path):
assert end.type.intent == 'in'

with pytest.raises(RuntimeError):
_, _ = get_loop_bounds(routine, y)
_, _ = get_loop_bounds(routine, y) # pylint: disable=unbalanced-tuple-unpacking

# Test type-bound symbol resolution
start, end = get_loop_bounds(routine, z)
start, end = get_loop_bounds(routine, z) # pylint: disable=unbalanced-tuple-unpacking
assert isinstance(start, sym.Scalar)
assert start.type.dtype == BasicType.INTEGER
assert start.type.kind == '8'
Expand Down
1 change: 0 additions & 1 deletion loki/transformations/transpile/tests/test_sdfg.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import importlib
import itertools
from shutil import rmtree
from pathlib import Path
import numpy as np
import pytest
Expand Down

0 comments on commit 655d74a

Please sign in to comment.