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
|
PROGRAM mul
USE naivmat
USE bettmat
USE dotmat
USE bettmat2
USE iso_fortran_env, only: error_unit
IMPLICIT none
integer, parameter :: seed = 123456
real :: time
integer :: dim = 10, multype, real_kind, stat
character(5) :: kind_arg, impl_arg
IF (command_argument_count() < 2) THEN
call print_usage()
STOP
END IF
call get_command_argument(1, kind_arg)
read (kind_arg, *, iostat = stat) real_kind
IF (stat .ne. 0) THEN
write (error_unit, '(A)') "Couldn't parse kind number argument"
call print_usage()
STOP
END IF
call get_command_argument(2, impl_arg)
IF (trim(impl_arg) .eq. "naiv") THEN
multype = 1
ELSE IF (trim(impl_arg) .eq. "bett") THEN
multype = 2
ELSE IF (trim(impl_arg) .eq. "dot") THEN
multype = 3
ELSE IF (trim(impl_arg) .eq. "mat") THEN
multype = 4
ELSE IF (trim(impl_arg) .eq. "bett2") THEN
multype = 5
ELSE
write (error_unit, '(A)') "Unrecognized implementation argument"
call print_usage()
STOP
END IF
DO WHILE (dim < 2000)
SELECT CASE(real_kind)
CASE (4,1)
time = measure_4(dim)
CASE (8,2)
time = measure_8(dim)
CASE (16,3)
time = measure_16(dim)
CASE default
write (error_unit, '(A,I6)') &
"wrong kind for real:", real_kind
STOP
END SELECT
print '(I11," ",ES11.5)', dim, time
dim = dim * 2
END DO
CONTAINS
SUBROUTINE print_usage()
write (*, '(A)') &
"Usage: mull KIND IMPLEMENTATION" // char(10) // &
"where KIND is one of: 4, 8, 16" // char(10) // &
" IMPLEMENTATION is one of: naiv, bett, dot, mat"
END SUBROUTINE print_usage
SUBROUTINE init_random_seed()
integer :: i, n
call random_seed(size = n)
call random_seed(put = (/ ((seed + i) * 37, i = 1, n) /))
END SUBROUTINE init_random_seed
real FUNCTION measure_4(dim) result(time)
integer, intent(in) :: dim
real(kind=4), dimension(:,:), allocatable :: mat1, mat2, res
real :: start, end
call init_random_seed()
allocate(mat1(dim,dim))
allocate(mat2(dim,dim))
allocate(res(dim,dim))
call random_number(mat1)
call random_number(mat2)
call cpu_time(start)
SELECT CASE(multype)
CASE (1)
res = naivmull(mat1, mat2)
CASE (2)
res = bettmull(mat1, mat2)
CASE (3)
res = dotmull(mat1, mat2)
CASE (4)
res = matmul(mat1, mat2)
CASE default
res = bett2mull(mat1, mat2)
END SELECT
call cpu_time(end)
time = end - start
END FUNCTION measure_4
real FUNCTION measure_8(dim) result(time)
integer, intent(in) :: dim
real(kind=8), dimension(:,:), allocatable :: mat1, mat2, res
real :: start, end
call init_random_seed()
allocate(mat1(dim,dim))
allocate(mat2(dim,dim))
allocate(res(dim,dim))
call random_number(mat1)
call random_number(mat2)
call cpu_time(start)
SELECT CASE(multype)
CASE (1)
res = naivmull(mat1, mat2)
CASE (2)
res = bettmull(mat1, mat2)
CASE (3)
res = dotmull(mat1, mat2)
CASE (4)
res = matmul(mat1, mat2)
CASE default
res = bett2mull(mat1, mat2)
END SELECT
call cpu_time(end)
time = end - start
END FUNCTION measure_8
real FUNCTION measure_16(dim) result(time)
integer, intent(in) :: dim
real(kind=16), dimension(:,:), allocatable :: mat1, mat2, res
real :: start, end
call init_random_seed()
allocate(mat1(dim,dim))
allocate(mat2(dim,dim))
allocate(res(dim,dim))
call random_number(mat1)
call random_number(mat2)
call cpu_time(start)
SELECT CASE(multype)
CASE (1)
res = naivmull(mat1, mat2)
CASE (2)
res = bettmull(mat1, mat2)
CASE (3)
res = dotmull(mat1, mat2)
CASE (4)
res = matmul(mat1, mat2)
CASE default
res = bett2mull(mat1, mat2)
END SELECT
call cpu_time(end)
time = end - start
END FUNCTION measure_16
END PROGRAM mul
|