-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmatrix.t
316 lines (283 loc) · 11.1 KB
/
matrix.t
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
-- SPDX-FileCopyrightText: 2024 René Hiemstra <[email protected]>
-- SPDX-FileCopyrightText: 2024 Torsten Keßler <[email protected]>
--
-- SPDX-License-Identifier: MIT
import "terraform"
local concepts = require("concepts")
local blas = require("blas")
local err = require("assert")
local vecblas = require("vector_blas")
local Integer = concepts.Integer
local Real = concepts.Real
local Number = concepts.Number
local Stack = concepts.Stack
local Matrix = concepts.Matrix
local Transpose = concepts.Transpose
local BLASFloat = concepts.BLASFloat
local BLASComplexFloat = concepts.ComplexOverField(BLASFloat)
local BLASNumber = concepts.BLASNumber
local BLASVector = concepts.BLASVector
local BLASMatrix = concepts.BLASMatrix
local BLASMatrixReal = BLASMatrix(BLASFloat)
local BLASMatrixComplex = BLASMatrix(BLASComplexFloat)
local MatrixBase = function(matrix)
local T = matrix.traits.eltype
local Concept = {
Stack = concepts.Stack(T),
Vector = concepts.Vector(T),
Matrix = concepts.Matrix(T)
}
terra matrix:fill(a : T)
for i = 0, self:rows() do
for j = 0, self:cols() do
self:set(i, j, a)
end
end
end
terra matrix:clear()
self:fill(0)
end
terraform matrix:copy(trans : bool, other : &M) where {M : Concept.Matrix}
var ns = self:rows()
var ms = self:cols()
var no = other:rows()
var mo = other:cols()
if trans then
err.assert(ns == mo and ms == no)
for i = 0, ns do
for j = 0, ms do
self:set(i, j, other:get(j, i))
end
end
else
err.assert(ns == no and ms == mo)
for i = 0, ns do
for j = 0, ms do
self:set(i, j, other:get(i, j))
end
end
end
end
terraform matrix:swap(trans : bool, other : &M) where {M : Concept.Matrix}
var ns = self:rows()
var ms = self:cols()
var no = other:rows()
var mo = other:cols()
if trans then
err.assert(ns == mo and ms == no)
for i = 0, ns do
for j = 0, ms do
var s = self:get(i, j)
var o = other:get(j, i)
self:set(i, j, o)
other:set(j, i, s)
end
end
else
err.assert(ns == no and ms == mo)
for i = 0, ns do
for j = 0, ms do
var s = self:get(i, j)
var o = other:get(i, j)
self:set(i, j, o)
other:set(i, j, s)
end
end
end
end
terra matrix:scal(a : T)
var ns = self:rows()
var ms = self:cols()
for i = 0, ns do
for j = 0, ms do
self:set(i, j, a * self:get(i, j))
end
end
end
terraform matrix:axpy(a : T, trans : bool, other : &M) where {M : Concept.Matrix}
var ns = self:rows()
var ms = self:cols()
var no = other:rows()
var mo = other:cols()
if trans then
err.assert(ns == mo and ms == no)
for i = 0, ns do
for j = 0, ms do
self:set(i, j, self:get(i, j) + a * other:get(j, i))
end
end
else
err.assert(ns == no and ms == mo)
for i = 0, ns do
for j = 0, ms do
self:set(i, j, self:get(i, j) + a * other:get(i, j))
end
end
end
end
terraform matrix:dot(trans : bool, other : &M) where {M : Concept.Matrix}
var ns = self:rows()
var ms = self:cols()
var no = other:rows()
var mo = other:cols()
if trans then
err.assert(ns == mo and ms == no)
var sum = self:get(0, 0) * other:get(0, 0)
for i = 0, ns do
for j = 0, ms do
if i > 0 or j > 0 then
sum = sum + self:get(i, j) * other:get(j, i)
end
end
end
return sum
else
err.assert(ns == no and ms == mo)
var sum = self:get(0, 0) * other:get(0, 0)
for i = 0, ns do
for j = 0, ms do
if i > 0 or j > 0 then
sum = sum + self:get(i, j) * other:get(i, j)
end
end
end
return sum
end
end
assert(Concept.Matrix(matrix))
end
--gemv - naive falback implementation
--compute y[i] = alpha * A[i,j] * x[j] + beta * y[i]
local terraform gemv(alpha : T, A : &M, x : &V1, beta : T, y : &V2)
where {T : Number, M : Matrix(Number), V1 : Stack(Number), V2 : Stack(Number)}
var ns = A:rows()
var ms = A:cols()
var nx = x:length()
var ny = y:length()
err.assert(ns == ny and ms == nx)
for i = 0, ns do
var res = T(0)
for j = 0, ms do
res = res + A:get(i, j) * x:get(j)
end
y:set(i, beta * y:get(i) + alpha * res)
end
end
--gemv - blas wrappers
local gemvsetup = macro(function(self, x, y)
return quote
var nx, xptr, incx = x:getblasinfo()
var ny, yptr, incy = y:getblasinfo()
var rows, cols, aptr, ld = self:getblasdenseinfo()
err.assert(rows == ny and cols == nx)
in
xptr, incx, yptr, incy, rows, cols, aptr, ld
end
end)
--compute y[i] = alpha * A[i,j] * x[j] + beta * y[i]
terraform gemv(alpha : T, A : &M, x : &V1, beta : T, y : &V2)
where {T : BLASNumber, M : BLASMatrix(BLASNumber), V1 : BLASVector(BLASNumber), V2 : BLASVector(BLASNumber)}
var xptr, incx, yptr, incy, rows, cols, aptr, ld = gemvsetup(A, x, y)
blas.gemv(blas.RowMajor, blas.NoTrans, rows, cols, alpha, aptr, ld, xptr, incx, beta, yptr, incy)
end
terraform gemv(alpha : T, A : &M, x : &V1, beta : T, y : &V2)
where {T : BLASFloat, M : Transpose(BLASMatrixReal), V1 : BLASVector(BLASFloat), V2 : BLASVector(BLASFloat)}
var xptr, incx, yptr, incy, rows, cols, aptr, ld = gemvsetup(A, x, y)
blas.gemv(blas.RowMajor, blas.Trans, cols, rows, alpha, aptr, ld, xptr, incx, beta, yptr, incy)
end
terraform gemv(alpha : T, A : &M, x : &V1, beta : T, y : &V2)
where {T : BLASComplexFloat, M : Transpose(BLASMatrixComplex), V1 : BLASVector(BLASComplexFloat), V2 : BLASVector(BLASComplexFloat)}
var xptr, incx, yptr, incy, rows, cols, aptr, ld = gemvsetup(A, x, y)
blas.gemv(blas.RowMajor, blas.ConjTrans, cols, rows, alpha, aptr, ld, xptr, incx, beta, yptr, incy)
end
--gemm - naive falback implementation
--C[i,j] = alpha * A[i,k] * B[k,j] + beta * C[i,j]
local terraform gemm(alpha : T, A : &M1, B : &M2, beta : T, C : &M3)
where {T : Number, M1 : Matrix(Number), M2 : Matrix(Number), M3 : Matrix(Number)}
err.assert(A:cols() == B:rows(), "ArgumentError: matrix dimensions in C = alpha*C + beta * A * B are not consistent.")
err.assert(C:rows() == A:rows() and C:cols() == B:cols(), "ArgumentError: matrix dimensions in C = alpha*C + beta * A * B are not consistent.")
for i = 0, C:rows() do
for j = 0, C:cols() do
var sum = beta * C:get(i, j)
for k = 0, A:cols() do
sum = sum + alpha * A:get(i, k) * B:get(k, j)
end
C:set(i, j, sum)
end
end
end
--gemm - blas wrappers
local gemmsetup = macro(function(A, B, C)
return quote
var na, ma, ptra, lda = A:getblasdenseinfo()
var nb, mb, ptrb, ldb = B:getblasdenseinfo()
var nc, mc, ptrc, ldc = C:getblasdenseinfo()
err.assert(nc == na)
err.assert(mc == mb)
err.assert(ma == nb)
var m : uint64 = nc
var n : uint64 = mc
var k : uint64 = ma
in
n, m, k, ptra, lda, ptrb, ldb, ptrc, ldc
end
end)
--C[i,j] = alpha * A[i,k] * B[k,j] + beta * C[i,j]
terraform gemm(alpha : T, A : &M1, B : &M2, beta : T, C : &M3)
where {T : Real, M1 : BLASMatrix(BLASFloat), M2 : BLASMatrix(BLASFloat), M3 : BLASMatrix(BLASFloat)}
var n, m, k, ptra, lda, ptrb, ldb, ptrc, ldc = gemmsetup(A, B, C)
blas.gemm(blas.RowMajor, blas.NoTrans, blas.NoTrans,
n, m, k, alpha, ptra, lda, ptrb, ldb, beta, ptrc, ldc)
end
--C[i,j] = alpha * A[i,k] * B[k,j] + beta * C[i,j]
terraform gemm(alpha : T, A : &M1, B : &M2, beta : T, C : &M3)
where {T : Real, M1 : Transpose(BLASMatrixReal), M2 : BLASMatrixReal, M3 : BLASMatrixReal}
var n, m, k, ptra, lda, ptrb, ldb, ptrc, ldc = gemmsetup(A, B, C)
blas.gemm(blas.RowMajor, blas.Trans, blas.NoTrans,
n, m, k, alpha, ptra, lda, ptrb, ldb, beta, ptrc, ldc)
end
--C[i,j] = alpha * A[i,k] * B[k,j] + beta * C[i,j]
terraform gemm(alpha : T, A : &M1, B : &M2, beta : T, C : &M3)
where {T : Real, M1 : BLASMatrixReal, M2 : Transpose(BLASMatrixReal), M3 : BLASMatrixReal}
var n, m, k, ptra, lda, ptrb, ldb, ptrc, ldc = gemmsetup(A, B, C)
blas.gemm(blas.RowMajor, blas.NoTrans, blas.Trans,
n, m, k, alpha, ptra, lda, ptrb, ldb, beta, ptrc, ldc)
end
--C[i,j] = alpha * A[i,k] * B[k,j] + beta * C[i,j]
terraform gemm(alpha : T, A : &M1, B : &M2, beta : T, C : &M3)
where {T : Real, M1 : Transpose(BLASMatrixReal), M2 : Transpose(BLASMatrixReal), M3 : BLASMatrixReal}
var n, m, k, ptra, lda, ptrb, ldb, ptrc, ldc = gemmsetup(A, B, C)
blas.gemm(blas.RowMajor, blas.Trans, blas.Trans,
n, m, k, alpha, ptra, lda, ptrb, ldb, beta, ptrc, ldc)
end
--C[i,j] = alpha * A[i,k] * B[k,j] + beta * C[i,j]
terraform gemm(alpha : T, A : &M1, B : &M2, beta : T, C : &M3)
where {T : Number, M1 : BLASMatrixComplex, M2 : BLASMatrixComplex, M3 : BLASMatrixComplex}
var n, m, k, ptra, lda, ptrb, ldb, ptrc, ldc = gemmsetup(A, B, C)
blas.gemm(blas.RowMajor, blas.NoTrans, blas.NoTrans,
n, m, k, alpha, ptra, lda, ptrb, ldb, beta, ptrc, ldc)
end
--C[i,j] = alpha * A[i,k] * B[k,j] + beta * C[i,j]
terraform gemm(alpha : T, A : &M1, B : &M2, beta : T, C : &M3)
where {T : Number, M1 : Transpose(BLASMatrixComplex), M2 : BLASMatrixComplex, M3 : BLASMatrixComplex}
var n, m, k, ptra, lda, ptrb, ldb, ptrc, ldc = gemmsetup(A, B, C)
blas.gemm(blas.RowMajor, blas.ConjTrans, blas.NoTrans,
n, m, k, alpha, ptra, lda, ptrb, ldb, beta, ptrc, ldc)
end
terraform gemm(alpha : T, A : &M1, B : &M2, beta : T, C : &M3)
where {T : Number, M1 : BLASMatrixComplex, M2 : Transpose(BLASMatrixComplex), M3 : BLASMatrixComplex}
var n, m, k, ptra, lda, ptrb, ldb, ptrc, ldc = gemmsetup(A, B, C)
blas.gemm(blas.RowMajor, blas.NoTrans, blas.ConjTrans,
n, m, k, alpha, ptra, lda, ptrb, ldb, beta, ptrc, ldc)
end
terraform gemm(alpha : T, A : &M1, B : &M2, beta : T, C : &M3)
where {T : Number, M1 : Transpose(BLASMatrixComplex), M2 : Transpose(BLASMatrixComplex), M3 : BLASMatrixComplex}
var n, m, k, ptra, lda, ptrb, ldb, ptrc, ldc = gemmsetup(A, B, C)
blas.gemm(blas.RowMajor, blas.ConjTrans, blas.ConjTrans,
n, m, k, alpha, ptra, lda, ptrb, ldb, beta, ptrc, ldc)
end
return {
MatrixBase = MatrixBase,
gemv = gemv,
gemm = gemm
}