1 #include <stdio.h>
2 #include "slu_ddefs.h"
3 #include "slu_util.h"
4 
5 
6 void
dreadtriple(int * m,int * n,int * nonz,double ** nzval,int ** rowind,int ** colptr)7 dreadtriple(int *m, int *n, int *nonz,
8 	    double **nzval, int **rowind, int **colptr)
9 {
10 /*
11  * Output parameters
12  * =================
13  *   (a,asub,xa): asub[*] contains the row subscripts of nonzeros
14  *	in columns of matrix A; a[*] the numerical values;
15  *	row i of A is given by a[k],k=xa[i],...,xa[i+1]-1.
16  *
17  */
18     int    i, j, k, jsize, lasta, nnz, nz;
19     double *a, *val;
20     int    *asub, *xa, *row, *col;
21 
22     /* 	Matrix format:
23      *    First line:  #rows, #cols, #non-zero
24      *    Triplet in the rest of lines:
25      *                 row, col, value
26      */
27 
28     scanf("%d%d", n, nonz);
29     *m = *n;
30     printf("m %d, n %d, nonz %d\n", *m, *n, *nonz);
31     dallocateA(*n, *nonz, nzval, rowind, colptr); /* Allocate storage */
32     a    = *nzval;
33     asub = *rowind;
34     xa   = *colptr;
35 
36     val = (double *) SUPERLU_MALLOC(*nonz * sizeof(double));
37     row = (int *) SUPERLU_MALLOC(*nonz * sizeof(int));
38     col = (int *) SUPERLU_MALLOC(*nonz * sizeof(int));
39 
40     for (j = 0; j < *n; ++j) xa[j] = 0;
41 
42     /* Read into the triplet array from a file */
43     for (nnz = 0, nz = 0; nnz < *nonz; ++nnz) {
44 	scanf("%d%d%lf\n", &row[nz], &col[nz], &val[nz]);
45 	/* Change to 0-based indexing. */
46 #if 0
47 	--row[nz];
48 	--col[nz];
49 #endif
50 	if (row[nz] < 0 || row[nz] >= *m || col[nz] < 0 || col[nz] >= *n
51 	    /*|| val[nz] == 0.*/) {
52 	    fprintf(stderr, "nz %d, (%d, %d) = %e out of bound, removed\n",
53 		    nz, row[nz], col[nz], val[nz]);
54 	    exit(-1);
55 	} else {
56 	    ++xa[col[nz]];
57 	    ++nz;
58 	}
59     }
60 
61     *nonz = nz;
62 
63     /* Initialize the array of column pointers */
64     k = 0;
65     jsize = xa[0];
66     xa[0] = 0;
67     for (j = 1; j < *n; ++j) {
68 	k += jsize;
69 	jsize = xa[j];
70 	xa[j] = k;
71     }
72 
73     /* Copy the triplets into the column oriented storage */
74     for (nz = 0; nz < *nonz; ++nz) {
75 	j = col[nz];
76 	k = xa[j];
77 	asub[k] = row[nz];
78 	a[k] = val[nz];
79 	++xa[j];
80     }
81 
82     /* Reset the column pointers to the beginning of each column */
83     for (j = *n; j > 0; --j)
84 	xa[j] = xa[j-1];
85     xa[0] = 0;
86 
87     SUPERLU_FREE(val);
88     SUPERLU_FREE(row);
89     SUPERLU_FREE(col);
90 
91 #ifdef CHK_INPUT
92     for (i = 0; i < *n; i++) {
93 	printf("Col %d, xa %d\n", i, xa[i]);
94 	for (k = xa[i]; k < xa[i+1]; k++)
95 	    printf("%d\t%16.10f\n", asub[k], a[k]);
96     }
97 #endif
98 
99 }
100 
101 
dreadrhs(int m,double * b)102 void dreadrhs(int m, double *b)
103 {
104     FILE *fp, *fopen();
105     int i, j;
106 
107     if ( !(fp = fopen("b.dat", "r")) ) {
108         fprintf(stderr, "dreadrhs: file does not exist\n");
109 	exit(-1);
110     }
111     for (i = 0; i < m; ++i)
112       fscanf(fp, "%lf\n", &b[i]);
113       /*fscanf(fp, "%d%lf\n", &j, &b[i]);*/
114     /*        readpair_(j, &b[i]);*/
115     fclose(fp);
116 }
117 
118 
119