|
| 1 | +using Test |
| 2 | +import KernelAbstractions |
| 3 | +import LocalMath |
| 4 | + |
| 5 | +struct OrderedFoldSourceOrderDomain end |
| 6 | +struct OrderedFoldSourceOrderEvaluator end |
| 7 | + |
| 8 | +struct OrderedFoldCanonicalKey end |
| 9 | +@inline (::OrderedFoldCanonicalKey)(value::Int32) = value |
| 10 | +struct OrderedFoldCanonicalIdentity end |
| 11 | +@inline (::OrderedFoldCanonicalIdentity)(value::Int32) = value |
| 12 | +struct AlternateOrderedFoldCanonicalKey end |
| 13 | +@inline (::AlternateOrderedFoldCanonicalKey)(value::Int32) = value |
| 14 | +struct AlternateOrderedFoldCanonicalIdentity end |
| 15 | +@inline (::AlternateOrderedFoldCanonicalIdentity)(value::Int32) = value |
| 16 | + |
| 17 | +@inline function (::OrderedFoldSourceOrderEvaluator)(item::Int32, reads, parameters) |
| 18 | + return (event = LocalMath.FoldValue( |
| 19 | + item, something(reads[1][1].value)),) |
| 20 | +end |
| 21 | + |
| 22 | +struct OrderedFoldSourceOrderTrace |
| 23 | + halt_at::Int32 |
| 24 | + invalid_at::Int32 |
| 25 | +end |
| 26 | + |
| 27 | +@inline function (transition::OrderedFoldSourceOrderTrace)( |
| 28 | + state, value, item, reads) |
| 29 | + destination = item == transition.invalid_at ? Int32(2) : Int32(1) |
| 30 | + next = state.result[Int32(1)] * Int32(10) + value |
| 31 | + return LocalMath.FoldStep((result = LocalMath.BoundedWrites( |
| 32 | + (destination,), (next,), Int32(1)),); |
| 33 | + halt = item == transition.halt_at) |
| 34 | +end |
| 35 | + |
| 36 | +function _ordered_fold_source_order_prepared( |
| 37 | + array_type, selected_values, emitted_values; |
| 38 | + selection::Symbol, halt_at::Int32 = Int32(0), |
| 39 | + invalid_at::Int32 = Int32(0), order = LocalMath.source_order()) |
| 40 | + n = length(selected_values) |
| 41 | + source = LocalMath.Space(OrderedFoldSourceOrderDomain, n) |
| 42 | + singleton = LocalMath.Space(OrderedFoldSourceOrderDomain, 1) |
| 43 | + selected = LocalMath.Field(source, Bool) |
| 44 | + emitted = LocalMath.Field(source, Bool) |
| 45 | + initial = LocalMath.Field(singleton, Int32) |
| 46 | + result = LocalMath.Field(singleton, Int32) |
| 47 | + identity_relation = LocalMath.IdentityRelation(source) |
| 48 | + control = selection === :mask ? LocalMath.Control(; mask = selected) : |
| 49 | + selection === :subset ? LocalMath.Control(; subset = |
| 50 | + LocalMath.MaskedRelation(identity_relation, selected)) : |
| 51 | + error("unknown source-order selection") |
| 52 | + state = LocalMath.initialized_state(; |
| 53 | + result = LocalMath.FoldComponent(result; from = initial)) |
| 54 | + publication = LocalMath.Publication((LocalMath.FoldPublication( |
| 55 | + LocalMath.PublicationValue(:event)),), LocalMath.OrderedFold( |
| 56 | + Int32, state, |
| 57 | + OrderedFoldSourceOrderTrace(halt_at, invalid_at); order)) |
| 58 | + stage = LocalMath.Stage( |
| 59 | + source, |
| 60 | + (emitted = LocalMath.Access(emitted, identity_relation; |
| 61 | + required = true),), |
| 62 | + (publication,), LocalMath.Evaluator(OrderedFoldSourceOrderEvaluator()), |
| 63 | + control, |
| 64 | + LocalMath.SourceOrigin( |
| 65 | + @__FILE__, @__LINE__; label = :source_order_direct_traversal)) |
| 66 | + selected_storage = array_type(selected_values) |
| 67 | + emitted_storage = array_type(emitted_values) |
| 68 | + destination = array_type(Int32[91]) |
| 69 | + prepared = LocalMath.prepare( |
| 70 | + LocalMath.LocalLaw(stage), |
| 71 | + selected => selected_storage, |
| 72 | + emitted => emitted_storage, |
| 73 | + initial => array_type(Int32[0]), result => destination; |
| 74 | + backend = KernelAbstractions.get_backend(destination)) |
| 75 | + return (; prepared, destination) |
| 76 | +end |
| 77 | + |
| 78 | +function _ordered_fold_source_order_execute(prepared) |
| 79 | + return try |
| 80 | + wait(LocalMath.execute!(prepared)) |
| 81 | + nothing |
| 82 | + catch error |
| 83 | + error |
| 84 | + end |
| 85 | +end |
| 86 | + |
| 87 | +function ordered_fold_source_order_contracts(array_type) |
| 88 | + return @testset "source-order direct traversal preserves sparse semantics" begin |
| 89 | + selected = Bool[false, true, true, false, true, false, true] |
| 90 | + emitted = Bool[true, true, false, true, true, true, true] |
| 91 | + for selection in (:mask, :subset) |
| 92 | + witness = _ordered_fold_source_order_prepared( |
| 93 | + array_type, selected, emitted; selection) |
| 94 | + @test _ordered_fold_source_order_execute(witness.prepared) === nothing |
| 95 | + @test Array(witness.destination) == Int32[257] |
| 96 | + facts = LocalMath.inspect(witness.prepared) |
| 97 | + phases = map(phase -> phase.kind, |
| 98 | + only(facts.stages).planning.phases) |
| 99 | + @test :ordered_fold_bitonic ∉ phases |
| 100 | + @test facts.planning.base_provider_launch_count == 6 |
| 101 | + |
| 102 | + halted = _ordered_fold_source_order_prepared( |
| 103 | + array_type, selected, emitted; selection, |
| 104 | + halt_at = Int32(5)) |
| 105 | + @test _ordered_fold_source_order_execute(halted.prepared) === nothing |
| 106 | + @test Array(halted.destination) == Int32[25] |
| 107 | + |
| 108 | + rejected = _ordered_fold_source_order_prepared( |
| 109 | + array_type, selected, emitted; selection, |
| 110 | + invalid_at = Int32(7)) |
| 111 | + failure = _ordered_fold_source_order_execute(rejected.prepared) |
| 112 | + @test failure isa LocalMath.LocalMathValidationError |
| 113 | + @test failure.contract === :runtime_ordered_fold_validation |
| 114 | + @test failure.actual.failure_class === :invalid_destination |
| 115 | + @test failure.actual.source_item == Int32(7) |
| 116 | + @test failure.actual.canonical_position == Int32(3) |
| 117 | + @test failure.actual.witness == Int32(2) |
| 118 | + @test Array(rejected.destination) == Int32[91] |
| 119 | + end |
| 120 | + |
| 121 | + empty = _ordered_fold_source_order_prepared( |
| 122 | + array_type, Bool[], Bool[]; selection = :mask) |
| 123 | + @test _ordered_fold_source_order_execute(empty.prepared) === nothing |
| 124 | + @test Array(empty.destination) == Int32[0] |
| 125 | + @test LocalMath.inspect(empty.prepared).planning.base_provider_launch_count == 6 |
| 126 | + |
| 127 | + canonical = _ordered_fold_source_order_prepared( |
| 128 | + array_type, trues(7), trues(7); selection = :mask, |
| 129 | + order = LocalMath.canonical_by( |
| 130 | + OrderedFoldCanonicalKey(), OrderedFoldCanonicalIdentity())) |
| 131 | + @test _ordered_fold_source_order_execute(canonical.prepared) === nothing |
| 132 | + @test Array(canonical.destination) == Int32[1234567] |
| 133 | + canonical_facts = LocalMath.inspect(canonical.prepared) |
| 134 | + canonical_phases = map(phase -> phase.kind, |
| 135 | + only(canonical_facts.stages).planning.phases) |
| 136 | + @test :ordered_fold_bitonic in canonical_phases |
| 137 | + @test canonical_facts.planning.base_provider_launch_count == 12 |
| 138 | + |
| 139 | + alternate_canonical = _ordered_fold_source_order_prepared( |
| 140 | + array_type, trues(7), trues(7); selection = :mask, |
| 141 | + order = LocalMath.canonical_by( |
| 142 | + AlternateOrderedFoldCanonicalKey(), |
| 143 | + AlternateOrderedFoldCanonicalIdentity())) |
| 144 | + @test _ordered_fold_source_order_execute( |
| 145 | + alternate_canonical.prepared) === nothing |
| 146 | + @test Array(alternate_canonical.destination) == Int32[1234567] |
| 147 | + alternate_facts = LocalMath.inspect(alternate_canonical.prepared) |
| 148 | + @test only(alternate_facts.stages).planning.phases == |
| 149 | + only(canonical_facts.stages).planning.phases |
| 150 | + @test alternate_facts.planning.base_provider_launch_count == |
| 151 | + canonical_facts.planning.base_provider_launch_count |
| 152 | + end |
| 153 | +end |
0 commit comments