-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsparse_demo_test.go
329 lines (302 loc) · 6.45 KB
/
csparse_demo_test.go
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
package sparse
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"math"
"os/exec"
"path/filepath"
"testing"
"time"
)
type problem struct {
A *Matrix
C *Matrix
sym int
x []float64
b []float64
resid []float64
}
func print_problem(P *problem, tmpfile io.Writer) {
fmt.Fprintf(tmpfile, "Matrix A:\n")
P.A.Print(tmpfile, false)
fmt.Fprintf(tmpfile, "Matrix C:\n")
P.C.Print(tmpfile, false)
fmt.Fprintf(tmpfile, "sym = %v\n", P.sym)
fmt.Fprintf(tmpfile, "Vector x\n")
for i := 0; i < P.A.n; i++ {
fmt.Fprintf(tmpfile, "x[%d] = %f\n", i, P.x[i])
}
for i := 0; i < P.A.n; i++ {
fmt.Fprintf(tmpfile, "b[%d] = %f\n", i, P.b[i])
}
for i := 0; i < P.A.n; i++ {
fmt.Fprintf(tmpfile, "resid[%d] = %f\n", i, P.resid[i])
}
}
// norm - infinity-norm of x
func norm(x []float64, n int) float64 {
var normx float64
for i := 0; i < n; i++ {
normx = func() float64 {
if normx > math.Abs(x[i]) {
return normx
}
return math.Abs(x[i])
}()
}
return normx
}
// rhs - create a right-hand side
func rhs(x []float64, b []float64, m int) {
for i := 0; i < m; i++ {
b[i] = 1 + float64(i)/float64(m)
}
for i := 0; i < m; i++ {
x[i] = b[i]
}
}
// make_sym - C = A + triu(A,1)'
func make_sym(A *Matrix) *Matrix {
// AT = A'
AT, err := Transpose(A)
if err != nil {
return nil
}
// drop diagonal entries from AT
Fkeep(AT, func(i int, j int, aij float64) bool {
// dropdiag - true for off-diagonal entries
return (i != j)
})
// C = A+AT
C, err := Add(A, AT, 1, 1)
if err != nil {
panic(err)
}
cs_free(AT)
return (C)
}
// print_resid - compute residual, norm(A*x-b,inf) / (norm(A,1)*norm(x,inf) + norm(b,inf))
func print_resid(ok bool, A *Matrix, x []float64, b []float64, resid []float64, tmpfile io.Writer) {
if !ok {
fmt.Fprintf(tmpfile, " (failed)\n")
return
}
m := A.m
n := A.n
// resid = -b
for i := 0; i < m; i++ {
resid[i] = -b[i]
}
// resid = resid + A*x
Gaxpy(A, x, resid)
// fmt.Fprintf(tmpfile,"resid: %8.2e\n", norm(resid, m)/func() float64 {
// if n == 0 {
// return 1
// }
// return cs_norm(A)*norm(x, n) + norm(b, m)
// }())
_ = n
fmt.Fprintf(tmpfile, "\n")
}
// print_order -
func print_order(order int, output bool, tmpfile io.Writer) {
if !output {
return
}
switch order {
case 0:
fmt.Fprintf(tmpfile, "natural ")
case 1:
fmt.Fprintf(tmpfile, "amd(A+A') ")
case 2:
fmt.Fprintf(tmpfile, "amd(S'*S) ")
case 3:
fmt.Fprintf(tmpfile, "amd(A'*A) ")
}
}
// cs_dropzeros
func cs_dropzeros(A *Matrix) (int, error) {
// keep all nonzero entries
return Fkeep(A, func(i, j int, aij float64) bool {
// cs_nonzero
return aij != 0
})
}
// get_problem - read a problem from a file; use %g for integers to avoid csi conflicts */
func get_problem(f io.Reader, tol float64, output bool, tmpfile io.Writer) *problem {
var nz1 int
var nz2 int
Prob := new(problem)
if Prob == nil {
return nil
}
// load triplet matrix T from a file */
T, err := Load(f)
if err != nil {
panic(err)
}
A, err := Compress(T)
if err != nil {
panic(err)
}
// A = compressed-column form of T */
Prob.A = A
// clear T */
cs_free(T)
if err := Dupl(A); err != nil {
// sum up duplicates */
return nil
}
// determine if A is symmetric */
sym := is_sym(A)
Prob.sym = sym
m := A.m
n := A.n
mn := n
if m > n {
mn = m
}
nz1 = A.p[n]
// drop zero entries */
cs_dropzeros(A)
nz2 = A.p[n]
if output {
fmt.Fprintf(tmpfile, "n = %d\n", n)
fmt.Fprintf(tmpfile, "nz1 = %d\n", nz1)
fmt.Fprintf(tmpfile, "nz2 = %d\n", nz2)
fmt.Fprintf(tmpfile, "A->p[%d] = %d\n", n, A.p[n])
fmt.Fprintf(tmpfile, "A before drop\n")
A.Print(tmpfile, false)
fmt.Fprintf(tmpfile, "tol = %.5e\n", tol)
}
if tol > 0 {
// drop tiny entries (just to test) */
ok, err := cs_droptol(A, tol)
if err != nil {
panic(err)
}
if output {
fmt.Fprintf(tmpfile, "droptol = %d\n", ok)
}
}
if output {
fmt.Fprintf(tmpfile, "A before make_sym\n")
A.Print(tmpfile, false)
}
// C = A + triu(A,1)', or C=A */
C := func() *Matrix {
if sym != 0 {
return make_sym(A)
}
return A
}()
Prob.C = C
if C == nil {
return nil
}
if output {
fmt.Fprintf(tmpfile, "\n--- Matrix: %g-by-%g, nnz: %g (sym: %g: nnz %g), norm: %8.2e\n",
float64((m)),
float64((n)),
float64((A.p[n])),
float64((sym)),
float64((func() int {
if sym != 0 {
return C.p[n]
}
return 0
}())),
0.0)
// cs_norm(C))
if nz1 != nz2 {
fmt.Fprintf(tmpfile, "zero entries dropped: %g\n", float64(nz1-nz2))
}
A.Print(tmpfile, false)
fmt.Fprintf(tmpfile, "nz2 = %d\n", nz2)
fmt.Fprintf(tmpfile, "A->p[%d] = %d\n", n, A.p[n])
if nz2 != A.p[n] {
fmt.Fprintf(tmpfile, "tiny entries dropped: %g\n", float64((int32(nz2 - A.p[n]))))
}
}
Prob.b = make([]float64, mn)
Prob.x = make([]float64, mn)
Prob.resid = make([]float64, mn)
return (func() *problem {
if Prob.b == nil || Prob.x == nil || Prob.resid == nil {
return nil
}
return Prob
}())
}
// tic -
func tic() float64 {
return 0
}
// toc -
func toc(t float64) float64 {
return 0
}
func buildC(t *testing.T, filename string, output bool) {
// CSparse C source
csFiles, err := filepath.Glob("CSparse/Source/" + "*.c")
if err != nil {
t.Fatal(err)
}
// build testdata application :
//
// clang -ICSparse/Include/ \
// ./CSparse/Source/*.c \
// ./testdata/csparse_test.c \
// -lm \
// -o \
// ./testdata/csparse_test
var args []string
args = append(args, "-ICSparse/Include/")
args = append(args, csFiles...)
args = append(args, filename)
args = append(args, "-lm")
if output {
args = append(args, "-DPRINT")
}
args = append(args, "-o")
args = append(args, "testdata/csparse_test")
cmd := exec.Command("clang", args...)
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err = cmd.Run()
if err != nil {
t.Fatalf("cmd.Run() failed with %s.\n%s\n%s\n",
err,
stderr.String(),
stdout.String(),
)
}
}
func getCresult(t *testing.T, matrix string) (in []byte, out string, dur time.Duration) {
cmd := exec.Command(
"./testdata/csparse_test",
)
var stdin, stdout, stderr bytes.Buffer
b, err := ioutil.ReadFile(matrix)
if err != nil {
t.Fatal(err)
}
stdin.Write(b)
cmd.Stdin = &stdin
cmd.Stdout = &stdout
cmd.Stderr = &stderr
start := time.Now()
err = cmd.Run()
end := time.Now()
if err != nil {
t.Fatalf("cmd.Run() failed with %s.\n%s\n%s\n",
err,
stderr.String(),
stdout.String(),
)
}
return b, stdout.String(), end.Sub(start)
}