pathrex-sys 0.1.0

Native FFI bindings for SuiteSparse:GraphBLAS and LAGraph used by the pathrex crate.
Documentation
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
//------------------------------------------------------------------------------
// LAGraph_MMRead: read a matrix from a Matrix Market file
//------------------------------------------------------------------------------

// LAGraph, (c) 2019-2022 by The LAGraph Contributors, All Rights Reserved.
// SPDX-License-Identifier: BSD-2-Clause
//
// For additional details (including references to third party source code and
// other files) see the LICENSE file or contact permission@sei.cmu.edu. See
// Contributors.txt for a full list of contributors. Created, in part, with
// funding and support from the U.S. Government (see Acknowledgments.txt file).
// DM22-0790

// Contributed by Timothy A. Davis, Texas A&M University

//------------------------------------------------------------------------------

// LAGraph_MMRead: read a matrix from a Matrix Market file

// 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).

// The Matrix Market format is described at:
// https://math.nist.gov/MatrixMarket/formats.html

// Return values:
//  GrB_SUCCESS: input file and output matrix are valid
//  LAGRAPH_IO_ERROR: the input file cannot be read or has invalid content
//  GrB_NULL_POINTER:  A or f are NULL on input
//  GrB_NOT_IMPLEMENTED: complex types not yet supported
//  other: return values directly from GrB_* methods

#define LG_FREE_WORK                    \
{                                       \
    LAGraph_Free ((void **) &I, NULL) ; \
    LAGraph_Free ((void **) &J, NULL) ; \
    LAGraph_Free ((void **) &X, NULL) ; \
}

#define LG_FREE_ALL                     \
{                                       \
    LG_FREE_WORK ;                      \
    GrB_free (A) ;                      \
}

#include "LG_internal.h"

//------------------------------------------------------------------------------
// 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 (MATCH (p, "inf", 3) || MATCH (p, "+inf", 4))
    {
        (*rval) = INFINITY ;
    }
    else if (MATCH (p, "-inf", 4))
    {
        (*rval) = -INFINITY ;
    }
    else if (MATCH (p, "nan", 3))
    {
        (*rval) = NAN ;
    }
    else
    {
        if (sscanf (p, "%lg", rval) != 1)
        {
            // invalid file format, EOF, or other I/O error
            return (false) ;
        }
    }
    return (true) ;
}

//------------------------------------------------------------------------------
// read_entry: read a numerical value and typecast to the given type
//------------------------------------------------------------------------------

static inline bool read_entry   // returns true if successful, false if failure
(
    char *p,        // string containing the value
    GrB_Type type,  // type of value to read
    bool structural,   // if true, then the value is 1
    uint8_t *x      // value read in, a pointer to space of size of the type
)
{

    int64_t ival = 1 ;
    double rval = 1, zval = 0 ;

    while (*p && isspace (*p)) p++ ;   // skip any spaces

    if (type == GrB_BOOL)
    {
        if (!structural && sscanf (p, "%" SCNd64, &ival) != 1) return (false) ;
        if (ival < 0 || ival > 1)
        {
            // entry out of range
            return (false) ;
        }
        bool *result = (bool *) x ;
        result [0] = (bool) ival ;
    }
    else if (type == GrB_INT8)
    {
        if (!structural && sscanf (p, "%" SCNd64, &ival) != 1) return (false) ;
        if (ival < INT8_MIN || ival > INT8_MAX)
        {
            // entry out of range
            return (false) ;
        }
        int8_t *result = (int8_t *) x ;
        result [0] = (int8_t) ival ;
    }
    else if (type == GrB_INT16)
    {
        if (!structural && sscanf (p, "%" SCNd64, &ival) != 1) return (false) ;
        if (ival < INT16_MIN || ival > INT16_MAX)
        {
            // entry out of range
            return (false) ;
        }
        int16_t *result = (int16_t *) x ;
        result [0] = (int16_t) ival ;
    }
    else if (type == GrB_INT32)
    {
        if (!structural && sscanf (p, "%" SCNd64, &ival) != 1) return (false) ;
        if (ival < INT32_MIN || ival > INT32_MAX)
        {
            // entry out of range
            return (false) ;
        }
        int32_t *result = (int32_t *) x ;
        result [0] = (int32_t) ival ;
    }
    else if (type == GrB_INT64)
    {
        if (!structural && sscanf (p, "%" SCNd64, &ival) != 1) return (false) ;
        int64_t *result = (int64_t *) x ;
        result [0] = (int64_t) ival ;
    }
    else if (type == GrB_UINT8)
    {
        if (!structural && sscanf (p, "%" SCNd64, &ival) != 1) return (false) ;
        if (ival < 0 || ival > UINT8_MAX)
        {
            // entry out of range
            return (false) ;
        }
        uint8_t *result = (uint8_t *) x ;
        result [0] = (uint8_t) ival ;
    }
    else if (type == GrB_UINT16)
    {
        if (!structural && sscanf (p, "%" SCNd64, &ival) != 1) return (false) ;
        if (ival < 0 || ival > UINT16_MAX)
        {
            // entry out of range
            return (false) ;
        }
        uint16_t *result = (uint16_t *) x ;
        result [0] = (uint16_t) ival ;
    }
    else if (type == GrB_UINT32)
    {
        if (!structural && sscanf (p, "%" SCNd64, &ival) != 1) return (false) ;
        if (ival < 0 || ival > UINT32_MAX)
        {
            // entry out of range
            return (false) ;
        }
        uint32_t *result = (uint32_t *) x ;
        result [0] = (uint32_t) ival ;
    }
    else if (type == GrB_UINT64)
    {
        uint64_t uval = 1 ;
        if (!structural && sscanf (p, "%" SCNu64, &uval) != 1) return (false) ;
        uint64_t *result = (uint64_t *) x ;
        result [0] = (uint64_t) uval ;
    }
    else if (type == GrB_FP32)
    {
        if (!structural && !read_double (p, &rval)) return (false) ;
        float *result = (float *) x ;
        result [0] = (float) rval ;
    }
    else if (type == GrB_FP64)
    {
        if (!structural && !read_double (p, &rval)) return (false) ;
        double *result = (double *) x ;
        result [0] = rval ;
    }
#if 0
    else if (type == GxB_FC32)
    {
        if (!structural && !read_double (p, &rval)) return (false) ;
        while (*p && !isspace (*p)) p++ ;   // skip real part
        if (!structural && !read_double (p, &zval)) return (false) ;
        float *result = (float *) x ;
        result [0] = (float) rval ;     // real part
        result [1] = (float) zval ;     // imaginary part
    }
    else if (type == GxB_FC64)
    {
        if (!structural && !read_double (p, &rval)) return (false) ;
        while (*p && !isspace (*p)) p++ ;   // skip real part
        if (!structural && !read_double (p, &zval)) return (false) ;
        double *result = (double *) x ;
        result [0] = rval ;     // real part
        result [1] = zval ;     // imaginary part
    }
#endif

    return (true) ;
}

//------------------------------------------------------------------------------
// negate_scalar: negate a scalar value
//------------------------------------------------------------------------------

// negate the scalar x.  Do nothing for bool or uint*.

static inline void negate_scalar
(
    GrB_Type type,
    uint8_t *x
)
{

    if (type == GrB_INT8)
    {
        int8_t *value = (int8_t *) x ;
        (*value) = - (*value) ;
    }
    else if (type == GrB_INT16)
    {
        int16_t *value = (int16_t *) x ;
        (*value) = - (*value) ;
    }
    else if (type == GrB_INT32)
    {
        int32_t *value = (int32_t *) x ;
        (*value) = - (*value) ;
    }
    else if (type == GrB_INT64)
    {
        int64_t *value = (int64_t *) x ;
        (*value) = - (*value) ;
    }
    else if (type == GrB_FP32)
    {
        float *value = (float *) x ;
        (*value) = - (*value) ;
    }
    else if (type == GrB_FP64)
    {
        double *value = (double *) x ;
        (*value) = - (*value) ;
    }
#if 0
    else if (type == GxB_FC32)
    {
        float complex *value = (float complex *) x ;
        (*value) = - (*value) ;
    }
    else if (type == GxB_FC64)
    {
        double complex *value = (double complex *) x ;
        (*value) = - (*value) ;
    }
#endif
}

//------------------------------------------------------------------------------
// set_value
//------------------------------------------------------------------------------

// Add the (i,j,x) triplet to the I,J,X arrays as the kth triplet, and
// increment k.  No typecasting is done.

static inline void set_value
(
    size_t typesize,        // size of the numerical type, in bytes
    GrB_Index i,
    GrB_Index j,
    uint8_t *x,             // scalar, an array of size at least typesize
    GrB_Index *I,
    GrB_Index *J,
    uint8_t *X,
    GrB_Index *k            // # of triplets
)
{
    I [*k] = i ;
    J [*k] = j ;
    memcpy (X + ((*k) * typesize), x, typesize) ;
    (*k)++ ;
}

//------------------------------------------------------------------------------
// LAGraph_MMRead
//------------------------------------------------------------------------------

int LAGraph_MMRead
(
    // output:
    GrB_Matrix *A,  // handle of matrix to create
    // input:
    FILE *f,        // file to read from, already open
    char *msg
)
{

    //--------------------------------------------------------------------------
    // check inputs
    //--------------------------------------------------------------------------

    GrB_Index *I = NULL, *J = NULL ;
    uint8_t *X = NULL ;
    LG_CLEAR_MSG ;
    LG_ASSERT (A != NULL, GrB_NULL_POINTER) ;
    LG_ASSERT (f != NULL, GrB_NULL_POINTER) ;
    (*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 ;
    GrB_Type type = GrB_FP64 ;
    size_t typesize = sizeof (double) ;
    GrB_Index nrows = 0 ;
    GrB_Index ncols = 0 ;
    GrB_Index 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.  The 2nd line is also optional (the %%MatrixMarket
    // line is required for this 2nd line to be recognized):
    //
    //          %%GraphBLAS type <Ctype>
    //
    // where the Ctype is one of: bool, int8_t, int16_t, int32_t, int64_t,
    // uint8_t, uint16_t, uint32_t, uint64_t, float, or double.
    //
    // 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
    //          %%GraphBLAS type double
    //
    // If the first data line contains 2 items (nrows ncols), then the implicit
    // header is:
    //
    //          %%MatrixMarket matrix array real general
    //          %%GraphBLAS type double
    //
    // The implicit header is an extension of the Matrix Market format.

    char buf [MAXLINE+1] ;

    bool got_mm_header = false ;
    bool got_first_data_line = false ;
    int64_t line ;

    for (line = 1 ; get_line (f, buf) ; line++)
    {

        //----------------------------------------------------------------------
        // parse the line
        //----------------------------------------------------------------------

        if ((line == 1) && MATCH (buf, "%%matrixmarket", 14))
        {

            //------------------------------------------------------------------
            // 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

            if (!MATCH (p, "matrix", 6))
            {
                // invalid Matrix Market object
                LG_ASSERT_MSG (false,
                    LAGRAPH_IO_ERROR, "invalid MatrixMarket header"
                    " ('matrix' token missing)") ;
            }
            p += 6 ;                                // skip past token "matrix"

            //------------------------------------------------------------------
            // get the fmt token
            //------------------------------------------------------------------

            while (*p && isspace (*p)) p++ ;        // skip any leading spaces

            if (MATCH (p, "coordinate", 10))
            {
                MM_fmt = MM_coordinate ;
                p += 10 ;
            }
            else if (MATCH (p, "array", 5))
            {
                MM_fmt = MM_array ;
                p += 5 ;
            }
            else
            {
                // invalid Matrix Market format
                LG_ASSERT_MSG (false,
                    LAGRAPH_IO_ERROR, "invalid format in MatrixMarket header"
                    " (format must be 'coordinate' or 'array')") ;
            }

            //------------------------------------------------------------------
            // get the Matrix Market type token
            //------------------------------------------------------------------

            while (*p && isspace (*p)) p++ ;        // skip any leading spaces

            if (MATCH (p, "real", 4))
            {
                MM_type = MM_real ;
                type = GrB_FP64 ;
                typesize = sizeof (double) ;
                p += 4 ;
            }
            else if (MATCH (p, "integer", 7))
            {
                MM_type = MM_integer ;
                type = GrB_INT64 ;
                typesize = sizeof (int64_t) ;
                p += 7 ;
            }
            else if (MATCH (p, "complex", 7))
            {
                MM_type = MM_complex ;
#if 0
                type = GxB_FC64 ;
                typesize = sizeof (GxB_FC64_t) ;
                p += 7 ;
#endif
                LG_ASSERT_MSG (false,
                GrB_NOT_IMPLEMENTED, "complex types not supported") ;
            }
            else if (MATCH (p, "pattern", 7))
            {
                MM_type = MM_pattern ;
                type = GrB_BOOL ;
                typesize = sizeof (bool) ;
                p += 7 ;
            }
            else
            {
                // invalid Matrix Market type
                LG_ASSERT_MSG (false,
                    LAGRAPH_IO_ERROR, "invalid MatrixMarket type") ;
            }

            //------------------------------------------------------------------
            // get the storage token
            //------------------------------------------------------------------

            while (*p && isspace (*p)) p++ ;        // skip any leading spaces

            if (MATCH (p, "general", 7))
            {
                MM_storage = MM_general ;
            }
            else if (MATCH (p, "symmetric", 9))
            {
                MM_storage = MM_symmetric ;
            }
            else if (MATCH (p, "skew-symmetric", 14))
            {
                MM_storage = MM_skew_symmetric ;
            }
            else if (MATCH (p, "hermitian", 9))
            {
                MM_storage = MM_hermitian ;
            }
            else
            {
                // invalid Matrix Market storage
                LG_ASSERT_MSG (false,
                    LAGRAPH_IO_ERROR, "invalid MatrixMarket storage") ;
            }

            //------------------------------------------------------------------
            // ensure the combinations are valid
            //------------------------------------------------------------------

            if (MM_type == MM_pattern)
            {
                // (coodinate) x (pattern) x (general or symmetric)
                LG_ASSERT_MSG (
                    (MM_fmt == MM_coordinate &&
                    (MM_storage == MM_general || MM_storage == MM_symmetric)),
                    LAGRAPH_IO_ERROR,
                    "invalid MatrixMarket pattern combination") ;
            }

            if (MM_storage == MM_hermitian)
            {
                // (coordinate or array) x (complex) x (Hermitian)
                LG_ASSERT_MSG (MM_type == MM_complex,
                    LAGRAPH_IO_ERROR,
                    "invalid MatrixMarket complex combination") ;
            }

        }
        else if (got_mm_header && MATCH (buf, "%%graphblas", 11))
        {

            //------------------------------------------------------------------
            // %%GraphBLAS structured comment
            //------------------------------------------------------------------

            char *p = buf + 11 ;
            while (*p && isspace (*p)) p++ ;        // skip any leading spaces

            if (MATCH (p, "type", 4) && !got_first_data_line)
            {

                //--------------------------------------------------------------
                // %%GraphBLAS type <Ctype>
                //--------------------------------------------------------------

                // This must appear after the %%MatrixMarket header and before
                // the first data line.  Otherwise the %%GraphBLAS line is
                // treated as a pure comment.

                p += 4 ;
                while (*p && isspace (*p)) p++ ;    // skip any leading spaces

                // Ctype is one of: bool, int8_t, int16_t, int32_t, int64_t,
                // uint8_t, uint16_t, uint32_t, uint64_t, float, or double.
                // The complex types "float complex", or "double complex" are
                // not yet supported.

                if (MATCH (p, "bool", 4))
                {
                    type = GrB_BOOL ;
                    typesize = sizeof (bool) ;
                }
                else if (MATCH (p, "int8_t", 6))
                {
                    type = GrB_INT8 ;
                    typesize = sizeof (int8_t) ;
                }
                else if (MATCH (p, "int16_t", 7))
                {
                    type = GrB_INT16 ;
                    typesize = sizeof (int16_t) ;
                }
                else if (MATCH (p, "int32_t", 7))
                {
                    type = GrB_INT32 ;
                    typesize = sizeof (int32_t) ;
                }
                else if (MATCH (p, "int64_t", 7))
                {
                    type = GrB_INT64 ;
                    typesize = sizeof (int64_t) ;
                }
                else if (MATCH (p, "uint8_t", 7))
                {
                    type = GrB_UINT8 ;
                    typesize = sizeof (uint8_t) ;
                }
                else if (MATCH (p, "uint16_t", 8))
                {
                    type = GrB_UINT16 ;
                    typesize = sizeof (uint16_t) ;
                }
                else if (MATCH (p, "uint32_t", 8))
                {
                    type = GrB_UINT32 ;
                    typesize = sizeof (uint32_t) ;
                }
                else if (MATCH (p, "uint64_t", 8))
                {
                    type = GrB_UINT64 ;
                    typesize = sizeof (uint64_t) ;
                }
                else if (MATCH (p, "float complex", 13))
                {
#if 0
                    type = GxB_FC32 ;
                    typesize = sizeof (GxB_FC32_t) ;
#endif
                    LG_ASSERT_MSG (false,
                        GrB_NOT_IMPLEMENTED, "complex types not supported") ;
                }
                else if (MATCH (p, "double complex", 14))
                {
#if 0
                    type = GxB_FC64 ;
                    typesize = sizeof (GxB_FC64_t) ;
#endif
                    LG_ASSERT_MSG (false,
                        GrB_NOT_IMPLEMENTED, "complex types not supported") ;
                }
                else if (MATCH (p, "float", 5))
                {
                    type = GrB_FP32 ;
                    typesize = sizeof (float) ;
                }
                else if (MATCH (p, "double", 6))
                {
                    type = GrB_FP64 ;
                    typesize = sizeof (double) ;
                }
                else
                {
                    // unknown type
                    LG_ASSERT_MSG (false,
                        LAGRAPH_IO_ERROR, "unknown type") ;
                }

                if (MM_storage == MM_skew_symmetric && (type == GrB_BOOL ||
                    type == GrB_UINT8  || type == GrB_UINT16 ||
                    type == GrB_UINT32 || type == GrB_UINT64))
                {
                    // matrices with unsigned types cannot be skew-symmetric
                    LG_ASSERT_MSG (false, LAGRAPH_IO_ERROR,
                        "skew-symmetric matrices cannot have an unsigned type");
                }
            }
            else
            {
                // %%GraphBLAS line but no "type" as the 2nd token; ignore it
                continue ;
            }

        }
        else if (is_blank_line (buf))
        {

            // -----------------------------------------------------------------
            // blank line or comment line
            // -----------------------------------------------------------------

            continue ;

        }
        else
        {

            // -----------------------------------------------------------------
            // read the first data line
            // -----------------------------------------------------------------

            // format: [nrows ncols nvals] or just [nrows ncols]

            got_first_data_line = true ;
            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 = GrB_FP64 ;
                    typesize = sizeof (double) ;
                }
                if (MM_storage == MM_general)
                {
                    // dense general matrix
                    nvals = nrows * ncols ;
                }
                else
                {
                    // dense symmetric, skew-symmetric, or hermitian matrix
                    nvals = nrows + ((nrows * nrows - nrows) / 2) ;
                }
            }
            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 = GrB_FP64 ;
                    typesize = sizeof (double) ;
                }
            }
            else
            {
                // wrong number of items in first data line
                LG_ASSERT_MSGF (false,
                    LAGRAPH_IO_ERROR, "invalid 1st data line"
                    " (line %" PRId64 " of input file)", line) ;
            }

            if (nrows != ncols)
            {
                // a rectangular matrix must be in the general storage
                LG_ASSERT_MSG (MM_storage == MM_general,
                    LAGRAPH_IO_ERROR, "invalid rectangular storage") ;
            }

            //------------------------------------------------------------------
            // header has been read in
            //------------------------------------------------------------------

            break ;
        }
    }

    //--------------------------------------------------------------------------
    // create the matrix
    //--------------------------------------------------------------------------

    GRB_TRY (GrB_Matrix_new (A, type, nrows, ncols)) ;

    //--------------------------------------------------------------------------
    // quick return for empty matrix
    //--------------------------------------------------------------------------

    if (nrows == 0 || ncols == 0 || nvals == 0)
    {
        // success: return an empty matrix.  This is not an error.
        return (GrB_SUCCESS) ;
    }

    //--------------------------------------------------------------------------
    // allocate space for the triplets
    //--------------------------------------------------------------------------

    GrB_Index nvals3 = ((MM_storage == MM_general) ? 1 : 2) * (nvals + 1) ;
    LG_TRY (LAGraph_Malloc ((void **) &I, nvals3, sizeof (GrB_Index), msg)) ;
    LG_TRY (LAGraph_Malloc ((void **) &J, nvals3, sizeof (GrB_Index), msg)) ;
    LG_TRY (LAGraph_Malloc ((void **) &X, nvals3, typesize, msg)) ;

    //--------------------------------------------------------------------------
    // read in the triplets
    //--------------------------------------------------------------------------

    GrB_Index i = -1, j = 0 ;
    GrB_Index nvals2 = 0 ;
    for (int64_t k = 0 ; k < nvals ; k++)
    {

        //----------------------------------------------------------------------
        // get the next triplet, skipping blank lines and comment lines
        //----------------------------------------------------------------------

        uint8_t x [MAXLINE] ;       // scalar value

        while (true)
        {

            //------------------------------------------------------------------
            // read the file until finding the next triplet
            //------------------------------------------------------------------

            bool ok = get_line (f, buf) ;
            line++ ;
            LG_ASSERT_MSG (ok, LAGRAPH_IO_ERROR, "premature EOF") ;
            if (is_blank_line (buf))
            {
                // blank line or comment
                continue ;
            }

            //------------------------------------------------------------------
            // get the row and column index
            //------------------------------------------------------------------

            char *p = buf ;
            if (MM_fmt == MM_array)
            {
                // array format, column major order
                i++ ;
                if (i == nrows)
                {
                    j++ ;
                    if (MM_storage == MM_general)
                    {
                        // dense matrix in column major order
                        i = 0 ;
                    }
                    else
                    {
                        // dense matrix in column major order, only the lower
                        // triangular form is present, including the diagonal
                        i = j ;
                    }
                }
            }
            else
            {
                // coordinate format; read the row index and column index
                int inputs = sscanf (p, "%" SCNu64 " %" SCNu64, &i, &j) ;
                LG_ASSERT_MSGF (inputs == 2, LAGRAPH_IO_ERROR,
                    "line %" PRId64 " of input file: indices invalid", line) ;
                // check the indices (they are 1-based in the MM file format)
                LG_ASSERT_MSGF (i >= 1 && i <= nrows, GrB_INDEX_OUT_OF_BOUNDS,
                    "line %" PRId64 " of input file: row index %" PRIu64
                    " out of range (must be in range 1 to %" PRIu64")",
                    line, i, nrows) ;
                LG_ASSERT_MSGF (j >= 1 && j <= ncols, GrB_INDEX_OUT_OF_BOUNDS,
                    "line %" PRId64 " of input file: column index %" PRIu64
                    " out of range (must be in range 1 to %" PRIu64")",
                    line, j, ncols) ;
                // convert from 1-based to 0-based.
                i-- ;
                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 the row index
                while (*p &&  isspace (*p)) p++ ;   // skip any spaces
                while (*p && !isspace (*p)) p++ ;   // skip the column index
            }

            //------------------------------------------------------------------
            // read the value of the entry
            //------------------------------------------------------------------

            while (*p && isspace (*p)) p++ ;        // skip any spaces

            ok = read_entry (p, type, MM_type == MM_pattern, x) ;
            LG_ASSERT_MSGF (ok, LAGRAPH_IO_ERROR, "entry value invalid on line"
                " %" PRId64 " of input file", line) ;

            //------------------------------------------------------------------
            // set the value in the matrix
            //------------------------------------------------------------------

            set_value (typesize, i, j, x, I, J, X, &nvals2) ;

            //------------------------------------------------------------------
            // also set the A(j,i) entry, if symmetric
            //------------------------------------------------------------------

            if (i != j && MM_storage != MM_general)
            {
                if (MM_storage == MM_symmetric)
                {
                    set_value (typesize, j, i, x, I, J, X, &nvals2) ;
                }
                else if (MM_storage == MM_skew_symmetric)
                {
                    negate_scalar (type, x) ;
                    set_value (typesize, j, i, x, I, J, X, &nvals2) ;
                }
                #if 0
                else if (MM_storage == MM_hermitian)
                {
                    double complex *value = (double complex *) x ;
                    (*value) = conj (*value) ;
                    set_value (typesize, j, i, x, I, J, X, &nvals2) ;
                }
                #endif
            }

            // one more entry has been read in
            break ;
        }
    }

    //--------------------------------------------------------------------------
    // build the final matrix
    //--------------------------------------------------------------------------

    if (type == GrB_BOOL)
    {
        GRB_TRY (GrB_Matrix_build_BOOL (*A, I, J, (bool *) X, nvals2, GxB_IGNORE_DUP)) ;
    }
    else if (type == GrB_INT8)
    {
        GRB_TRY (GrB_Matrix_build_INT8 (*A, I, J, (int8_t *) X, nvals2, NULL)) ;
    }
    else if (type == GrB_INT16)
    {
        GRB_TRY (GrB_Matrix_build_INT16 (*A, I, J, (int16_t *) X, nvals2, NULL)) ;
    }
    else if (type == GrB_INT32)
    {
        GRB_TRY (GrB_Matrix_build_INT32 (*A, I, J, (int32_t *) X, nvals2, NULL)) ;
    }
    else if (type == GrB_INT64)
    {
        GRB_TRY (GrB_Matrix_build_INT64 (*A, I, J, (int64_t *) X, nvals2, NULL)) ;
    }
    else if (type == GrB_UINT8)
    {
        GRB_TRY (GrB_Matrix_build_UINT8 (*A, I, J, (uint8_t *) X, nvals2, NULL)) ;
    }
    else if (type == GrB_UINT16)
    {
        GRB_TRY (GrB_Matrix_build_UINT16 (*A, I, J, (uint16_t *) X, nvals2, NULL)) ;
    }
    else if (type == GrB_UINT32)
    {
        GRB_TRY (GrB_Matrix_build_UINT32 (*A, I, J, (uint32_t *) X, nvals2, NULL)) ;
    }
    else if (type == GrB_UINT64)
    {
        GRB_TRY (GrB_Matrix_build_UINT64 (*A, I, J, (uint64_t *) X, nvals2, NULL)) ;
    }
    else if (type == GrB_FP32)
    {
        GRB_TRY (GrB_Matrix_build_FP32 (*A, I, J, (float *) X, nvals2, NULL)) ;
    }
    else if (type == GrB_FP64)
    {
        GRB_TRY (GrB_Matrix_build_FP64 (*A, I, J, (double *) X, nvals2, NULL)) ;
    }
#if 0
    else if (type == GxB_FC32)
    {
        GRB_TRY (GxB_Matrix_build_FC32 (*A, I, J, (GxB_FC32_t *) X, nvals2, NULL)) ;
    }
    else if (type == GxB_FC64)
    {
        GRB_TRY (GxB_Matrix_build_FC64 (*A, I, J, (GxB_FC64_t *) X, nvals2, NULL)) ;
    }
#endif

    //--------------------------------------------------------------------------
    // use 32-bit integers if possible
    //--------------------------------------------------------------------------

    // FUTURE: use 32-bit integers throughout, when using GraphBLAS v10 and
    // later, instead of converting to 32-bit indices here at the end.

    #if LAGRAPH_SUITESPARSE
    #if GxB_IMPLEMENTATION >= GxB_VERSION (10,0,0)
    GRB_TRY (GrB_Matrix_set_INT32 (*A, 32, GxB_ROWINDEX_INTEGER_HINT)) ;
    GRB_TRY (GrB_Matrix_set_INT32 (*A, 32, GxB_COLINDEX_INTEGER_HINT)) ;
    GRB_TRY (GrB_Matrix_set_INT32 (*A, 32, GxB_OFFSET_INTEGER_HINT)) ;
    #endif
    #endif

    //--------------------------------------------------------------------------
    // free workspace and return result
    //--------------------------------------------------------------------------

    LG_FREE_WORK ;
    return (GrB_SUCCESS) ;
}