text:
module ParTest

using Comm, Foldable, Functor, FunRefs, Funs, Monad, Par
using Base.Test
using Combinatorics

function inc(i::Integer)
    i+1
end

function local_fun_untyped()
    # par
    r1 = par() do
        inc(1)
    end
    @test isa(r1, FunRef{Any})
    @test islocal(r1)
    @test r1[] == 2

    # rpar
    r3 = rpar(mod1(2, Comm.nprocs())) do
        inc(3)
    end
    @test isa(r3, FunRef{Any})
    @test islocal(r3)
    @test r3[] == 4

    # rcall
    v4 = rcall(mod1(2, Comm.nprocs())) do
        inc(4)
    end
    @test v4 == 5

    # remote
    r6 = remote(mod1(2, Comm.nprocs())) do
        inc(6)
    end
    @test isa(r6, FunRef{Any})
    @test (Comm.nprocs()==1) == islocal(r6)

    # get
    v6 = rcall(proc(r6)) do
        r6[]
    end
    @test v6 == 7

    # make_local
    r7 = make_local(r6)
    @test isa(r7, FunRef{Any})
    @test islocal(r7)
    @test r7[] == 7

    # get_local
    v7 = get_local(r7)
    @test v7 == 7

    # unwrap
    rr = rpar(mod1(2, Comm.nprocs())) do
        identity(r6)
    end
    @test isa(rr, FunRef{Any})
    r = unwrap(rr)
    @test isa(r, FunRef{Any})
    crr = rcall(proc(rr)) do
        r = rr[]
        rcall(proc(r)) do
            r[] == 7
        end
    end
    @test crr
    cr = rcall(proc(r)) do
        r[] == 7
    end
    @test cr
end

function local_fun_typed()
    # par
    r1 = par(R=Int) do
        inc(1)
    end
    @test isa(r1, FunRef{Int})
    @test islocal(r1)
    @test r1[] == 2

    # rpar
    r3 = rpar(R=Int, mod1(2, Comm.nprocs())) do
        inc(3)
    end
    @test isa(r3, FunRef{Int})
    @test islocal(r3)
    @test r3[] == 4

    # rcall
    v4 = rcall(R=Int, mod1(2, Comm.nprocs())) do
        inc(4)
    end
    @test v4 == 5

    # remote
    r6 = remote(R=Int, mod1(2, Comm.nprocs())) do
        inc(6)
    end
    @test isa(r6, FunRef{Int})
    @test (Comm.nprocs()==1) == islocal(r6)

    # get
    v6 = rcall(R=Int, proc(r6)) do
        r6[]
    end
    @test v6 == 7

    # make_local
    r7 = make_local(r6)
    @test isa(r7, FunRef{Int})
    @test islocal(r7)
    @test r7[] == 7

    # get_local
    v7 = get_local(r7)
    @test v7 == 7

    # unwrap
    rr = rpar(R=FunRef{Int}, mod1(3, Comm.nprocs())) do
        identity(r6)
    end
    @test isa(rr, FunRef{FunRef{Int}})
    r = unwrap(rr)
    @test isa(r, FunRef{Int})
    crr = rcall(R=Bool, proc(rr)) do
        r = rr[]
        rcall(R=Bool, proc(r)) do
            r[] == 7
        end
    end
    @test crr
    cr = rcall(R=Bool, proc(r)) do
        r[] == 7
    end
    @test cr
end

#function local_mac_untyped()
#    # par
#    r1 = @par inc(1)
#    @test isa(r1, FunRef{Any})
#    @test islocal(r1)
#    @test r1[] == 2
#
#    # rpar
#    r3 = @rpar mod1(2, Comm.nprocs()) inc(3)
#    @test isa(r3, FunRef{Any})
#    @test islocal(r3)
#    @test r3[] == 4
#
#    # rcall
#    v4 = @rcall mod1(2, Comm.nprocs()) inc(4)
#    @test v4 == 5
#
#    # remote
#    r6 = @remote mod1(2, Comm.nprocs()) inc(6)
#    @test isa(r6, FunRef{Any})
#    @test (Comm.nprocs()==1) == islocal(r6)
#
#    # get_cmp
#    v6 = @rcall proc(r6) r6[]
#    @test v6 == 7
#
#    # make_local
#    r7 = make_local(r6)
#    @test isa(r7, FunRef{Any})
#    @test islocal(r7)
#    @test r7[] == 7
#
#    # get_local
#    v7 = get_local(r7)
#    @test v7 == 7
#
#    # unwrap
#    rr = @rpar mod1(3, Comm.nprocs()) r6
#    @test isa(rr, FunRef{Any})
#    r = unwrap(rr)
#    @test isa(r, FunRef{Any})
#    vrr = rr[]
#    vvrr = @rcall proc(r6) vrr[]
#    @test vvrr == 7
#    vr = @rcall proc(r) r[]
#    @test vr == 7
#end

#function local_mac_typed()
#    # par
#    r1 = @par Int inc(1)
#    @test isa(r1, FunRef{Int})
#    @test islocal(r1)
#    @test r1[] == 2
#
#    # rpar
#    r3 = @rpar Int mod1(2, Comm.nprocs()) inc(3)
#    @test isa(r3, FunRef{Int})
#    @test islocal(r3)
#    @test r3[] == 4
#
#    # rcall
#    v4 = @rcall Int mod1(2, Comm.nprocs()) inc(4)
#    @test v4 == 5
#
#    # remote
#    r6 = @remote Int mod1(2, Comm.nprocs()) inc(6)
#    @test isa(r6, FunRef{Int})
#    @test (Comm.nprocs()==1) == islocal(r6)
#
#    # get_cmp
#    v6 = @rcall Int proc(r6) r6[]
#    @test v6 == 7
#
#    # make_local
#    r7 = make_local(r6)
#    @test isa(r7, FunRef{Int})
#    @test islocal(r7)
#    @test r7[] == 7
#
#    # get_local
#    v7 = get_local(r7)
#    @test v7 == 7
#
#    # unwrap
#    rr = @rpar FunRef{Int} mod1(3, Comm.nprocs()) r6
#    @test isa(rr, FunRef{FunRef{Int}})
#    r = unwrap(rr)
#    @test isa(r, FunRef{Int})
#    vrr = rr[]
#    vvrr = @rcall Int proc(r6) vrr[]
#    @test vvrr == 7
#    vr = @rcall Int proc(r) r[]
#    @test vr == 7
#end

function local_unwrap_untyped()
    p1 = mod1(2, Comm.nprocs())
    p2 = mod1(3, Comm.nprocs())
    # Try creating and readying the refs in all possible orders. These are all
    # permutations of the five steps, with the constraint that :creatN must
    # occur before :readyN, and :unwrap after :creat2.
    allsteps = [:creat1, :creat2, :ready1, :ready2, :unwrap]
    for steps in permutations(allsteps)
        function isbefore(a::Symbol, b::Symbol)
            findin(steps, [a])[1] < findin(steps, [b])[1]
        end
        if !isbefore(:creat1, :ready1) continue end
        if !isbefore(:creat2, :ready2) continue end
        if !isbefore(:creat1, :creat2) continue end
        if !isbefore(:creat2, :unwrap) continue end
        rcall(p1) do
            global cond1=Condition()
        end
        rcall(p2) do
            global cond2=Condition()
        end
        local r1, r2, ru
        r1ready = r2ready = ruready = false
        haver1 = haver2 = haveru = false
        for step in steps
            if step == :creat1
                r1 = remote(p1) do
                    global cond1; wait(cond1); 8
                end
                @test typeof(r1) == FunRef{Any}
                haver1 = true
            elseif step == :creat2
                r2 = remote(p2) do
                    global cond2; wait(cond2); r1
                end
                @test typeof(r2) == FunRef{Any}
                haver2 = true
            elseif step == :ready1
                rcall(p1) do
                    global cond1; notify(cond1)
                end
                r1ready = true
                wait(r1)
            elseif step == :ready2
                rcall(p2) do
                    global cond2; notify(cond2)
                end
                r2ready = true
                wait(r2)
            elseif step == :unwrap
                ru = unwrap(r2)
                @test typeof(ru) == FunRef{Any}
                haveru = true
            else
                @assert false
            end
            if haveru
                ruready = r1ready && r2ready
            end
            if ruready
                wait(ru)
            end
            sleep(0.1)
            if haver1
                @test isready(r1) == r1ready
            end
            if haver2
                @test isready(r2) == r2ready
            end
            if haveru
                @test isready(ru) == ruready
            end
            if r1ready
                @test get_local(r1) == 8
            end
            if r1ready && r2ready
                @test get_local(get_local(r2)) == 8
            end
            if ruready
                @test get_local(ru) == 8
            end
        end
        @assert r1ready && r2ready && ruready
    end
end

function local_unwrap_typed()
    p1 = mod1(2, Comm.nprocs())
    p2 = mod1(3, Comm.nprocs())
    # Try creating and readying the refs in all possible orders. These are all
    # permutations of the five steps, with the constraint that :creatN must
    # occur before :readyN, and :unwrap after :creat2.
    allsteps = [:creat1, :creat2, :ready1, :ready2, :unwrap]
    for steps in permutations(allsteps)
        function isbefore(a::Symbol, b::Symbol)
            findin(steps, [a])[1] < findin(steps, [b])[1]
        end
        if !isbefore(:creat1, :ready1) continue end
        if !isbefore(:creat2, :ready2) continue end
        if !isbefore(:creat1, :creat2) continue end
        if !isbefore(:creat2, :unwrap) continue end
        rcall(p1) do
            global cond1=Condition()
        end
        rcall(p2) do
            global cond2=Condition()
        end
        local r1, r2, ru
        r1ready = r2ready = ruready = false
        haver1 = haver2 = haveru = false
        for step in steps
            if step == :creat1
                r1 = remote(R=Int, p1) do
                    global cond1; wait(cond1); 8
                end
                @test typeof(r1) == FunRef{Int}
                haver1 = true
            elseif step == :creat2
                r2 = remote(R=FunRef{Int}, p2) do
                    global cond2; wait(cond2); r1
                end
                @test typeof(r2) == FunRef{FunRef{Int}}
                haver2 = true
            elseif step == :ready1
                rcall(p1) do
                    global cond1; notify(cond1)
                end
                r1ready = true
                wait(r1)
            elseif step == :ready2
                rcall(p2) do
                    global cond2; notify(cond2)
                end
                r2ready = true
                wait(r2)
            elseif step == :unwrap
                ru = unwrap(r2)
                @test typeof(ru) == FunRef{Int}
                haveru = true
            else
                @assert false
            end
            if haveru
                ruready = r1ready && r2ready
            end
            if ruready
                wait(ru)
            end
            sleep(0.1)
            if haver1
                @test isready(r1) == r1ready
            end
            if haver2
                @test isready(r2) == r2ready
            end
            if haveru
                @test isready(ru) == ruready
            end
            if r1ready
                @test get_local(r1) == 8
            end
            if r1ready && r2ready
                @test get_local(get_local(r2)) == 8
            end
            if ruready
                @test get_local(ru) == 8
            end
        end
        @assert r1ready && r2ready && ruready
    end
end

function test_foldable()
    r1 = FunRef(2)
    r2 = FunRef(3)
    @test freduce(+, 0, r1) == 2
    @test freduce(+, 0, r1, r2) == 5

    r3 = remote(()->4, mod1(2,Comm.nprocs()))
    @test freduce(+, 0, r3) == 4
    @test freduce(+, 0, r3, r1) == 6
end

function test_functor()
    r1 = FunRef(2)
    r2 = FunRef(3)
    r3 = remote(()->4, R=Int, mod1(2,Comm.nprocs()))
    @test proc(r3) == mod1(2,Comm.nprocs())
    fr1 = fmap(R=Int, x->2x, r1)
    fr2 = fmap(R=Int, (x,y)->2x+y, r1, r2)
    fr3 = fmap(R=Int, (x,y)->2x+y, r1, r3)
    fr4 = fmap(R=Int, (x,y)->2x+y, r3, r1)
    @test islocal(fr1)
    @test islocal(fr2)
    @test islocal(fr3)
    @test proc(fr4) == mod1(2,Comm.nprocs())
    @test fr1[] == 4
    @test fr2[] == 7
    @test fr3[] == 8
    @test get_local(fr4) == 10
end

function test_monad()
    u = munit(FunRef{Int}, 42)
    @test islocal(u)
    @test u[] == 42

    uu = munit(FunRef{FunRef{Int}}, u)
    @test islocal(uu)
    @test uu[][] == 42

    j = mjoin(uu)
    @test j[] == 42

    b = mbind(x::Int->FunRef(x+1.0), FunRef(1), R=FunRef{Float64})
    @test b[] == 2.0

    b = mbind(Fun{FunRef{Float64}}(x->FunRef(x+1.0)), FunRef(1))
    @test b[] == 2.0
end

function main()
    local_fun_untyped()
    local_fun_typed()
    #local_mac_untyped()
    #local_mac_typed()
    local_unwrap_untyped()
    local_unwrap_typed()
    test_foldable()
    test_functor()
    test_monad()
end

end


text:
using SegmentationModels
using Test

@testset "SegmentationModels.jl" begin
    # Write your tests here.
end

ext: jl


text:
@testset "TotalDegree" begin
    @test HC.bezout_number([2 1; 2 1; 1 1], [2, 1]) == 8
    @test HC.bezout_number([2 1 0; 1 1 1; 0 1 1], [1, 1, 1]) == 5
end


text:
function fastModularExponentiation(m, e, n::Integer)
    n > 0  || error("n must be positive")
    e >= 0 || error("e must be non-negative")
    r = 1
    while e > 0
        if (e & 1) > 0
            r = (r*m)%n
        end
        e >>= 1
        m = (m^2)%n
    end
    return r>0 ? r : r+n
end

max_stars_repo_path: Utility Functions/Algorithms/FME.jl

max_issues_repo_path: Utility Functions/Algorithms/FME.jl

max_forks_repo_path: Utility Functions/Algorithms/FME.jl


text:
using Cxx
const path_to_lib = dirname(@__FILE__())
addHeaderDir(path_to_lib, kind=C_System)

# Compiling lastools.so is left as an exercise to the reader
Libdl.dlopen(path_to_lib * "/lastools.so", Libdl.RTLD_GLOBAL)
cxxinclude("~/git/LAStools/LASlib/inc/lasreader_txt.hpp")  # random header

# Read .laz file
a = @cxxnew LASreadOpener()
@cxx a -> set_file_name(pointer("~/01608_4.laz"))
reader = @cxx a -> open()
p = @cxx reader -> read_point()
p = @cxx reader -> point
x = @cxx p -> get_X()
y = @cxx p -> get_Y()
z = @cxx p -> get_Z()

# Read .lax file
cxxinclude("~/git/LAStools/LASzip/src/lasindex.hpp")
i = @cxxnew LASindex()
@cxx i -> read(pointer("~/01608_4.lax"))
si = @cxx i -> get_spatial()


text:
using CompScienceMeshes
using BEAST
using StaticArrays
using LinearAlgebra
using IterativeSolvers

c = 3e8
μ = 4*π*1e-7
ε = 1/(μ*c^2)
f = 1e8
λ = c/f
k = 2*π/λ
ω = k*c
η = sqrt(μ/ε)

a = 1
Γ_orig = CompScienceMeshes.meshcuboid(a,a,a,0.05)
Γ = translate(Γ_orig,SVector(-a/2,-a/2,-a/2))

Φ, Θ = [0.0], range(0,stop=π,length=100)
pts = [point(cos(ϕ)*sin(θ), sin(ϕ)*sin(θ), cos(θ)) for ϕ in Φ for θ in Θ]

# This is an electric dipole
# The pre-factor (1/ε) is used to resemble
# (9.18) in Jackson's Classical Electrodynamics
E = (1/ε) * dipolemw3d(location=SVector(0.4,0.2,0),
                    orientation=1e-9.*SVector(0.5,0.5,0),
                    wavenumber=k)

n = BEAST.NormalVector()

𝒆 = (n × E) × n
H = (-1/(im*μ*ω))*curl(E)
𝒉 = (n × H) × n

𝓣 = Maxwell3D.singlelayer(wavenumber=k)
𝓝 = BEAST.NCross()
𝓚 = Maxwell3D.doublelayer(wavenumber=k)

X = raviartthomas(Γ)

println("Number of RWG functions: ", numfunctions(X))

T = hassemble(
    𝓣,
    X,
    X,
    treeoptions = BoxTreeOptions(nmin=100),
    threading=:multi,
    quadstrat=BEAST.DoubleNumQStrat(1,1),
    verbose=true,
    svdrecompress=true
)

e = assemble(𝒆,X)
##
println("Enter iterative solver")
@time j_EFIE, ch = IterativeSolvers.gmres(T, e, log=true, reltol=1e-4, maxiter=500)
println("Finished iterative solver part. Number of iterations: ", ch.iters)

nf_E_EFIE = potential(MWSingleLayerField3D(wavenumber=k), pts, j_EFIE, X)
nf_H_EFIE = potential(BEAST.MWDoubleLayerField3D(wavenumber=k), pts, j_EFIE, X) ./ η
ff_E_EFIE = potential(MWFarField3D(wavenumber=k), pts, j_EFIE, X)

@test norm(nf_E_EFIE - E.(pts))/norm(E.(pts)) ≈ 0 atol=0.01
@test norm(nf_H_EFIE - H.(pts))/norm(H.(pts)) ≈ 0 atol=0.01
@test norm(ff_E_EFIE - E.(pts, isfarfield=true))/norm(E.(pts, isfarfield=true)) ≈ 0 atol=0.01

max_stars_repo_name: sbadrian/FastBEAST

max_issues_repo_name: sbadrian/FastBEAST

max_forks_repo_name: sbadrian/FastBEAST


text:
# Credential() = Credential(Base.SecretBuffer())

# function set_credential!(cred::Credential,
#                          value::AbstractString)::Nothing
#     Base.shred!(cred.secret_buffer)
#     seekstart(cred.secret_buffer)
#     write(cred.secret_buffer, value)
#     return nothing
# end

function Base.shred!(anonymous_auth::AnonymousAuth)::Nothing
    return nothing
end


text:
"""
        greedy_star1_coloring

Find a coloring of a given input graph such that
no two vertices connected by an edge have the same
color using greedy approach. The number of colors
used may be equal or greater than the chromatic
number `χ(G)` of the graph.

A star coloring is a special type of distance - 1  coloring,
For a coloring to be called a star coloring, it must satisfy
two conditions:

1. every pair of adjacent vertices receives distinct  colors
(a distance-1 coloring)

2. For any vertex v, any color that leads to a two-colored path
involving v and three other vertices  is  impermissible  for  v.
In other words, every path on four vertices uses at least three
colors.

Reference: Gebremedhin AH, Manne F, Pothen A. **What color is your Jacobian? Graph coloring for computing derivatives.** SIAM review. 2005;47(4):629-705.
"""
function color_graph(g::LightGraphs.AbstractGraph, ::GreedyStar1Color)
    v = nv(g)
    colorvec = zeros(Int, v)

    forbidden_colors = zeros(Int, v+1)

    for vertex_i = vertices(g)

        for w in inneighbors(g, vertex_i)
            if colorvec[w] != 0
                forbidden_colors[colorvec[w]] = vertex_i
            end

            for x in inneighbors(g, w)
                if colorvec[x] != 0
                    if colorvec[w] == 0
                        forbidden_colors[colorvec[x]] = vertex_i
                    else
                        for y in inneighbors(g, x)
                           if colorvec[y] != 0
                                if y != w && colorvec[y] == colorvec[w]
                                    forbidden_colors[colorvec[x]] = vertex_i
                                    break
                                end
                            end
                        end
                    end
                end
            end
        end

        colorvec[vertex_i] = find_min_color(forbidden_colors, vertex_i)
    end

    colorvec
end

function find_min_color(forbidden_colors::AbstractVector, vertex_i::Integer)
    c = 1
    while (forbidden_colors[c] == vertex_i)
        c+=1
    end
    c
end


text:
module MagicSquares

export MagicSquare, ismagical

immutable MagicSquare{T<:Integer}
    n::Integer
    data::Array{T}

    MagicSquare(data::Array{T}) = begin
        n = convert(Int, √length(data))
        new(n, data)
    end
end

MagicSquare{T <: Integer}(data::Array{T}) = MagicSquare{T}(data)
MagicSquare{T <: Integer}(n::T) = MagicSquare{T}(zeros(T, n*n))

Base.shuffle!(m::MagicSquare) = shuffle!(m.data)
Base.show(io::IO, m::MagicSquare) = begin
    if(ismagical(m))
        println("Magic square with sum=$(sum(m.data[1:m.n]))")
    else
        println("Non-magic square.")
    arr = reshape(m.data, m.n, m.n)
    for i ∈ 1:m.n
        println(io, arr[i, :])
    end
end

function ismagical(m::MagicSquare)
    n = m.n
    s = sum(m.data[1:n])
    data1 = reshape(m.data, n, n)
    data2 = data1'
    diagsum1 = s
    diagsum2 = s
    for i ∈ 1:n
        if(s != sum(data1[i, :]) || s != sum(data2[i, :]))
            return false
        end
        diagsum1 -= data1[i, i]
        diagsum2 -= data1[i, n-i+1]
    end
    if diagsum1 == 0 && diagsum2 == 0
        return true
    end
    return false
end

end # module


text:
struct chain
    x :: Vector{Vector{<:Real}}
    diagnostics :: chain_diagnostics
end

function chain(x0::Vector{<:Real}, num_samples :: Int)
    x = Vector{Vector}(undef, num_samples); x[1] = x0
    diagnostics = chain_diagnostics(num_samples)
    return chain(x, diagnostics)
end

function mcmc_step(current_state::state, log_likelihood :: Function, proposal_distribution :: Function)
    x1 = proposal_distribution(current_state.x)
    new_likelihood = log_likelihood(x1)
    alpha = -new_likelihood + current_state.log_likelihood[1]
    u = log(rand())
    if u <= alpha
        current_state.x[:] .= x1
        current_state.log_likelihood[1] = new_likelihood
        return 1
    else
        return 0
    end
end

function mcmc(x0 :: Vector{<:Real}, log_likelihood :: Function, proposal_distribution :: Function, num_burnin :: Int, num_samples :: Int)

    # Initialize
    current_state = state(x0, log_likelihood(x0))

    # Burnin
    burnin_chain = chain(x0, num_burnin)
    burnin_chain.diagnostics.log_likelihood[1] = log_likelihood(x0)
    for iteration = 1:num_burnin-1
        flag = mcmc_step(current_state, log_likelihood, proposal_distribution)
        burnin_chain.x[iteration+1] = copy(current_state.x)
        burnin_chain.diagnostics.log_likelihood[iteration+1] = current_state.log_likelihood[1]
        burnin_chain.diagnostics.acceptance_ratio[iteration+1] = ( burnin_chain.diagnostics.acceptance_ratio[iteration] * (iteration - 1) + flag ) / iteration
    end

    # Sampling
    sampling_chain = chain(current_state.x, num_samples)
    sampling_chain.diagnostics.log_likelihood[1] = log_likelihood(x0)

    for iteration = 1:num_samples-1
        flag = mcmc_step(current_state, log_likelihood, proposal_distribution)
        sampling_chain.x[iteration+1] = copy(current_state.x)
        sampling_chain.diagnostics.log_likelihood[iteration+1] = current_state.log_likelihood[1]
        sampling_chain.diagnostics.acceptance_ratio[iteration+1] = ( sampling_chain.diagnostics.acceptance_ratio[iteration] * (iteration - 1) + flag ) / iteration
    end

    return burnin_chain, sampling_chain
end


text:
module Basic

using Flux
using Requires
using Requires: @init

using ..Transformers: Abstract3DTensor, Container, epsilon, batchedmul, scatter_add!, batched_tril!

export CompositeEmbedding, TransformerModel, set_classifier, clear_classifier
export PositionEmbedding, Embed, getmask,
    Vocabulary, gather, encode, decode
export OneHotArray, indices2onehot, onehot2indices,
    onehotarray, onehot, tofloat
export Transformer, TransformerDecoder

export @toNd, Positionwise

export logkldivergence, logcrossentropy

include("./extend3d.jl")
include("./embeds/Embeds.jl")
include("./mh_atten.jl")
include("./transformer.jl")
include("./loss.jl")
include("./model.jl")

end


text:
"""CW complex type"""
mutable struct CWComplex <: AbstractComplex
    cells::Dict{Int,Vector{Cell}}   # cells per dimension
    order::Dict{UInt,Tuple{Int,Int}}     # cell index, id => (dim, pos)
end
show(io::IO, cplx::CWComplex) = print(io, "CWComplex($(size(cplx)))")

(::Type{CWComplex})() = CWComplex(Dict{Int,Vector{Cell}}(), Dict{UInt,Tuple{Int,Int}}())
# CWComplex() = CWComplex{Cell{Int}}()
function CWComplex(cells::Cell...)
    cplx = CWComplex()
    for c in cells
        push!(cplx, c)
    end
    return cplx
end

#
# AbstractComplex Interface
#
eltype(cplx::CWComplex) = Cell

function cells(cplx::CWComplex)
    length(cplx.cells) == 0 && return Vector{Cell}[] # no cell in complex
    dims = maximum(keys(cplx.cells))
    return [haskey(cplx.cells, d) ? cplx.cells[d] : Cell[] for d in 0:dims]
end

cells(cplx::CWComplex, d::Int) = get(cplx.cells, d,  Cell[])

function faces(cplx::CWComplex, ci::IX) where {IX<:Integer}
    d, p = cplx.order[ci]
    d == 0 && return IX[]
    return map(hash, faces(cplx.cells[d][p]))
end

function cofaces(cplx::CWComplex, ci::IX) where {IX<:Integer}
    ret = IX[]
    d, p = cplx.order[ci]
    d+=1
    d > dim(cplx) && return ret
    for cid in map(hash, cplx.cells[d])
        ci ∈ faces(cplx, cid) && push!(ret, cid)
    end
    return ret
end

"""
    push!(complex, cell)

Attach a new `cell` to to a `complex`.
"""
function push!(cplx::CWComplex, c::Cell; recursive=false)
    cdim = dim(c)
    !haskey(cplx.cells, cdim) && setindex!(cplx.cells, Cell[], cdim)
    push!(cplx.cells[cdim], c)
    cplx.order[hash(c)] = (cdim, length(cplx.cells[cdim]))
    return [c]
end


text:
export weight_tuple

"""
    trainable_first(net)

Returns the first array in trainable(net), by recursively applying first to it.
"""
trainable_first(net) = trainable_first(first(trainable(net)))
trainable_first(x::AbstractArray)   = x

"""
    trainable_length

"""
trainable_length(net::NeuralNetwork) = _tlen(net)
function _tlen(t)
    i = 0
    for el=trainable(t)
        i+=_tlen(el)
    end
    return i
end
_tlen(t::AbstractArray) = length(t)

"""
    weight_tuple(net)

Returns a named tuple holding all the fields in the network, and an
extra field named `tuple_all_weights` who has the same type
"""
function weight_tuple(obj, vec=similar(trainable_first(obj), 0),
                      start=1)
    x = trainable(obj)
    i = 0
    if x isa Tuple
        d=Vector()
        for f=propertynames(x)
            di, val = weight_tuple(getproperty(x,f), vec, start+i)
            i += di; push!(d, val)
        end
        i, Tuple(d)
    else
        d=Dict{Symbol, Any}()
        for f=propertynames(x)
            di, val = weight_tuple(getproperty(x,f), vec, start+i)
            i += di; push!(d, f=>val)
        end
        i, (;d...)
    end
end

function weight_tuple(obj::Tuple, vec::AbstractVector, start)
    x = trainable(obj)
    i = 0
    d=Vector()
    for f=propertynames(x)
        di, val = weight_tuple(getfield(x,f), vec, start+i)
        i += di; push!(d, val)
    end
    i, Tuple(d)
end

function weight_tuple(x::AbstractArray{<:Number}, vec::AbstractVector, start)
    length(vec) < start+length(x)-1 && resize!(vec, start+length(x)-1)
    @views data_vec = vec[start:start+length(x)-1]
    if size(x) == size(data_vec)
        reshpd_params = data_vec
    else
        reshpd_params = reshape(data_vec, size(x))
    end
    return length(x), reshpd_params
end

## For Batches
function batched_weight_tuple(grad_tup, bsz=1)
    @info "useless"
    all_weights = grad_tup.tuple_all_weights
    all_weghts_new = [similar(w, length(w), bsz) for w=all_weights][1]
    return batched_weight_tuple(grad_tup, all_weghts_new)[2]
end

function batched_weight_tuple(obj, vec::AbstractMatrix, start=1)
    x = trainable(obj)
    i = 0
    if x isa Tuple
        d=Vector()
        for f=propertynames(x)
            f==:tuple_all_weights && continue
            di, val = batched_weight_tuple(getfield(x,f), vec, start+i)
            i += di; push!(d, val)
        end
        i, Tuple(d)
    else
        d=Dict{Symbol, Any}()
        for f=propertynames(x)
            f==:tuple_all_weights && continue
            di, val = batched_weight_tuple(getfield(x,f), vec, start+i)
            i += di; push!(d, f=>val)
        end
        i, (;d...)
    end
end

function batched_weight_tuple(x::AbstractArray{<:Number}, vec::AbstractMatrix, start)
    @assert length(vec) >= start+length(x)-1
    bsz = size(vec, 2)

    @views data_vec = vec[start:start+length(x)-1, :]
    if size(data_vec) != (size(x)..., bsz)
        reshpd_params = reshape(data_vec, size(x)..., bsz)
        if x isa Array
            reshpd_params = StridedView(reshpd_params)
        end
    else
        reshpd_params = data_vec
    end
    return length(x), reshpd_params
end # ? stridedView?

"""
    apply_recurse!(out::tuple, orig::Zygote.Grads, weights, op=identity)

recurses into all fields of `out`, which is assumed to have the structure
generated by `trainable(weights)`. When it hits abstractarrays, it copies
the corresponding field `orig[weights[key]]` to `out[key]`, by applying the
elementwise unary operation `op`.

This convoluted way is because zygote returns gradient results that do not respect
the trainable interface, and we need to reconstruct it.
"""
function apply_recurse!(out, orig, weights, op=identity)
    weights = trainable(weights)
    for key=keys(out)
        apply_recurse!(out[key], orig, weights[key], op)
    end
    return out
end

apply_recurse!(out::AbstractArray, orig, weight::AbstractArray, op) =
    op(copyto!(out, orig[weight]))

"""
    accum_recurse!(out::tuple, orig::Zygote.Grads, weights, op=identity; init=true)

Similar to `apply_recurse!`, but accumulates and only sets the out to 0 if
`init==true`.
"""
function accum_recurse!(out, orig, weights, op=identity; init=true)
    weights = trainable(weights)
    for key=keys(out)
        accum_recurse!(out[key], orig, weights[key], op; init=init)
    end
    return out
end

function accum_recurse!(out::AbstractArray, orig, weight::AbstractArray, op; init=true)
    if init
        out .= 0
    end

    if length(size(out)) == 1 && length(size(orig[weight])) == 2
        out .+= op.(view(orig[weight],:))
    else
        out .+= op.(orig[weight])
    end
end


text:
if PROGRAM_FILE == basename(@__FILE__)
    println("please  julia -i sevenstars.jl")
    exit()
end

module WASM

export WasmController

using Bukdu # ApplicationController Conn JavaScript Plug.Static Router Utils CLI render routes get plug
import Bukdu.Actions: index
import ..Layout: layout

#=
using Charlotte # @code_wasm

    relu(x) = ifelse(x < 0, 0, x)
    w = @code_wasm relu(1)
    wast = string(w)
    Render("application/octet-stream", unsafe_wrap(Vector{UInt8}, wast))

(func (param i64) (result i64)
  (i64.const 0)
  (get_local 0)
  (get_local 0)
  (i64.const 0)
  (i64.lt_s)
  (select)
  (return))
=#

struct WasmController <: ApplicationController
    conn::Conn
end

function hello_wast(::WasmController)
    render(Text, """
(module
  (type (;0;) (func (param i32 i32)))
  (type (;1;) (func))
  (import "env" "memory" (memory (;0;) 256 256))
  (import "env" "print" (func (;0;) (type 0)))
  (func (;1;) (type 1)
    i32.const 1400
    i32.const 13  ;; length
    call 0)
  (export "_main" (func 1))
  (data (i32.const 1400) "Hello, world!"))
""")
end

# https://gist.github.com/SpaceManiac/daf03e0ac6ed56e7a7723ccdeaf5cfe2
function hello_js(::WasmController)
    render(JavaScript, """
var memory = new WebAssembly.Memory({initial:256, maximum:256});
var imports = { 'env': { 'memory': memory, 'print': print, } };

function console_out(text) {
    let LF = "\\n";
    var c = document.getElementById('console');
    c.innerText += text + LF;
}

function print(ptr, len) {
    let text = new TextDecoder().decode(new DataView(memory.buffer, ptr, len));
    console_out(text);
};

async function fetch_wast() {
    console_out("fetch_wast /hello.wast");
    let res = await fetch('/hello.wast');
    return res.arrayBuffer();
}

function wast_to_wasm(buf) {
    console_out("wast_to_wasm (wast: " + buf.byteLength + " bytes)");
    let module = wabt.parseWat('hello.wast', buf);
    module.resolveNames();
    module.validate();
    let bin = module.toBinary({log: true, write_debug_names: true});
    return bin.buffer;
}

function wasm_instantiate(bytes) {
    console_out("wasm_instantiate (wasm: " + bytes.length + " bytes)");
    return WebAssembly.instantiate(bytes, imports);
}

function call_result_instance_main(result) {
    console_out("call_result_instance_main()");
    result.instance.exports._main();
}

document.addEventListener('DOMContentLoaded', function () {
    wabt.ready.then(fetch_wast)
              .then(wast_to_wasm)
              .then(wasm_instantiate)
              .then(call_result_instance_main);
});
""")
end

using InteractiveUtils # versioninfo
function get_banner_versioninfo()
    Utils.read_stdout() do
        Base.banner()
        versioninfo()
    end
end

end # module WASM

import .Layout: layout
import .WASM: get_banner_versioninfo
function WASM.index(::WASM.WasmController)
    title = " - web assembly demo"
    script = """

"""
    style = """

"""
    body = """

Back

WebAssembly Demo

$(get_banner_versioninfo())

"""
    layout(title, script, style, body)
end

max_stars_repo_name: wookay/TestBukdu

max_issues_repo_name: wookay/TestBukdu

max_forks_repo_name: wookay/TestBukdu


text:
module StaticStrings

import Base:
    iterate, length, getindex, lastindex, sizeof,
    ncodeunits, codeunit, isvalid, read, write

using Base: @propagate_inbounds

export StaticString, @static_str

include("static.jl")

end


text:
function compress_then_write(b::Vector{SubString{String}}, io)
    @warn "JDF is writing a SubString vector. When loaded back it will be a String vector not a SubString vector"
    compress_then_write(String.(b), io)
end

function column_loader(::Type{SubString{String}}, io, metadata)
    @warn "JDF is loading SubString vector. It will be loaded as a String vector not a SubString vector"
    column_loader(String, io, metadata)
end

max_stars_repo_name: xiaodaigh/JDF

max_issues_repo_name: xiaodaigh/JDF

max_forks_repo_name: xiaodaigh/JDF


text:
@discord_object struct AuditLogInfo
    delete_member_days::String
    members_removed::String
    channel_id::Snowflake
    message_id::Snowflake
    count::String
    id::Snowflake
    type::String
    role_name::String
end


text:
function time_integration(u,v,η,sst)

    # FORCING
    Fx = wind()
    f_q = beta_plane()
    H = topography()
    η_ref = interface_relaxation()
    sst_ref = sst_initial()

    # add halo with ghost point copy
    u,v,η,sst = add_halo(u,v,η,sst)

    # PREALLOCATE
    du,u0,u1,dudx,dudy = preallocate_u_vars()
    dv,v0,v1,dvdx,dvdy = preallocate_v_vars()
    dη,η0,η1,h = preallocate_η_vars()
    h_u,U,h_v,V,dUdx,dVdy = preallocate_continuity()
    h_q,q,q_v,qhu,U_v,q_u,qhv,V_u = preallocate_Sadourny()
    qα,qβ,qγ,qδ = preallocate_ArakawaHsu()
    u²,v²,KEu,KEv,p,dpdx,dpdy = preallocate_Bernoulli()
    sqrtKE,sqrtKE_u,sqrtKE_v,Bu,Bv = preallocate_bottomdrag()
    Lu,Lv,dLudx,dLudy,dLvdx,dLvdy = preallocate_Laplace()
    DT,DS,DS_q,νSmag,νSmag_q,S11,S12,S21,S22,LLu1,LLu2,LLv1,LLv2 = preallocate_Smagorinsky()
    xd,yd,um,vm,u_T,um_T,v_T,vm_T,uinterp,vinterp,ssti = preallocate_semiLagrange()

    # propagate initial conditions
    u0 .= u
    v0 .= v
    η0 .= η

    # feedback and output
    t0,progrtxt = feedback_ini()
    ncs, iout = output_ini(u,v,η,sst)
    nans_detected = false

    t = 0           # model time
    for i = 1:nt

        # ghost point copy for boundary conditions
        ghost_points!(u,v,η)
        u1 .= u
        v1 .= v
        η1 .= η

        # Runge-Kutta 4th order / 3rd order
        for rki = 1:RKo
            if rki > 1
                ghost_points!(u1,v1,η1)
            end

            rhs!(du,dv,dη,u1,v1,η1,Fx,f_q,H,η_ref,
                dvdx,dudy,dpdx,dpdy,
                p,u²,v²,KEu,KEv,dUdx,dVdy,
                h,h_u,h_v,h_q,U,V,U_v,V_u,
                qhv,qhu,q,q_u,q_v,
                qα,qβ,qγ,qδ)

            if rki < RKo
                caxb!(u1,u,RKb[rki]*Δt,du)  #u1 .= u .+ RKb[rki]*Δt*du
                caxb!(v1,v,RKb[rki]*Δt,dv)  #v1 .= v .+ RKb[rki]*Δt*dv
                caxb!(η1,η,RKb[rki]*Δt,dη)  #η1 .= η .+ RKb[rki]*Δt*dη
            end

            # sum RK-substeps on the go
            axb!(u0,RKa[rki]*Δt,du)  #u0 .+= RKa[rki]*Δt*du
            axb!(v0,RKa[rki]*Δt,dv)  #v0 .+= RKa[rki]*Δt*dv
            axb!(η0,RKa[rki]*Δt,dη)  #η0 .+= RKa[rki]*Δt*dη
        end

        ghost_points!(u0,v0,η0)

        # ADVECTION and CORIOLIS TERMS
        # although included in the tendency of every RK substep,
        # only update every nstep_advcor steps!
        if (i % nstep_advcor) == 0
            rhs_advcor!(u0,v0,η0,H,h,h_u,h_v,h_q,U,V,dvdx,dudy,u²,v²,KEu,KEv,
                        q,f_q,qhv,qhu,qα,qβ,qγ,qδ,q_u,q_v,V_u,U_v)
        end

        # DIFFUSIVE TERMS - SEMI-IMPLICIT EULER
        # use u0 = u^(n+1) to evaluate tendencies, add to u0 = u^n + rhs
        if (i % nstep_diff) == 0    # evaluate only every nstep_diff time steps
            bottom_drag!(Bu,Bv,KEu,KEv,sqrtKE,sqrtKE_u,sqrtKE_v,u0,v0,η0,
                H,h,u²,v²,h_u,h_v)
            diffusive!(dudx,dudy,dvdx,dvdy,DS,DS_q,DT,νSmag,νSmag_q,Lu,Lv,
                dLudx,dLudy,dLvdx,dLvdy,S11,S12,S21,S22,
                LLu1,LLu2,LLv1,LLv2,u0,v0)
            add_drag_diff_tendencies!(u0,v0,Bu,Bv,LLu1,LLu2,LLv1,LLv2)
        end

        # RK3/4 copy back from substeps
        u .= u0
        v .= v0
        η .= η0
        t += dtint

        # TRACER ADVECTION
        # mid point (in time) velocity for the advective time step
        if tracer_advection && ((i+nadvstep_half) % nadvstep) == 0
            um .= u
            vm .= v
        end

        if tracer_advection && (i % nadvstep) == 0
            departure!(u,v,u_T,v_T,um,vm,um_T,vm_T,uinterp,vinterp,xd,yd)
            adv_sst!(ssti,sst,xd,yd)
            if tracer_relaxation
                tracer_relax!(ssti,sst_ref)
            end
            ghost_points_sst!(ssti)
            sst .= ssti

            # conserved?
            #println(mean(sst[halosstx+1:end-halosstx,halossty+1:end-halossty].*h[haloη+1:end-haloη,haloη+1:end-haloη]))
        end

        # feedback and output
        t0,nans_detected = feedback(u,v,η,sst,i,t0,nt,nans_detected,progrtxt)
        ncs,iout = output_nc(ncs,u,v,η,sst,i,iout)

        if nans_detected
            break
            #TODO break all MPI processes
        end
    end

    # finalise feeback and output
    feedback_end(progrtxt,t0)
    output_close(ncs,progrtxt)

    return u,v,η,sst
end

""" Add multiply a with b. a += x*b """
function axb!(a::AbstractMatrix,x::Real,b::AbstractMatrix)
    m,n = size(a)
    @boundscheck (m,n) == size(b) || throw(BoundsError())

    @inbounds for j ∈ 1:n
        for i ∈ 1:m
           a[i,j] += x*b[i,j]
        end
    end
end

""" C equals add multiply a with b. c = a + x*b """
function caxb!(c::AbstractMatrix,a::AbstractMatrix,x::Real,b::AbstractMatrix)
    m,n = size(a)
    @boundscheck (m,n) == size(b) || throw(BoundsError())
    @boundscheck (m,n) == size(c) || throw(BoundsError())

    @inbounds for j ∈ 1:n
        for i ∈ 1:m
           c[i,j] = a[i,j] + x*b[i,j]
        end
    end
end

max_stars_repo_name: eschnett/juls

max_issues_repo_name: eschnett/juls

max_forks_repo_name: eschnett/juls


text:
struct IdentityMap{T} <: LinearMap{T} # T will be determined from maps to which I is added
    M::Int
end
IdentityMap(T::Type, M::Int) = IdentityMap{T}(M)
IdentityMap(T::Type, M::Int, N::Int) = (M == N ? IdentityMap{T}(M) : error("IdentityMap needs to be square"))
IdentityMap(T::Type, sz::Tuple{Int, Int}) = (sz[1] == sz[2] ? IdentityMap{T}(sz[1]) : error("IdentityMap needs to be square"))
IdentityMap(M::Int) = IdentityMap(Int8, M)
IdentityMap(M::Int, N::Int) = IdentityMap(Int8, M, N)
IdentityMap(sz::Tuple{Int, Int}) = IdentityMap(Int8, sz)

# properties
Base.size(A::IdentityMap) = (A.M, A.M)
Base.isreal(::IdentityMap) = true
LinearAlgebra.issymmetric(::IdentityMap) = true
LinearAlgebra.ishermitian(::IdentityMap) = true
LinearAlgebra.isposdef(::IdentityMap) = true

# multiplication with vector
A_mul_B!(y::AbstractVector, A::IdentityMap, x::AbstractVector) =
    (length(x) == length(y) == A.M ? copyto!(y, x) : throw(DimensionMismatch("A_mul_B!")))
Base.:(*)(A::IdentityMap, x::AbstractVector) = x

At_mul_B!(y::AbstractVector, A::IdentityMap, x::AbstractVector) =
    (length(x) == length(y) == A.M ? copyto!(y, x) : throw(DimensionMismatch("At_mul_B!")))

Ac_mul_B!(y::AbstractVector, A::IdentityMap, x::AbstractVector) =
    (length(x) == length(y) == A.M ? copyto!(y, x) : throw(DimensionMismatch("Ac_mul_B!")))

# combine LinearMap and UniformScaling objects in linear combinations
Base.:(+)(A1::LinearMap, A2::UniformScaling{T}) where {T} = A1 + A2[1,1] * IdentityMap{T}(size(A1, 1))
Base.:(+)(A1::UniformScaling{T}, A2::LinearMap) where {T} = A1[1,1] * IdentityMap{T}(size(A2, 1)) + A2
Base.:(-)(A1::LinearMap, A2::UniformScaling{T}) where {T} = A1 - A2[1,1] * IdentityMap{T}(size(A1, 1))
Base.:(-)(A1::UniformScaling{T}, A2::LinearMap) where {T} = A1[1,1] * IdentityMap{T}(size(A2, 1)) - A2