-
Notifications
You must be signed in to change notification settings - Fork 1
/
lu.c
90 lines (78 loc) · 1.99 KB
/
lu.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
#include <omp.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
double begin, end;
int ARRAYSIZE = 16;
#define INDEX(i,j) i*ARRAYSIZE+j
struct rec
{
int x,y,z;
};
double *u;
void ludecomp(int n,double *a){
int i,j,k;
#pragma omp parallel shared(a,n) private(i,j,k)
{
for(k = 0; k < n - 1; ++k) {
#pragma omp for schedule(guided)
for(i = k + 1; i < n; i++) {
const double aik = a[INDEX(i,k)] / a[INDEX(k,k)];
for(j = k + 1; j < n; j++) {
a[INDEX(i,j)] -= aik * a[INDEX(k,j)];
}
}
}
}
}
int main (int argc, char *argv[])
{
printf("Cores available: %i\n",omp_get_num_procs());
omp_set_num_threads(omp_get_max_threads());
begin =omp_get_wtime();
int i,j;
int nthreads, tid;
char f_name[50];
if (argc > 1){
printf("Receiving: %s\n", argv[1]);
ARRAYSIZE = atoi(argv[1]);
}
double *a;
a = (double*)calloc(ARRAYSIZE*ARRAYSIZE, sizeof(double));
double det;
double logsum;
//Create filename
sprintf(f_name,"%d.bin",ARRAYSIZE);
printf("Reading array file %s of size %dx%d\n",f_name,ARRAYSIZE,ARRAYSIZE);
//Open file
FILE *datafile=fopen(f_name,"rb");
//Read elelements
for (i=0; i< ARRAYSIZE; i++)
for (j=0; j< ARRAYSIZE; j++)
{
fread(&a[INDEX(i,j)],sizeof(double),1,datafile);
}
printf("Matrix has been read.\n");
printf("Performing LU decomp.\n");
ludecomp(ARRAYSIZE,a);
//Calculate determinant
int neg_nums = 0;
det = a[INDEX(0,0)];
logsum = log10(fabs(a[INDEX(0,0)]));
for (i = 1; i < ARRAYSIZE; i++){
if (a[INDEX(i,i)] > 0){
neg_nums++;
logsum += log10(a[INDEX(i,i)]);
}
else
logsum += log10(fabs(a[INDEX(i,i)]));
}
det = pow(10.0, logsum);
if (neg_nums % 2 != 0)
det = -det;
printf("Determinant: %f\n",det);
printf("log10(fabs(det)): %f\n",logsum);
end = omp_get_wtime();
printf("%i, %f\n",omp_get_max_threads(), (double)(end - begin) / CLOCKS_PER_SEC);
}