-
Notifications
You must be signed in to change notification settings - Fork 0
/
dolittle.cpp
145 lines (135 loc) · 4.28 KB
/
dolittle.cpp
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
#include<bits/stdc++.h>
#include<omp.h>
#define ld long double
#define n 2000
#define chunk 10
#include <sys/types.h>
#include <sys/times.h>
#include <sys/time.h>
#include <time.h>
using namespace std;
ld a[n][n],lwr[n][n],upr[n][n];
int main(){
struct timeval start, stop; /* Elapsed times using gettimeofday() */
struct timezone tz;
clock_t start2, stop2; /* Elapsed times using times() */
unsigned long long usecstart, usecstop ;
struct tms cputstart, cputstop; /* CPU times for processes */
int i,j,k,nthreads,tid;
//double start,end; // for measuring time
//cout<<"enter order of matrix : ";
//cin>>n;
/*----------------------------Initalization of matrix-------------------------------*/
srand(time(NULL));
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
a[i][j]=1+rand()%1000;
// cin>>a[i][j];
}
}
/*----------------------------Parallel run of algorithm-----------------------------*/
printf("\nStarting clock for parallel algorithm.\n");
gettimeofday(&start, &tz);
start2 = times(&cputstart);
//start=omp_get_wtime();
//start = clock_t
#pragma omp parallel shared(lwr,upr,a,nthreads) private(tid,i,k,j)
{
tid=omp_get_thread_num();
if(tid==0)
nthreads=omp_get_num_threads();
for(k=0;k<n;k++)
{
lwr[k][k]=1;
#pragma omp for schedule(static,chunk)
for(j=k;j<n;j++)
{
ld sum=0;
for(int s=0;s<=k-1;s++)
{
sum+=lwr[k][s]*upr[s][j];
}
upr[k][j]=a[k][j]-sum;
}
#pragma omp for schedule(static,chunk)
for(i=k+1;i<n;i++)
{
ld sum=0;
for(int s=0;s<=k-1;s++)
{
sum+=lwr[i][s]*upr[s][k];
}
lwr[i][k]=(a[i][k]-sum)/upr[k][k];
}
}
}
//end=omp_get_wtime();
//printf("time for parallel run : %lf\n",end-start);
gettimeofday(&stop, &tz);
stop2 = times(&cputstop);
printf("Stopped clock.\n");
usecstart = (unsigned long long)start.tv_sec * 1000000 + start.tv_usec;
usecstop = (unsigned long long)stop.tv_sec * 1000000 + stop.tv_usec;
printf("Total elapsed time = %g ms.\n",(float)(usecstop-usecstart)/(float)CLOCKS_PER_SEC * 1000) ;
printf("Total elapsed time by system in calling and executing = %g ms.\n",(float)(stop2-start2) / (float)CLOCKS_PER_SEC * 1000) ;
printf("My total CPU time for parent = %g ms.\n", (float)( (cputstop.tms_utime + cputstop.tms_stime) - (cputstart.tms_utime + cputstart.tms_stime) ) /(float)CLOCKS_PER_SEC * 1000);
printf("My total CPU time for child processes = %g ms.\n", (float)( (cputstop.tms_cutime + cputstop.tms_cstime) - (cputstart.tms_cutime + cputstart.tms_cstime) ) / (float)CLOCKS_PER_SEC * 1000);
printf("--------------------------------------------\n");
/*----------------------------Sequential run of algorithm-----------------------------*/
//start=omp_get_wtime();
printf("\nStarting clock for sequential algorithm.\n");
gettimeofday(&start, &tz);
start2 = times(&cputstart);
for(k=0;k<n;k++)
{
lwr[k][k]=1;
for(j=k;j<n;j++)
{
ld sum=0;
for(int s=0;s<=k-1;s++)
{
sum+=lwr[k][s]*upr[s][j];
}
upr[k][j]=a[k][j]-sum;
}
for(i=k+1;i<n;i++)
{
ld sum=0;
for(int s=0;s<=k-1;s++)
{
sum+=lwr[i][s]*upr[s][k];
}
lwr[i][k]=(a[i][k]-sum)/upr[k][k];
}
}
gettimeofday(&stop, &tz);
stop2 = times(&cputstop);
printf("Stopped clock.\n");
usecstart = (unsigned long long)start.tv_sec * 1000000 + start.tv_usec;
usecstop = (unsigned long long)stop.tv_sec * 1000000 + stop.tv_usec;
printf("Total elapsed time = %g ms.\n",(float)(usecstop-usecstart)/(float)CLOCKS_PER_SEC * 1000) ;
printf("Total elapsed time by system in calling and executing = %g ms.\n",(float)(stop2-start2) / (float)CLOCKS_PER_SEC * 1000) ;
printf("My total CPU time for parent = %g ms.\n", (float)( (cputstop.tms_utime + cputstop.tms_stime) - (cputstart.tms_utime + cputstart.tms_stime) ) /(float)CLOCKS_PER_SEC * 1000);
printf("My total CPU time for child processes = %g ms.\n", (float)( (cputstop.tms_cutime + cputstop.tms_cstime) - (cputstart.tms_cutime + cputstart.tms_cstime) ) / (float)CLOCKS_PER_SEC * 1000);
printf("--------------------------------------------\n");
//end=omp_get_wtime();
//printf("\ntime for sequential run : %lf\n",end-start);
/*for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
cout<<lwr[i][j]<<" ";
}
cout<<endl;
}*/
/*for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
cout<<upr[i][j]<<" ";
}
cout<<endl;
}*/
}