/
stridedarray.jl
256 lines (225 loc) · 9.89 KB
/
stridedarray.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# implementation/stridedarray.jl
#
# High-level implementation of tensor operations for StridedArray from Julia
# Base Library. Checks dimensions and converts to StridedData before passing
# to low-level (recursive) function.
add!(α, A::StridedArray, CA::Type{<:Val}, β, C::StridedArray, p1::IndexTuple, p2::IndexTuple) = add!(α, A, CA, β, C, (p1...,p2...))
"""
add!(α, A, conjA, β, C, indCinA)
Implements `C = β*C+α*permute(op(A))` where `A` is permuted according to `indCinA`
and `op` is `conj` if `conjA=Val{:C}` or the identity map if `conjA=Val{:N}`. The
indexable collection `indCinA` contains as nth entry the dimension of `A` associated
with the nth dimension of `C`.
"""
function add!(α, A::StridedArray, ::Type{Val{CA}}, β, C::StridedArray, indCinA) where CA
for i = 1:ndims(C)
size(A, indCinA[i]) == size(C, i) || throw(DimensionMismatch("$(size(A)), $(size(C)), $(indCinA)"))
end
dims, stridesA, stridesC, minstrides = add_strides(size(C), _permute(strides(A), indCinA), strides(C))
dataA = StridedData(A, stridesA, Val{CA})
offsetA = 0
dataC = StridedData(C, stridesC)
offsetC = 0
if α == 0
β == 1 || _scale!(dataC,β,dims)
elseif α == 1 && β == 0
add_rec!(_one, dataA, _zero, dataC, dims, offsetA, offsetC, minstrides)
elseif α == 1 && β == 1
add_rec!(_one, dataA, _one, dataC, dims, offsetA, offsetC, minstrides)
elseif β == 0
add_rec!(α, dataA, _zero, dataC, dims, offsetA, offsetC, minstrides)
elseif β == 1
add_rec!(α, dataA, _one, dataC, dims, offsetA, offsetC, minstrides)
else
add_rec!(α, dataA, β, dataC, dims, offsetA, offsetC, minstrides)
end
return C
end
trace!(α, A::StridedArray, CA::Type{<:Val}, β, C::StridedArray, p1, p2, cindA1, cindA2) = trace!(α, A, CA, β, C, (p1..., p2...), cindA1, cindA2)
"""
trace!(α, A, conjA, β, C, indCinA, cindA1, cindA2)
Implements `C = β*C+α*partialtrace(op(A))` where `A` is permuted and partially traced,
according to `indCinA`, `cindA1` and `cindA2`, and `op` is `conj` if `conjA=Val{:C}`
or the identity map if `conjA=Val{:N}`. The indexable collection `indCinA` contains
as nth entry the dimension of `A` associated with the nth dimension of `C`. The
partial trace is performed by contracting dimension `cindA1[i]` of `A` with dimension
`cindA2[i]` of `A` for all `i in 1:length(cindA1)`.
"""
function trace!(α, A::StridedArray, ::Type{Val{CA}}, β, C::StridedArray, indCinA, cindA1, cindA2) where CA
NC = ndims(C)
NA = ndims(A)
for i = 1:NC
size(A,indCinA[i]) == size(C,i) || throw(DimensionMismatch(""))
end
map(i->size(A,i), cindA1) == map(i->size(A,i), cindA2) || throw(DimensionMismatch(""))
pA = (indCinA..., cindA1..., cindA2...)
dims, stridesA, stridesC, minstrides = trace_strides(_permute(size(A), pA), _permute(strides(A), pA), strides(C))
dataA = StridedData(A, stridesA, Val{CA})
offsetA = 0
dataC = StridedData(C, stridesC)
offsetC = 0
if α == 0
β == 1 || _scale!(dataC, β, dims)
elseif α == 1 && β == 0
trace_rec!(_one, dataA, _zero, dataC, dims, offsetA, offsetC, minstrides)
elseif α == 1 && β == 1
trace_rec!(_one, dataA, _one, dataC, dims, offsetA, offsetC, minstrides)
elseif β == 0
trace_rec!(α, dataA, _zero, dataC, dims, offsetA, offsetC, minstrides)
elseif β == 1
trace_rec!(α, dataA, _one, dataC, dims, offsetA, offsetC, minstrides)
else
trace_rec!(α, dataA, β, dataC, dims, offsetA, offsetC, minstrides)
end
return C
end
contract!(α, A::StridedArray, CA::Type{<:Val}, B::StridedArray, CB::Type{<:Val}, β, C::StridedArray, oindA, cindA, oindB, cindB, p1, p2, method::Type{<:Val} = Val{:BLAS}) =
contract!(α, A, CA, B, CB, β, C, oindA, cindA, oindB, cindB, (p1..., p2...), method)
"""
contract!(α, A, conjA, B, conjB, β, C, oindA, cindA, oindB, cindB, indCinoAB, [method])
Implements `C = β*C+α*contract(op(A),op(B))` where `A` and `B` are contracted according
to `oindA`, `cindA`, `oindB`, `cindB` and `indCinoAB`. The operation `op` acts as
`conj` if `conjA` or `conjB` equal `Val{:C}` or as the identity map if `conjA` (`conjB`)
equal `Val{:N}`. The dimension `cindA[i]` of `A` is contracted with dimension `cindB[i]`
of `B`. The `n`th dimension of C is associated with an uncontracted (open) dimension
of `A` or `B` according to `indCinoAB[n] < NoA ? oindA[indCinoAB[n]] : oindB[indCinoAB[n]-NoA]`
with `NoA=length(oindA)` the number of open dimensions of `A`.
The optional argument `method` specifies whether the contraction is performed using
BLAS matrix multiplication by specifying `Val{:BLAS}` (default), or using a native
algorithm by specifying `Val{:native}`. The native algorithm does not copy the data
but is typically slower.
"""
function contract!(α, A::StridedArray, ::Type{Val{CA}}, B::StridedArray, ::Type{Val{CB}}, β, C::StridedArray{TC}, oindA, cindA, oindB, cindB, indCinoAB, ::Type{Val{:BLAS}}=Val{:BLAS}) where {CA,CB,TC<:BlasFloat}
NA = ndims(A)
NB = ndims(B)
NC = ndims(C)
TA = eltype(A)
TB = eltype(B)
# dimension checking
dimA = size(A)
dimB = size(B)
dimC = size(C)
cdimsA = map(i->dimA[i], cindA)
cdimsB = map(i->dimB[i], cindB)
odimsA = map(i->dimA[i], oindA)
odimsB = map(i->dimB[i], oindB)
odimsAB = tuple(odimsA..., odimsB...)
cdimsA == cdimsB || throw(DimensionMismatch())
cdims = cdimsA
for i = 1:length(indCinoAB)
dimC[i] == odimsAB[indCinoAB[i]] || throw(DimensionMismatch())
end
olengthA = prod(odimsA)
olengthB = prod(odimsB)
clength = prod(cdims)
# permute A
if CA == :C
conjA = 'C'
pA = (cindA..., oindA...)
if isa(A, Array{TC}) && pA == (1:NA...,)
Amat = reshape(A, (clength, olengthA))
else
Apermuted = Array{TC}(uninitialized, (cdims..., odimsA...))
# tensorcopy!(A, 1:NA, Apermuted, pA)
add!(1, A, Val{:N}, 0, Apermuted, pA)
Amat = reshape(Apermuted, (clength, olengthA))
end
else
conjA = 'N'
pA = (oindA..., cindA...)
if isa(A, Array{TC}) && pA == (1:NA...,)
Amat = reshape(A, (olengthA, clength))
elseif isa(A, Array{TC}) && (cindA..., oindA...) == (1:NA...,)
conjA = 'T'
Amat = reshape(A, (clength, olengthA))
else
Apermuted = Array{TC}(uninitialized, (odimsA..., cdims...))
# tensorcopy!(A, 1:NA, Apermuted, pA)
add!(1, A, Val{:N}, 0, Apermuted, pA)
Amat = reshape(Apermuted, (olengthA, clength))
end
end
# permute B
if CB == :C
conjB = 'C'
pB = (oindB..., cindB...)
if isa(B, Array{TC}) && pB == (1:NB...,)
Bmat = reshape(B, (olengthB, clength))
else
Bpermuted = Array{TC}(uninitialized, (odimsB..., cdims...))
# tensorcopy!(B, 1:NB, Bpermuted, pB)
add!(1, B, Val{:N}, 0, Bpermuted, pB)
Bmat = reshape(Bpermuted, (olengthB, clength))
end
else
conjB = 'N'
pB = (cindB..., oindB...)
if isa(B, Array{TC}) && pB == (1:NB...,)
Bmat = reshape(B, (clength, olengthB))
elseif isa(B, Array{TC}) && (oindB..., cindB...) == (1:NB...,)
conjB = 'T'
Bmat = reshape(B, (olengthB, clength))
else
Bpermuted = Array{TC}(uninitialized, (cdims..., odimsB...))
# tensorcopy!(B, 1:NB, Bpermuted, pB)
add!(1, B, Val{:N}, 0, Bpermuted, pB)
Bmat = reshape(Bpermuted, (clength, olengthB))
end
end
# calculate C
if isa(C, Array) && indCinoAB == (1:NC...,)
Cmat = reshape(C, (olengthA, olengthB))
BLAS.gemm!(conjA, conjB, TC(α), Amat, Bmat, TC(β), Cmat)
else
Cmat = Array{TC}(uninitialized, (olengthA, olengthB))
BLAS.gemm!(conjA, conjB, TC(1), Amat, Bmat, TC(0), Cmat)
add!(α, reshape(Cmat, (odimsA..., odimsB...)), Val{:N}, β, C, indCinoAB)
end
return C
end
function contract!(α, A::StridedArray, ::Type{Val{CA}}, B::StridedArray, ::Type{Val{CB}}, β, C::StridedArray, oindA, cindA, oindB, cindB, indCinoAB, ::Type{Val{:native}}=Val{:native}) where {CA,CB}
NA = ndims(A)
NB = ndims(B)
NC = ndims(C)
# dimension checking
dimA = size(A)
dimB = size(B)
dimC = size(C)
cdimsA = map(i->dimA[i], cindA)
cdimsB = map(i->dimB[i], cindB)
odimsA = map(i->dimA[i], oindA)
odimsB = map(i->dimB[i], oindB)
odimsAB = tuple(odimsA..., odimsB...)
cdimsA == cdimsB || throw(DimensionMismatch())
for i = 1:length(indCinoAB)
dimC[i] == odimsAB[indCinoAB[i]] || throw(DimensionMismatch())
end
# Perform contraction
pA = (oindA..., cindA...)
pB = (oindB..., cindB...)
sA = _permute(strides(A), pA)
sB = _permute(strides(B), pB)
sC = _permute(strides(C), tinvperm(indCinoAB))
dimsA = _permute(size(A), pA)
dimsB = _permute(size(B), pB)
dims, stridesA, stridesB, stridesC, minstrides = contract_strides(dimsA, dimsB, sA, sB, sC)
offsetA = offsetB = offsetC = 0
dataA = StridedData(A, stridesA, Val{CA})
dataB = StridedData(B, stridesB, Val{CB})
dataC = StridedData(C, stridesC)
# contract via recursive divide and conquer
if α == 0
β == 1 || _scale!(dataC, β, dims)
elseif α == 1 && β == 0
contract_rec!(_one, dataA, dataB, _zero, dataC, dims, offsetA, offsetB, offsetC, minstrides)
elseif α == 1 && β == 1
contract_rec!(_one, dataA, dataB, _one, dataC, dims, offsetA, offsetB, offsetC, minstrides)
elseif β == 0
contract_rec!(α, dataA, dataB, _zero, dataC, dims, offsetA, offsetB, offsetC, minstrides)
elseif β == 1
contract_rec!(α, dataA, dataB, _one, dataC, dims, offsetA, offsetB, offsetC, minstrides)
else
contract_rec!(α, dataA, dataB, β, dataC, dims, offsetA, offsetB, offsetC, minstrides)
end
return C
end