Pastix.cpp 5.41 KB
Newer Older
1 2 3
#include "Pastix.hpp"

#include "../../Helper.hpp"
4
#include <iostream>
5 6
#include <cstdlib>
#include <ctype.h>
7 8 9 10 11 12

int pastix_read_symbol(FILE* stream, symbol_matrix_t* matrix)
{
    int32_t versval;
    int32_t cblknum;
    int32_t bloknum;
13
    int32_t nodenbr;
14

15
    int32_t result = 0;
16

Mathieu Faverge's avatar
Mathieu Faverge committed
17 18 19 20 21
	result += pastix_read_int(stream, &versval);
	result += pastix_read_int(stream, &matrix->m_cblknbr);
	result += pastix_read_int(stream, &matrix->m_bloknbr);
	result += pastix_read_int(stream, &nodenbr);
	result += pastix_read_int(stream, &matrix->m_baseval);
22
    matrix->m_sndenbr = matrix->m_cblknbr;
23

24
    if ((result != 5) ||
25
        (versval < -1)                                     ||
26 27
        (versval > 1)                                      ||
        (matrix->m_bloknbr < matrix->m_cblknbr)            ||
28
        (nodenbr < matrix->m_cblknbr))
29
    {
30 31
        Helper::log(LogStatus::FATAL, "Loading symbol file, bad header !");

32 33
        return 1;
    }
34
    Helper::log(LogStatus::MESSAGE, "Version %d", versval);
35

36
    matrix->m_sndetab = (int32_t*)      malloc((matrix->m_sndenbr + 1) * sizeof(int32_t)      );
37
    matrix->m_cblktab = (symbol_cblk_t*)malloc((matrix->m_cblknbr + 1) * sizeof(symbol_cblk_t));
38
    matrix->m_bloktab = (symbol_blok_t*)malloc( matrix->m_bloknbr      * sizeof(symbol_blok_t));
39

40 41 42
    if ( (matrix->m_sndetab == nullptr) ||
         (matrix->m_cblktab == nullptr) ||
         (matrix->m_bloktab == nullptr) )
43
    {
44
        Helper::log(LogStatus::FATAL, "Out of memory while allocating tabs !");
45 46 47 48

        return 1;
    }

49
    symbol_cblk_t *cblk = matrix->m_cblktab;
50
    int32_t *snde   = matrix->m_sndetab;
51
    int32_t cblknbr = matrix->m_cblknbr;
52
    for (cblknum = 0; cblknum < cblknbr; ++cblknum, ++cblk, ++snde)
53
    {
54 55 56
        result =  pastix_read_int(stream, &(cblk->m_fcolnum));
        result += pastix_read_int(stream, &(cblk->m_lcolnum));
        result += pastix_read_int(stream, &(cblk->m_bloknum));
57 58

        if ((result != 3) ||
59
            (cblk->m_fcolnum > cblk->m_lcolnum))
60
        {
61
            Helper::log(LogStatus::FATAL, "Bad input while reading cblk !");
62 63 64

            return 1;
        }
65

Mathieu Faverge's avatar
Mathieu Faverge committed
66
        cblk->m_color = 0.f;
67
        cblk->m_flags = 0;
68
        *snde = 0;
69
    }
70
    /* Extra cblk and supernode */
71 72
    cblk->m_fcolnum = cblk->m_lcolnum = nodenbr + matrix->m_baseval;
    cblk->m_bloknum = matrix->m_bloknbr + matrix->m_baseval;
73
    *snde = cblknum;
74

75
    symbol_blok_t *blok = matrix->m_bloktab;
76
    int32_t bloknbr = matrix->m_bloknbr;
77
    for (bloknum = 0; bloknum < bloknbr; ++bloknum, ++blok)
78
    {
79 80 81
        result  = pastix_read_int(stream, &(blok->m_frownum));
        result += pastix_read_int(stream, &(blok->m_lrownum));
        result += pastix_read_int(stream, &(blok->m_fcblknm));
82
        if ((result != 3) ||
83
            (blok->m_frownum > blok->m_lrownum))
84
        {
85
            Helper::log(LogStatus::FATAL, "Bad input while reading blok %d !", bloknum);
86 87 88

            return 1;
        }
89

90 91
        blok->m_localization = 0;
        blok->m_flags = 0;
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
        if (versval == -1) {
            int32_t color;
            result = pastix_read_int(stream, &color);

            if (result != 1)
            {
                Helper::log(LogStatus::FATAL, "Bad input while reading blok %d color !", bloknum);
                return 1;
            }

            blok->m_color = (float)color;
        }
        else {
            blok->m_color = -1.f;
        }
107

108 109 110 111 112
        // Compatibility block
        {
            int32_t tmp;
            if ((versval == 0) && (pastix_read_int(stream, &tmp) != 1))
            {
113
                Helper::log(LogStatus::FATAL, "Bad input while reading lefval !");
114 115 116
                return 1;
            }
        }
117 118
    }

119 120 121 122 123 124 125 126 127 128 129 130 131
    if (versval == -1) {
        int32_t maxval;
        result = pastix_read_int(stream, &(maxval));

        symbol_blok_t *blok = matrix->m_bloktab;
        int32_t bloknbr = matrix->m_bloknbr;
        for (bloknum = 0; bloknum < bloknbr; ++bloknum, blok++)
        {
            blok->m_color = blok->m_color / (float)maxval;
        }
        fprintf(stderr, "Max rank reduction is %d\n", maxval);
    }

132
    matrix->m_dof = 1;
133 134
    matrix->m_colsnbr = nodenbr;
    matrix->m_rowsnbr = nodenbr;
135

136
    Helper::log(LogStatus::MESSAGE,
137
		"Loaded header:\n"
138
		"  File Version:             %10d\n"
139 140 141 142 143 144 145
		"  Number of cblk:      %10d\n"
		"  Number of rows:      %10d\n"
		"  Number of blok:      %10d\n"
		"  Number of nodes:     %10d\n"
		"  Base value:          %10d\n",
		versval,
		matrix->m_cblknbr, matrix->m_rowsnbr,
146
		matrix->m_bloknbr, nodenbr,
147
		matrix->m_baseval
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
    return 0;
}

int pastix_read_int(FILE* stream, int32_t* const value)
{
    int32_t val;
    int     sign;
    int     car;

    sign = 0;
    for ( ; ; )
    {
        car = getc(stream);
        if (isspace(car))
            continue;
        if ((car >= '0') && (car <= '9'))
            break;
        if (car == '-')
        {
            sign = 1;
            car  = getc(stream);
            break;
        }
        if (car == '+')
        {
            car = getc(stream);
            break;
        }
        return (0);
    }

    if ((car < '0') || (car > '9'))
    {
        return (0);
    }
    val = car - '0';
    for ( ; ; )
    {
        car = getc(stream);
        if ((car < '0') || (car > '9'))
        {
            ungetc(car, stream);
            break;
        }
        val = val * 10 + (car - '0');
    }

    // Maybe do: *value = (sign << 31 | val);
    *value = (sign != 0) ? (-val) : val;

    return (1);
}