|
43 | 43 |
|
44 | 44 | # implement necessary permutations |
45 | 45 | function _blas_contract!(C, A, pA, B, pB, pAB, α, β, backend, allocator) |
| 46 | + # note: the inputs are made BLAS-contractable one at a time, passing them on to the next |
| 47 | + # step rather than returning them, since their types depend on whether a copy was made, |
| 48 | + # and returning such a union would require heap allocating the result |
| 49 | + isblascontractable(A, pA) && eltype(A) == eltype(C) && |
| 50 | + return _blas_contract_B!(C, A, pA, B, pB, pAB, α, β, backend, allocator) |
| 51 | + A_ = _blascontractable_copy(A, pA, eltype(C), backend, allocator) |
| 52 | + _blas_contract_B!(C, SV(A_), trivialpermutation(pA), B, pB, pAB, α, β, backend, allocator) |
| 53 | + tensorfree!(A_, allocator) |
| 54 | + return C |
| 55 | +end |
| 56 | +function _blas_contract_B!(C, A, pA, B, pB, pAB, α, β, backend, allocator) |
| 57 | + isblascontractable(B, pB) && eltype(B) == eltype(C) && |
| 58 | + return _blas_contract_C!(C, A, pA, B, pB, pAB, α, β, backend, allocator) |
| 59 | + B_ = _blascontractable_copy(B, pB, eltype(C), backend, allocator) |
| 60 | + _blas_contract_C!(C, A, pA, SV(B_), trivialpermutation(pB), pAB, α, β, backend, allocator) |
| 61 | + tensorfree!(B_, allocator) |
| 62 | + return C |
| 63 | +end |
| 64 | +function _blas_contract_C!(C, A, pA, B, pB, pAB, α, β, backend, allocator) |
46 | 65 | TC = eltype(C) |
47 | | - |
48 | | - A_, pA, flagA = makeblascontractable(A, pA, TC, backend, allocator) |
49 | | - B_, pB, flagB = makeblascontractable(B, pB, TC, backend, allocator) |
50 | | - |
51 | 66 | # the partition of `ipAB` is required by `isblasdestination` and `tensoralloc_add`, but |
52 | 67 | # the actual contraction only needs the linearized permutation |
53 | 68 | ipAB = oindABinC(pAB, pA, pB) |
54 | 69 | ipAB′ = linearize(ipAB) |
55 | 70 | flagC = isblasdestination(C, ipAB) |
56 | 71 | if flagC |
57 | | - C_ = C |
58 | | - _unsafe_blas_contract!(C_, A_, pA, B_, pB, ipAB′, α, β) |
| 72 | + _unsafe_blas_contract!(C, A, pA, B, pB, ipAB′, α, β) |
59 | 73 | else |
60 | 74 | C_ = SV(tensoralloc_add(TC, C, ipAB, false, Val(true), allocator)) |
61 | 75 | _unsafe_blas_contract!( |
62 | | - C_, A_, pA, B_, pB, trivialpermutation(ipAB′), |
| 76 | + C_, A, pA, B, pB, trivialpermutation(ipAB′), |
63 | 77 | one(TC), zero(TC) |
64 | 78 | ) |
65 | 79 | # `C` already exists, so only the permutation of `pAB` matters here and it can be |
66 | 80 | # handed over in the canonical `{N,0}` partition |
67 | 81 | tensoradd!(C, C_, (pAB, ()), false, α, β, backend, allocator) |
68 | 82 | tensorfree!(C_.parent, allocator) |
69 | 83 | end |
70 | | - flagA || tensorfree!(A_.parent, allocator) |
71 | | - flagB || tensorfree!(B_.parent, allocator) |
72 | 84 | return C |
73 | 85 | end |
74 | 86 |
|
| 87 | +# Make a permuted copy of `A` with scalar type `TC`, that can be used directly in a BLAS |
| 88 | +# contraction with the trivial partition. The copy should be freed afterwards. |
| 89 | +# note: inlined such that the wrapper of the copy does not need to be allocated if it does not |
| 90 | +# escape the caller, such as for an `Array` that only serves as the parent of a `StridedView` |
| 91 | +@inline function _blascontractable_copy(A, pA, ::Type{TC}, backend, allocator) where {TC} |
| 92 | + A_ = tensoralloc_add(TC, A, pA, false, Val(true), allocator) |
| 93 | + tensoradd!(SV(A_), A, pA, false, One(), Zero(), backend, allocator) |
| 94 | + return A_ |
| 95 | +end |
| 96 | + |
75 | 97 | # perform the actual contraction, assuming it can be done as matrix multiplication by simply |
76 | 98 | # reshaping without any further allocations |
77 | 99 | function _unsafe_blas_contract!( |
|
0 commit comments