@@ -11,7 +11,7 @@ using TensorOperations: add_labels, trace_labels, contract_labels
1111using TensorOperations: tensoralloc_add, tensorfree!, select_backend
1212
1313using TBLIS
14- using TBLIS: len_type, stride_type
14+ using TBLIS: len_type, stride_type, tblis_tensor
1515
1616const SV = StridedView
1717
@@ -22,48 +22,58 @@ const TBLISFloat = Union{Float32, Float64, ComplexF32, ComplexF64}
2222# -------------------------------------------------------------------------------------------
2323# Wrapping Julia arrays as TBLIS tensors
2424# -------------------------------------------------------------------------------------------
25- tblis_type (:: Type{Float32} ) = TBLIS. TYPE_SINGLE
26- tblis_type (:: Type{Float64} ) = TBLIS. TYPE_DOUBLE
27- tblis_type (:: Type{ComplexF32} ) = TBLIS. TYPE_SCOMPLEX
28- tblis_type (:: Type{ComplexF64} ) = TBLIS. TYPE_DCOMPLEX
25+ # TBLIS.jl's own `tblis_tensor` constructor is restricted to `StridedArray`, which excludes
26+ # the `StridedView`s that the tensor operations work with, and it has no way to express the
27+ # conjugation flag. We therefore initialize the descriptor through the low-level bindings and
28+ # patch in the flag afterwards.
29+ for (T, init) in (
30+ (:Float32 , :tblis_init_tensor_scaled_s ),
31+ (:Float64 , :tblis_init_tensor_scaled_d ),
32+ (:ComplexF32 , :tblis_init_tensor_scaled_c ),
33+ (:ComplexF64 , :tblis_init_tensor_scaled_z ),
34+ )
35+ @eval function init_tensor! (
36+ p:: Ptr{tblis_tensor} , A:: StridedView{$T, N} , α:: $T ,
37+ len:: Vector{len_type} , stride:: Vector{stride_type}
38+ ) where {N}
39+ return TBLIS.$ init (p, α, Cuint (N), pointer (len), pointer (A), pointer (stride))
40+ end
41+ end
2942
3043"""
3144 TBLISTensor(A::StridedView, α, isconj)
3245
3346Owning counterpart of `TBLIS.tblis_tensor`, which itself only stores raw pointers into the
3447array it views and into the buffers holding its lengths and strides. Rooting a `TBLISTensor`
35- with `GC.@preserve` also roots all of those, as they are reachable from it.
36-
37- Use [`unsafe_tblis_tensor`](@ref) to obtain the plain C struct that the library expects, and
38- keep the `TBLISTensor` alive for at least as long as TBLIS may access it.
48+ with `GC.@preserve` also roots all of those, as they are reachable from it; pass its `ref`
49+ field wherever the library expects a `Ptr{tblis_tensor}`.
3950"""
4051struct TBLISTensor{T, N, A <: StridedView{T, N} }
4152 array:: A
4253 len:: Vector{len_type}
4354 stride:: Vector{stride_type}
44- scalar:: T
45- isconj:: Bool
55+ ref:: Base.RefValue{tblis_tensor}
4656end
4757
48- function TBLISTensor (A:: StridedView{T, N} , α:: T , isconj:: Bool = hasconj (A)) where {T, N}
58+ function TBLISTensor (
59+ A:: StridedView{T, N} , α:: T , isconj:: Bool = hasconj (A)
60+ ) where {T <: TBLISFloat , N}
4961 len = collect (len_type, size (A))
5062 stride = collect (stride_type, strides (A))
51- return TBLISTensor {T, N, typeof(A)} (A, len, stride, α, isconj)
63+ ref = Ref {tblis_tensor} ()
64+ GC. @preserve A len stride ref begin
65+ p = Base. unsafe_convert (Ptr{tblis_tensor}, ref)
66+ init_tensor! (p, A, α, len, stride)
67+ isconj && setproperty! (p, :conj , Cint (1 ))
68+ end
69+ return TBLISTensor {T, N, typeof(A)} (A, len, stride, ref)
5270end
5371
5472# A `StridedView` carries its conjugation in an `op` field rather than in the data, which is
5573# exactly the representation TBLIS wants. For real element types `conj` is the identity, and
5674# flagging it would only confuse the library.
5775hasconj (A:: StridedView{T} ) where {T} = T <: Complex && (A. op === conj || A. op === adjoint)
5876
59- function unsafe_tblis_tensor (t:: TBLISTensor{T, N} ) where {T, N}
60- return TBLIS. tblis_tensor (
61- tblis_type (T), Cint (t. isconj), TBLIS. tblis_scalar (t. scalar),
62- Ptr {Cvoid} (pointer (t. array)), Cuint (N),
63- pointer (t. len), pointer (t. stride)
64- )
65- end
66-
6777# TBLIS takes the index labels as one `char` per dimension; `add_labels` and friends hand us
6878# `Char` tuples that are guaranteed to be ASCII.
6979labels (ein:: Tuple{Vararg{Char}} ) = String (UInt8[c for c in ein])
@@ -183,9 +193,8 @@ function unsafe_add!(
183193 tA = TBLISTensor (A, α)
184194 tC = TBLISTensor (C, β)
185195 GC. @preserve tA tC begin
186- tblis_tensor_add (
187- unsafe_tblis_tensor (tA), labels (einA),
188- unsafe_tblis_tensor (tC), labels (einC)
196+ TBLIS. tblis_tensor_add (
197+ C_NULL , C_NULL , tA. ref, labels (einA), tC. ref, labels (einC)
189198 )
190199 end
191200 return C
@@ -247,10 +256,9 @@ function unsafe_mult!(
247256 tB = TBLISTensor (B, one (T), false )
248257 tC = TBLISTensor (C, β, false )
249258 GC. @preserve tA tB tC begin
250- tblis_tensor_mult (
251- unsafe_tblis_tensor (tA), labels (einA),
252- unsafe_tblis_tensor (tB), labels (einB),
253- unsafe_tblis_tensor (tC), labels (einC)
259+ TBLIS. tblis_tensor_mult (
260+ C_NULL , C_NULL , tA. ref, labels (einA), tB. ref, labels (einB),
261+ tC. ref, labels (einC)
254262 )
255263 end
256264 return C
0 commit comments