File: dreadtriple_noheader.c

package info (click to toggle)
superlu 7.0.1%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 12,292 kB
  • sloc: ansic: 59,338; makefile: 413; csh: 141; f90: 125; fortran: 77
file content (192 lines) | stat: -rw-r--r-- 4,746 bytes parent folder | download
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
/*
Copyright (c) 2003, The Regents of the University of California, through
Lawrence Berkeley National Laboratory (subject to receipt of any required
approvals from U.S. Dept. of Energy)

All rights reserved.

The source code is distributed under BSD license, see the file License.txt
at the top-level directory.
*/

/*! \file
 * \brief Read matrix in triplet format.
 *
 * \ingroup Example
 */

#include <stdio.h>
#include "slu_ddefs.h"

#undef EXPAND_SYM

/*!
 * \brief Read matrix in triplet format from stdin.
 *
 * File format: Triplet in a line for each nonzero entry:
 * \code row col value \endcode or
 * \code row col real_part imaginary_part \endcode
 *
 * \param [out] m      Number of rows in the matrix
 * \param [out] n      Number of columns in the matrix
 * \param [out] nonz   Number of non-zero entries in the matrix
 * \param [out] rowind Contains the row subscripts of nonzeros in columns of matrix A
 * \param [out] nzval  The numerical values
 * \param [out] colptr Column i of A is given by (*nzval)[k], k = (*rowind)[i],...,(*rowind)[i+1]-1.
 */
void
dreadtriple_noheader(int *m, int *n, int_t *nonz,
                     double **nzval, int_t **rowind, int_t **colptr)
{
    int    i, j, jsize, minn = 100;
    double *a, *val, vali;
    int_t  *asub, *xa, k, nnz, nz, new_nonz;
    int    *row, *col;
    int    zero_base = 0, ret_val = 0;
    FILE *fp = stdin;

    /* First pass: determine N and NNZ */
    nz = *n = 0;

    ret_val = fscanf(fp, "%d%d%lf\n", &i, &j, &vali);
    printf("%d\t%d\t %f\n", i, j, vali);

    while (ret_val != EOF) {
	*n = SUPERLU_MAX(*n, i);
	*n = SUPERLU_MAX(*n, j);
	minn = SUPERLU_MIN(minn, i);
	minn = SUPERLU_MIN(minn, j);
	++nz;

        ret_val = fscanf(fp, "%d%d%lf\n", &i, &j, &vali);
    }
    
    if ( minn == 0 ) { /* zero-based indexing */
	zero_base = 1;
	++(*n);
	printf("triplet file: row/col indices are zero-based.\n");
    } else {
	printf("triplet file: row/col indices are one-based.\n");
    }

    *m = *n;
    *nonz = nz;
    rewind(fp);  // Move to the start of the input file 

#ifdef EXPAND_SYM
    new_nonz = 2 * *nonz - *n;
#else
    new_nonz = *nonz;
#endif

    /* Second pass: read the actual matrix values */
    printf("m %d, n %d, nonz %lld, new_nonz %lld\n", *m, *n,
	   (long long)*nonz, (long long)new_nonz);
    dallocateA(*n, new_nonz, nzval, rowind, colptr); /* Allocate storage */
    a    = *nzval;
    asub = *rowind;
    xa   = *colptr;

    if ( !(val = (double *) SUPERLU_MALLOC(new_nonz * sizeof(double))) )
        ABORT("Malloc fails for val[]");
    if ( !(row = (int *) SUPERLU_MALLOC(new_nonz * sizeof(int))) )
        ABORT("Malloc fails for row[]");
    if ( !(col = (int *) SUPERLU_MALLOC(new_nonz * sizeof(int))) )
        ABORT("Malloc fails for col[]");

    for (j = 0; j < *n; ++j) xa[j] = 0;

    /* Read into the triplet array from a file */
    for (nnz = 0, nz = 0; nnz < *nonz; ++nnz) {
	
	fscanf(fp, "%d%d%lf\n", &row[nz], &col[nz], &val[nz]);

	if ( !zero_base ) {
	    /* Change to 0-based indexing. */
	    --row[nz];
	    --col[nz];
	}

	if (row[nz] < 0 || row[nz] >= *m || col[nz] < 0 || col[nz] >= *n
	    /*|| val[nz] == 0.*/) {
	    fprintf(stderr, "nz %lld, (%d, %d) = %e out of bound, removed\n",
		    (long long)nz, row[nz], col[nz], val[nz]);
	    exit(-1);
	} else {
	    ++xa[col[nz]];
#ifdef EXPAND_SYM
	    if ( row[nz] != col[nz] ) { /* Excluding diagonal */
	      ++nz;
	      row[nz] = col[nz-1];
	      col[nz] = row[nz-1];
	      val[nz] = val[nz-1];
	      ++xa[col[nz]];
	    }
#endif
	    ++nz;
	}
    }

    *nonz = nz;
    
#ifdef EXPAND_SYM
    printf("new_nonz after symmetric expansion:\t%lld\n", (long long)*nonz);
#endif


    /* Initialize the array of column pointers */
    k = 0;
    jsize = xa[0];
    xa[0] = 0;
    for (j = 1; j < *n; ++j) {
	k += jsize;
	jsize = xa[j];
	xa[j] = k;
    }

    /* Copy the triplets into the column oriented storage */
    for (nz = 0; nz < *nonz; ++nz) {
	j = col[nz];
	k = xa[j];
	asub[k] = row[nz];
	a[k] = val[nz];
	++xa[j];
    }

    /* Reset the column pointers to the beginning of each column */
    for (j = *n; j > 0; --j)
	xa[j] = xa[j-1];
    xa[0] = 0;

    SUPERLU_FREE(val);
    SUPERLU_FREE(row);
    SUPERLU_FREE(col);

    
#ifdef CHK_INPUT
    for (i = 0; i < *n; i++) {
	printf("Col %d, xa[i] %d, xa[i+1] %d\n", i, xa[i], xa[i+1]);
	for (k = xa[i]; k < xa[i+1]; k++)
	    printf("%d\t%16.10f\n", asub[k], a[k]);
    }
    exit(-1);
#endif

}

#if 0
void dreadrhs(int m, double *b)
{
    FILE *fp, *fopen();
    int i, j;

    if ( !(fp = fopen("b.dat", "r")) ) {
        fprintf(stderr, "zreadrhs: file does not exist\n");
	exit(-1);
    }
    for (i = 0; i < m; ++i)
      fscanf(fp, "%lf\n", &b[i]);

    fclose(fp);
}
#endif