1
/* zuse: cc -o poisson_test -fast -native poisson_test.c mmio.c pcg.c -lm -xlic_lib=sunperf */
2
/* ru-lt13: cc -O poisson_test.c mmio.c pcg.c -o poisson_test -lblas -lg2c -lm */
9
static double *va_s, *da_s;
10
static int *ja_s, *ia_s;
12
/* CONVERT_COO_SSS - convert sparse matrix from COO to SSS format
14
void convert_COO_SSS(int n, int nz,
15
int *i_coo, int *j_coo, double *v_coo,
16
int **ia, int **ja, double **va, double **da) {
20
root = (int *)malloc(n * sizeof(int));
23
/* allocate SSS matrix structure (1st part) */
24
(*da) = (double *)malloc(n * sizeof(double));
26
for (i = 0; i < n; i ++) {
31
/* build n linked lists */
33
for (k = 0; k < nz; k ++) {
34
if (i_coo[k] == j_coo[k])
35
/* diagonal element */
36
(*da)[i_coo[k]] = v_coo[k];
38
/* off diagonal element */
39
if (i_coo[k] < j_coo[k]) { /* move to lower triangle */
44
i = i_coo[k]; /* link */
51
/* allocate SSS matrix structure (2nd part) */
52
(*ia) = (int *)malloc((n+1) * sizeof(int));
53
(*va) = (double *)malloc((nnz * sizeof(double)));
54
(*ja) = (int *)malloc(nnz * sizeof(int));
56
/* fill SSS matrix structure */
58
for (i = 0; i < n; i ++) {
73
/* READ_MTX - read symmetric sparse matrix in MatrixMarket format
75
void read_MTX_SSS(char *fname, int *n,
76
double **va, double **da, int **ja, int **ia) {
77
int m, nz, ret_code, i;
83
f = fopen(fname, "r");
85
ret_code = mm_read_banner(f, &matcode);
86
assert(ret_code == 0);
87
assert(mm_is_real(matcode) && mm_is_matrix(matcode) &&
88
mm_is_sparse(matcode) && mm_is_symmetric(matcode));
89
ret_code = mm_read_mtx_crd_size(f, &m, n, &nz);
90
assert(ret_code == 0);
93
i_coo = (int *)malloc(nz * sizeof(int));
94
j_coo = (int *)malloc(nz * sizeof(int));
95
v_coo = (double *)malloc(nz * sizeof(double));
96
assert(i_coo && j_coo && v_coo);
97
for (i = 0; i < nz; i ++) {
98
fscanf(f, "%d %d %lg\n", &i_coo[i], &j_coo[i], &v_coo[i]);
99
i_coo[i]--; /* adjust from 1-based to 0-based */
103
/* convert to SSS format */
104
convert_COO_SSS(*n, nz, i_coo, j_coo, v_coo, ia, ja, va, da);
105
free(i_coo); free(j_coo); free(v_coo);
108
/* MATVEC - matrix vector multiplications
110
void matvec(double *x, double *y) {
114
for (i = 0; i < n_s; i ++) {
117
for (k = ia_s[i]; k < ia_s[i+1]; k ++) {
123
y[i] = s + da_s[i]*xi;
128
double *x, *b, *work;
133
read_MTX_SSS("matrices/poi2d_100.mtx", &n_s, &va_s, &da_s, &ja_s, &ia_s);
135
x = (double *) malloc(n_s * sizeof(double));
136
b = (double *) malloc(n_s * sizeof(double));
137
work = (double *) malloc(4*n_s * sizeof(double));
138
assert(x != NULL && b != NULL && work != NULL);
140
for (i = 0; i < n_s; i ++) {
145
printf("Starting PCG solver...\n");
146
pcg(n_s, x, b, 1e-12, 2000, 1, &iter, &relres, &flag, work, matvec, NULL);