|
| 1 | +using Dagger |
| 2 | +using Dagger.Distributed |
| 3 | + |
| 4 | +using DataGraphs: DataGraphs, get_edge_data, get_vertex_data, is_edge_assigned, |
| 5 | + is_vertex_assigned, set_edge_data!, set_vertex_data!, underlying_graph |
| 6 | +using Dictionaries: Indices |
| 7 | +using Graphs: AbstractEdge, AbstractGraph, dst, edges, src, vertices |
| 8 | +using ITensorNetworksNext.ITensorNetworksNextParallel: DaggerBeliefPropagationCache, |
| 9 | + DaggerNestedAlgorithm, DaggerState, ITensorNetworksNextParallel, dagger_algorithm, |
| 10 | + subcache |
| 11 | +using ITensorNetworksNext: BeliefPropagation, BeliefPropagationCache, |
| 12 | + BeliefPropagationProblem, BeliefPropagationState, ITensorNetworksNext, |
| 13 | + beliefpropagation, forest_cover_edge_sequence, select_algorithm |
| 14 | +using NamedGraphs.PartitionedGraphs: QuotientVertex, quotientedges, quotientvertices |
| 15 | +using NamedGraphs: NamedGraphs |
| 16 | +using NamedGraphs.GraphsExtensions: boundary_edges |
| 17 | + |
| 18 | +function ITensorNetworksNextParallel.subcache(cache::DaggerBeliefPropagationCache, inds) |
| 19 | + return subcache(cache.underlying_cache, inds) |
| 20 | +end |
| 21 | + |
| 22 | +function ITensorNetworksNextParallel.DaggerBeliefPropagationCache(network::AbstractGraph) |
| 23 | + underlying_cache = BeliefPropagationCache(network) |
| 24 | + |
| 25 | + keys = Indices(quotientvertices(underlying_cache)) |
| 26 | + |
| 27 | + workers = Iterators.cycle(Distributed.workers()) |
| 28 | + worker_dict = similar(keys, Int) |
| 29 | + |
| 30 | + for quotient_vertex in keys |
| 31 | + worker, workers = Iterators.peel(workers) |
| 32 | + worker_dict[quotient_vertex] = worker |
| 33 | + end |
| 34 | + |
| 35 | + quotient_chunks = map(keys) do quotient_vertex |
| 36 | + worker = worker_dict[quotient_vertex] |
| 37 | + iterate = subcache(underlying_cache, quotient_vertex) |
| 38 | + chunk = Dagger.@mutable worker = worker BeliefPropagationState(; iterate) |
| 39 | + return chunk |
| 40 | + end |
| 41 | + |
| 42 | + return DaggerBeliefPropagationCache(underlying_cache, quotient_chunks) |
| 43 | +end |
| 44 | + |
| 45 | +DataGraphs.underlying_graph(cache::DaggerBeliefPropagationCache) = underlying_graph(cache.underlying_cache) |
| 46 | + |
| 47 | +DataGraphs.is_vertex_assigned(bpc::DaggerBeliefPropagationCache, vertex) = is_vertex_assigned(bpc.underlying_cache, vertex) |
| 48 | +DataGraphs.is_edge_assigned(bpc::DaggerBeliefPropagationCache, edge) = is_edge_assigned(bpc.undelying_cache, edge) |
| 49 | + |
| 50 | +DataGraphs.get_vertex_data(bpc::DaggerBeliefPropagationCache, vertex) = get_vertex_data(bpc.underlying_cache, vertex) |
| 51 | +DataGraphs.get_edge_data(bpc::DaggerBeliefPropagationCache, edge::AbstractEdge) = get_edge_data(bpc.undelying_caches, edge) |
| 52 | + |
| 53 | +DataGraphs.set_vertex_data!(bpc::DaggerBeliefPropagationCache, val, vertex) = set_vertex_data!(bpc.underlying_cache, val, vertex) |
| 54 | +DataGraphs.set_edge_data!(bpc::DaggerBeliefPropagationCache, val, edge) = set_edge_data!(bpc.underlying_cache, val, edge) |
| 55 | + |
| 56 | +NamedGraphs.to_graph_index(::DaggerBeliefPropagationCache, qv::QuotientVertex) = qv |
| 57 | +function DataGraphs.get_index_data(cache::DaggerBeliefPropagationCache, qv::QuotientVertex) |
| 58 | + return cache.quotient_chunks[qv] |
| 59 | +end |
| 60 | + |
| 61 | +function ITensorNetworksNext.beliefpropagation_sweep(cache::DaggerBeliefPropagationCache; edges, workers = workers(), kwargs...) |
| 62 | + |
| 63 | + keys = collect(quotientvertices(cache)) |
| 64 | + |
| 65 | + return dagger_algorithm(keys; keys, workers) do quotient_vertex |
| 66 | + |
| 67 | + subcache = fetch(cache[quotient_vertex]).iterate |
| 68 | + |
| 69 | + subcache_edges = forest_cover_edge_sequence(subcache) ∩ edges |
| 70 | + incoming_edges = boundary_edges(cache, vertices(cache, quotient_vertex); dir = :in) |
| 71 | + |
| 72 | + alg = select_algorithm( |
| 73 | + beliefpropagation, |
| 74 | + subcache; |
| 75 | + # Don't update the incoming messages |
| 76 | + edges = setdiff(subcache_edges, incoming_edges), |
| 77 | + maxiter = 1, |
| 78 | + kwargs... |
| 79 | + ) |
| 80 | + |
| 81 | + return alg |
| 82 | + end |
| 83 | +end |
| 84 | + |
| 85 | +function AI.initialize_state( |
| 86 | + problem::AIE.Problem, |
| 87 | + algorithm::BeliefPropagation{<:DaggerNestedAlgorithm}; |
| 88 | + kwargs... |
| 89 | + ) |
| 90 | + return initialize_dagger_state(problem, algorithm; kwargs...) |
| 91 | +end |
| 92 | + |
| 93 | +function AIE.get_subproblem( |
| 94 | + problem::BeliefPropagationProblem, |
| 95 | + algorithm::DaggerNestedAlgorithm, |
| 96 | + state::DaggerState, |
| 97 | + ) |
| 98 | + subproblem = problem |
| 99 | + subalgorithm = algorithm.algorithms[state.iteration] |
| 100 | + |
| 101 | + quotient_vertex = algorithm.keys[state.iteration] |
| 102 | + |
| 103 | + cache = state.iterate.iterate |
| 104 | + |
| 105 | + subiterate = cache[quotient_vertex] |
| 106 | + |
| 107 | + return subproblem, subalgorithm, subiterate |
| 108 | +end |
| 109 | + |
| 110 | +function AIE.set_substate!( |
| 111 | + ::BeliefPropagationProblem, |
| 112 | + algorithm::AIE.NestedAlgorithm, |
| 113 | + state::AIE.State, |
| 114 | + substate::DaggerState, |
| 115 | + ) |
| 116 | + |
| 117 | + dst_cache = state.iterate.iterate |
| 118 | + |
| 119 | + state.iterate.maxdiff = 0.0 |
| 120 | + |
| 121 | + current_algorithm = algorithm.algorithms[state.iteration] |
| 122 | + |
| 123 | + for (i, quotient_vertex) in enumerate(current_algorithm.keys) |
| 124 | + get_maxdiff = dtask -> dtask.iterate.maxdiff |
| 125 | + src_maxdiff = fetch(Dagger.@spawn get_maxdiff(substate.remote_results[i])) |
| 126 | + |
| 127 | + if src_maxdiff > state.iterate.maxdiff |
| 128 | + state.iterate.maxdiff = src_maxdiff |
| 129 | + end |
| 130 | + end |
| 131 | + |
| 132 | + |
| 133 | + transfer_edges! = (dst_chunk, src_chunk, edges) -> begin |
| 134 | + src_subcache = src_chunk.iterate |
| 135 | + dst_subcache = dst_chunk.iterate |
| 136 | + for edge in edges |
| 137 | + dst_subcache[edge] = src_subcache[edge] |
| 138 | + end |
| 139 | + end |
| 140 | + |
| 141 | + transfer_dtasks = map(quotientedges(dst_cache)) do quotient_edge |
| 142 | + src_subcache = dst_cache[src(quotient_edge)] |
| 143 | + dst_subcache = dst_cache[dst(quotient_edge)] |
| 144 | + return Dagger.@spawn transfer_edges!(dst_subcache, fetch(src_subcache), edges(dst_cache, quotient_edge)) |
| 145 | + end |
| 146 | + |
| 147 | + wait.(transfer_dtasks) |
| 148 | + |
| 149 | + return state |
| 150 | +end |
0 commit comments