-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLU_Parallel.c
298 lines (286 loc) · 6.39 KB
/
LU_Parallel.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
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <stdbool.h>
#include <omp.h>
struct Compare { float val; int index; };
#pragma omp declare reduction(maximum : struct Compare : omp_out = omp_in.val > omp_out.val ? omp_in : omp_out)
double **mat,**P,**L,**U,**matTemp, *B,*Btemp, *x, *v;
double start=0,stop=0,res=0,ptotal=0;
void swap(double *xp, double *yp)
{
double temp = *xp;
*xp = *yp;
*yp = temp;
}
void printMatrix(double **mat,int size)
{
printf("\n \n");
int i,j;
for (i=0;i<size;i++) {
for(j=0;j<size;j++) {
if(mat[i][j]>=0)
printf(" ");
printf(" %f ",mat[i][j]);
}
printf("\n");
}
printf("\n");
}
void freeMatrix(int size)
{
int i =0;
for(i = 0; i < size; i++)
{
free(mat[i]);
free(P[i]);
free(L[i]);
free(U[i]);
free(matTemp[i]);
}
//free(mat);
//free(P);
//free(L);
//free(U);
//free(matTemp);
//free(B);
//free(Btemp);
//free(x);
//free(v);
}
void lu_pivot(int size)
{
start=omp_get_wtime();
int k;
for(k = 0; k < size-1; k++){
struct Compare max;
max.val = U[k][k];
max.index = k;
int maxi=k;
#pragma omp parallel for reduction(maximum:max)
for(int i = k+1; i<size; i++) {
if(U[i][k] > max.val)
{
max.val = U[i][k];
max.index = i;
}
}
maxi=max.index;
if(maxi!=k){
#pragma omp parallel for
for(int i=k;i<size;i++)
swap(&U[k][i],&U[maxi][i]);
#pragma omp parallel for
for(int i=0;i<=k-1;i++)
swap(&L[k][i],&L[maxi][i]);
#pragma omp parallel for
for(int i=0;i<size;i++)
swap(&P[k][i],&P[maxi][i]);
}
if(U[k][k]==0)
{
printf("\n INFINITE MANY SOLUTION\n");
exit(0);
}
#pragma omp parallel for
for(int row = k+1 ; row < size; row++){
L[row][k] = U[row][k]/U[k][k];
#pragma omp parallel for
for(int col = k; col < size; col++)
U[row][col] = U[row][col] - L[row][k]*U[k][col];
}
}
stop=omp_get_wtime();
res=stop-start;
printf("\nExecution Time for Partial Pivot LU Factorization = %f\n",res);
ptotal+=res;
}
void lu_npivot(int size)
{
start=omp_get_wtime();
int k;
for(k = 0; k < size-1; k++){
if(U[k][k]==0)
{
printf("\nError...\n");
exit(0);
}
#pragma omp parallel for
for(int row = k+1 ; row < size; row++){
L[row][k] = U[row][k]/U[k][k];
#pragma omp parallel for
for(int col = k; col < size; col++)
U[row][col] = U[row][col] - L[row][k]*U[k][col];
}
}
stop=omp_get_wtime();
res=stop-start;
printf("\nExecution Time for No Pivot LU Factorization = %f\n",res);
ptotal+=res;
}
void generate(int size)
{
mat = malloc(size *(sizeof *mat));
matTemp = malloc(size *(sizeof *mat));
P = malloc(size * (sizeof *P));
L = malloc(size *(sizeof *L));
U = malloc(size *(sizeof *U));
int i =0, j=0;
for(i=0;i<size;i++){
mat[i] = malloc((sizeof *mat[i]) * size);
matTemp[i] = malloc((sizeof *mat[i]) * size);
P[i] = malloc((sizeof *P[i]) * size);
L[i] = malloc((sizeof *L[i]) * size);
U[i] = malloc((sizeof *U[i]) * size);
}
for(i=0;i<size;i++){
for( j=0; j< size; j++) {
mat[i][j] = i+1;
mat[j][i] = i+1;
U[i][j]=mat[i][j];
U[j][i]=mat[j][i];
if(i==j)
{
P[i][j]=1;
L[i][j]=1;
}
else
{
P[i][j]=0;
L[i][j]=0;
}
matTemp[i][j]=0;
}
}
x = malloc(size*(sizeof(double)));
v = malloc(size*(sizeof(double)));
B= malloc(size*(sizeof(double)));
Btemp= malloc(size*(sizeof(double)));
for(int i=0;i<size;i++)
{
B[i]=2*i+7;
Btemp[i] = 0;
}
}
void printVector(double *t,int size)
{
printf("\n\n");
if(t==B){
for(int i=0;i<size;i++)
printf("B[%d]: %f\n",i,B[i]);
}
else if( t== v){
for(int i=0;i<size;i++)
printf("v[%d]: %f\n",i,v[i]);
}
else if(t== x)
{
for(int i=0;i<size;i++)
printf("x[%d]: %f\n",i,x[i]);
}
else
{
for(int i=0;i<size;i++)
printf("PB[%d]: %f\n",i,Btemp[i]);
}
}
void SolveV(int size)
{
start=omp_get_wtime();
for(int i=0;i<size;i++)
{
v[i]=B[i];
double sub=0;
#pragma omp parallel for reduction(+:sub)
for(int j=0;j<i;j++)
sub+=L[i][j]*v[j];
//printf("\n sub: %f",sub);
v[i]-=sub;
}
stop=omp_get_wtime();
res=stop-start;
printf("\nExecution Time for Evaluating 'V' = %f\n",res);
ptotal+=res;
}
void SolveX(int size)
{
start=omp_get_wtime();
for(int i=size-1;i>=0;i--)
{
x[i]=v[i];
double sum=0;
#pragma omp parallel for reduction(+:sum)
for(int j=i+1;j<size;j++)
sum+=U[i][j]*x[j];
x[i]-=sum;
x[i]/=U[i][i];
}
stop=omp_get_wtime();
res=stop-start;
printf("\nExecution Time for Evaluating 'X' = %f\n",res);
ptotal+=res;
}
int main()
{
int size,choice;
printf("Enter The Square Matrix Size: ");
scanf("%d",&size);
generate(size);
//printf("\nDone:\n");
int i,j;
printf("\n A");
printMatrix(mat,size);
printVector(B,size);
//printMatrix(L,size);
//printMatrix(U,size);
printf("\n Partial Pivoted ....1");
printf("\n No Pivoted..........2\n");
scanf("%d",&choice);
if(choice==1){
lu_pivot(size);
//printMatrix(mat,size);
printf("\n LU PARTIAL PIVOTED\n");
printf("\n L");
printMatrix(L,size);
printf("\n U");
printMatrix(U,size);
printf("\n P");
printMatrix(P,size);
start=omp_get_wtime();
#pragma omp parallel for
for(int i=0;i<size;i++)
for(int k=0;k<size;k++)
Btemp[i]+=P[i][k]*B[k];
stop=omp_get_wtime();
res=stop-start;
#pragma omp parallel for
for(int i=0;i<size;i++)
#pragma omp parallel for
for(int j=0;j<size;j++)
for(int k=0;k<size;k++)
matTemp[i][j]+=P[i][k]*mat[k][j];
printf("\nExecution Time for getting PB = %f\n",res);
ptotal+=res;
printf("\n PA");
printMatrix(matTemp,size);
printVector(Btemp,size);
B = Btemp;
SolveV(size);
printVector(v,size);
SolveX(size);
printVector(x,size);
}
else if(choice ==2){
printf("\n LU \n");
lu_npivot(size);
printf("\n L");
printMatrix(L,size);
printf("\n U");
printMatrix(U,size);
SolveV(size);
printVector(v,size);
SolveX(size);
printVector(x,size);
}
printf("\n\nTotal Time of Execution = %f",ptotal);
}