|
1 | 1 | using CUDA.CUSPARSE
|
2 | 2 | using LinearAlgebra, SparseArrays
|
3 | 3 |
|
4 |
| -@testset "opnorm and norm $T" for T in [Float32, Float64, ComplexF32, ComplexF64] |
5 |
| - S = sprand(T, 10, 10, 0.1) |
6 |
| - dS_csc = CuSparseMatrixCSC(S) |
7 |
| - dS_csr = CuSparseMatrixCSR(S) |
8 |
| - @test opnorm(S, Inf) ≈ opnorm(dS_csc, Inf) |
9 |
| - @test opnorm(S, Inf) ≈ opnorm(dS_csr, Inf) |
10 |
| - @test opnorm(S, 1) ≈ opnorm(dS_csc, 1) |
11 |
| - @test opnorm(S, 1) ≈ opnorm(dS_csr, 1) |
12 |
| - @test_throws ArgumentError("p=2 is not supported") opnorm(dS_csr, 2) |
13 |
| - @test norm(S, 0) ≈ norm(dS_csc, 0) |
14 |
| - @test norm(S, 0) ≈ norm(dS_csr, 0) |
15 |
| - @test norm(S, 1) ≈ norm(dS_csc, 1) |
16 |
| - @test norm(S, 1) ≈ norm(dS_csr, 1) |
17 |
| - @test norm(S, 2) ≈ norm(dS_csc, 2) |
18 |
| - @test norm(S, 2) ≈ norm(dS_csr, 2) |
19 |
| - @test norm(S, Inf) ≈ norm(dS_csc, Inf) |
20 |
| - @test norm(S, Inf) ≈ norm(dS_csr, Inf) |
21 |
| - @test norm(S, -Inf) ≈ norm(dS_csc, -Inf) |
22 |
| - @test norm(S, -Inf) ≈ norm(dS_csr, -Inf) |
23 |
| -end |
24 |
| - |
25 |
| -@testset "triu tril exp $typ" for |
26 |
| - typ in [CuSparseMatrixCSR, CuSparseMatrixCSC] |
27 |
| - |
28 |
| - a = sprand(ComplexF32, 100, 100, 0.02) |
29 |
| - A = typ(a) |
30 |
| - @test Array(triu(A)) ≈ triu(a) |
31 |
| - @test Array(triu(A, 1)) ≈ triu(a, 1) |
32 |
| - @test Array(tril(A)) ≈ tril(a) |
33 |
| - @test Array(tril(A, 1)) ≈ tril(a, 1) |
34 |
| - if CUSPARSE.version() > v"11.4.1" |
35 |
| - @test Array(exp(A)) ≈ exp(collect(a)) |
| 4 | +m = 10 |
| 5 | +@testset "T = $T" for T in [Float32, Float64, ComplexF32, ComplexF64] |
| 6 | + A = sprand(T, m, m, 0.2) |
| 7 | + B = sprand(T, m, m, 0.3) |
| 8 | + ZA = spzeros(T, m, m) |
| 9 | + C = I(div(m, 2)) |
| 10 | + @testset "type = $typ" for typ in [CuSparseMatrixCSR, CuSparseMatrixCSC] |
| 11 | + dA = typ(A) |
| 12 | + dB = typ(B) |
| 13 | + dZA = typ(ZA) |
| 14 | + @testset "opnorm and norm" begin |
| 15 | + @test opnorm(A, Inf) ≈ opnorm(dA, Inf) |
| 16 | + @test opnorm(A, 1) ≈ opnorm(dA, 1) |
| 17 | + @test_throws ArgumentError("p=2 is not supported") opnorm(dA, 2) |
| 18 | + @test norm(A, 0) ≈ norm(dA, 0) |
| 19 | + @test norm(A, 1) ≈ norm(dA, 1) |
| 20 | + @test norm(A, 2) ≈ norm(dA, 2) |
| 21 | + @test norm(A, Inf) ≈ norm(dA, Inf) |
| 22 | + @test norm(A, -Inf) ≈ norm(dA, -Inf) |
| 23 | + end |
| 24 | + @testset "triu tril exp" begin |
| 25 | + @test Array(triu(dA)) ≈ triu(A) |
| 26 | + @test Array(triu(dA, 1)) ≈ triu(A, 1) |
| 27 | + @test Array(tril(dA)) ≈ tril(A) |
| 28 | + @test Array(tril(dA, 1)) ≈ tril(A, 1) |
| 29 | + if CUSPARSE.version() > v"11.4.1" |
| 30 | + @test Array(exp(dA)) ≈ exp(collect(A)) |
| 31 | + end |
| 32 | + end |
| 33 | + @testset "kronecker product opa = $opa, opb = $opb" for opa in (identity, transpose, adjoint), opb in (identity, transpose, adjoint) |
| 34 | + if !(opa == transpose && opb == adjoint) && !(opa == adjoint && opb == transpose) |
| 35 | + @test collect(kron(opa(dA), opb(dB))) ≈ kron(opa(A), opb(B)) |
| 36 | + @test collect(kron(opa(dZA), opb(dB))) ≈ kron(opa(ZA), opb(B)) |
| 37 | + end |
| 38 | + end |
| 39 | + @testset "kronecker product with I opa = $opa" for opa in (identity, transpose, adjoint) |
| 40 | + @test collect(kron(opa(dA), C)) ≈ kron(opa(A), C) |
| 41 | + @test collect(kron(C, opa(dA))) ≈ kron(C, opa(A)) |
| 42 | + @test collect(kron(opa(dZA), C)) ≈ kron(opa(ZA), C) |
| 43 | + @test collect(kron(C, opa(dZA))) ≈ kron(C, opa(ZA)) |
| 44 | + end |
36 | 45 | end
|
37 | 46 | end
|
38 | 47 |
|
39 |
| -@testset "$typ kronecker product" for |
40 |
| - typ in [CuSparseMatrixCSR, CuSparseMatrixCSC] |
41 |
| - |
42 |
| - a = sprand(ComplexF32, 100, 100, 0.1) |
43 |
| - b = sprand(ComplexF32, 100, 100, 0.1) |
44 |
| - A = typ(a) |
45 |
| - B = typ(b) |
46 |
| - za = spzeros(ComplexF32, 100, 100) |
47 |
| - ZA = typ(za) |
48 |
| - @test collect(kron(A, B)) ≈ kron(a, b) |
49 |
| - @test collect(kron(transpose(A), B)) ≈ kron(transpose(a), b) |
50 |
| - @test collect(kron(A, transpose(B))) ≈ kron(a, transpose(b)) |
51 |
| - @test collect(kron(transpose(A), transpose(B))) ≈ kron(transpose(a), transpose(b)) |
52 |
| - @test collect(kron(A', B)) ≈ kron(a', b) |
53 |
| - @test collect(kron(A, B')) ≈ kron(a, b') |
54 |
| - @test collect(kron(A', B')) ≈ kron(a', b') |
55 |
| - |
56 |
| - @test collect(kron(ZA, B)) ≈ kron(za, b) |
57 |
| - @test collect(kron(transpose(ZA), B)) ≈ kron(transpose(za), b) |
58 |
| - @test collect(kron(ZA, transpose(B))) ≈ kron(za, transpose(b)) |
59 |
| - @test collect(kron(transpose(ZA), transpose(B))) ≈ kron(transpose(za), transpose(b)) |
60 |
| - @test collect(kron(ZA', B)) ≈ kron(za', b) |
61 |
| - @test collect(kron(ZA, B')) ≈ kron(za, b') |
62 |
| - @test collect(kron(ZA', B')) ≈ kron(za', b') |
63 |
| - |
64 |
| - C = I(50) |
65 |
| - @test collect(kron(A, C)) ≈ kron(a, C) |
66 |
| - @test collect(kron(C, A)) ≈ kron(C, a) |
67 |
| - @test collect(kron(transpose(A), C)) ≈ kron(transpose(a), C) |
68 |
| - @test collect(kron(C, transpose(A))) ≈ kron(C, transpose(a)) |
69 |
| - @test collect(kron(adjoint(A), C)) ≈ kron(adjoint(a), C) |
70 |
| - @test collect(kron(C, adjoint(A))) ≈ kron(C, adjoint(a)) |
71 |
| - @test collect(kron(A', C)) ≈ kron(a', C) |
72 |
| - @test collect(kron(C, A')) ≈ kron(C, a') |
73 |
| - |
74 |
| - @test collect(kron(ZA, C)) ≈ kron(za, C) |
75 |
| - @test collect(kron(C, ZA)) ≈ kron(C, za) |
76 |
| - @test collect(kron(transpose(ZA), C)) ≈ kron(transpose(za), C) |
77 |
| - @test collect(kron(C, transpose(ZA))) ≈ kron(C, transpose(za)) |
78 |
| - @test collect(kron(adjoint(ZA), C)) ≈ kron(adjoint(za), C) |
79 |
| - @test collect(kron(C, adjoint(ZA))) ≈ kron(C, adjoint(za)) |
80 |
| - @test collect(kron(ZA', C)) ≈ kron(za', C) |
81 |
| - @test collect(kron(C, ZA')) ≈ kron(C, za') |
82 |
| -end |
83 |
| - |
84 | 48 | @testset "Reshape $typ (100,100) -> (20, 500) and droptol" for
|
85 | 49 | typ in [CuSparseMatrixCSR, CuSparseMatrixCSC]
|
86 | 50 |
|
87 |
| - a = sprand(ComplexF32, 100, 100, 0.1) |
88 |
| - dims = (20, 500) |
| 51 | + a = sprand(ComplexF32, 10, 10, 0.1) |
| 52 | + dims = (20, 5) |
89 | 53 | A = typ(a)
|
90 | 54 | @test Array(reshape(A, dims)) ≈ reshape(a, dims)
|
91 | 55 | droptol!(a, 0.4)
|
|
96 | 60 | @testset "Generalized dot product for $typ and $elty" for
|
97 | 61 | typ in [CuSparseMatrixCSR, CuSparseMatrixCSC], elty in [Int64, Float32, Float64, ComplexF64]
|
98 | 62 |
|
99 |
| - N1 = 1000*2 |
100 |
| - N2 = 1000*3 |
| 63 | + N1 = 100*2 |
| 64 | + N2 = 100*3 |
101 | 65 | x = rand(elty, N1)
|
102 | 66 | y = rand(elty, N2)
|
103 | 67 | A = sprand(elty, N1, N2, 1/N1)
|
|
0 commit comments