-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.F90
247 lines (215 loc) · 5.75 KB
/
main.F90
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
module linear_algebra
use omp_lib
implicit none
public
contains
subroutine v_set(n,v,val)
integer, intent(in):: n !vector dimension
real(8), intent(inout):: v(n) !vector
real(8), intent(in):: val !value to set for each element of the vector
integer:: i
!$OMP PARALLEL DEFAULT(NONE) SHARED(n,v,val) PRIVATE(i)
!$OMP DO SCHEDULE(STATIC)
do i=1,n
v(i)=val
enddo
!$OMP END DO
!$OMP END PARALLEL
end subroutine v_set
subroutine m_set(n,m,w,val)
integer, intent(in):: n,m !matrix dimensions
real(8), intent(inout):: w(n,m) !matrix
real(8), intent(in):: val !value to set for each element of the matrix
integer:: i,j
!$OMP PARALLEL DEFAULT(NONE) SHARED(n,m,w,val) PRIVATE(i,j)
!$OMP DO SCHEDULE(STATIC) COLLAPSE(2)
do j=1,m
do i=1,n
w(i,j)=val
enddo
enddo
!$OMP END DO
!$OMP END PARALLEL
end subroutine m_set
subroutine v_norm2(n,v,d)
integer, intent(in):: n !vector dimension
real(8), intent(in):: v(n) !vector
real(8), intent(out):: d !vector norm
integer:: i
d=0d0
!$OMP PARALLEL DEFAULT(NONE) SHARED(n,v) PRIVATE(i) REDUCTION(+:d)
!$OMP DO SCHEDULE(GUIDED)
do i=1,n
d=d+v(i)*v(i)
enddo
!$OMP END DO
!$OMP END PARALLEL
d=sqrt(d)
end subroutine v_norm2
subroutine m_norm2(n,m,w,d)
integer, intent(in):: n,m !matrix dimensions
real(8), intent(in):: w(n,m) !matrix
real(8), intent(out):: d !matrix norm
integer:: i,j
d=0d0
!$OMP PARALLEL DEFAULT(NONE) SHARED(n,m,w) PRIVATE(i,j) REDUCTION(+:d)
!$OMP DO SCHEDULE(GUIDED) COLLAPSE(2)
do j=1,m
do i=1,n
d=d+w(i,j)*w(i,j)
enddo
enddo
!$OMP END DO
!$OMP END PARALLEL
d=sqrt(d)
end subroutine m_norm2
subroutine vv_mul(n,v1,v2,d)
integer, intent(in):: n !vector dimension
real(8), intent(in):: v1(n) !vector 1
real(8), intent(in):: v2(n) !vector 2
real(8), intent(out):: d !dot-product
integer:: i,nth
d=0d0
!$OMP PARALLEL DEFAULT(NONE) SHARED(nth,n,v1,v2) PRIVATE(i) REDUCTION(+:d)
!$OMP MASTER
nth=omp_get_num_threads()
!$OMP END MASTER
!$OMP DO SCHEDULE(GUIDED)
do i=1,n
d=d+v1(i)*v2(i)
enddo
!$OMP END DO
!$OMP END PARALLEL
print *,'vv_mul: num_threads = ',nth
end subroutine vv_mul
subroutine mv_mul(variant,n,m,w1,v1,v2)
integer, intent(in):: variant !variant of the code: {1,2}
integer, intent(in):: n,m !matrix dimensions
real(8), intent(in):: w1(n,m) !input matrix
real(8), intent(in):: v1(m) !input vector
real(8), intent(inout):: v2(n) !output vector
integer:: i,j
real(8):: tmp
select case(variant)
case(1)
!$OMP PARALLEL DEFAULT(NONE) SHARED(n,m,w1,v1,v2) PRIVATE(i,j,tmp)
!$OMP DO SCHEDULE(GUIDED)
do i=1,n
tmp=0d0
do j=1,m
tmp=tmp+w1(i,j)*v1(j)
enddo
v2(i)=tmp
enddo
!$OMP END DO
!$OMP END PARALLEL
case(2)
!$OMP PARALLEL DEFAULT(NONE) SHARED(n,m,w1,v1,v2) PRIVATE(i,j,tmp)
!$OMP DO SCHEDULE(GUIDED)
do i=1,n
v2(i)=0d0
enddo
!$OMP END DO
do j=1,m
tmp=v1(j)
!$OMP DO SCHEDULE(GUIDED)
do i=1,n
v2(i)=v2(i)+w1(i,j)*tmp
enddo
!$OMP END DO
enddo
!$OMP END PARALLEL
end select
end subroutine mv_mul
subroutine mm_mul(variant,n,m,l,w1,w2,w3)
integer, intent(in):: variant !variant of the code: {1}
integer, intent(in):: n,m,l !matrix multiplication dimensions
real(8), intent(in):: w1(n,l),w2(l,m) !input matrices
real(8), intent(inout):: w3(n,m) !output matrix
integer:: i,j,k
real(8):: tmp
select case(variant)
case(1)
!$OMP PARALLEL DEFAULT(NONE) SHARED(n,m,l,w1,w2,w3) PRIVATE(i,j,k,tmp)
!$OMP DO SCHEDULE(GUIDED) COLLAPSE(2)
do i=1,n
do j=1,m
tmp=0d0
do k=1,l
tmp=tmp+w1(i,k)*w2(k,j)
enddo
w3(i,j)=tmp
enddo
enddo
!$OMP END DO
!$OMP END PARALLEL
end select
end subroutine mm_mul
function mm_flops(n,m,l,tm) result(gflops)
real(8):: gflops !GFlop/s
integer, intent(in):: n,m,l !matrix multiplication dimensions
real(8), intent(in):: tm !time taken by matrix multiplications
gflops=2d0*real(n,8)*real(m,8)*real(l,8)/tm/1d9
end function mm_flops
end module linear_algebra
program main
use omp_lib
use linear_algebra
integer, parameter:: n=1000000,m=100,l=200
real(8), allocatable:: v1(:),v2(:),v3(:),w1(:,:),w2(:,:),w3(:,:)
real(8):: d,tms,tmf
!Allocate data:
allocate(w3(n,m),w1(n,l),w2(l,m),v3(n),v2(n),v1(m))
!Set the desired number of threads:
call omp_set_num_threads(1)
!Set vector 1:
tms=omp_get_wtime()
call v_set(m,v1,1d0)
tmf=omp_get_wtime()
print *,'Vector 1 set; Time (s) = ',tmf-tms
!Set vector 2:
tms=omp_get_wtime()
call v_set(n,v2,0d0)
tmf=omp_get_wtime()
print *,'Vector 2 set; Time (s) = ',tmf-tms
!Set vector 3:
tms=omp_get_wtime()
call v_set(n,v3,3d0)
tmf=omp_get_wtime()
print *,'Vector 3 set; Time (s) = ',tmf-tms
!Set matrix 1:
tms=omp_get_wtime()
call m_set(n,l,w1,1d0)
tmf=omp_get_wtime()
print *,'Matrix 1 set; Time (s) = ',tmf-tms
!Set matrix 2:
tms=omp_get_wtime()
call m_set(l,m,w2,1d0)
tmf=omp_get_wtime()
print *,'Matrix 2 set; Time (s) = ',tmf-tms
!Set matrix 3:
tms=omp_get_wtime()
call m_set(n,m,w3,1d0)
tmf=omp_get_wtime()
print *,'Matrix 3 set; Time (s) = ',tmf-tms
!Matrix-vector multiplication:
tms=omp_get_wtime()
call mv_mul(1,n,m,w3,v1,v2)
tmf=omp_get_wtime()
call v_norm2(n,v2,d)
print *,'Matrix-vector product norm = ',d,'; Time (s) = ',tmf-tms
!Vector-Vector multiplication (dot-product):
tms=omp_get_wtime()
call vv_mul(n,v2,v3,d)
tmf=omp_get_wtime()
print *,'Vector-vector product = ',d,'; Time (s) = ',tmf-tms
!Matrix-matrix multiplication:
call m_set(n,m,w3,0d0) !reset matrix 3 to zero
tms=omp_get_wtime()
call mm_mul(1,n,m,l,w1,w2,w3)
tmf=omp_get_wtime()
call m_norm2(n,m,w3,d)
print *,'Matrix-matrix product norm = ',d,'; Time (s) = ',tmf-tms,'; GFlop/s = ',mm_flops(n,m,l,tmf-tms)
!Deallocate data:
deallocate(w3,w1,w2,v2,v1)
end program main