-
Notifications
You must be signed in to change notification settings - Fork 0
/
optimized_dgesv.c
122 lines (97 loc) · 2.6 KB
/
optimized_dgesv.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
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
double *generate_matrix(int size)
{
int i;
double *matrix = (double *)malloc(sizeof(double) * size * size);
srand(1);
for (i = 0; i < size * size; i++)
{
matrix[i] = rand() % 100;
}
return matrix;
}
void print_matrix(const char *name, double *matrix, int size)
{
int i, j;
printf("matrix: %s \n", name);
for (i = 0; i < size; i++)
{
for (j = 0; j < size; j++)
{
printf("%f ", matrix[i * size + j]);
}
printf("\n");
}
}
int my_dgesv(int n, double *a, double *b) {
//Replace with your implementation
int i,j,k,col;
double c,sum=0.0;
double matriz_aumentada[n][n+1];
double x[n][n];
// Para cada columna de b
for (col = 0; col < n; col++)
{
//Copiar el contenido de a en la matriz aumentada
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
matriz_aumentada[i][j]=a[i*n+j];
}
// Para cada fila i añadir el valor de la columna B a la matriz aumentada
matriz_aumentada[i][j]=b[i*n +col];
}
for(j=0; j<n; j++) // loop for the generation of upper triangular matrix
{
for(i=0; i<n; i++)
{
if(i>j)
{
c=matriz_aumentada[i][j]/matriz_aumentada[j][j];
for(k=0; k<=n; k++)
{
matriz_aumentada[i][k]=matriz_aumentada[i][k]-c*matriz_aumentada[j][k];
}
}
}
}
x[n-1][col]=matriz_aumentada[n-1][n]/matriz_aumentada[n-1][n-1];
// this loop is for backward substitution
for(i=n-2; i>=0; i--)
{
sum=0;
for(j=i; j<n; j++)
{
sum=sum+matriz_aumentada[i][j]*x[j][col];
}
x[i][col]=(matriz_aumentada[i][n]-sum)/matriz_aumentada[i][i];
}
}
// Poner la solución en B
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
b[i*n+j]=x[i][j];
}
}
}
void main(int argc, char *argv[])
{
int size = atoi(argv[1]);
double *a;
double *b;
a = generate_matrix(size);
b = generate_matrix(size);
//print_matrix("A", a, size);
//print_matrix("B", b, size);
// Usar eliminacion Gaussiana
clock_t tStart = clock();
my_dgesv(size, a, b);
printf("Time taken by my implementation: %.2fs\n", (double)(clock() - tStart) / CLOCKS_PER_SEC);
// print_matrix("X", b, size);
// print_matrix("Xref", bref, size);
}