Skip to content

Commit 5789dfe

Browse files
committed
Touchups
1 parent 2b1448c commit 5789dfe

1 file changed

Lines changed: 18 additions & 16 deletions

File tree

‎ext/TensorKitGPUArraysExt.jl‎

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -149,11 +149,11 @@ an entry of an `AbelianTreeTransformer`.
149149
struct AbelianTransformerBlock{T, N}
150150
coeff::T
151151
sz::NTuple{N, Int}
152-
densestrides::NTuple{N, Int}
153-
st_dst::NTuple{N, Int}
154-
offs_dst::Int
155-
pst_src::NTuple{N, Int} # source strides, permuted by `p`
156-
offs_src::Int
152+
dense_strides::NTuple{N, Int}
153+
strides_dst::NTuple{N, Int}
154+
offsets_dst::Int
155+
permuted_strides_src::NTuple{N, Int} # source strides, permuted by `p`
156+
offsets_src::Int
157157
end
158158

159159
# device-side simple struct that GPU kernels can use
@@ -164,17 +164,17 @@ struct DeviceAbelianTreeTransformer{VB <: AbstractVector{<:AbelianTransformerBlo
164164
end
165165

166166
# strides of a dense array of shape `sz`
167-
_densestrides(sz::NTuple{N, Int}) where {N} = ntuple(n -> prod(sz[1:(n - 1)]; init = 1), Val(N))
168-
_permutestrides(st::NTuple{N, Int}, p) where {N} = ntuple(n -> st[p[n]], Val(N))
167+
_dense_strides(size::NTuple{N, Int}) where {N} = ntuple(n -> prod(size[1:(n - 1)]; init = 1), Val(N))
168+
_permute_strides(strides::NTuple{N, Int}, p) where {N} = ntuple(n -> strides[p[n]], Val(N))
169169

170170
# `permute(Vsrc, p) == Vdst` is enforced when the transformer is built, so the permuted
171171
# source shape always matches `sz_dst` and the two views share Cartesian inds.
172172
function _abelian_block(
173-
coeff::T, (sz_dst, st_dst, offs_dst), (_, st_src, offs_src), p
173+
coeff::T, (size_dst, strides_dst, offsets_dst), (_, strides_src, offsets_src), p
174174
) where {T}
175-
return AbelianTransformerBlock{T, length(sz_dst)}(
176-
coeff, sz_dst, _densestrides(sz_dst), st_dst, offs_dst,
177-
_permutestrides(st_src, p), offs_src
175+
return AbelianTransformerBlock{T, length(size_dst)}(
176+
coeff, size_dst, _dense_strides(size_dst), strides_dst, offsets_dst,
177+
_permute_strides(strides_src, p), offsets_src
178178
)
179179
end
180180

@@ -254,7 +254,9 @@ _device_transformer(t::TensorKit.AbelianTreeTransformer, p) = DeviceAbelianTreeT
254254
# kernels are not reachable by coverage
255255

256256
# largest `i` with `offsets[i] <= w`. This corresponds to the
257-
# block which this kernel thread will work on.
257+
# block which this kernel thread will work on. Since this is
258+
# used inside a GPU kernel, searchsortedlast/searchsortedfirst
259+
# won't work.
258260
@inline function _searchblock(offsets, w)
259261
lo, hi = 1, length(offsets)
260262
while lo < hi
@@ -272,14 +274,14 @@ end
272274
# Computed once per thread and then reused for every strided view of that subblock.
273275
# This avoids `StridedView` redoing these divisions on every single element access.
274276
# Integer division on GPU is usually pretty slow.
275-
@inline function _coordinates(w, sz::NTuple{N, Int}, densestrides::NTuple{N, Int}) where {N}
276-
return ntuple(n -> (w ÷ densestrides[n]) % sz[n], Val(N))
277+
@inline function _coordinates(w, size::NTuple{N, Int}, dense_strides::NTuple{N, Int}) where {N}
278+
return ntuple(n -> (w ÷ dense_strides[n]) % size[n], Val(N))
277279
end
278280

279281
# finds the overall offset in the output and input arrays corresponding to the **sublock**
280282
# coordinates currently being worked on
281-
@inline function _offset(coords::NTuple{N, Int}, st::NTuple{N, Int}, offs) where {N}
282-
return offs + sum(ntuple(n -> coords[n] * st[n], Val(N))) + 1
283+
@inline function _offset(coords::NTuple{N, Int}, strides::NTuple{N, Int}, offset) where {N}
284+
return offset + sum(ntuple(n -> coords[n] * strides[n], Val(N))) + 1
283285
end
284286

285287
# One thread per destination element in `data_dst`.

0 commit comments

Comments
 (0)