/
lu_intel.c
226 lines (199 loc) · 4.55 KB
/
lu_intel.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <time.h>
#include <omp.h>
typedef void (solver)(double**, double**, double**, int);
void zero(double **A, int n) {
memset(&A[0][0], 0, sizeof(double) * n * n);
}
void copy(double** dest, double** src, int n) {
memcpy(&dest[0][0], &src[0][0], sizeof(double) * n * n);
}
void init(double** A, int n) {
for (int i = 0; i < n; ++i)
A[i][i] = 1;
}
double** new_matrix(int n) {
double **A = (double**) malloc(sizeof(double*) * n);
A[0] = (double*) malloc(sizeof(double) * n * n);
for(int i=0; i < n; i++)
A[i] = A[0] + n*i;
return A;
}
double** rand_matrix(int n) {
double **A = new_matrix(n);
for(int i=0; i < n; i++)
for(int j=0; j < n; j++)
A[i][j] = (double) rand() / (double) (RAND_MAX / 100);
return A;
}
void show(double **A, int n) {
for(int i=0; i < n; i++) {
for(int j=0; j < n; j++)
printf("%.3f ",A[i][j]);
printf("\n");
}
printf("\n");
}
void mult(double **A, double **B, double **C, int n) {
for(int i=0; i < n; i++)
for(int j=0; j < n; j++) {
double val = 0;
for(int k=0; k < n; k++)
val += A[i][k] * B[k][j];
C[i][j] = val;
}
}
void lu(double **A, double **L, double **U, int n) {
zero (L, n);
copy (U, A, n);
init (L, n);
for(int j=0; j < n; j++){
// this loop can be parallelized
for(int i=j+1; i < n; i++) {
double m = U[i][j] / U[j][j];
L[i][j] = m;
// this loop can also be parallelized
for(int k=j; k < n; k++)
U[i][k] -= m*U[j][k];
}
}
}
/**
* GCC: gcc -o lu -fopenmp -std=c99 lu.c
*/
void luParallel(double **A, double **L, double **U, int n) {
zero (L, n);
copy (U, A, n);
init (L, n);
#pragma omp parallel
{
for(int j=0; j < n; j++) {
#pragma omp for
for(int i=j+1; i < n; i++) {
double m = U[i][j] / U[j][j];
L[i][j] = m;
for(int k=j; k < n; k++)
U[i][k] -= m*U[j][k];
}
#pragma omp barrier
}
}
}
void luAll(double **A, double **L, double **U, int n) {
zero (L, n);
copy (U, A, n);
init (L, n);
#pragma omp parallel
{
for(int j=0; j < n; j++) {
#pragma omp for
for(int i=j+1; i < n; i++) {
double m = U[i][j] / U[j][j];
L[i][j] = m;
#pragma vector always
for(int k=j; k < n; k++)
U[i][k] -= m*U[j][k];
}
#pragma omp barrier
}
}
}
void luUnroll(double **A, double **L, double **U, int n) {
zero (L, n);
copy (U, A, n);
init (L, n);
for(int j=0; j < n; j++){
for(int i=j+1; i < n; i++) {
double m = U[i][j] / U[j][j];
L[i][j] = m;
for(int k=j; k < n; k++)
U[i][k] -= m*U[j][k];
}
}
}
void luVec(double **A, double **L, double **U, int n) {
zero (L, n);
copy (U, A, n);
init (L, n);
for(int j=0; j < n; j++){
for(int i=j+1; i < n; i++) {
double m = U[i][j] / U[j][j];
L[i][j] = m;
#pragma vector always
for(int k=j; k < n; k++)
U[i][k] -= m*U[j][k];
}
}
}
/**
* LU Factorization with loop unrolling and vectorization
* Compiler options
* Intel compiler: icc -xHOST -O3
* GCC: gcc -march=native -O3
*/
void luUnrollVec(double **A, double **L, double **U, int n) {
zero (L, n);
copy (U, A, n);
init (L, n);
for(int j=0; j < n; j++){
for(int i=j+1; i < n; i++) {
double m = U[i][j] / U[j][j];
L[i][j] = m;
#pragma vector always
for(int k=j; k < n; k++)
U[i][k] -= m*U[j][k];
}
}
}
void check_LU(solver *f, int n) {
double **A = rand_matrix(n);
double **L = new_matrix(n);
double **U = new_matrix(n);
(*f)(A,L,U,n);
double **LU = new_matrix(n);
mult(L,U,LU,n);
for(int i=0; i < n; i++)
for(int j=0; j < n; j++)
if(abs(A[i][j] - LU[i][j]) > 0.00000001) {
printf("Test Failed!! \n");
show(A,n); show(L,n); show(U,n);
}
free(L); free(U); free(A);
printf("Test Passed!! \n");
}
float benchmark_LU(solver *f, int n, int trials){
double t = 0;
double **L = new_matrix(n);
double **U = new_matrix(n);
for(int i=0; i < trials; i++) {
double **A = rand_matrix(n);
t -= omp_get_wtime();
(*f)(A,L,U,n);
t += omp_get_wtime();
free(A);
}
free(L); free(U);
//printf( "A matrix of size %d x %d took an average of %f seconds to factor.\n", n, n, ((float)t)/(trials*CLOCKS_PER_SEC));
return (float) t / trials;
}
void benchmark() {
solver *solvers[6] = { &lu, &luVec, &luUnroll, &luUnrollVec, &luParallel, &luAll };
for(int i=0; i < 6; i++)
check_LU(solvers[i],25);
printf("n, lu, luVec, luUnroll, luVecUnroll, luParallel, luAll\n");
for(int n=100; n <= 2000; n*=2) {
printf("%d, ", n);
for(int s=0; s < 6; s++) {
float t = benchmark_LU(solvers[s], n, 8);
printf("%f, ",t);
}
printf("\n");
}
}
int main() {
benchmark();
return 0;
}