-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.c
355 lines (249 loc) · 7.11 KB
/
functions.c
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
// Libraries
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
// Header
#include "functions.h"
void ReadMMtoCSR(const char *filename, CSRMatrix *matrix)
{
// reading file
FILE *file = fopen(filename, "r");
char buffer[255];
// catching errors
if (file == NULL)
{
printf("Error Opening File.\n");
}
else
{
while ((fgets(buffer, 255, file)) != NULL)
{
char ch;
ch = buffer[0];
// ignores comments
if (ch == '%')
{
continue;
}
// first line gives dimensions
else
{
sscanf(buffer, "%d %d %d", &matrix->num_rows, &matrix->num_cols, &matrix->num_non_zeros);
break;
}
}
// building csr_data, col_indices and row_ptr
matrix->csr_data = (double *)malloc(matrix->num_non_zeros * sizeof(double));
matrix->col_ind = (int *)malloc(matrix->num_non_zeros * sizeof(int));
matrix->row_ptr = (int *)malloc((matrix->num_rows + 1) * sizeof(int));
int *row_ind = (int *)malloc((matrix->num_non_zeros) * sizeof(int));
int row_index;
// each non-zero value corresponds to an entry of the matrix
for (int i = 0; i < matrix->num_non_zeros; ++i)
{
// scanning line and assigning it
fgets(buffer, 255, file);
sscanf(buffer, "%d %d %lf", &row_ind[i], &matrix->col_ind[i], &matrix->csr_data[i]);
// row_ptr can be constructed as follows
for (int j = row_ind[i]; j < matrix->num_rows + 1; ++j)
{
matrix->row_ptr[j]++;
}
}
// selection sort to get the row indices in order
int min;
for (int i = 0; i < matrix->num_non_zeros - 1; ++i)
{
min = i;
for (int j = i + 1; j < matrix->num_non_zeros; ++j)
{
if (row_ind[j] < row_ind[min])
{
min = j;
}
}
if (min != i)
{
int temp = row_ind[i];
row_ind[i] = row_ind[min];
row_ind[min] = temp;
double data = matrix->csr_data[i];
matrix->csr_data[i] = matrix->csr_data[min];
matrix->csr_data[min] = data;
int col = matrix->col_ind[i];
matrix->col_ind[i] = matrix->col_ind[min];
matrix->col_ind[min] = col;
}
}
// normalizing (going from matrix -> array type indicing)
for (int i = 0; i < matrix->num_non_zeros; ++i)
{
matrix->col_ind[i]--;
}
//freeing memory
free(row_ind);
}
fclose(file);
}
//function for printing CSRMatrix
void printCSRMatrix(const CSRMatrix *matrix){
//printing num_rows, num_cols and num_non_zeros
printf("Number of rows: %d\n", matrix -> num_rows);
printf("Number of columns: %d\n", matrix -> num_cols);
printf("Number of non-zero elements %d\n", matrix->num_non_zeros);
printf("CSR data\n");
//printing data
for(int i=0; i < matrix->num_non_zeros; ++i){
printf("%lf ", matrix->csr_data[i]);
}
printf("\n");
//printing column indices
printf("col indices: ");
for(int i=0; i < matrix->num_non_zeros; ++i){
printf("%d ", matrix->col_ind[i]);
}
printf("\n");
//printing row_ptr
printf("row pointer: ");
for(int i=0; i <=matrix->num_rows; ++i){
printf("%d ", matrix->row_ptr[i]);
}
printf("\n");
}
void spmv_csr(const CSRMatrix *A, const double *x, double *y)
{
//iterating over rows
for (int i = 0; i < A->num_rows; ++i)
{
//iterating over each non-zero element in each row
for (int j = A->row_ptr[i]; j < A->row_ptr[i + 1]; ++j)
{
y[i] += A->csr_data[j] * x[A->col_ind[j]];
}
}
}
double compute_norm(const double *r, int size)
{
// computing dot product
double sum = 0.0;
for (int i = 0; i < size; ++i)
{
sum += r[i] * r[i];
}
// sqrt of dot product is norm
return sqrt(sum);
}
void fullMatrix(CSRMatrix *A){
//this function converts the lower triangular matrix to a symmetric matrix
//calculating new num_non_zeros
int num_non_zeros = A->num_non_zeros*2 - A->num_rows;
//initializing
double *csr_data = (double *)malloc(num_non_zeros*sizeof(double));
int *col_ind = (int *)malloc(num_non_zeros*sizeof(int));
int *row_ptr = (int *)malloc((A->num_rows+1)*sizeof(int));
//calculating # of non-diag elements in cols
int *colCount = (int *)calloc(A->num_rows, sizeof(int));
for(int i =0; i < A->num_non_zeros; ++i){
colCount[A->col_ind[i]]++;
}
row_ptr[0] = 0;
for(int i =0; i < A->num_rows; ++i){
colCount[i]--;
}
//changing it to be a cumulative sum like row_ptr
for(int i=1; i < A->num_rows; ++i){
colCount[i] = colCount[i] + colCount[i-1];
}
//adding this to row_ptr adjusts it for the full matrix
for(int i =1; i < A->num_rows+1; ++i){
row_ptr[i] = A->row_ptr[i] + colCount[i-1];
}
//filling csr_data and col_ind
for(int i =0; i < A->num_rows; ++i){
//iterating over each non-zero element in lower triangular matrix
for(int j =A->row_ptr[i]; j < A->row_ptr[i+1]; ++j){
int col = A->col_ind[j];
double value = A->csr_data[j];
//two cases:
//1 for diagonal elems
if(i != col){
csr_data[row_ptr[i]] = value;
col_ind[row_ptr[i]] = col;
row_ptr[i]++;
csr_data[row_ptr[col]]=value;
col_ind[row_ptr[col]]=i;
row_ptr[col]++;
}
//1 for non-diag
else{
csr_data[row_ptr[i]] =value;
col_ind[row_ptr[i]] = col;
row_ptr[i]++;
}
}
}
//correcting row_ptr
for(int i = A->num_rows; i > 0; --i){
row_ptr[i] = row_ptr[i-1];
}
row_ptr[0] = 0;
//freeing old memory
free(A->row_ptr);
free(A->csr_data);
free(A->col_ind);
//replacing it
A->num_non_zeros = num_non_zeros;
A->csr_data = csr_data;
A->row_ptr = row_ptr;
A->col_ind = col_ind;
}
void sorIteration(const CSRMatrix *A, double *x, double *b, double omega){
int n= A->num_rows;
//defining diagonal element and sigma
double aii;
double sigma;
for(int i =0; i <n; ++i){
sigma =0.0;
for(int j=A->row_ptr[i]; j < A->row_ptr[i+1]; ++j){
int col_ind = A->col_ind[j];
//initializing aii
if(i == col_ind){
aii = A->csr_data[j];
}
//sigma is result of matrix multiplication
else{
sigma += A->csr_data[j]*x[col_ind];
}
}
//updating x
x[i] = (1-omega)*x[i] + omega*(b[i] - sigma)/aii;
}
}
void solver(CSRMatrix *A, double *x, double *b, double omega, double max_iter, double tolerance){
//defining and initializing residual
double *residual = (double *)calloc(A->num_rows, sizeof(double));
for(int i =0; i < A->num_rows; ++i){
residual[i] = 1.0;
}
double resi_norm;
int n = A->num_rows;
int k=0;
//loop will break after max_iter or if tolerance is acheived (1e-14)
while((compute_norm(residual, n) > tolerance) && (k < max_iter)){
spmv_csr(A, x, residual);
//calc residual
for(int i =0; i < n; ++i){
residual[i]= b[i] - residual[i];
}
resi_norm = compute_norm(residual, n);
if(resi_norm < tolerance){
break;
}
//perform iteration of SOR
sorIteration(A,x,b, omega);
k++;
}
//freeing residual
free(residual);
}