|
| 1 | +function nonzero_blocks(A::CuTensorBS) |
| 2 | + return A.nonzero_data |
| 3 | +end |
| 4 | + |
| 5 | +function contract!( |
| 6 | + @nospecialize(alpha::Number), |
| 7 | + @nospecialize(A), Ainds::ModeType, opA::cutensorOperator_t, |
| 8 | + @nospecialize(B), Binds::ModeType, opB::cutensorOperator_t, |
| 9 | + @nospecialize(beta::Number), |
| 10 | + @nospecialize(C), Cinds::ModeType, opC::cutensorOperator_t, |
| 11 | + opOut::cutensorOperator_t; |
| 12 | + jit::cutensorJitMode_t=JIT_MODE_NONE, |
| 13 | + workspace::cutensorWorksizePreference_t=WORKSPACE_DEFAULT, |
| 14 | + algo::cutensorAlgo_t=ALGO_DEFAULT, |
| 15 | + compute_type::Union{DataType, cutensorComputeDescriptorEnum, Nothing}=nothing, |
| 16 | + plan::Union{CuTensorPlan, Nothing}=nothing) |
| 17 | + |
| 18 | + actual_plan = if plan === nothing |
| 19 | + plan_contraction(A, Ainds, opA, B, Binds, opB, C, Cinds, opC, opOut; |
| 20 | + jit, workspace, algo, compute_type) |
| 21 | + else |
| 22 | + plan |
| 23 | + end |
| 24 | + |
| 25 | + contractBS!(actual_plan, alpha, nonzero_blocks(A), nonzero_blocks(B), beta, nonzero_blocks(C)) |
| 26 | + |
| 27 | + if plan === nothing |
| 28 | + CUDACore.unsafe_free!(actual_plan) |
| 29 | + end |
| 30 | + |
| 31 | + return C |
| 32 | +end |
| 33 | + |
| 34 | +## This function assumes A, B, and C are Arrays of pointers to CuArrays. |
| 35 | +## Please overwrite the `nonzero_blocks` function for your datatype to access this function from contract! |
| 36 | +function contractBS!(plan::CuTensorPlan, |
| 37 | + @nospecialize(alpha::Number), |
| 38 | + @nospecialize(A::AbstractArray), |
| 39 | + @nospecialize(B::AbstractArray), |
| 40 | + @nospecialize(beta::Number), |
| 41 | + @nospecialize(C::AbstractArray)) |
| 42 | + scalar_type = plan.scalar_type |
| 43 | + |
| 44 | + # Extract GPU pointers from each CuArray block |
| 45 | + # cuTENSOR expects a host-accessible array of GPU pointers |
| 46 | + A_ptrs = CuPtr{Cvoid}[pointer(block) for block in A] |
| 47 | + B_ptrs = CuPtr{Cvoid}[pointer(block) for block in B] |
| 48 | + C_ptrs = CuPtr{Cvoid}[pointer(block) for block in C] |
| 49 | + |
| 50 | + cutensorBlockSparseContract(handle(), plan, |
| 51 | + Ref{scalar_type}(alpha), A_ptrs, B_ptrs, |
| 52 | + Ref{scalar_type}(beta), C_ptrs, C_ptrs, |
| 53 | + plan.workspace, sizeof(plan.workspace), stream()) |
| 54 | + return C |
| 55 | +end |
| 56 | + |
| 57 | +function plan_contraction( |
| 58 | + @nospecialize(A), Ainds::ModeType, opA::cutensorOperator_t, |
| 59 | + @nospecialize(B), Binds::ModeType, opB::cutensorOperator_t, |
| 60 | + @nospecialize(C), Cinds::ModeType, opC::cutensorOperator_t, |
| 61 | + opOut::cutensorOperator_t; |
| 62 | + jit::cutensorJitMode_t=JIT_MODE_NONE, |
| 63 | + workspace::cutensorWorksizePreference_t=WORKSPACE_DEFAULT, |
| 64 | + algo::cutensorAlgo_t=ALGO_DEFAULT, |
| 65 | + compute_type::Union{DataType, cutensorComputeDescriptorEnum, Nothing}=nothing) |
| 66 | + |
| 67 | + !is_unary(opA) && throw(ArgumentError("opA must be a unary op!")) |
| 68 | + !is_unary(opB) && throw(ArgumentError("opB must be a unary op!")) |
| 69 | + !is_unary(opC) && throw(ArgumentError("opC must be a unary op!")) |
| 70 | + !is_unary(opOut) && throw(ArgumentError("opOut must be a unary op!")) |
| 71 | + |
| 72 | + descA = CuTensorBSDescriptor(A) |
| 73 | + descB = CuTensorBSDescriptor(B) |
| 74 | + descC = CuTensorBSDescriptor(C) |
| 75 | + # for now, D must be identical to C (and thus, descD must be identical to descC) |
| 76 | + |
| 77 | + modeA = collect(Cint, Ainds) |
| 78 | + modeB = collect(Cint, Binds) |
| 79 | + modeC = collect(Cint, Cinds) |
| 80 | + |
| 81 | + actual_compute_type = if compute_type === nothing |
| 82 | + contraction_compute_types[(eltype(A), eltype(B), eltype(C))] |
| 83 | + else |
| 84 | + compute_type |
| 85 | + end |
| 86 | + |
| 87 | + |
| 88 | + desc = Ref{cutensorOperationDescriptor_t}() |
| 89 | + cutensorCreateBlockSparseContraction(handle(), |
| 90 | + desc, |
| 91 | + descA, modeA, opA, |
| 92 | + descB, modeB, opB, |
| 93 | + descC, modeC, opC, |
| 94 | + descC, modeC, actual_compute_type) |
| 95 | + |
| 96 | + plan_pref = Ref{cutensorPlanPreference_t}() |
| 97 | + cutensorCreatePlanPreference(handle(), plan_pref, algo, jit) |
| 98 | + |
| 99 | + plan = CuTensorPlan(desc[], plan_pref[]; workspacePref=workspace) |
| 100 | + cutensorDestroyOperationDescriptor(desc[]) |
| 101 | + cutensorDestroyPlanPreference(plan_pref[]) |
| 102 | + return plan |
| 103 | +end |
0 commit comments