File: run_tests.py

package info (click to toggle)
magma 2.9.0%2Bds-2
  • links: PTS, VCS
  • area: contrib
  • in suites: trixie
  • size: 83,212 kB
  • sloc: cpp: 709,115; fortran: 121,916; ansic: 32,343; python: 25,603; f90: 15,208; makefile: 942; xml: 253; csh: 232; sh: 203; perl: 104
file content (835 lines) | stat: -rwxr-xr-x 29,152 bytes parent folder | download | duplicates (3)
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
#!/usr/bin/env python3
#
# MAGMA (version 2.9.0) --
# Univ. of Tennessee, Knoxville
# Univ. of California, Berkeley
# Univ. of Colorado, Denver
# @date January 2025

## @file
#  @author Mark Gates
#  @author Hartwig Anzt
#
# Script to run testers with various matrix sizes.
# Small sizes are chosen around block sizes (e.g., 30...34 around 32) to
# detect bugs that occur at the block size, and the switch over from
# LAPACK to MAGMA code.
# Tall and wide sizes are chosen to exercise different aspect ratios,
# e.g., nearly square, 2:1, 10:1, 1:2, 1:10.
# The -h or --help option provides a summary of the options.
#
# Batch vs. interactive mode
# --------------------------
# When output is redirected to a file, it runs in batch mode, printing a
# short summary to stderr on the console and all other output to the file.
# For example:
#
#       ./run_tests.py --lu --precision s --small > lu.txt
#       testing_sgesv_gpu -c                      ok
#       testing_sgetrf_gpu -c2                    ok
#       testing_sgetf2_gpu -c                     ok
#       testing_sgetri_gpu -c                     ** 45 tests failed
#       testing_sgetrf_mgpu -c2                   ok
#       testing_sgesv -c                          ok
#       testing_sgetrf -c2                        ok
#
#       ****************************************************************************************************
#       summary
#       ****************************************************************************************************
#         282 tests in 7 commands passed
#          45 tests failed accuracy test
#           0 errors detected (crashes, CUDA errors, etc.)
#       routines with failures:
#           testing_sgetri_gpu -c
#
# When output is to console (tty), it runs in interactive mode, pausing after
# each test. At the pause, typing "M" re-makes and re-runs that tester,
# while typing enter goes to the next tester.
# For example (some output suppressed with ... for brevity):
#
#       ./run_tests.py --lu --precision s --small
#       ****************************************************************************************************
#       ./testing_sgesv_gpu -c --range 1:20:1 ...
#       ****************************************************************************************************
#           N  NRHS   CPU GFlop/s (sec)   GPU GFlop/s (sec)   ||B - AX|| / N*||A||*||X||
#       ================================================================================
#           1     1     ---   (  ---  )      0.00 (   0.00)   9.26e-08   ok
#           2     1     ---   (  ---  )      0.00 (   0.00)   1.32e-08   ok
#           3     1     ---   (  ---  )      0.00 (   0.00)   8.99e-09   ok
#       ...
#         ok
#       [enter to continue; M to make and re-run]
#
#       ****************************************************************************************************
#       ./testing_sgetri_gpu -c --range 1:20:1 ...
#       ****************************************************************************************************
#       % MAGMA 1.4.0 svn compiled for CUDA capability >= 3.0
#       % CUDA runtime 6000, driver 6000. MAGMA not compiled with OpenMP.
#       % device 0: GeForce GT 750M, 925.5 MHz clock, 2047.6 MB memory, capability 3.0
#       Usage: ./testing_sgetri_gpu [options] [-h|--help]
#
#           N   CPU GFlop/s (sec)   GPU GFlop/s (sec)   ||R||_F / (N*||A||_F)
#       =================================================================
#           1      0.00 (   0.00)      0.00 (   0.00)   6.87e+01   failed
#           2      0.00 (   0.00)      0.00 (   0.00)   2.41e+00   failed
#           3      0.01 (   0.00)      0.00 (   0.00)   1.12e+00   failed
#       ...
#         ** 45 tests failed
#       [enter to continue; M to make and re-run]
#
#       ...
#
#       ****************************************************************************************************
#       summary
#       ****************************************************************************************************
#         282 tests in 7 commands passed
#          45 tests failed accuracy test
#           0 errors detected (crashes, CUDA errors, etc.)
#       routines with failures:
#           testing_sgetri_gpu -c
#
#
# What tests are run
# ------------------
# The --blas, --aux, --chol, --lu, --qr, --syev, --sygv, --geev, --svd options run
# particular sets of tests. By default, all tests are run.
#
# The --start option skips all testers before the given one, then continues
# with testers from there. This is helpful to restart a batch. For example:
#
#       ./run_tests.py --start testing_spotrf
#
# If specific testers are named on the command line, only those are run.
# For example:
#
#       ./run_tests.py testing_spotrf testing_sgetrf
#
# The -p/--precision option controls what precisions are tested, the default
# being "sdcz" for all four precisions. For example, to run single and double:
#
#       ./run_tests.py -p sd
#
# The -s/--small, -m/--medium, -l/--large options control what sizes are tested,
# the default being all three sets.
#       -s/--small  does small  tests, N < 300.
#       -m/--medium does medium tests, N < 1000.
#       -l/--large  does large  tests, N > 1000.
# For example, running small and medium tests:
#
#       ./run_tests.py -s -m
#
#
# What is checked
# ------------------
# The --memcheck option runs cuda-memcheck. This is very helpful for finding
# memory bugs (reading & writing outside allocated memory). It is, however, slow.
#
# The --tol option sets the tolerance to verify accuracy. This is 30 by default,
# which may be too tight for some testers. Setting it somewhat higher
# (e.g., 50 or 100) filters out spurious accuracy failures.

import os
import re
import sys
import time

import subprocess
from subprocess import PIPE, STDOUT

from optparse import OptionParser

# on a TTY screen, stop after each test for user input
# when redirected to file ("batch mode"), don't stop
batch = not sys.stdout.isatty()

parser = OptionParser()
parser.add_option('-p', '--precisions',  action='store',      dest='precisions', help='run given precisions', default='sdcz' )
parser.add_option(      '--start',       action='store',      dest='start',      help='start with given routine; useful to restart an interupted run')
parser.add_option(      '--memcheck',    action='store_true', dest='memcheck',   help='run with cuda-memcheck (slow)')
parser.add_option(      '--tol',         action='store',      dest='tol',        help='set tolerance')

parser.add_option('-s', '--small',       action='store_true', dest='small',      help='run small  tests, N < 300')
parser.add_option('-m', '--medium',      action='store_true', dest='med',        help='run medium tests, N < 1000')
parser.add_option('-l', '--large',       action='store_true', dest='large',      help='run large  tests, N > 1000')
parser.add_option('-n', '--nonsym',      action='store_true', dest='nonsym',     help='run nonsym  tests')

parser.add_option(      '--sparse-blas', action='store_true', dest='sparse_blas', help='run sparse BLAS tests')
parser.add_option(      '--solver',      action='store_true', dest='solver',      help='run sparse solvers')
parser.add_option(      '--control',     action='store_true', dest='control',     help='run sparse IO, copy, etc.')

parser.add_option(      '--csr',         action='store_true', dest='csr',         help='run CSR matrix format')
parser.add_option(      '--ell',         action='store_true', dest='ell',         help='run ELL matrix format')
parser.add_option(      '--sellp',       action='store_true', dest='sellp',       help='run SELLP matrix format')
parser.add_option(      '--csr5',        action='store_true', dest='csr5',        help='run CSR5 matrix format')

parser.add_option(      '--cg'               , action='store_true', dest='cg'            , help='run cg'            )
parser.add_option(      '--cg_merge'         , action='store_true', dest='cg_merge'      , help='run cg_merge'      )
parser.add_option(      '--pcg'              , action='store_true', dest='pcg'           , help='run pcg'           )
parser.add_option(      '--bicgstab'         , action='store_true', dest='bicgstab'      , help='run bicgstab'      )
parser.add_option(      '--bicgstab_merge'   , action='store_true', dest='bicgstab_merge', help='run bicgstab_merge')
parser.add_option(      '--pbicgstab'        , action='store_true', dest='pbicgstab'     , help='run pbicgstab'     )
parser.add_option(      '--gmres'            , action='store_true', dest='gmres'         , help='run gmres'         )
parser.add_option(      '--pgmres'           , action='store_true', dest='pgmres'        , help='run pgmres'        )
parser.add_option(      '--lobpcg'           , action='store_true', dest='lobpcg'        , help='run lobpcg'        )
parser.add_option(      '--iterref'          , action='store_true', dest='iterref'       , help='run iterref'       )
parser.add_option(      '--jacobi'           , action='store_true', dest='jacobi'        , help='run jacobi'        )
parser.add_option(      '--ba'               , action='store_true', dest='ba'            , help='run ba-iter'       )
parser.add_option(      '--idr'              , action='store_true', dest='idr'           , help='run idr'           )
parser.add_option(      '--idr_merge'        , action='store_true', dest='idr_merge'     , help='run idr_merge'     )
parser.add_option(      '--pidr'             , action='store_true', dest='pidr'          , help='run pidr'          )
parser.add_option(      '--cgs'              , action='store_true', dest='cgs'           , help='run cgs'           )
parser.add_option(      '--cgs_merge'        , action='store_true', dest='cgs_merge'     , help='run cgs_merge'     )
parser.add_option(      '--qmr'              , action='store_true', dest='qmr'           , help='run qmr'           )
parser.add_option(      '--qmr_merge'        , action='store_true', dest='qmr_merge'     , help='run qmr_merge'     )
parser.add_option(      '--pcgs'             , action='store_true', dest='pcgs'          , help='run pcgs'          )
parser.add_option(      '--pcgs_merge'       , action='store_true', dest='pcgs_merge'    , help='run pcgs_merge'    )
parser.add_option(      '--pqmr'             , action='store_true', dest='pqmr'          , help='run pqmr'          )
parser.add_option(      '--pqmr_merge'       , action='store_true', dest='pqmr_merge'    , help='run pqmr_merge'    )
parser.add_option(      '--bombard'          , action='store_true', dest='bombard'       , help='run bombard'       )
parser.add_option(      '--bombard_merge'    , action='store_true', dest='bombard_merge' , help='run bombard_merge' )
parser.add_option(      '--lsqr'             , action='store_true', dest='lsqr'          , help='run lsqr'          )
parser.add_option(      '--bicg'             , action='store_true', dest='bicg'          , help='run bicg'          )
parser.add_option(      '--pbicg'            , action='store_true', dest='pbicg'         , help='run pbicg'         )


parser.add_option(      '--jacobi-prec'      , action='store_true', dest='jacobi_prec'   , help='run Jacobi preconditioner')
parser.add_option(      '--ilu-prec'         , action='store_true', dest='ilu_exact_prec', help='run ILU + exact solve preconditioner')
parser.add_option(      '--ilu-jac'          , action='store_true', dest='ilu_jac_prec',   help='run ILU + Jacobi solve preconditioner')
parser.add_option(      '--ilu-bjac'         , action='store_true', dest='ilu_bjac_prec',  help='run ILU + Block Jacobi solve preconditioner')
parser.add_option(      '--ilu-isai-prec'    , action='store_true', dest='ilu_isai_prec' , help='run ILU + ISAI preconditioner')
parser.add_option(      '--ilut-prec'        , action='store_true', dest='ilut_prec',      help='run threshold ILU + exact solve preconditioner')

(opts, args) = parser.parse_args()

# default if no sizes given is all sizes
if ( not opts.small and not opts.med and not opts.large and not opts.nonsym ):
    opts.small  = True
    opts.med    = False
    opts.large  = False
    opts.nonsym = True
# end

# default if no groups given is all groups
if (     not opts.sparse_blas
     and not opts.solver
     and not opts.control
     and not opts.csr
     and not opts.ell
     and not opts.sellp ):
    opts.sparse_blas = True
    opts.solver      = True
    opts.control     = True
    opts.csr         = True
    opts.ell         = True
    opts.sellp       = True
    opts.csr5        = True
# end

# default if no solvers given is all solvers
if (     not opts.cg
     and not opts.cg_merge
     and not opts.pcg
     and not opts.bicgstab
     and not opts.bicgstab_merge
     and not opts.pbicgstab
     and not opts.gmres
     and not opts.pgmres
     and not opts.lobpcg
     and not opts.iterref
     and not opts.jacobi
     and not opts.ba
     and not opts.idr
     and not opts.ba
     and not opts.idr_merge
     and not opts.qmr
     and not opts.qmr_merge
     and not opts.cgs
     and not opts.cgs_merge
     and not opts.pqmr
     and not opts.pqmr_merge
     and not opts.pcgs
     and not opts.pcgs_merge
     and not opts.bombard
     and not opts.bombard_merge
     and not opts.bicg
     and not opts.pbicg
     and not opts.lsqr
     and not opts.pidr ):
    opts.cg             = True
    opts.cg_merge       = True
    opts.pcg            = True
    opts.bicgstab       = True
    opts.bicgstab_merge = True
    opts.pbicgstab      = True
    opts.gmres          = True
    opts.pgmres         = True
    opts.lobpcg         = True
    opts.iterref        = True
    opts.jacobi         = True
    opts.ba             = True
    opts.idr            = True
    opts.idr_merge      = True
    opts.pidr           = True
    opts.cgs            = True
    opts.cgs_merge      = True
    opts.qmr            = True
    opts.qmr_merge      = True
    opts.pcgs           = True
    opts.pcgs_merge     = True
    opts.pqmr           = True
    opts.pqmr_merge     = True
    opts.bombard        = True
    opts.bombard_merge  = True
    opts.bicg           = True
    opts.pbicg          = True
    opts.lsqr           = True
# end

# default if no preconditioners given all
if (     not opts.jacobi_prec
     and not opts.ilu_exact_prec
     and not opts.ilut_prec
     and not opts.ilu_jac_prec
     and not opts.ilu_bjac_prec
     and not opts.ilu_isai_prec ):
    opts.jacobi_prec      = True
    opts.ilu_prec         = True
    opts.ilu_jac_prec     = True
    opts.ilu_isai_prec    = True
    opts.ilu_bjac_prec    = True
    opts.ilut_prec        = True
# end

# default if no sizes given is all sizes
if ( not opts.small and not opts.med and not opts.large and not opts.nonsym ):
    opts.small = True
    opts.med   = True
    opts.large = True
    opts.nonsym = True
# end

print('opts', opts)
print('args', args)


# ----------------------------------------------------------------------

# looping over formats
formats = []
if ( opts.csr ):
    formats += ['--format CSR']
# end
if ( opts.ell ):
    formats += ['--format ELL']
# end
if ( opts.sellp ):
    formats += ['--format SELLP']
# end
if ( opts.csr5 ):
    formats += ['--format CSR5']
# end

# looping over solvers
solvers = []
if ( opts.cg ):
    solvers += ['--solver CG --basic']
# end
if ( opts.cg_merge ):
    solvers += ['--solver CG']
# end
if ( opts.bicgstab ):
    solvers += ['--solver BICGSTAB --basic']
# end
if ( opts.bicgstab_merge ):
    solvers += ['--solver BICGSTAB']
# end
if ( opts.gmres ):
    solvers += ['--solver GMRES']
# end
if ( opts.lobpcg ):
    solvers += ['--solver LOBPCG']
# end
if ( opts.jacobi ):
    solvers += ['--solver JACOBI']
# end
if ( opts.ba ):
    solvers += ['--solver BA']
# end
if ( opts.idr ):
    solvers += ['--solver IDR --basic']
# end
if ( opts.idr_merge ):
    solvers += ['--solver IDR']
# end
if ( opts.cgs ):
    solvers += ['--solver CGS --basic']
# end
if ( opts.cgs_merge ):
    solvers += ['--solver CGS --basic']
# end
if ( opts.qmr ):
    solvers += ['--solver QMR --basic']
# end
if ( opts.qmr_merge ):
    solvers += ['--solver QMR']
# end
if ( opts.lsqr ):
    solvers += ['--solver LSQR']
# end
if ( opts.bicg ):
    solvers += ['--solver BICG']
# end
if ( opts.bombard ):
    solvers += ['--solver BOMBARDMENT']
# end
if ( opts.bombard_merge ):
    solvers += ['--solver BOMBARDMENT --basic']
# end


# looping over precsolvers
precsolvers = []
if ( opts.pcg ):
    precsolvers += ['--solver PCG ']
# end
if ( opts.pbicgstab ):
    precsolvers += ['--solver PBICGSTAB ']
# end
if ( opts.pgmres ):
    precsolvers += ['--solver PGMRES ']
# end
if ( opts.pidr ):
    precsolvers += ['--solver PIDR ']
# end
if ( opts.pqmr ):
    precsolvers += ['--solver PQMR ']
# end
if ( opts.pidr ):
    precsolvers += ['--solver PCGS ']
# end
if ( opts.pbicg ):
    precsolvers += ['--solver PBICG ']
# end
if ( opts.lsqr ):
    precsolvers += ['--solver PLSQR ']
# end



# looping over eigensolvers
IR = []
if ( opts.iterref ):
    IR += ['--solver LOBPCG ']
# end



# looping over preconditioners
precs = ['--precond NONE ']
if ( opts.jacobi_prec ):
    precs += ['--precond JACOBI ']
# end
if ( opts.ilu_exact_prec ):
    precs += ['--precond ILU ']
# end
if ( opts.ilut_prec ):
    precs += ['--precond PARILUT --prestart 1 --psweeps 5 --plevels 0 --prtol 0.05 --patol 0.2 ']
# end
if ( opts.ilu_jac_prec ):
    precs += ['--precond ILU --trisolver JACOBI --ppattern 1 --piters 5 ']
# end
if ( opts.ilu_bjac_prec ):
    precs += ['--precond ILU --trisolver JACOBI --ppattern 4 --piters 3 ']
# end
if ( opts.ilu_isai_prec ):
    precs += ['--precond ILU --trisolver ISAI --ppattern 1 --piters 1 ']
# end


# looping over preconditioners for Iter-Ref
IRprecs = []
if ( opts.iterref ):
    IRprecs += ['--precond CG ']
    IRprecs += ['--precond CGS ']
    IRprecs += ['--precond BICGSTAB ']
    IRprecs += ['--precond BICG ']
    IRprecs += ['--precond GMRES ']
    IRprecs += ['--precond QMR ']
# end



# ----------------------------------------------------------------------
# blocksizes
# ----------
blocksizes = []
blocksizes += ['--blocksize 4']
blocksizes += ['--blocksize 8']
blocksizes += ['--blocksize 16']
#end


# ----------------------------------------------------------------------
# alignments
# ----------
alignments = []
alignments += ['--alignment 4']
alignments += ['--alignment 8']
alignments += ['--alignment 16']
alignments += ['--alignment 32']


# ----------------------------------------------------------------------
# problem sizes
# n    is squared
# ----------
sizes = []
if opts.small:
    sizes += ['LAPLACE2D 47']
if opts.med:
    sizes += ['LAPLACE2D 95']
if opts.large:
    sizes += ['LAPLACE2D 317']
if opts.nonsym:
    sizes += ['test_matrices/ani5_crop.mtx']
#end


# ----------------------------------------------------------------------
precisions = (
    's', 'd', 'c', 'z'
)

subs = (
    ('',              'dlag2s',      '',              'zlag2c'    ),
    ('ssy',           'dsy',         'che',           'zhe'       ),
    ('sor',           'dor',         'cun',           'zun'       ),
    ('sy2sb',         'sy2sb',       'he2hb',         'he2hb'     ),
    ('',              'testing_ds',  '',              'testing_zc'),
    ('testing_s',     'testing_d',   'testing_c',     'testing_z' ),
    ('lansy',         'lansy',       'lanhe',         'lanhe'     ),
    ('blas_s',        'blas_d',      'blas_c',        'blas_z'    ),
)

# ----------
# simple precision generation
def substitute( txt, pfrom, pto ):
    if ( pfrom != pto ):
        ifrom = precisions.index( pfrom )
        ito   = precisions.index( pto )
        for sub in subs:
            txt = re.sub( sub[ifrom], sub[ito], txt )
    # end
    return txt
# end


# ----------------------------------------------------------------------
# test problems
#
# Each test has 4 fields:  (program, options, sizes, comments).
# If program begins with #, that test is disabled and will be printed as such.
# In some cases, the line is commented out, so nothing is printed.
#
# These match the order in the Makefile,
# except in some cases the "d" version from the Makefile isn't required here
# To compare with Makefile:
# grep '^\s+testing_\w+' Makefile >! a
# grep "\('#?testing_\w\w+" run_tests.py | perl -pe "s/#?\('#?//; s/',.*/.cpp \\/;" | uniq > ! b
# diff -w a b


tests = []


# ----------------------------------------------------------------------
if ( opts.control):
    for precision in opts.precisions:
        for size in sizes:
            # precision generation
            cmd = substitute( 'testing_zio', 'z', precision )
            tests.append( [cmd, '', size, ''] )

# ----------------------------------------------------------------------
if ( opts.control):
    for precision in opts.precisions:
        for size in sizes:
            # precision generation
            cmd = substitute( 'testing_zmatrix', 'z', precision )
            tests.append( [cmd, '', size, ''] )

# ----------------------------------------------------------------------
if ( opts.control):
    for precision in opts.precisions:
        for size in sizes:
            # precision generation
            cmd = substitute( 'testing_zmconverter', 'z', precision )
            tests.append( [cmd, '', size, ''] )

# ----------------------------------------------------------------------
if ( opts.control):
    for precision in opts.precisions:
        for size in sizes:
            # precision generation
            cmd = substitute( 'testing_zmcompressor', 'z', precision )
            tests.append( [cmd, '', size, ''] )

# ----------------------------------------------------------------------
if ( opts.control):
    for precision in opts.precisions:
        for size in sizes:
            # precision generation
            cmd = substitute( 'testing_zmadd', 'z', precision )
            tests.append( [cmd, '', size + ' ' + size, ''] )


# ----------------------------------------------------------------------
if ( opts.sparse_blas):
    for precision in opts.precisions:
        for size in sizes:
            for blocksize in blocksizes:
                for alignment in alignments:
                    # precision generation
                    cmd = substitute( 'testing_zspmv', 'z', precision )
                    tests.append( [cmd, alignment + ' ' + blocksize, size, ''] )


# ----------------------------------------------------------------------
if ( opts.sparse_blas):
    for precision in opts.precisions:
        for size in sizes:
            for blocksize in blocksizes:
                for alignment in alignments:
                    # precision generation
                    cmd = substitute( 'testing_zspmm', 'z', precision )
                    tests.append( [cmd, alignment + ' ' + blocksize, size, ''] )


# ----------------------------------------------------------------------
for solver in solvers:
    for size in sizes:
        for precision in opts.precisions:
            # precision generation
            cmd = substitute( 'testing_zsolver', 'z', precision )
            tests.append( [cmd, solver, size, ''] )


# ----------------------------------------------------------------------
for solver in precsolvers:
    for precond in precs:
        for size in sizes:
            for precision in opts.precisions:
                # precision generation
                cmd = substitute( 'testing_zsolver', 'z', precision )
                tests.append( [cmd, solver + ' ' + precond, size, ''] )


# ----------------------------------------------------------------------
for solver in IR:
    for precond in IRprecs:
        for size in sizes:
            for precision in opts.precisions:
                # precision generation
                cmd = substitute( 'testing_zsolver', 'z', precision )
                tests.append( [cmd, solver + ' ' + precond, size, ''] )





# ----------------------------------------------------------------------
# runs command in a subprocess.
# returns list (okay, fail, errors, status)
# okay   is count of "ok"     in output.
# fail   is count of "failed" in output.
# error  is count of indications of other errors (exit, CUDA error, etc.).
# status is exit status of the command.
def run( cmd ):
    words = re.split( ' +', cmd.strip() )

    # stdout & stderr are merged
    p = subprocess.Popen( words, stdout=PIPE, stderr=STDOUT )

    okay  = 0
    fail  = 0
    nospd = 0
    error = 0
    nosupport = 0
    slowconv = 0
    # read unbuffered ("for line in p.stdout" will buffer)
    while True:
        line = p.stdout.readline().decode()
        if not line:
            break
        print(line.rstrip())
        if re.search( r'\bok *$', line ):
            okay += 1
        if re.search( 'failed', line ):
            fail += 1
        if re.search( 'exit|memory mapping error|CUDA runtime error|illegal value|ERROR SUMMARY: [1-9]', line ):
            error += 1
        m = re.search( r'solver info: (-?\d+)', line )
        if ( m ):
            info = int( m.group(1) )
            if ( info == 0 ):
                okay += 1
            if ( info == -103 ):
                nosupport += 1
            if ( info == -201 ):
                slowconv += 1
            if ( info == -202 ):
                fail += 1
            if ( info == -203 ):
                fail += 1
        # end
    # end

    status = p.wait()
    return (okay, fail, error, status)
# end


# ----------------------------------------------------------------------
ntest  = 0
nokay  = 0
nfail  = 0
nerror = 0
failures = {}

start = None
if ( opts.start ):
    start = re.compile( opts.start + r'\b' )

seen  = {}
pause = 0

last_cmd = None

for test in tests:
    (cmd, options, sizes, comments) = test

    make = False
    disabled = (cmd[0] == '#')
    if ( disabled ):
        cmd = cmd[1:]

    # command to run
    cmd_args = './' + cmd +' '+ options +' '+ sizes
    cmd_args = re.sub( '  +', ' ', cmd_args )  # compress spaces

    # skip tests before start
    if ( start and not start.search( cmd_args )):
        continue
    start = None

    # skip tests not in args, or duplicates, or non-existing
    if ((args and not cmd in args)
         or (not os.path.exists( cmd ))
         or (cmd_args in seen.keys())):
        continue
    # end
    seen[ cmd_args ] = True

    if ( opts.memcheck ):
        cmd_args = 'cuda-memcheck ' + cmd_args

    go = True
    while( go ):
        if pause > 0:
            time.sleep( pause )
            pause = 0
        # end

        print
        print('*'*100)
        print(cmd_args)
        print('*'*100)
        sys.stdout.flush()

        if ( batch ):
            if ( last_cmd and cmd != last_cmd ):
                sys.stderr.write( '\n' )
            last_cmd = cmd
            sys.stderr.write( '%-70s' % cmd_args )
            sys.stderr.flush()
        # end

        if ( disabled ):
            if ( comments ):
                sys.stderr.write( '  (disabled: ' + comments + ')\n' )
            else:
                sys.stderr.write( '  (disabled)\n' )
            go = False
            continue
        # end

        if ( make ):
            m = 'make lib ' + cmd
            print(m)
            run( m )
        # end

        t = time.time()
        (okay, fail, error, status) = run( cmd_args )
        t = time.time() - t

        # count stats
        ntest  += 1
        nokay  += okay
        nfail  += fail
        nerror += error

        errmsg = ''
        if ( fail > 0 ):
            errmsg += '  ** %d tests failed' % (fail)
        if ( error > 0 ):
            errmsg += '  ** %d errors' % (error)
        if ( status < 0 ):
            errmsg += '  ** exit with signal %d' % (-status)
            nerror += 1  # count crash as an error

        if ( errmsg != '' ):
            if ( batch ):
                sys.stderr.write( errmsg + '\n' )  # to console
            sys.stdout.write( errmsg + '\n' )  # to file
            failures[ cmd_args ] = True
        else:
            sys.stderr.write( '  ok\n' )
        # end

        if ( batch ):
            # set to sleep a few seconds before next test,
            # to allow processor to cool off some between tests.
            pause = min( t, 5. )
            go = False
        else:
            x = input( '[enter to continue; M to make and re-run] ' )
            if ( x in ('m','M')):
                make = True
            else:
                go = False
        # endif
    # end
# end


# print summary
msg  = '\n'
msg += '*'*100   + '\n'
msg += 'summary' + '\n'
msg += '*'*100   + '\n'

if ( nfail == 0 and nerror == 0 ):
    msg += 'all %d tests in %d commands passed!\n' % (nokay, ntest)
else:
    msg += '%5d tests in %d commands passed\n' % (nokay, ntest)
    msg += '%5d tests failed accuracy test\n' % (nfail)
    msg += '%5d errors detected (crashes, CUDA errors, etc.)\n' % (nerror)
    f = list(failures.keys())
    f.sort()
    msg += 'routines with failures:\n    ' + '\n    '.join( f ) + '\n'
# end

if ( batch ):
    sys.stderr.write( msg )  # to console
sys.stdout.write( msg )  # to file