|
| 1 | +__copyright__ = "Copyright (C) 2022 Isuru Fernando" |
| 2 | + |
| 3 | +__license__ = """ |
| 4 | +Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | +of this software and associated documentation files (the "Software"), to deal |
| 6 | +in the Software without restriction, including without limitation the rights |
| 7 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | +copies of the Software, and to permit persons to whom the Software is |
| 9 | +furnished to do so, subject to the following conditions: |
| 10 | +
|
| 11 | +The above copyright notice and this permission notice shall be included in |
| 12 | +all copies or substantial portions of the Software. |
| 13 | +
|
| 14 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 20 | +THE SOFTWARE. |
| 21 | +""" |
| 22 | + |
| 23 | +__doc__ = """ |
| 24 | +.. currentmodule:: loopy |
| 25 | +.. autofunction:: concatenate_arrays |
| 26 | +""" |
| 27 | + |
| 28 | +from typing import Sequence, Optional, List |
| 29 | + |
| 30 | +from loopy.kernel.data import ArrayArg, KernelArgument, TemporaryVariable, auto |
| 31 | +from loopy.symbolic import SubstitutionRuleMappingContext |
| 32 | +from loopy.kernel import LoopKernel |
| 33 | +from loopy.translation_unit import for_each_kernel |
| 34 | + |
| 35 | +import pymbolic.primitives as prim |
| 36 | +from pytools import all_equal |
| 37 | + |
| 38 | + |
| 39 | +@for_each_kernel |
| 40 | +def concatenate_arrays( |
| 41 | + kernel: LoopKernel, |
| 42 | + array_names: Sequence[str], |
| 43 | + new_name: Optional[str] = None, |
| 44 | + axis_nr: int = 0) -> LoopKernel: |
| 45 | + """Merges arrays (temporaries or arguments) into one array along the axis |
| 46 | + given by *axis_nr*. |
| 47 | +
|
| 48 | + :arg array_names: a list of names of temporary variables. |
| 49 | +
|
| 50 | + :arg axis_nr: the (zero-based) index of the axis of the arrays to be merged. |
| 51 | +
|
| 52 | + :arg new_name: new name for the merged temporary. If not given, a new name |
| 53 | + is generated. |
| 54 | + """ |
| 55 | + assert isinstance(kernel, LoopKernel) |
| 56 | + |
| 57 | + var_name_gen = kernel.get_var_name_generator() |
| 58 | + new_name = new_name or var_name_gen("concatenated_array") |
| 59 | + new_aggregate = prim.Variable(new_name) |
| 60 | + |
| 61 | + arrays = [] |
| 62 | + for array_name in array_names: |
| 63 | + ary = kernel.get_var_descriptor(array_name) |
| 64 | + if ary.shape is None or ary.shape is auto: |
| 65 | + raise ValueError(f"Shape of temporary variable '{array_name}' is " |
| 66 | + "unknown. Cannot merge with unknown shapes") |
| 67 | + |
| 68 | + assert isinstance(ary.shape, tuple) |
| 69 | + shape = list(ary.shape) |
| 70 | + # make the shape value at axis_nr a constant so that we can |
| 71 | + # check that the rest of the attributes (except name) are equal. |
| 72 | + shape[axis_nr] = 1 |
| 73 | + arrays.append(ary.copy(shape=tuple(shape), name=new_name)) |
| 74 | + |
| 75 | + if not all_equal(arrays): |
| 76 | + raise ValueError("Arrays must be identical except for shape " |
| 77 | + "(except for shape) in order to concatenate.") |
| 78 | + |
| 79 | + offsets = {} |
| 80 | + axis_length = 0 |
| 81 | + for array_name in array_names: |
| 82 | + offsets[array_name] = axis_length |
| 83 | + ary = kernel.temporary_variables[array_name] |
| 84 | + assert isinstance(ary.shape, tuple) |
| 85 | + axis_length += ary.shape[axis_nr] |
| 86 | + |
| 87 | + new_ary = arrays[0] |
| 88 | + new_shape = list(new_ary.shape) |
| 89 | + new_shape[axis_nr] = axis_length |
| 90 | + new_ary = new_ary.copy(shape=tuple(new_shape)) |
| 91 | + |
| 92 | + # {{{ rewrite subscripts |
| 93 | + |
| 94 | + from loopy.transform.padding import SubscriptRewriter |
| 95 | + |
| 96 | + def modify_array_access(expr): |
| 97 | + idx = expr.index |
| 98 | + if not isinstance(idx, tuple): |
| 99 | + idx = (idx,) |
| 100 | + idx = list(idx) |
| 101 | + idx[axis_nr] += offsets[expr.aggregate.name] |
| 102 | + |
| 103 | + return new_aggregate.index(tuple(idx)) |
| 104 | + |
| 105 | + rule_mapping_context = SubstitutionRuleMappingContext( |
| 106 | + kernel.substitutions, var_name_gen) |
| 107 | + aash = SubscriptRewriter(rule_mapping_context, |
| 108 | + array_names, modify_array_access) |
| 109 | + kernel = rule_mapping_context.finish_kernel(aash.map_kernel(kernel)) |
| 110 | + |
| 111 | + # }}} |
| 112 | + |
| 113 | + if isinstance(new_ary, TemporaryVariable): |
| 114 | + new_tvs = {name: tv for name, tv in kernel.temporary_variables.items() |
| 115 | + if name not in array_names} |
| 116 | + new_tvs[new_name] = new_ary |
| 117 | + return kernel.copy(temporary_variables=new_tvs) |
| 118 | + elif isinstance(new_ary, ArrayArg): |
| 119 | + new_args: List[KernelArgument] = [] |
| 120 | + inserted = False |
| 121 | + for arg in kernel.args: |
| 122 | + if arg.name in array_names: |
| 123 | + if not inserted: |
| 124 | + new_args.append(new_ary) |
| 125 | + inserted = True |
| 126 | + else: |
| 127 | + new_args.append(arg) |
| 128 | + return kernel.copy(args=new_args) |
| 129 | + else: |
| 130 | + raise AssertionError() |
0 commit comments