-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
59 lines (49 loc) · 1.61 KB
/
main.c
File metadata and controls
59 lines (49 loc) · 1.61 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include "functions.h"
int main(int argc, char *argv[])
{
if (argc != 2)
{
fprintf(stderr, "Usage: %s <matrix_file.mtx>\n", argv[0]);
return EXIT_FAILURE;
}
// <Handle the inputs here>
const char *filename = argv[1];
CSRMatrix A;
ReadMMtoCSR(filename, &A);
// Initializing the vector b and x(in Ax=b)
double *x = (double *)malloc(A.num_cols * sizeof(double));
double *b = (double *)malloc(A.num_rows * sizeof(double));
// Set all elements of b to 1 and x to 0
for (int i = 0; i < A.num_rows; ++i)
{
b[i] = 1.0;
x[i] = 2.0;
}
//calling on the solver and recording the time it takes
clock_t start_time = clock();
solver(&A, b, x);
clock_t end_time = clock();
double cpu_time_taken = ((double)(end_time - start_time)) / CLOCKS_PER_SEC;
//printing the outputs
printf("The matrix name: %s\n", filename);
printf("The dimension of the matrix: %d by %d\n", A.num_rows, A.num_cols);
printf("Number of non-zeros: %d\n", A.row_ptr[A.num_rows]);
printf("CPU time taken solve Ax=b: %lf\n", cpu_time_taken);
//computing and printing the residual and residual norm
double *residual = (double *)malloc(A.num_rows * sizeof(double));
compute_residual(&A, b, x, residual);
double residual_norm = compute_norm(residual, A.num_rows);
printf("Residual Norm: %.16e\n", residual_norm);
// Clean up
free(A.csr_data);
free(A.col_ind);
free(A.row_ptr);
free(x);
free(b);
free(residual);
return EXIT_SUCCESS;
}