Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify instructions which depend on unrolled inames. #900

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions examples/python/loop_unroll_codegen.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import numpy as np

import pyopencl as cl
import pyopencl.array

import loopy as lp
from loopy.version import LOOPY_USE_LANGUAGE_VERSION_2018_2 # noqa: F401


# setup
# -----
ctx = cl.create_some_context()
queue = cl.CommandQueue(ctx)

n = 15 * 10**6
a = cl.array.arange(queue, n, dtype=np.float32)

# create
# ------
knl = lp.make_kernel(
"{ [i]: 0<= i <8}",
"out[i] = a if i == 0 else (b if i == 1 else c)")

knl = lp.tag_inames(knl, {"i": "vec"})
from loopy.kernel.array import VectorArrayDimTag


try:
orig_knl = knl
knl = lp.tag_array_axes(knl, "out", [VectorArrayDimTag()])
knl = lp.add_and_infer_dtypes(knl, {"a": np.float32,
"b": np.float32,
"c": np.float32})

dev_code = lp.generate_code_v2(knl).device_code()
print(dev_code)

except Exception as err:
print(err)

print("No Vector Array Tag.")
knl = orig_knl
knl = lp.make_kernel(
"{ [i]: 0<= i <8}",
"out[i] = a if i == 0 else (b if i == 1 else c)")

knl = lp.tag_inames(knl, {"i": "ilp.unr"})
knl = lp.add_and_infer_dtypes(knl, {"a": np.float32, "b": np.float32, "c": np.float32})
dev_code = lp.generate_code_v2(knl).device_code()
print(dev_code)
36 changes: 35 additions & 1 deletion loopy/codegen/loop.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@
from loopy.codegen.control import build_loop_nest
from loopy.codegen.result import merge_codegen_results
from loopy.diagnostic import LoopyError, warn
from loopy.symbolic import flatten
from loopy.symbolic import ConstantFoldingMapper, SubstitutionMapper, flatten
from pymbolic.mapper.substitutor import make_subst_func
from loopy.transform.parameter import fix_parameters


# {{{ conditional-reducing slab decomposition
Expand Down Expand Up @@ -151,9 +153,41 @@ def generate_unroll_loop(codegen_state, sched_index):

result = []

fold_consts = ConstantFoldingMapper()

for i in range(length):
idx_aff = lower_bound_aff + i
new_codegen_state = codegen_state.fix(iname, idx_aff)
original_knl_ = new_codegen_state.kernel.copy()
context = new_codegen_state.var_subst_map
# Add in the other variables as variables.

from loopy.kernel.instruction import Assignment
#new_knl = fix_parameters(original_knl_, **context)

subst_func = make_subst_func(context)
mymapper = SubstitutionMapper(subst_func)

new_insns = []
for insn in new_codegen_state.kernel.instructions:
"""
new_insn = mymapper(insn)
new_insns.append(fold_consts(new_insn))

"""
if isinstance(insn, Assignment):
# We can update the evaluation of this potentially.
new_expr = mymapper(insn.expression)
new_expr = fold_consts(new_expr)
new_insns.append(insn.copy(expression=new_expr))
else:
new_insns.append(insn)

new_knl = original_knl_.copy(instructions=new_insns)
new_codegen_state = new_codegen_state.copy(kernel=new_knl)

#new_codegen_state = new_codegen_state.copy(kernel=new_knl)

result.append(
build_loop_nest(new_codegen_state, sched_index+1))

Expand Down
Loading