script/mat2harbo.pl untested

filter: Statementstmt 0.0% 0 / 294 Branchbran 0.0% 0 / 112 Conditioncond 0.0% 0 / 31 MC/DCmcdc 0.0% 0 / 17 Subroutinesub 0.0% 0 / 5 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 462 CC 1 SCAR 6.9
CC
1
Cov
0%
showminimal
2.0
showhelp
2.0
showversion
2.0
CRAP 2.0
1 #!/usr/local/bin/perl -w
2  
3 =head1 NAME
4  
5 mat2harbo.pl - Convert matrix in Senseclusters sparse format to Harwell-Boeing (HB)
6 format and set input parameters (lap2) for input to SVDPACKC.
7  
8 =head1 SYNOPSIS
9  
10 mat2harbo.pl [OPTIONS] MATRIX
11  
12 The file input is a SenseClusters sparse matrix
13  
14 cat input
15  
16 Output =>
17  
18 5 4 12
19 1 1.5 3 2.5 4 1.0
20 2 2.5 3 2.5
21 1 1.5 3 2.5 4 1.0
22 2 2.5 3 2.5
23 2 2.5 3 2.5
24  
25 Convert that to Harwell-Boeing form.
26  
27 mat2harbo.pl input --title "matrix format convestion" --id "sample" --numform 10f8.4
28  
29 Output =>
30  
31 matrix format convestion sample
32 #
33 rra 5 4 12 0
34 (10i8) (10i8) (10f8.4) (10f8.4)
35 1 3 6 11 13
36 1 3 2 4 5 1 2 3 4 5
37 1 3
38 1.5000 1.5000 2.5000 2.5000 2.5000 2.5000 2.5000 2.5000 2.5000 2.5000
39 1.0000 1.0000
40  
41 The Harwell Boeing format stores data in 80 columns. The numform 10f8.4 says that
42 there should be 10 numbers per line, each with 8 numeric values, where 4 digits are to
43 the right of the decimal point.
44  
45 See L<http://math.nist.gov/MatrixMarket/formats.html#hb> for a detailed explanation of
46 Harwell Boeing format.
47  
48 Type C<mat2harbo.pl --help> for a quick summary of options
49  
50 =head1 DESCRIPTION
51  
52 Converts a sparse matrix in SenseClusters format to Harwell-Boeing (HB)
53 sparse format, which is the format required by SVDPACKC. This program also creates
54 (optionally) the lap2 file which provides parameter settings for SVDPACKC.
55  
56 =head1 INPUT
57  
58 =head2 Required Arguments:
59  
60 =head3 MATRIX
61  
62 A sparse MATRIX in SenseClusters' format that is to be converted into
63 Harwell Boeing format.
64  
65 First line should show exactly 3 numbers separated by blanks as :
66  
67 #nrows #ncols #nnz
68  
69 where
70  
71 #nrows = Number of rows
72 #ncols = Number of columns
73 #nnz = Total number of non-zero values
74  
75 in the MATRIX.
76  
77 Each line thereafter should show a row of the MATRIX in sparse format.
78 A sparse row should be a space separated list of pairs of numbers
79 where the first number shows the column index of a non-zero value
80 and second number is the non-zero value itself that appears at that column
81 index.
82  
83 Column index counting starts from 1.
84  
85 Sample MATRIX examples =>
86  
87 =over 4
88  
89 =item 1
90  
91 5 5 15
92 2 9 4 9
93 1 6 2 5 3 7 4 8 5 6
94 1 4 2 5
95 1 7 2 6 3 7
96 1 9 2 8 3 9
97  
98 Shows a 5 x 5 integer matrix containing total 15 non-zero elements.
99 Each ith line after the first line shows the non-zero elements in the ith
100 row. e.g. 2nd line (1st row) has 2 non-zero values (both 9) at column indices
101 2 and 4. 6th line (5th row) has 3 non-zero values; 9 at index 1, 8 at index 2
102 and 9 at index 3.
103  
104 =item 2
105  
106 7 8 34
107 1 0.160 2 -0.059 3 1.864 5 0.724 6 -0.472 7 -0.467
108 2 -0.209 4 1.487 5 6.728 7 -3.085 8 1.396
109 1 14.594 3 -2.858 4 -0.618 6 16.510 8 -2.314
110 3 -0.384 5 -1.189 7 -0.155 8 0.006
111 1 -0.128 3 0.020 4 -0.125 8 0.039
112 2 0.062 3 0.058 4 0.016 5 0.057 7 0.407 8 0.015
113 4 0.033 6 1.377 7 0.074 8 0.994
114  
115 Shows a 7 x 8 real matrix =>
116  
117 7 8
118 0.160 -0.059 1.864 0.000 0.724 -0.472 -0.467 0.000
119 0.000 -0.209 0.000 1.487 6.728 0.000 -3.085 1.396
120 14.594 0.000 -2.858 -0.618 0.000 16.510 0.000 -2.314
121 0.000 0.000 -0.384 0.000 -1.189 0.000 -0.155 0.006
122 -0.128 0.000 0.020 -0.125 0.000 0.000 0.000 0.039
123 0.000 0.062 0.058 0.016 0.057 0.000 0.407 0.015
124 0.000 0.000 0.000 0.033 0.000 1.377 0.074 0.994
125  
126 =back
127  
128 =head2 Optional Arguments:
129  
130 =head4 --title TITLE
131  
132 Allows user to specify the Title of the MATRIX which is displayed at Line1
133 (1-72) of the output HB matrix.
134 If --title is not specified, mat2harbo uses the MATRIX file name as
135 the default title.
136  
137 =head4 --id ID
138  
139 Programs processing the HB formatted matrix can identify the matrix by the ID
140 specified at Line1 (73-80). Default ID is "harbomat". This identifier is limited to 8
141 characters.
142  
143 =head4 --cpform CPFORM
144  
145 Specifies the Column Pointer Format. The column pointer should have the format
146 of type MiN which indicates that each line in Block1 contains M integer
147 pointers each occupying N character spaces. Default format is 10i8.
148  
149 Note: M x N must be 80.
150  
151 =head4 --rpform RPFORM
152  
153 Specifies the Row Pointer Format for row pointers in Block2. This has same
154 MiN type of format as --cpform.
155  
156 =head4 --numform NUMFORM
157  
158 Specifies the Numeric Format to represent the matrix values in Block3.
159  
160 mat2harbo allows 2 numeric formats :
161  
162 =over
163 =item 1. MiN type, which has same interpretation as --cpform and --rpform.
164  
165 =item 2. MfD.F - which means that there are total M real numbers on each line of block3, each occupying total D digit/character space, of which last F digits show fractional portion.
166 =back
167  
168 Thus, Matrix values could be Integer or Real, selected by specifying a
169 particular format.
170  
171 Default NUMFORM is (5f16.10) which uses 16 digits for each MATRIX value of which
172 last 10 digits stand for the fractional part and each line contains 5 such
173 real numbers.
174  
175 =head3 Parameter Setting Options :
176  
177 The options listed in this section create the parameter file (lap2) for las2.c
178 automatically.
179  
180 =head4 --param
181  
182 Creates the parameter file lap2 that can be directly used while running las2.
183  
184 =head4 --k K
185  
186 Sets the value of maxprs option in LAP2 file to K i.e.
187 requests K singular triplets from las2. Value of K should not exceed the
188 number of columns of MATRIX.
189 Default K = 300
190  
191 =head4 --rf RF
192  
193 Reduces the dimensions of the column space of the MATRIX by scaling factor RF
194 i.e. if the MATRIX has N columns, maxprs is set to N/RF where RF > = 1
195  
196 In other words, N/RF singular triplets are requested from las2.
197 Default RF = 10 that reduces the column space to 10% or preserves 10% of the
198 original dimensions.
199  
200 If both --k and --rf are specified, maxprs = min(K,N/RF)
201 Thus, default maxprs = min(300,N/10)
202  
203 =head4 --iter I
204  
205 Specifies the number of iterations for las2.
206 I, if specified, should not exceed the number of columns in the MATRIX
207 and I should be at least as high as maxprs.
208 Default I = min((3 * maxprs),#cols) where maxprs = min(K,N/RF).
209  
210 =head3 Help on setting parameters in file las2.h
211  
212 The header file las2.h in SVDPACKC specifies values of various constants
213 for las2. This section provides some guidelines on setting these constants
214 for using SenseClusters. Please note that the version of SVDPACKC found in /External
215 has been modified with the settings as described below.
216  
217 =over
218  
219 =item * NMAX
220  
221 Specifies the maximum possible number of columns in the matrix given to las2.
222 las2.h initially has a value of NMAX = 3000, which allows a maximum of
223 3000 columns. However, we have found this default is too small for many
224 of our experiments, so we recommend setting NMAX much higher. We
225 routinely use a value of 30,000, and will assume that the user has
226 reset NMAX in las2.h to this value in the rest of this discussion.
227  
228 In general, this value should be higher than NCOLS shown by the 3rd column
229 on the 3rd line in the output of mat2harbo.pl.
230  
231 =item * NZMAX
232  
233 Specifies the maximum possible number of non-zero values in the matrix.
234 Initially the settings in las2.h have NZMAX = 100000. However,
235 again we have found this to be too small. If the user sets NMAX to 30,000,
236 and if we assume a 30,000 x 30,000 matrix is approximately 1% dense,
237 NZMAX could be set to 9,000,000 (30,000 x 30,000 / 100). This is the value
238 we routinely use, and we will assume that the user has reset NZMAX to this
239 value in the rest of this discussion.
240  
241 The user can check the exact NZMAX for their matrix on line 3 column 4 of
242 the output matrix displayed by mat2harbo.pl and then set NZMAX to
243 something higher than that.
244  
245 =item * LMTNW
246  
247 This specifies the maximum total memory to be allocated by las2. The
248 initial setting of LMTNW in las2.h is 600000, however, we find that this
249 is often too small. In general, the size of LMTNW is determined by the
250 values you set NMAX and NZMAX to. LMTNW should be at least as large as :
251  
252 LMTNW = (6*NMAX + 4*NMAX + 1 + NZMAX*NZMAX)
253  
254 mat2harbo.p assumes that NMAX has been reset to 30,000 and that NZMAX is
255 set to 9,000,000. Thus,
256  
257 LMTNW = ((6 * 30,000) + (4 * 30,000) + 1 + (30,000 * 30,000))
258  
259 This leads to the new value for LMTNW of 900,300,001, which is equivalent
260 to a maximum working memory size of 1 GB. We have found this size to be
261 more than adquate to do SVD on a 25,000 x 25,000 matrix.
262  
263 math2arbo.pl will show an advisory message indicating the minimum size
264 that LMNTW should be set for, and will issue a warning message if the
265 actual size needed for the user matrix exceeds 900,300,001 (approx 1 GB).
266  
267 Memory is dynamically allocated by las2 depending upon the size of the input
268 matrix, irrespective of the value of LMTNW. In short, LMTNW specifies the
269 upper limit on memory consumption and the actual consumption depends on the
270 size of the matrix. Hence, LMTNW doesn't specify the total memory that las2
271 will *always* consume rather its an upper limit that could be consumed if
272 necessary.
273  
274 =back
275  
276 In case if las2 fails due to insufficient values of these parameters
277 as indicated by the las2.h file, an error message will be shown in output
278 file lao2 suggesting that the matrix is too large or something ... User is
279 adviced to check 3rd line of the matrix in Harwell-Boeing format (as produced
280 by this program) that is given to las2. Check if NCOLS shown at column 3 of
281 line 3 in the HB matrix exceeds NMAX. If so, increase NMAX to something higher
282 than NCOLS. If not, check if NNZ shown by column 4 on line 3 of the HB matrix
283 exceeds NZMAX in las2.h, if so, increase NZMAX. If not, increase the LMTNW
284 to something higher than (6*NMAX + 4*NMAX + 1 + NMAX*NMAX), or simply
285 increase it without too much computations until las2 succeeds :-)
286  
287 The other problem that a user might notice is that sometimes las2 runs
288 for a very long time like more than few days. In such case, user is advised
289 to restart las2 by reducing the values of parameters 'maxprs' and 'iter' in
290 parameter file lap2. Specifically, the 2nd parameter in lap2 is iter and
291 the 3rd one is maxprs. Remember that, iter has to be >= maxprs.
292  
293 =head3 Other Options :
294  
295 =head4 --help
296  
297 Displays this message.
298  
299 =head4 --version
300  
301 Displays the version information.
302  
303 =cut
304  
305 ###############################################################################
306  
307 # THE CODE STARTS HERE
308  
309 #$0 contains the program name along with
310 #the complete path. Extract just the program
311 #name and use in error messages
312 $0=~s/.*\/(.+)/$1/;
313  
314 ###############################################################################
315  
316 # ================================
317 # COMMAND LINE OPTIONS AND USAGE
318 # ================================
319  
320 # command line options
321 use Getopt::Long;
322 use POSIX 'ceil';
323  
324 GetOptions ("help","version","title=s","id=s","cpform=s","rpform=s","numform=s","param","k=i","iter=i","rf=i");
325 # show help option
326 if(defined $opt_help)
327 {
328 $opt_help=1;
329 &showhelp();
330 exit;
331 }
332  
333 # show version information
334 if(defined $opt_version)
335 {
336 $opt_version=1;
337 &showversion();
338 exit;
339 }
340  
341 # show minimal usage message if no arguments
342 if($#ARGV<0)
343 {
344 &showminimal();
345 exit;
346 }
347  
348 #############################################################################
349  
350 # ================================
351 # INITIALIZATION AND INPUT
352 # ================================
353  
354 if(!defined $ARGV[0])
355 {
356 print STDERR "ERROR($0):
357 Please specify the Matrix file ...\n";
358 exit;
359 }
360 #accept the input file name
361 $infile=$ARGV[0];
362 if(!-e $infile)
363 {
364 print STDERR "ERROR($0):
365 Matrix file <$infile> doesn't exist...\n";
366 exit;
367 }
368  
369 open(IN,$infile) || die "Error($0):
370 Error(code=$!) in opening Matrix file <$infile>.\n";
371  
372 # ---------------
373 # Reading infile
374 # ---------------
375  
376 $line=1;
377  
378 # line1 in Matrix file should either show the
379 # <keyfile> tag or #rows #cols #nnz
380 $line1=<IN>;
381 if($line1=~/keyfile/)
382 {
383 $line1=<IN>;
384 $line++;
385 }
386  
387 if($line1=~/^\s*(\d+)\s+(\d+)\s+(\d+)\s*$/)
388 {
389 $rows=$1;
390 $cols=$2;
391 $nnz1=$3;
392 }
393 else
394 {
395 print STDERR "ERROR($0):
396 Line $line in Matrix file <$infile> should show #rows #cols #nnz.\n";
397 exit;
398 }
399  
400 # default parameters
401 if(defined $opt_param)
402 {
403 if(!defined $opt_k)
404 {
405 $opt_k=300;
406 }
407 if(!defined $opt_rf)
408 {
409 $opt_rf=10;
410 }
411 elsif($opt_rf<1)
412 {
413 print STDERR "ERROR($0):
414 --rf = $opt_rf should be greater than 1.\n";
415 exit;
416 }
417
418 my $tmp_ratio = ceil($cols/$opt_rf);
419 $maxprs=$opt_k > $tmp_ratio ? $tmp_ratio : $opt_k;
420  
421 if(!defined $opt_iter || $opt_iter < $maxprs || $opt_iter > $cols)
422 {
423 $opt_iter=(3*$maxprs) > $cols ? $cols : (3*$maxprs);
424 }
425 $iter=$opt_iter;
426 }
427  
428 # output format options
429 if(defined $opt_cpform)
430 {
431 if($opt_cpform =~ /^(\d+)i(\d+)$/)
432 {
433 if(($1 * $2) !=80)
434 {
435 print STDERR "ERROR($0):
436 (#pointers/line ($1)) * (#digits/pointer ($2)) != 80
437 in --cpform = $opt_cpform.\n";
438 exit;
439 }
440 }
441 else
442 {
443 print STDERR "ERROR($0):
444 Invalid column pointer format --cpform=$opt_cpform.\n";
445 exit;
446 }
447 }
448 if(defined $opt_rpform)
449 {
450 if($opt_rpform =~ /^(\d+)i(\d+)$/)
451 {
452 if($1 * $2 !=80)
453 {
454 print STDERR "ERROR($0):
455 (#pointers/line ($1)) * (#digits/pointer ($2)) !=80
456 in --rpform = $opt_rpform.\n";
457 exit;
458 }
459 }
460 else
461 {
462 print STDERR "ERROR($0):
463 Invalid row pointer format --rpform=$opt_rpform.\n";
464 exit;
465 }
466 }
467 if(defined $opt_numform)
468 {
469 if($opt_numform =~ /^(\d+)i(\d+)$/)
470 {
471 if($1 * $2 != 80)
472 {
473 print STDERR "ERROR($0):
474 (#integers/line ($1)) * (#digits/integer ($2)) != 80
475 in --numform = $opt_numform.\n";
476 exit;
477 }
478 }
479 elsif($opt_numform =~ /^(\d+)f(\d+)\.\d+$/)
480 {
481 if($1 * $2 != 80)
482 {
483 print STDERR "ERROR($0):
484 (#reals/line ($1)) * (#digits/real ($2)) != 80
485 in --numform = $opt_numform.\n";
486 exit;
487 }
488 }
489 else
490 {
491 print STDERR "ERROR($0):
492 Invalid number format --numform=$opt_numform.\n";
493 exit;
494 }
495 }
496  
497 # reading the sparse matrix
498 # in row order
499 $row=0;
500 while(<IN>)
501 {
502 $line++;
503 $row++;
504 chomp;
505 s/^\s*//;
506 s/\s*$//;
507 if(/^\s*$/)
508 {
509 next;
510 }
511 @pairs=split(/\s+/);
512 for($i=0; $i<$#pairs; $i=$i+2)
513 {
514 $index=$pairs[$i];
515 if($index > $cols)
516 {
517 print STDERR "ERROR($0):
518 Index <$index> at line <$line> in Matrix file <$infile> exceeds the
519 total number of columns <$cols> specified on line 1.\n";
520 exit;
521 }
522 $value=$pairs[$i+1];
523 if($value==0)
524 {
525 print STDERR "ERROR($0):
526 Given Matrix <$infile> is not sparse. Caught 0 value at line $line.\n";
527 exit;
528 }
529 # storing in column-wise order
530 $sparse_matrix{$index}{$row}=$value;
531 $nnz++;
532 }
533 }
534  
535 close IN;
536 if($row != $rows)
537 {
538 print STDERR "ERROR($0):
539 1st line in Matrix file <$infile> shows #vectors = #rows = $rows
540 while the actual #vectors found in this file = $row.\n";
541 exit;
542 }
543  
544 if($nnz != $nnz1)
545 {
546 print STDERR "ERROR($0):
547 1st line in Matrix file <$infile> shows #nnz = $nnz1 while the
548 actual #nnz found in the file = $nnz.\n";
549 exit;
550 }
551  
552 foreach $col (1..$cols)
553 {
554 if(!defined $sparse_matrix{$col})
555 {
556 print STDERR "ERROR($0):
557 Null column at $col.\n";
558 exit;
559 }
560 }
561  
562 ##############################################################################
563  
564 # ==========================
565 # PROGRAM SECTION
566 # ==========================
567  
568 =head1 Harwell Boeing Format
569  
570 =head2 Header Section
571  
572 =over
573  
574 =item * Line1 (Title[72], Id[8])
575  
576 =item * Line2 Skipped (as SVDPack ignores this line)
577  
578 =item * Line3 (Type[3], 11x, Nrows[14], Ncols[14], NNZ[14], Nrhs[14])
579  
580 where Type[3] is a 3 Character Field in which
581  
582 =over
583  
584 =item 1. char[1] =
585  
586 r for Real matrix,
587  
588 c for Complex matrix,
589  
590 p for Pattern matrix
591  
592 =item 2. char[2] =
593  
594 u for Unsymmetric matrix,
595  
596 h for Hermitian matrix (Aij=Aji* where Aji* is a complex conjugate of Aij),
597  
598 z for Skew Symmetrix matrix
599  
600 r for Rectangular matrix
601  
602 =item 3. char[3]=
603  
604 a for Assembled matrix
605  
606 f for Unassembled Finite Elements
607  
608 =back
609  
610 Nrows = Number of Rows
611  
612 Ncols = Number of Columns
613  
614 NNZ = Number of Non-Zero Elements
615  
616 Nrhs = Number of Right-Hand Sides (not used in SenseClusters)
617  
618 =item * Line4
619  
620 (Pointer_Format[16], Row_index_Format[16], Numeric_Value_Format[20],
621 RHS_Format[20])
622  
623 Pointers and Row Indices could have MiN type of format which specifies
624 that there are M intergers on each line and each represented with N digits.
625 (M x N must be = 80 as this format only supports column width of maximum 80
626 characters)
627  
628 Numeric Values can have either MiN format with same interpretation of
629 M and N as above or MfD.F format which specifies that there are M real
630 numbers on each line, each occupying total D digit space of each last F digits
631 show the fractional part.
632  
633 Note: D is that total space used to represent a number that includes the
634 decimal point and +/- sign if any.
635  
636 =back
637  
638 The above 4 Lines form the Header of the HB sparse matrix.
639  
640 =head2 Data Section
641  
642 This section contains 3 blocks which contain the non-zero values in the matrix
643 along with their row and column index information.
644  
645 *************************************************************************
646 NON-ZERO ENTRIES ARE STORED IN COLUMN ORDER !!!
647 *************************************************************************
648  
649 We consider data section of 3 blocks:
650  
651 =head3 BLOCK1 POINTERS
652  
653 The first block is an array whose entries show the indices (in block3) of the
654 leading non-zero value of every column.
655  
656 e.g. If a given matrix is
657  
658 4 6
659 2 3 0 0 0 1
660 0 2 0 1 2 0
661 0 0 2 4 1 0
662 1 1 0 0 5 0
663  
664 Then the first block will contain the pointers
665  
666 [1 3 6 7 9 12 13]
667  
668 This shows that
669  
670 The first column begins at the 1st non-zero entry (2)
671 The second column begins at the 3rd non-zero entry (3) [in COLUMN ORDER]
672 The third column begins at the 6th non-zero entry (2)
673 The forth column begins at the 7th non-zero entry (1)
674 and so on ...
675  
676 *************************************************************************
677 NULL columns (having no non-zero elements) are not allowed.
678 *************************************************************************
679  
680 Note: The column pointers start at 1.
681  
682 The last entry in @pointers contains an extra pointer pointing to one location
683 after the last entry. So the last index in @pointers will always be #nnz + 1
684 (where #nnz = total no. of non-zero entries)
685  
686 =head3 BLOCK2 ROW_INDICES
687  
688 This block stores the row indices of the non-zero matrix entries in column
689 order.
690  
691 For above matrix, this block will look like
692  
693 [1 4 1 2 4 3 2 3 2 3 4 1]
694  
695 which shows that
696  
697 The 1st non-zero entry is at 1st row.
698 The 2nd non-zero entry is at 4th row.
699 The 3rd non-zero entry is at 1st row
700 and so on ....
701  
702 Note: Row indices start from 1.
703  
704 =head3 BLOCK3 VALUES
705  
706 This block contains the actual non-zero values from the matrix in
707 column order.
708  
709 Thus, the block3 for the above shown matrix will look like
710  
711 [2 1 3 2 1 2 1 4 2 1 5 1]
712  
713  
714 General Observations:
715  
716 =over 4
717  
718 =item 1. The length(block2)=length(block3) and each is equal to the number
719 of non-zero entries in the matrix.
720  
721 =item 2. The length(block1) = #columns of matrix + 1 as each column will have
722 an entry in block1 that shows the position of the leading non-zero element
723 in it and there are no NULL columns allowed.
724  
725 +1 because there is an extra pointer pointing to the location after the last
726 non-zero entry.
727  
728 =item 3. The column pointers in block1 are also the pointers to block3 entries
729 where the leading(first) non-zero entry of each column is located.
730  
731 =back
732  
733 =head2 Sample Output
734  
735 matrix.dat harbomat
736  
737 #
738  
739 rra 4 6 11 0
740  
741 (10i8) (10i8) (8f10.3) (8f10.3)
742  
743 1 3 5 6 7 10 12
744 2 3 2 3 4 1 1 2
745 3 3 4
746 1.000 1.000 2.000 4.000 1.000 2.000 1.000
747 2.000 2.000 3.000 1.000
748  
749 Shows the HB format for a 4 x 6 matrix :
750  
751 4 6
752 0 0 0 2 1 0
753 1 2 0 0 2 0
754 1 4 0 0 2 3
755 0 0 1 0 0 1
756  
757 =cut
758  
759 ############################################################################
760  
761 # =============================
762 # CONVERSION SECTION
763 # =============================
764  
765 # creating temporary files to store sparse data blocks
766 $blk1="blk1". time(). ".mat2harbo";
767 $blk2="blk2". time(). ".mat2harbo";
768 $blk3="blk3". time(). ".mat2harbo";
769 # opening temp files in w
770 open(BLK1,">$blk1") || die "Error($0):
771 Error(code=$!) in opening <$blk1> file.\n";
772 open(BLK2,">$blk2") || die "Error($0):
773 Error(code=$!) in opening <$blk2> file.\n";
774 open(BLK3,">$blk3") || die "Error($0):
775 Error(code=$!) in opening <$blk3> file.\n";
776  
777 # reading the MATRIX in column order
778 # and making entries for each nnz in block files
779 $nnz=0;
780  
781 @column_indices=keys %sparse_matrix;
782 @sorted_columns=sort {$a <=> $b} @column_indices;
783 foreach $col (@sorted_columns)
784 {
785 @row_indices=keys %{$sparse_matrix{$col}};
786 @sorted_rows=sort {$a <=> $b} @row_indices;
787 $newcol=1;
788 foreach $row (@sorted_rows)
789 {
790 $nnz++;
791 if($newcol)
792 {
793 print BLK1 "$nnz\n";
794 $newcol=0;
795 }
796 print BLK2 "$row\n";
797 print BLK3 $sparse_matrix{$col}{$row} . "\n";
798 }
799 }
800  
801 print BLK1 ($nnz+1);
802  
803 ##############################################################################
804  
805 # =========================
806 # PRINTING SECTION
807 # =========================
808  
809 # printing Line1 (Title[72], Id[8])
810  
811 # if the title is specified by the user
812 if(defined $opt_title)
813 {
814 print pack("A72",$opt_title);
815 }
816 # use matrix file name as the default title
817 else
818 {
819 print pack("A72",$infile);
820 }
821  
822 # if the Id(also called as the KEY by SVDPack)
823 # is specified by the user
824 if(defined $opt_id)
825 {
826 $id=$opt_id;
827 }
828 # use default id = "harbomat"
829 else
830 {
831 $id="harbomat";
832 }
833 print pack("A8",$id);
834  
835 # done with Line1
836 print "\n";
837  
838 # Skipping Line2 by putting # as this line is
839 # ignored by SVDPack. But should have at least
840 # single character
841  
842 print "#\n";
843  
844 # printing Line3 (Type[3], 11x, Nrows[14], Ncols[14], NNZ[14], Nrhs[14])
845  
846 # setting type characters
847 $c1="r"; # we deal with Real matrices only
848  
849 $c2="r"; # always assume rectangular
850  
851 $c3="a"; # always assembled matrices
852 $type=$c1.$c2.$c3;
853  
854 printf("%3s%11s%14s%14s%14s%14s",$type," ",$rows,$cols,$nnz,0);
855 print "\n";
856  
857 # done with Line3
858  
859 # printing formats
860  
861 # if column pointer format is specified
862 if(defined $opt_cpform)
863 {
864 $ptrform=$opt_cpform;
865 # check : valid format
866 if($ptrform =~ /^(\d+)i(\d+)$/)
867 {
868 $ptrs_online=$1;
869 $digits_per_ptr=$2;
870 $ptr_string="%".$digits_per_ptr."d";
871  
872 $upper_cpform="";
873 while(length($upper_cpform)<($digits_per_ptr-1))
874 {
875 $upper_cpform.="9";
876 }
877 }
878 else
879 {
880 print STDERR "ERROR($0):
881 Invalid column pointer format $ptrform.\n";
882 exit;
883 }
884 $ptrform="(".$opt_cpform.")";
885 }
886 # use default (10i8)
887 else
888 {
889 $ptrform="(10i8)";
890 $ptr_string="%8d";
891 $ptrs_online=10;
892 $upper_cpform="9999999";
893 }
894  
895 # if row pointer format is specified
896 if(defined $opt_rpform)
897 {
898 $rowform=$opt_rpform;
899 # check : valid format
900 if($rowform =~ /^(\d+)i(\d+)$/)
901 {
902 $rowinds_online=$1;
903 $digits_per_rowind=$2;
904 $row_string="%".$digits_per_rowind."d";
905  
906 $upper_rpform="";
907 while(length($upper_rpform)<($digits_per_rowind-1))
908 {
909 $upper_rpform.="9";
910 }
911 }
912 else
913 {
914 print STDERR "ERROR($0):
915 Invalid row pointer format $rowform.\n";
916 exit;
917 }
918 $rowform="(".$opt_rpform.")";
919 }
920 # use default (10i8)
921 else
922 {
923 $rowform="(10i8)";
924 $row_string="%8d";
925 $rowinds_online=10;
926 $upper_rpform="9999999";
927 }
928  
929 # if numeric value format is specified
930 if(defined $opt_numform)
931 {
932 $numform=$opt_numform;
933 # check : valid format
934 # integer ?
935 if($numform =~ /^(\d+)i(\d+)$/)
936 {
937 $nums_online=$1;
938 $digits_per_num=$2;
939 $num_string="%".$digits_per_num."d";
940  
941 $lower_numform="-";
942 while(length($lower_numform)<($digits_per_num-1))
943 {
944 $lower_numform.="9";
945 }
946 if($lower_numform eq "-")
947 {
948 $lower_numform="0";
949 }
950 $upper_numform="";
951 while(length($upper_numform)<($digits_per_num-1))
952 {
953 $upper_numform.="9";
954 }
955 }
956 # real ?
957 elsif($numform=~/^(\d+)f(\d+)\.(\d+)$/)
958 {
959 $nums_online=$1;
960 $digits_per_num=$2;
961 $fract=$3;
962 $num_string="%".$digits_per_num. "." .$fract."f";
963  
964 $lower_numform="-";
965 while(length($lower_numform)<($digits_per_num-$fract-2))
966 {
967 $lower_numform.="9";
968 }
969 $lower_numform.=".";
970 while(length($lower_numform)<($digits_per_num-1))
971 {
972 $lower_numform.="9";
973 }
974  
975 $upper_numform="";
976 while(length($upper_numform)<($digits_per_num-$fract-2))
977 {
978 $upper_numform.="9";
979 }
980 $upper_numform.=".";
981 while(length($upper_numform)<($digits_per_num-1))
982 {
983 $upper_numform.="9";
984 }
985 }
986 # invalid
987 else
988 {
989 print STDERR "ERROR($0):
990 Invalid number format $numform.\n";
991 exit;
992 }
993 $numform="(".$opt_numform.")";
994 }
995 # use default (5f16.10)
996 else
997 {
998 $numform="(5f16.10)";
999 $num_string="%16.10f";
1000 $nums_online=5;
1001  
1002 $lower_numform="-999.9999999999";
1003 $upper_numform="9999.9999999999";
1004 }
1005  
1006 # we don't use rhs
1007  
1008 # printing formats
1009 printf("%16s%16s%20s%20s",$ptrform,$rowform,$numform,$numform);
1010  
1011 # done with Line 4
1012 print "\n";
1013  
1014 # done with Header section !
1015  
1016 # now data blocks ...
1017  
1018 # print from temp files
1019  
1020 # block1 : column pointers
1021  
1022 open(BLK1,$blk1) || die "Error($0):
1023 Error(code=$!) in opening <$blk1> file.\n";
1024  
1025 # each line of BLK1 contains a single column pointer
1026 $ptr=0;
1027 while(<BLK1>)
1028 {
1029 $ptr++;
1030 $value=$_;
1031  
1032 # check if valid pointer index
1033 if($value !~ /^\d+$/)
1034 {
1035 print STDERR "ERROR($0):
1036 Wrong pointer value<$value> at line $ptr in file<$blk1>.\n";
1037 exit;
1038 }
1039 if($ptr!=1 && ($ptr-1) % $ptrs_online == 0)
1040 {
1041 print "\n";
1042 }
1043 $formatted_cp=sprintf($ptr_string,$value);
1044  
1045 if($formatted_cp>$upper_cpform)
1046 {
1047 print STDERR "ERROR($0):
1048 Floating point overflow.
1049 Column pointer <$formatted_cp> can't be represented with format $ptr_string.\n";
1050 exit 1;
1051 }
1052 print "$formatted_cp";
1053 }
1054  
1055 print "\n";
1056  
1057 # block2 : row indices
1058  
1059 open(BLK2,$blk2) || die "Error($0):
1060 Error(code=$!) in opening <$blk2> file.\n";
1061  
1062 # each row of BLK2 contains a single row pointer
1063  
1064 $ptr=0;
1065 while(<BLK2>)
1066 {
1067 $ptr++;
1068 $value=$_;
1069  
1070 # check if valid pointer index
1071 if($value !~ /^\d+$/)
1072 {
1073 print STDERR "ERROR($0):
1074 Wrong row pointer value <$value> at line $ptr in file<$blk2>.\n";
1075 exit;
1076 }
1077  
1078 if($ptr!=1 && ($ptr-1) % $rowinds_online == 0)
1079 {
1080 print "\n";
1081 }
1082 $formatted_rp=sprintf($row_string,$value);
1083 if($formatted_rp>$upper_rpform)
1084 {
1085 print STDERR "ERROR($0):
1086 Floating point overflow.
1087 Row pointer <$formatted_rp> can't be represented with format $row_string.\n";
1088 exit 1;
1089 }
1090 print "$formatted_rp";
1091 }
1092  
1093 print "\n";
1094  
1095 # block3 : non-zero matrix values
1096  
1097 open(BLK3,$blk3) || die "Error($0):
1098 Error(code=$!) in opening <$blk3> file.\n";
1099  
1100 # BLK3 stores a single non-zero value per line
1101  
1102 $ptr=0;
1103 while(<BLK3>)
1104 {
1105 $ptr++;
1106 s/^\s+//;
1107 s/\s+$//;
1108 $value=$_;
1109  
1110 # check if valid number
1111 if($value !~ /^-?\d+\.?\d*$/)
1112 {
1113 print STDERR "ERROR($0):
1114 Wrong non-zero value<$value> at line $ptr in file<$blk3>.\n";
1115 exit;
1116 }
1117  
1118 if($ptr!=1 && ($ptr-1) % $nums_online == 0)
1119 {
1120 print "\n";
1121 }
1122 $formatted_value=sprintf($num_string,$value);
1123  
1124 if($formatted_value<$lower_numform)
1125 {
1126 print STDERR "ERROR($0):
1127 Floating point underflow.
1128 Value <$formatted_value> can't be represented with format $num_string.\n";
1129 exit 1;
1130 }
1131 if($formatted_value>$upper_numform)
1132 {
1133 print STDERR "ERROR($0):
1134 Floating point overflow.
1135 Value <$formatted_value> can't be represented with format $num_string.\n";
1136 exit 1;
1137 }
1138 print "$formatted_value";
1139 }
1140  
1141 print "\n";
1142  
1143 # printing complete !
1144  
1145 unlink "$blk1";
1146 unlink "$blk2";
1147 unlink "$blk3";
1148  
1149 ##############################################################################
1150  
1151 # ==============================
1152 # Creating Parameter file lap2
1153 # ==============================
1154  
1155 # Now, create the paramters
1156  
1157 if(defined $opt_param)
1158 {
1159 if(-e "lap2")
1160 {
1161 print STDERR "Warning($0):
1162 Parameter file <lap2> already exists, overwrite (y/n)? ";
1163 $ans=<STDIN>;
1164 if($ans !~ /(y|Y)(es)?/)
1165 {
1166 undef $opt_param;
1167 }
1168 }
1169  
1170 if(defined $opt_param) {
1171 open(PAR,">lap2") || die "Error($0):
1172 Error(code=$!) in opening parameter file <lap2>.\n";
1173  
1174 # the need for these warnings is somewhat less now, given that
1175 # we are providing a las2.h file set with fairly large values
1176  
1177 # # the minimum required amt of memory, based on the data we have...
1178 # $lmtnw_minimum = (6*$cols)+(4*$iter)+1+($iter**2);
1179  
1180 # print STDERR "NOTE($0):
1181 # The size of your default work area (LMTNW) in las2.h should be
1182 # greater than $lmtnw_minimum (cols=$cols, iter=$iter).\n";
1183
1184 # the actual value in las2.h for LMTNW is:
1185 ### 900,300,001
1186  
1187 ## if ($lmtnw_minimum >= 900,300,001) {
1188 ## print STDERR "Warning($0):
1189 ## The value of LMTNW in las2.h/SVDPACKC is too small!
1190 ## Please change LMTNW to a value greater than $lmtnw_minimum.\n";
1191 ## }
1192  
1193 # left and right threshold for e-values
1194 $endl="-1.0e-30";
1195 $endr="1.0e-30";
1196 # allowed tolerance
1197 $kappa="1.0e-6";
1198 print PAR "'$id' $iter $maxprs $endl $endr TRUE $kappa 0\n";
1199 }
1200 }
1201  
1202 ##############################################################################
1203  
1204 # ==========================
1205 # SUBROUTINE SECTION
1206 # ==========================
1207  
1208 #-----------------------------------------------------------------------------
1209 #show minimal usage message
1210 sub showminimal()
1211 {
1212 print "Usage: mat2harbo.pl [OPTIONS] MATRIX";
1213 print "\nTYPE mat2harbo.pl --help for help\n";
1214 }
1215  
1216 #-----------------------------------------------------------------------------
1217 #show help
1218 sub showhelp()
1219 {
1220 print "Usage: mat2harbo.pl [OPTIONS] MATRIX
1221  
1222 Converts a given matrix in SenseClusters' sparse format to Harwell-Boeing
1223 sparse matrix format.
1224  
1225 MATRIX
1226 Matrix in SenseClusters' sparse matrix format that is to be converted
1227 into Harwell-Boeing sparse matrix format.
1228  
1229 OPTIONS:
1230  
1231 --title TITLE
1232 Specify the TITLE to be used for the MATRIX at Line1 of Harwell-Boeing
1233 format. Default TITLE uses the MATRIX file name.
1234  
1235 --id ID
1236 Specify the ID to be used to identify the MATRIX at Line1 of Harwell-
1237 Boeing format. Default ID is 'harbomat'.
1238  
1239 --cpform CPFORM
1240 Specify the Column Pointer FORMat. Default is 10i8.
1241  
1242 --rpform RPFORM
1243 Specify the Row Pointer FORMat. Default is 10i8.
1244  
1245 --numform NUMFORM
1246 Specify the NUMber FORMat to represent the non-zero MATRIX values.
1247 Default is 5f16.10.
1248  
1249 Parameter Options :
1250  
1251 These options help to automatically create the parameter file lap2 used by
1252 the las2.c program in SVDPack.
1253  
1254 --param
1255 Creates the parameter file lap2 that can be directly used while
1256 running las2.
1257  
1258 --k K
1259 Sets the value of maxprs parameter in lap2 file to K.
1260 Default K=300
1261  
1262 --rf RF
1263 Reduces the column space of a given MATRIX by scaling factor RF
1264 such that maxprs = #columns(MATRIX) / RF , where RF >=1
1265 Default RF = 10
1266  
1267 Both --k and --rf allow user to control the maxprs value in lap2 file
1268 which specifies the number of singular triplets to be returned by las2.
1269  
1270 If both --k and --rf are specified, maxprs = min(K,#columns(MATRIX)/RF)
1271  
1272 --iter ITER
1273 Specifies number of iterations (or lanmax) in LAP2.
1274 Default ITER = min((3 * maxprs), #columns(MATRIX))
1275  
1276 ITER should be < = #columns(MATRIX)
1277 and
1278 ITER should be > = (maxprs = min(K,#columns(MATRIX)/RF))
1279  
1280 --help
1281 Displays this message.
1282  
1283 --version
1284 Displays the version information.
1285  
1286 Type 'perldoc mat2harbo.pl' to view detailed documentation of mat2harbo.\n";
1287  
1288 }
1289  
1290 #------------------------------------------------------------------------------
1291 #version information
1292 sub showversion()
1293 {
1294 print '$Id: mat2harbo.pl,v 1.35 2008/03/31 21:31:13 tpederse Exp $';
1295 ## print "mat2harbo.pl - Version 0.4";
1296 print "\nConvert a matrix in SenseClusters sparse format to Harwell-Boeing sparse format\n";
1297 ## print "\nCopyright (c) 2002-2006, Amruta Purandare & Ted Pedersen\n";
1298 ## print "Date of Last Update: 11/06/2004\n";
1299 }
1300  
1301 #############################################################################
1302  
1303 =head1 AUTHORS
1304  
1305 Amruta Purandare, University of Pittsburgh
1306  
1307 Ted Pedersen, University of Minnesota, Duluth
1308 tpederse at d.umn.edu
1309  
1310 =head1 COPYRIGHT
1311  
1312 Copyright (c) 2003-2008, Amruta Purandare and Ted Pedersen
1313  
1314 This program is free software; you can redistribute it and/or modify it under
1315 the terms of the GNU General Public License as published by the Free Software
1316 Foundation; either version 2 of the License, or (at your option) any later
1317 version.
1318  
1319 This program is distributed in the hope that it will be useful, but WITHOUT
1320 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
1321 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
1322  
1323 You should have received a copy of the GNU General Public License along with
1324 this program; if not, write to
1325  
1326 The Free Software Foundation, Inc.,
1327 59 Temple Place - Suite 330,
1328 Boston, MA 02111-1307, USA.
1329  
1330 =cut
1331