-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_blas.t
337 lines (279 loc) · 9.15 KB
/
test_blas.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
-- SPDX-FileCopyrightText: 2024 René Hiemstra <[email protected]>
-- SPDX-FileCopyrightText: 2024 Torsten Keßler <[email protected]>
--
-- SPDX-License-Identifier: MIT
local blas = require("blas")
local concepts = require("concepts")
local complex = require("complex")
local C = terralib.includecstring[[
#include <stdio.h>
]]
local mathfun = require("mathfuns")
local complexFloat = complex.complex(float)
local complexDouble = complex.complex(double)
local types = {
["s"] = float,
["d"] = double,
["c"] = complexFloat,
["z"] = complexDouble}
local unit = {
["s"] = `float(0),
["d"] = `double(0),
["c"] = `complexFloat.I(),
["z"] = `complexDouble.I()}
import "terratest/terratest"
for prefix, T in pairs(types) do
local I = unit[prefix]
testenv(T) "BLAS level 1" do
testset "swap scalar" do
terracode
var x = T(1)
var y = T(2)
blas.swap(1, &x, 1, &y, 1)
end
test x == T(2)
test y == T(1)
end -- testset
testset "swap vectors" do
local n = 4
local incx = 3
local incy = 2
terracode
var x: T[n * incx]
var y: T[n * incy]
var xold: T[n * incx]
var yold: T[n * incy]
for i = 0, n do
x[i * incx] = T(i) + I * T(n - i)
xold[i * incx] = x[i * incx]
y[i * incy] = T(n - i) + I * T(i)
yold[i * incy] = y[i * incy]
end
blas.swap(n, &x[0], incx, &y[0], incy)
end
for i = 0, n - 1 do
test x[i * incx] == yold[i * incy]
test y[i * incy] == xold[i * incx]
end
end -- testset swap
testset "scal scalar" do
terracode
var x = T(2)
var xold = x
var a = T(6)
blas.scal(1, a, &x, 1)
end
test x == xold * a
end --testset scal
testset "scal vector" do
local n = 4
local incx = 3
terracode
var x: T[n * incx]
var xold: T[n * incx]
var a = T(5) + I * T(2)
for i = 0, n do
x[i * incx] = T(i) + I * T(i * i)
xold[i * incx] = x[i * incx]
end
blas.scal(n, a, &x[0], incx)
end
for i = 0, n - 1 do
test x[i * incx] == a * xold[i * incx]
end
end -- testset scal
testset "copy scalar" do
terracode
var x = T(3)
var y = T(1)
blas.copy(1, &x, 1, &y, 1)
end
test x == y
end -- testset copy
testset "copy vector" do
local n = 4
local incx = 3
local incy = 2
terracode
var x: T[n * incx]
var y: T[n * incy]
for i = 0, n do
x[i * incx] = T(i) + I * T(n * n - i + 1)
end
blas.copy(n, &x[0], incx, &y[0], incy)
end
for i = 0, n - 1 do
test y[i * incy] == x[i * incx]
end
end -- testset copy
testset "axpy scalar" do
terracode
var a = T(2)
var x = T(2)
var y = T(3)
var yold = y
blas.axpy(1, a, &x, 1, &y, 1)
end
test y == a * x + yold
end -- testset axpy
testset "axpy vectors" do
local n = 4
local incx = 3
local incy = 2
terracode
var a = T(2) + I * T(-3)
var x: T[n * incx]
var y: T[n * incy]
var yold: T[n * incy]
for i = 0, n do
x[i * incx] = T(i) + I * T(-2 * n - i)
y[i * incy] = T(n - i) + I * T(3 * i)
yold[i * incy] = y[i * incy]
end
blas.axpy(n, a, &x[0], incx, &y[0], incy)
end
for i = 0, n - 1 do
test y[i * incy] == a * x[i * incx] + yold[i * incy]
end
end -- testenv axpy
testset "dot vectors" do
local n = 4
local incx = 3
local incy = 2
terracode
var x: T[n * incx]
var xconj: T[n * incx]
var y: T[n * incy]
for i = 0, n do
x[i * incx] = T(i) + I * T(i * i - n)
xconj[i * incx] = T(i) - I * T(i * i - n)
y[i * incy] = T(n - i) + I * T(-n - i)
end
var num = blas.dot(n, &x[0], incx, &y[0], incy)
var ref = T(0)
for i = 0, n do
ref = ref + xconj[i * incx] * y[i * incy]
end
end
test ref == num
end --testset dot
testset "norm scalar" do
local nrm = 3
if not T:isfloat() then
nrm = nrm + 2
end
terracode
var x = 3 + 4 * I
var xconj = 3 - 4 * I
var num = blas.nrm2(1, &x, 1)
end
test num == nrm
end --testset norm
testset "norm vectors" do
local n = 4
local incx = 3
local Ts = concepts.Complex(T) and T.traits.eltype or T
terracode
var x: T[n * incx]
var xre: Ts[n * incx]
var xim: Ts[n * incx]
for i = 0, n do
xre[i * incx] = i
xim[i * incx] = n - i
x[i * incx] = xre[i * incx] + I * xim[i * incx]
end
var num: Ts = blas.nrm2(n, &x[0], incx)
var ref: Ts = 0
for i = 0, n do
ref = ref + xre[i * incx] * xre[i * incx]
end
escape
if concepts.Complex(T) then
emit quote
for i = 0, n do
ref = ref + xim[i * incx] * xim[i * incx]
end
end --quote
end --if
end --escape
ref = mathfun.sqrt(ref)
end
test num == ref
end
testset "L1 norm" do
local n = 5
local incx = 2
terracode
var x: T[n * incx]
for i = 0, n do
x[incx * i] = terralib.select(i % 2 == 0, T(i), T(-i))
end
var num = blas.asum(n, x, incx)
var ref = n * (n - 1) / 2
end
test num == ref
end
testset "L infinity norm" do
local n = 5
local incx = 2
terracode
var x: T[n * incx]
for i = 0, n do
x[incx * i] = terralib.select(i % 2 == 0, T(i), T(-i))
end
var num = blas.iamax(n, x, incx)
var ref = n - 1
end
test num == ref
end
end -- testenv BLAS level 1
testenv(T) "BLAS level 2" do
testset "matrix-vector multiplication" do
terracode
var a = arrayof(T, 1, 2, 3, 4)
var x = arrayof(T, 1, 1)
var y: T[2]
var yref = arrayof(T, 3, 7)
var alpha = T(1)
var beta = T(0)
blas.gemv(blas.RowMajor, blas.NoTrans, 2, 2, alpha, &a[0], 2,
&x[0], 1, beta, &y[0], 1)
end
for i = 0, 1 do
test y[i] == yref[i]
end
end
end
testenv(T) "BLAS level 3" do
testset "triangular solve" do
terracode
var a = arrayof(T, 1, 2, 0, 3)
var x = arrayof(T, 1, 2)
var y = arrayof(T, 0, 0)
var alpha = T(1)
var beta = T(0)
blas.gemv(blas.RowMajor, blas.NoTrans, 2, 2, alpha, &a[0], 2,
&x[0], 1, beta, &y[0], 1)
blas.trsv(blas.RowMajor, blas.Upper, blas.NoTrans, blas.NonUnit,
2, &a[0], 2, &y[0], 1)
end
for i = 0, 1 do
test y[i] == x[i]
end
end
testset "matrix-matrix multiplication" do
terracode
var a = arrayof(T, 1, 2, 3, 4)
var b = arrayof(T, 0, 0, 0, 0)
var bref = arrayof(T, 5, 11, 11, 25)
var alpha = T(1)
var beta = T(0)
blas.gemm(blas.RowMajor, blas.NoTrans, blas.Trans, 2, 2, 2, alpha,
&a[0], 2, &a[0], 2, beta, &b[0], 2)
end
for i = 0, 3 do
test b[i] == bref[i]
end
end
end
end -- for