|
| 1 | +using PrecompileTools: PrecompileTools |
| 2 | +using Preferences: @load_preference |
| 3 | + |
| 4 | +# Validate preferences input |
| 5 | +# -------------------------- |
| 6 | +function validate_precompile_eltypes(eltypes) |
| 7 | + eltypes isa Vector{String} || |
| 8 | + throw(ArgumentError("`precompile_eltypes` should be a vector of strings, got $(typeof(eltypes)) instead")) |
| 9 | + return map(eltypes) do Tstr |
| 10 | + T = eval(Meta.parse(Tstr)) |
| 11 | + (T isa DataType && T <: Number) || |
| 12 | + error("Invalid precompile_eltypes entry: `$Tstr`") |
| 13 | + return T |
| 14 | + end |
| 15 | +end |
| 16 | + |
| 17 | +function validate_add_ndims(add_ndims) |
| 18 | + add_ndims isa Int || |
| 19 | + throw(ArgumentError("`precompile_add_ndims` should be an `Int`, got `$add_ndims`")) |
| 20 | + add_ndims ≥ 0 || error("Invalid precompile_add_ndims: `$add_ndims`") |
| 21 | + return add_ndims |
| 22 | +end |
| 23 | + |
| 24 | +function validate_trace_ndims(trace_ndims) |
| 25 | + trace_ndims isa Vector{Int} && length(trace_ndims) == 2 || |
| 26 | + throw(ArgumentError("`precompile_trace_ndims` should be a `Vector{Int}` of length 2, got `$trace_ndims`")) |
| 27 | + all(≥(0), trace_ndims) || error("Invalid precompile_trace_ndims: `$trace_ndims`") |
| 28 | + return trace_ndims |
| 29 | +end |
| 30 | + |
| 31 | +function validate_contract_ndims(contract_ndims) |
| 32 | + contract_ndims isa Vector{Int} && length(contract_ndims) == 2 || |
| 33 | + throw(ArgumentError("`precompile_contract_ndims` should be a `Vector{Int}` of length 2, got `$contract_ndims`")) |
| 34 | + all(≥(0), contract_ndims) || |
| 35 | + error("Invalid precompile_contract_ndims: `$contract_ndims`") |
| 36 | + return contract_ndims |
| 37 | +end |
| 38 | + |
| 39 | +# Static preferences |
| 40 | +# ------------------ |
| 41 | +const PRECOMPILE_ELTYPES = validate_precompile_eltypes(@load_preference("precompile_eltypes", |
| 42 | + ["Float64", |
| 43 | + "ComplexF64"])) |
| 44 | +const PRECOMPILE_ADD_NDIMS = validate_add_ndims(@load_preference("precompile_add_ndims", 5)) |
| 45 | +const PRECOMPILE_TRACE_NDIMS = validate_trace_ndims(@load_preference("precompile_trace_ndims", |
| 46 | + [4, 2])) |
| 47 | +const PRECOMPILE_CONTRACT_NDIMS = validate_contract_ndims(@load_preference("precompile_contract_ndims", |
| 48 | + [4, 2])) |
| 49 | + |
| 50 | +# Using explicit precompile statements here instead of @compile_workload: |
| 51 | +# Actually running the precompilation through PrecompileTools leads to longer compile times |
| 52 | +# Keeping the workload_enabled functionality to have the option of disabling precompilation |
| 53 | +# in a compatible manner with the rest of the ecosystem |
| 54 | +if PrecompileTools.workload_enabled(@__MODULE__) |
| 55 | + # tensoradd! |
| 56 | + # ---------- |
| 57 | + for T in PRECOMPILE_ELTYPES |
| 58 | + for N in 0:PRECOMPILE_ADD_NDIMS |
| 59 | + C = Array{T,N} |
| 60 | + A = Array{T,N} |
| 61 | + pA = Index2Tuple{N,0} |
| 62 | + |
| 63 | + precompile(tensoradd!, (C, A, pA, Bool, One, Zero)) |
| 64 | + precompile(tensoradd!, (C, A, pA, Bool, T, Zero)) |
| 65 | + precompile(tensoradd!, (C, A, pA, Bool, T, T)) |
| 66 | + |
| 67 | + precompile(tensoralloc_add, (T, A, pA, Bool, Val{true})) |
| 68 | + precompile(tensoralloc_add, (T, A, pA, Bool, Val{false})) |
| 69 | + end |
| 70 | + end |
| 71 | + |
| 72 | + # tensortrace! |
| 73 | + # ------------ |
| 74 | + for T in PRECOMPILE_ELTYPES |
| 75 | + for N1 in 0:PRECOMPILE_TRACE_NDIMS[1], N2 in 0:PRECOMPILE_TRACE_NDIMS[2] |
| 76 | + C = Array{T,N1} |
| 77 | + A = Array{T,N1 + 2N2} |
| 78 | + p = Index2Tuple{N1,0} |
| 79 | + q = Index2Tuple{N2,N2} |
| 80 | + |
| 81 | + precompile(tensortrace!, (C, A, p, q, Bool, One, Zero)) |
| 82 | + precompile(tensortrace!, (C, A, p, q, Bool, T, Zero)) |
| 83 | + precompile(tensortrace!, (C, A, p, q, Bool, T, T)) |
| 84 | + |
| 85 | + # allocation re-uses tensoralloc_add |
| 86 | + end |
| 87 | + end |
| 88 | + |
| 89 | + # tensorcontract! |
| 90 | + # --------------- |
| 91 | + for T in PRECOMPILE_ELTYPES |
| 92 | + for N1 in 0:PRECOMPILE_CONTRACT_NDIMS[1], N2 in 0:PRECOMPILE_CONTRACT_NDIMS[2], |
| 93 | + N3 in 0:PRECOMPILE_CONTRACT_NDIMS[1] |
| 94 | + |
| 95 | + NA = N1 + N2 |
| 96 | + NB = N2 + N3 |
| 97 | + NC = N1 + N3 |
| 98 | + C, A, B = Array{T,NC}, Array{T,NA}, Array{T,NB} |
| 99 | + pA = Index2Tuple{N1,N2} |
| 100 | + pB = Index2Tuple{N2,N3} |
| 101 | + pAB = Index2Tuple{NC,0} |
| 102 | + |
| 103 | + precompile(tensorcontract!, (C, A, pA, Bool, B, pB, Bool, pAB, One, Zero)) |
| 104 | + precompile(tensorcontract!, (C, A, pA, Bool, B, pB, Bool, pAB, T, Zero)) |
| 105 | + precompile(tensorcontract!, (C, A, pA, Bool, B, pB, Bool, pAB, T, T)) |
| 106 | + |
| 107 | + precompile(tensoralloc_contract, (T, A, pA, Bool, B, pB, Bool, pAB, Val{true})) |
| 108 | + precompile(tensoralloc_contract, (T, A, pA, Bool, B, pB, Bool, pAB, Val{false})) |
| 109 | + end |
| 110 | + end |
| 111 | +end |
0 commit comments