1 
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include "slu_zdefs.h"
5 #include "slu_util.h"
6 
7 
8 void
zreadtriple(int * m,int * n,int * nonz,doublecomplex ** nzval,int ** rowind,int ** colptr)9 zreadtriple(int *m, int *n, int *nonz,
10 	    doublecomplex **nzval, int **rowind, int **colptr)
11 {
12 /*
13  * Output parameters
14  * =================
15  *   (a,asub,xa): asub[*] contains the row subscripts of nonzeros
16  *	in columns of matrix A; a[*] the numerical values;
17  *	row i of A is given by a[k],k=xa[i],...,xa[i+1]-1.
18  *
19  */
20     int    i, j, k, jsize, nz, lasta;
21     doublecomplex *a, *val;
22     int    *asub, *xa, *row, *col;
23 
24     /* 	Matrix format:
25      *    First line:  #rows, #cols, #non-zero
26      *    Triplet in the rest of lines:
27      *                 row, col, value
28      */
29 
30     scanf("%d%d%d", m, n, nonz);
31 #ifdef DEBUG
32     printf("zreadtriple(): *m %d, *n %d, *nonz, %d\n", *m, *n, *nonz);
33 #endif
34     zallocateA(*n, *nonz, nzval, rowind, colptr); /* Allocate storage */
35     a    = *nzval;
36     asub = *rowind;
37     xa   = *colptr;
38 
39     val = (doublecomplex *) SUPERLU_MALLOC(*nonz * sizeof(doublecomplex));
40     row = (int *) SUPERLU_MALLOC(*nonz * sizeof(int));
41     col = (int *) SUPERLU_MALLOC(*nonz * sizeof(int));
42 
43     /* Read into the triplet array from a file */
44     for (i = 0; i < *n+1; ++i) xa[i] = 0;
45     for (nz = 0; nz < *nonz; ++nz) {
46 	scanf("%d%d%lf%lf\n", &row[nz], &col[nz], &val[nz].r, &val[nz].i);
47 	if (row[nz] < 0 || row[nz] >= *m || col[nz] < 0 || col[nz] >= *n) {
48 	    fprintf(stderr, "(%d, %d) out of bound!\n", row[nz], col[nz]);
49 	    exit (-1);
50 	}
51 	++xa[col[nz]]; /* Count number of nonzeros in each column */
52     }
53 
54     /* Initialize the array of column pointers */
55     k = 0;
56     jsize = xa[0];
57     xa[0] = 0;
58     for (j = 1; j < *n; ++j) {
59 	k += jsize;
60 	jsize = xa[j];
61 	xa[j] = k;
62     }
63 
64     /* Copy the triplets into the column oriented storage */
65     for (nz = 0; nz < *nonz; ++nz) {
66 	j = col[nz];
67 	k = xa[j];
68 	asub[k] = row[nz];
69 	a[k] = val[nz];
70 	++xa[j];
71     }
72 
73     /* Reset the column pointers to the beginning of each column */
74     for (j = *n; j > 0; --j)
75 	xa[j] = xa[j-1];
76     xa[0] = 0;
77 
78     SUPERLU_FREE(val);
79     SUPERLU_FREE(row);
80     SUPERLU_FREE(col);
81 
82 #ifdef CHK_INPUT
83     for (i = 0; i < *n; i++) {
84 	printf("Col %d, xa %d\n", i, xa[i]);
85 	for (k = xa[i]; k < xa[i+1]; k++)
86 	    printf("%d\t%16.10f\n", asub[k], a[k]);
87     }
88 #endif
89 
90 }
91