-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.c
1640 lines (1451 loc) · 54.2 KB
/
test.c
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
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//------------------------------------------------------------------------------
// SPEX_Update/Test/test.c: support functions for the test programs
//------------------------------------------------------------------------------
// SPEX_Update: (c) 2020-2023, Christopher Lourenco, Jinhao Chen,
// Timothy A. Davis, and Erick Moreno-Centeno. All Rights Reserved.
// SPDX-License-Identifier: GPL-2.0-or-later or LGPL-3.0-or-later
//------------------------------------------------------------------------------
#include "test.h"
//------------------------------------------------------------------------------
// SPEX_mmread: read a matrix from a Matrix Market file, modified from LAGraph
//------------------------------------------------------------------------------
// The file format used here is compatible with all variations of the Matrix
// Market "coordinate" and "array" format (http://www.nist.gov/MatrixMarket).
// The format is fully described in SPEX/Doc/MatrixMarket.pdf, and
// summarized here (with extensions for SPEX).
// The first line of the file starts with %%MatrixMarket, with the following
// format:
// %%MatrixMarket matrix <fmt> <type> <storage>
// <fmt> is one of: coordinate or array. The former is a sparse matrix in
// triplet form. The latter is a dense matrix in column-major form.
// Both formats are returned as a SPEX_Matrix.
// <type> is one of: real, complex, pattern, or integer. The real,
// integer, and pattern formats are returned as SPEX_FP64, SPEX_INT64, and
// SPEX_BOOL, respectively, but these types are modified the %GraphBLAS
// structured comment described below. Complex matrices are returned
// using the SPEX_ComplexFP64 type (which is a GraphBLAS type corresponding
// to the ANSI C11 double complex type).
// <storage> is one of: general, Hermitian, symmetric, or skew-symmetric.
// The Matrix Market format is case-insensitive, so "hermitian" and
// "Hermitian" are treated the same).
// Not all combinations are permitted. Only the following are meaningful:
// (1) (coordinate or array) x (real, integer, or complex)
// x (general, symmetric, or skew-symmetric)
// (2) (coordinate or array) x (complex) x (Hermitian)
// (3) (coodinate) x (pattern) x (general or symmetric)
// Any other lines starting with "%" are treated as comments, and are ignored.
// Comments may be interspersed throughout the file. Blank lines are ignored.
// The Matrix Market header is optional in this routine (it is not optional in
// the Matrix Market format). If not present, the <fmt> defaults to
// coordinate, <type> defaults to real, and <storage> defaults to general. The
// remaining lines are space delimited, and free format (one or more spaces can
// appear, and each field has arbitrary width).
// The Matrix Market file <fmt> can be coordinate or array:
// coordinate: for this format, the first non-comment line must appear,
// and it must contain three integers:
// nrows ncols nvals
// For example, a 5-by-12 matrix with 42 entries would have:
// 5 12 42
// Each of the remaining lines defines one entry. The order is
// arbitrary. If the Matrix Market <type> is real or integer, each
// line contains three numbers: row index, column index, and value.
// For example, if A(3,4) is equal to 5.77, a line:
// 3 4 5.77
// would appear in the file. The indices in the Matrix Market are
// 1-based, so this entry becomes A(2,3) in the SPEX_Matrix returned to
// the caller. If the <type> is pattern, then only the row and column
// index appears. If <type> is complex, four values appear. If
// A(8,4) has a real part of 6.2 and an imaginary part of -9.3, then
// the line is:
// 8 4 6.2 -9.3
// and since the file is 1-based but a GraphBLAS matrix is always
// 0-based, one is subtracted from the row and column indices in the
// file, so this entry becomes A(7,3).
// array: for this format, the first non-comment line must appear, and
// it must contain just two integers:
// nrows ncols
// A 5-by-12 matrix would thus have the line
// 5 12
// Each of the remaining lines defines one entry, in column major
// order. If the <type> is real or integer, this is the value of the
// entry. An entry if <type> of complex consists of two values, the
// real and imaginary part. The <type> cannot be pattern in this
// case.
// For both coordinate and array formats, real and complex values may use
// the terms INF, +INF, -INF, and NAN to represent floating-point infinity
// and NaN values.
// The <storage> token is general, symmetric, skew-symmetric, or Hermitian:
// general: the matrix has no symmetry properties (or at least none
// that were exploited when the file was created).
// symmetric: A(i,j) == A(j,i). Only entries on or below the diagonal
// appear in the file. Each off-diagonal entry in the file creates
// two entries in the SPEX_Matrix that is returned.
// skew-symmetric: A(i,j) == -A(i,j). There are no entries on the
// diagonal. Only entries below the diagonal appear in the file.
// Each off-diagonal entry in the file creates two entries in the
// SPEX_Matrix that is returned.
// Hermitian: square complex matrix with A(i,j) = conj (A(j,i)).
// All entries on the diagonal are real. Each off-diagonal entry in
// the file creates two entries in the SPEX_Matrix that is returned.
// NOTE: THIS IS NOT HANDLED BY THIS FUNCTION, SINCE IT IS MEANINGLESS
// FOR LINEAR PROGRAMS
// According to the Matrix Market format, entries are always listed in
// column-major order. This rule is follwed by SPEX_mmwrite. However,
// SPEX_mmread can read the entries in any order.
// Parts of this code are from SuiteSparse/CHOLMOD/Check/cholmod_read.c, and
// are used here by permission of the author of CHOLMOD/Check (T. A. Davis).
//------------------------------------------------------------------------------
// get_line
//------------------------------------------------------------------------------
// Read one line of the file, return true if successful, false if EOF.
// The string is returned in buf, converted to lower case.
static inline bool get_line
(
FILE *f, // file open for reading
char *buf // size MAXLINE+1
)
{
// check inputs
ASSERT (f != NULL);
ASSERT (buf != NULL);
// read the line from the file
buf [0] = '\0' ;
buf [1] = '\0' ;
if (fgets (buf, MAXLINE, f) == NULL)
{
// EOF or other I/O error
return (false);
}
buf [MAXLINE] = '\0' ;
// convert the string to lower case
for (int k = 0 ; k < MAXLINE && buf [k] != '\0' ; k++)
{
buf [k] = tolower (buf [k]);
}
return (true);
}
//------------------------------------------------------------------------------
// is_blank_line
//------------------------------------------------------------------------------
// returns true if buf is a blank line or comment, false otherwise.
static inline bool is_blank_line
(
char *buf // size MAXLINE+1, never NULL
)
{
// check inputs
ASSERT (buf != NULL);
// check if comment line
if (buf [0] == '%')
{
// line is a comment
return (true);
}
// check if blank line
for (int k = 0 ; k <= MAXLINE ; k++)
{
int c = buf [k] ;
if (c == '\0')
{
// end of line
break ;
}
if (!isspace (c))
{
// non-space character; this is not an error
return (false);
}
}
// line is blank
return (true);
}
//------------------------------------------------------------------------------
// read_double
//------------------------------------------------------------------------------
// Read a single double value from a string. The string may be any string
// recognized by sscanf, or inf, -inf, +inf, or nan. The token infinity is
// also OK instead of inf (only the first 3 letters of inf* or nan* are
// significant, and the rest are ignored).
static inline bool read_double // true if successful, false if failure
(
char *p, // string containing the value
double *rval // value to read in
)
{
while (*p && isspace (*p)) p++ ; // skip any spaces
if ((strncmp (p, "inf", 3) == 0) || (strncmp (p, "+inf", 4) == 0))
{
(*rval) = INFINITY ;
}
else if (strncmp (p, "-inf", 4) == 0)
{
(*rval) = -INFINITY ;
}
else if (strncmp (p, "nan", 3) == 0)
{
(*rval) = NAN ;
}
else
{
if (sscanf (p, "%lg", rval) != 1)
{
// bad file format, EOF, or other I/O error
return (false);
}
}
return (true);
}
//------------------------------------------------------------------------------
// read_entry
//------------------------------------------------------------------------------
static inline bool read_entry // true if successful, false if failure
(
char *p, // string containing the value
SPEX_type type, // type of value to read, either SPEX_INT64 or SPEX_FP64
bool pattern, // if true, then the value is 1
char *x // value read in, a pointer to space of size of the type
)
{
int64_t ival = 1 ;
double rval = 1;
while (*p && isspace (*p)) p++ ; // skip any spaces
// printf ("read entry [%s]: ", p);
if (type == SPEX_INT64)
{
if (!pattern && sscanf (p, "%" SCNd64, &ival) != 1) return (false);
// printf ("%" PRId64 "\n", ival);
int64_t *result = (int64_t *) x ;
result [0] = (int64_t) ival ;
}
else if (type == SPEX_FP64)
{
if (!pattern && !read_double (p, &rval)) return (false);
// printf ("%g\n", rval);
double *result = (double *) x ;
result [0] = rval ;
}
else
{
// type not supported
printf ("SPEX_mmread: read_entry: type not supported\n");
return (false);
}
return (true);
}
//------------------------------------------------------------------------------
// negate_scalar: negate a scalar value
//------------------------------------------------------------------------------
// negate the scalar x. Do nothing for bool or uint*.
static inline void negate_scalar
(
SPEX_type type,
void *x
)
{
if (type == SPEX_INT64)
{
int64_t *value = (int64_t *) x ;
(*value) = - (*value);
}
else if (type == SPEX_FP64)
{
double *value = (double *) x ;
(*value) = - (*value);
}
}
//------------------------------------------------------------------------------
// set_value
//------------------------------------------------------------------------------
// A(i,j) = x using SPEX_Matrix_setElement_<type>. No typecasting is done.
static inline SPEX_info set_value
(
SPEX_matrix A,
SPEX_type type,
int64_t i,
int64_t j,
char *x
)
{
int64_t Anz = A->nz++;
A->i[Anz] = i;
A->j[Anz] = j;
if (type == SPEX_INT64)
{
int64_t *value = (int64_t *) x ;
A->x.int64[Anz] = *value;
return SPEX_OK;
}
else if (type == SPEX_FP64)
{
double *value = (double *) x ;
A->x.fp64[Anz] = *value;
return SPEX_OK;
}
else
{
// type not supported
return (SPEX_PANIC);
}
}
//------------------------------------------------------------------------------
// SPEX_mmread
//------------------------------------------------------------------------------
SPEX_info SPEX_mmread
(
SPEX_matrix *A_handle,// handle of matrix to create
FILE *f, // file to read from, already open
SPEX_options option
)
{
//--------------------------------------------------------------------------
// check inputs
//--------------------------------------------------------------------------
if (A_handle == NULL || f == NULL)
{
// input arguments invalid
printf ("SPEX_mmread: bad args\n");
return (SPEX_INCORRECT_INPUT);
}
*A_handle = NULL;
SPEX_matrix A = NULL;
//--------------------------------------------------------------------------
// set the default properties
//--------------------------------------------------------------------------
MM_fmt_enum MM_fmt = MM_coordinate ;
MM_type_enum MM_type = MM_real ;
MM_storage_enum MM_storage = MM_general ;
SPEX_type type = SPEX_FP64 ;
uint64_t nrows = 0, ncols = 0, nvals = 0 ;
//--------------------------------------------------------------------------
// read the Matrix Market header
//--------------------------------------------------------------------------
// Read the header. This consists of zero or more comment lines (blank, or
// starting with a "%" in the first column), followed by a single data line
// containing two or three numerical values. The first line is normally:
//
// %%MatrixMarket matrix <fmt> <type> <storage>
//
// but this is optional.
// If the %%MatrixMarket line is not present, then the <fmt> <type> and
// <storage> are implicit. If the first data line contains 3 items,
// then the implicit header is:
//
// %%MatrixMarket matrix coordinate real general
//
// If the first data line contains 2 items (nrows ncols), then the implicit
// header is:
//
// %%MatrixMarket matrix array real general
//
// The implicit header is an extension of the Matrix Market format.
char buf [MAXLINE+1] ;
bool got_mm_header = false ;
for (int64_t line = 1 ; get_line (f, buf); line++)
{
//----------------------------------------------------------------------
// parse the line
//----------------------------------------------------------------------
if ((line == 1) && (strncmp (buf, "%%matrixmarket", 14) == 0))
{
//------------------------------------------------------------------
// read a Matrix Market header
//------------------------------------------------------------------
// %%MatrixMarket matrix <fmt> <type> <storage>
// if present, it must be the first line in the file.
got_mm_header = true ;
char *p = buf + 14 ;
//------------------------------------------------------------------
// get "matrix" token and discard it
//------------------------------------------------------------------
while (*p && isspace (*p)) p++ ; // skip any leading spaces
// printf ("header now [%s]\n", p);
// printf ("compare %d\n", (strncmp (p, "matrix", 6)));
if (strncmp (p, "matrix", 6) != 0)
{
// invalid Matrix Market object
printf ("SPEX_mmread: bad object\n");
return (SPEX_INCORRECT_INPUT);
}
p += 6 ; // skip past token "matrix"
//------------------------------------------------------------------
// get the fmt token
//------------------------------------------------------------------
while (*p && isspace (*p)) p++ ; // skip any leading spaces
if (strncmp (p, "coordinate", 10) == 0)
{
MM_fmt = MM_coordinate ;
p += 10 ;
}
else if (strncmp (p, "array", 5) == 0)
{
MM_fmt = MM_array ;
p += 5 ;
}
else
{
// invalid Matrix Market format
printf ("SPEX_mmread: bad format\n");
return (SPEX_INCORRECT_INPUT);
}
//------------------------------------------------------------------
// get the Matrix Market type token
//------------------------------------------------------------------
while (*p && isspace (*p)) p++ ; // skip any leading spaces
if (strncmp (p, "real", 4) == 0)
{
MM_type = MM_real ;
type = SPEX_FP64 ;
p += 4 ;
}
else if (strncmp (p, "integer", 7) == 0)
{
MM_type = MM_integer ;
type = SPEX_INT64 ;
p += 7 ;
}
else if (strncmp (p, "pattern", 7) == 0)
{
MM_type = MM_pattern ;
type = SPEX_INT64 ;
p += 7 ;
}
else
{
// invalid Matrix Market type
printf ("SPEX_mmread: bad type\n");
return (SPEX_INCORRECT_INPUT);
}
//------------------------------------------------------------------
// get the storage token
//------------------------------------------------------------------
while (*p && isspace (*p)) p++ ; // skip any leading spaces
if (strncmp (p, "general", 7) == 0)
{
MM_storage = MM_general ;
}
else if (strncmp (p, "symmetric", 9) == 0)
{
MM_storage = MM_symmetric ;
}
else if (strncmp (p, "skew-symmetric", 14) == 0)
{
MM_storage = MM_skew_symmetric ;
}
else
{
// invalid Matrix Market storage
printf ("SPEX_mmread: bad type\n");
return (SPEX_INCORRECT_INPUT);
}
//------------------------------------------------------------------
// ensure the combinations are valid
//------------------------------------------------------------------
if (MM_type == MM_pattern)
{
// (coodinate) x (pattern) x (general or symmetric)
if (! (MM_fmt == MM_coordinate &&
(MM_storage == MM_general ||
MM_storage == MM_symmetric)))
{
// invalid combination
printf ("SPEX_mmread: bad pattern combo\n");
return (SPEX_INCORRECT_INPUT);
}
}
}
else if (is_blank_line (buf))
{
// -----------------------------------------------------------------
// blank line or comment line
// -----------------------------------------------------------------
continue ;
}
else
{
// -----------------------------------------------------------------
// read the first data line and return
// -----------------------------------------------------------------
// format: [nrows ncols nvals] or just [nrows ncols]
int nitems = sscanf (buf, "%" SCNu64 " %" SCNu64 " %" SCNu64,
&nrows, &ncols, &nvals);
if (nitems == 2)
{
// a dense matrix
if (!got_mm_header)
{
// if no header, treat it as if it were
// %%MatrixMarket matrix array real general
MM_fmt = MM_array ;
MM_type = MM_real ;
MM_storage = MM_general ;
type = SPEX_FP64 ;
}
nvals = nrows * ncols ;
}
else if (nitems == 3)
{
// a sparse matrix
if (!got_mm_header)
{
// if no header, treat it as if it were
// %%MatrixMarket matrix coordinate real general
MM_fmt = MM_coordinate ;
MM_type = MM_real ;
MM_storage = MM_general ;
type = SPEX_FP64 ;
}
}
else
{
// wrong number of items in first data line
printf ("SPEX_mmread: bad 1st line\n");
return (SPEX_INCORRECT_INPUT);
}
if (nrows != ncols)
{
if (! (MM_storage == MM_general))
{
// a rectangular matrix must be in the general storage
printf ("SPEX_mmread: bad rectangular\n");
return (SPEX_INCORRECT_INPUT);
}
}
//------------------------------------------------------------------
// header has been read in
//------------------------------------------------------------------
break ;
}
}
if (MM_storage == MM_symmetric || MM_storage == MM_skew_symmetric)
{
nvals *= 2;
}
//--------------------------------------------------------------------------
// create the matrix
//--------------------------------------------------------------------------
SPEX_info info = SPEX_matrix_allocate (&A, SPEX_TRIPLET, type, nrows,
ncols, nvals, false, true, option);
if (info != SPEX_OK)
{
// failed to construct matrix
// printf ("mmread: failed to construct A\n");
return (info);
}
//--------------------------------------------------------------------------
// quick return for empty matrix
//--------------------------------------------------------------------------
if (nrows == 0 || ncols == 0 || nvals == 0)
{
// success: return an empty matrix. This is not an error.
return (SPEX_OK);
}
//--------------------------------------------------------------------------
// read the entries
//--------------------------------------------------------------------------
for (uint64_t k = 0 ; k < nvals ; k++)
{
//----------------------------------------------------------------------
// get the next triplet, skipping blank lines and comment lines
//----------------------------------------------------------------------
uint64_t i, j ;
char x [MAXLINE] ;
for ( ; ; )
{
//------------------------------------------------------------------
// read the file until finding the next triplet
//------------------------------------------------------------------
if (!get_line (f, buf))
{
// premature end of file - not enough triplets read in
SPEX_matrix_free (&A, option);
printf ("SPEX:mmread: premature EOF\n");
return (SPEX_INCORRECT_INPUT);
}
if (is_blank_line (buf))
{
// blank line or comment
continue ;
}
//------------------------------------------------------------------
// get the row and column index
//------------------------------------------------------------------
char *p ;
if (MM_fmt == MM_array)
{
// array format, column major order
i = k % nrows ;
j = k / nrows ;
p = buf ;
// printf ("array now [%s]\n", p);
}
else
{
// coordinate format; read the row and column index
p = buf ;
if (sscanf (p, "%" SCNu64 " %" SCNu64, &i, &j) != 2)
{
// EOF or other I/O error
SPEX_matrix_free (&A, option);
printf ("SPEX_mmread: I/O error on indices\n");
return (SPEX_INCORRECT_INPUT);
}
// convert from 1-based to 0-based.
i-- ;
j-- ;
// printf ("got (%g,%g)\n", (double) i, (double) j);
// advance p to the 3rd token to get the value of the entry
while (*p && isspace (*p)) p++ ; // skip any leading spaces
while (*p && !isspace (*p)) p++ ; // skip nrows
while (*p && isspace (*p)) p++ ; // skip any spaces
while (*p && !isspace (*p)) p++ ; // skip nrows
// printf ("now [%s]\n", p);
}
//------------------------------------------------------------------
// read the value of the entry
//------------------------------------------------------------------
while (*p && isspace (*p)) p++ ; // skip any spaces
if (!read_entry (p, type, MM_type == MM_pattern, x))
{
// EOF or other I/O error, or value of entry out of range
SPEX_matrix_free (&A, option);
printf ("SPEX_mmread: I/O error on value\n");
return (SPEX_INCORRECT_INPUT);
}
//------------------------------------------------------------------
// set the value in the matrix
//------------------------------------------------------------------
info = set_value (A, type, i, j, x);
if (info != SPEX_OK)
{
// unable to set element: invalid indices, or out of memory
printf ("mmread: unable to set element\n");
SPEX_matrix_free (&A, option);
return (info);
}
// GxB_fprint (*A, GxB_COMPLETE, stdout);
//------------------------------------------------------------------
// also set the A(j,i) entry, if symmetric
//------------------------------------------------------------------
if (i != j && MM_storage != MM_general)
{
if (MM_storage == MM_symmetric)
{
info = set_value (A, type, j, i, x);
}
else if (MM_storage == MM_skew_symmetric)
{
negate_scalar (type, x);
info = set_value (A, type, j, i, x);
}
if (info != SPEX_OK)
{
// unable to set element: invalid indices, or out of memory
SPEX_matrix_free (&A, option);
// printf ("mmread: unable to set symmetric element\n");
return (info);
}
}
// one more entry has been read in
break ;
}
}
*A_handle = A;
return (SPEX_OK);
}
// -------------------------------------------------------------------------
// read the matrices and construct LP obj
// -------------------------------------------------------------------------
#define MY_MAT_X(M, i) \
(((M)->type == SPEX_FP64) ? (M)->x.fp64[i] : (M)->x.int64[i])
#define MY_SHIFT_X(M, i, j) \
{ \
if ((M)->type == SPEX_FP64) \
{ \
(M)->x.fp64[i] = (M)->x.fp64[j];\
} \
else \
{ \
(M)->x.int64[i] = (M)->x.int64[j];\
} \
}
#define MY_SHIFT_NEG_X(M, i, j) \
{ \
if ((M)->type == SPEX_FP64) \
{ \
(M)->x.fp64[i] = -1*((M)->x.fp64[j]);\
} \
else \
{ \
(M)->x.int64[i] = -1*((M)->x.int64[j]);\
} \
}
#define MY_FREE_WORK \
{ \
SPEX_matrix_free(&MA, option);\
SPEX_matrix_free(&Mlb, option);\
SPEX_matrix_free(&Mub, option);\
SPEX_FREE(I); \
SPEX_FREE(J); \
SPEX_FREE(X); \
SPEX_matrix_free(&MA_CSC, option);\
SPEX_matrix_free(&Mb, option);\
SPEX_matrix_free(&Mc, option);\
if (File != NULL) fclose(File);\
}
#define MY_FREE_ALL \
{ \
MY_FREE_WORK; \
}
#define OK1(method) \
{ \
info = method; \
if (info != SPEX_OK) \
{ \
MY_FREE_ALL; \
return info; \
} \
}
SPEX_info SPEX_construct_LP
(
glp_prob *LP,
SPEX_matrix *A_handle,
SPEX_matrix *b_handle,
SPEX_matrix *c_handle,
double *z0_handle,
char *file_name,
SPEX_options option
)
{
SPEX_info info;
double lb, ub;
int file_name_len = strlen(file_name);
SPEX_matrix MA = NULL, Mb = NULL, Mc = NULL, Mlb = NULL, Mub = NULL;
SPEX_matrix MA_CSC = NULL, tmp = NULL;
int *I = NULL, *J = NULL;
double *X = NULL;
FILE *File = NULL;
char *suffix = "";
// -------------------------------------------------------------------------
// read A matrix
// -------------------------------------------------------------------------
suffix = ".mtx";
strcpy(file_name+file_name_len,suffix);
printf("reading file %s\n",file_name);
File = fopen(file_name, "r");
if (File == NULL)
{
perror("Error while opening file");
MY_FREE_ALL;
return SPEX_INCORRECT_INPUT;
}
// Read matrix from given file as a triplet matrix
OK1(SPEX_mmread(&MA, File, option));
// convert to a CSC matrix
OK1(SPEX_matrix_copy(&MA_CSC, SPEX_CSC, MA->type, MA, option));
int64_t nz = MA->nz;
SPEX_matrix_free(&MA, option);
fclose(File); File = NULL;
// -------------------------------------------------------------------------
// read b matrix
// -------------------------------------------------------------------------
suffix = "_b.mtx";
strcpy(file_name+file_name_len,suffix);
printf("reading file %s\n",file_name);
File = fopen(file_name, "r");
if (File == NULL)
{
perror("Error while opening file\n");
MY_FREE_ALL;
return SPEX_INCORRECT_INPUT;
}
OK1(SPEX_mmread(&Mb, File, option));
fclose(File); File = NULL;
// convert to a dense matrix
OK1(SPEX_matrix_copy(&tmp, SPEX_DENSE, Mb->type, Mb, option));
SPEX_matrix_free(&Mb, option);
Mb = tmp;
// -------------------------------------------------------------------------
// read c matrix
// -------------------------------------------------------------------------
suffix = "_c.mtx";
strcpy(file_name+file_name_len,suffix);
printf("reading file %s\n",file_name);
File = fopen(file_name, "r");
if (File == NULL)
{
perror("Error while opening file\n");
MY_FREE_ALL;
return SPEX_INCORRECT_INPUT;
}
OK1(SPEX_mmread(&Mc, File, option));
fclose(File);File = NULL;
// convert to a dense matrix
OK1(SPEX_matrix_copy(&tmp, SPEX_DENSE, Mc->type, Mc, option));
SPEX_matrix_free(&Mc, option);
Mc = tmp;
// -------------------------------------------------------------------------
// read lb and ub matrix
// -------------------------------------------------------------------------
//read lower bound file
suffix = "_lo.mtx";
strcpy(file_name+file_name_len,suffix);
printf("reading file %s\n",file_name);
File = fopen(file_name, "r");
if (File == NULL)
{
perror("Error while opening file\n");
MY_FREE_ALL;
return SPEX_INCORRECT_INPUT;
}
OK1(SPEX_mmread(&Mlb, File, option));
fclose(File); File = NULL;
// convert to a dense matrix
OK1(SPEX_matrix_copy(&tmp, SPEX_DENSE, Mlb->type, Mlb, option));
SPEX_matrix_free(&Mlb, option);
Mlb = tmp;
// read upper bound file
suffix = "_hi.mtx";
strcpy(file_name+file_name_len,suffix);
printf("reading file %s\n",file_name);
File = fopen(file_name, "r");
if (File == NULL)
{
perror("Error while opening file\n");
MY_FREE_ALL;
return SPEX_INCORRECT_INPUT;
}
OK1(SPEX_mmread(&Mub, File, option));
fclose(File);File = NULL;
// convert to a dense matrix
OK1(SPEX_matrix_copy(&tmp, SPEX_DENSE, Mub->type, Mub, option));
SPEX_matrix_free(&Mub, option);
Mub = tmp;
// -------------------------------------------------------------------------
// check dimension
// -------------------------------------------------------------------------
int64_t nvars = MA_CSC->n;
int64_t neqs = MA_CSC->m;
if (Mlb->m != nvars || Mub->m != nvars || Mc->m != nvars || Mb->m != neqs)
{
printf("Dimension unmatched!\n");
MY_FREE_ALL;
return SPEX_INCORRECT_INPUT;
}
// -------------------------------------------------------------------------
// find out if there are any fixed variables, and remove all fixed
// variables if exist
// -------------------------------------------------------------------------
int64_t num_nz_removed = 0;
int64_t num_var_removed = 0;
for (int64_t j = 0; j < nvars; j++)
{
lb = (double) (MY_MAT_X(Mlb, j));
ub = (double) (MY_MAT_X(Mub, j));
//if (lb<0)printf("lb[%ld]=%f\n",j+1,lb);
if (lb == ub)
{
//printf("x[%ld]=%f\n",j+1,lb);
// remove all columns in A corresponding to these variables
for (int64_t p = MA_CSC->p[j]; p < MA_CSC->p[j+1]; p++)