make selection between floating point types possible
[AGH_fortran_course_solution.git] / src / main.F90
blobbe9dd588e403b01296784a77443d30aa1a653fb8
1 PROGRAM mul
2   USE naivmat
3   USE bettmat
4   USE dotmat
5   IMPLICIT none
7   integer, parameter :: seed = 123456
8   real :: time
9   integer :: dim = 10, multype = 1, realtype = 8
11   DO WHILE (dim < 2000)
13      SELECT CASE(realtype)
14         CASE (4)
15            time = measure_4(dim)
16         CASE (8)
17            time = measure_8(dim)
18         CASE (16)
19            time = measure_16(dim)
20         CASE default
21            write (*, *) "wrong kind for real: ", realtype
23      END SELECT
24         
25      print '(I11," ",ES11.5)', dim, time
27      dim = dim * 2
28      
29   END DO
31 CONTAINS
33   SUBROUTINE init_random_seed()
34     integer :: i, n
35     
36     call random_seed(size = n)
37     
38     call random_seed(put = (/ ((seed + i) * 37, i = 1, n) /))
39     
40   END SUBROUTINE init_random_seed
41   
43   real FUNCTION measure_4(dim) result(time)
44     integer, intent(in) :: dim
45     real(kind=4), dimension(:,:), allocatable :: mat1, mat2, res
46     real :: start, end
48     call init_random_seed()
50     allocate(mat1(dim,dim))
51     allocate(mat2(dim,dim))
52     allocate(res(dim,dim))
54     call random_number(mat1)
55     call random_number(mat2)
57     call cpu_time(start)
59     SELECT CASE(multype)
60        CASE (1)
61           res = naivmull(mat1, mat2)
62        CASE (2)
63           res = bettmull(mat1, mat2)
64        CASE (3)
65           res = dotmull(mat1, mat2)
66        CASE default
67           res = matmul(mat1, mat2)
68        
69     END SELECT
71     call cpu_time(end)
72     
73     time = end - start
74     
75   END FUNCTION measure_4
77   
78   real FUNCTION measure_8(dim) result(time)
79     integer, intent(in) :: dim
80     real(kind=8), dimension(:,:), allocatable :: mat1, mat2, res
81     real :: start, end
83     call init_random_seed()
85     allocate(mat1(dim,dim))
86     allocate(mat2(dim,dim))
87     allocate(res(dim,dim))
89     call random_number(mat1)
90     call random_number(mat2)
92     call cpu_time(start)
94     SELECT CASE(multype)
95        CASE (1)
96           res = naivmull(mat1, mat2)
97        CASE (2)
98           res = bettmull(mat1, mat2)
99        CASE (3)
100           res = dotmull(mat1, mat2)
101        CASE default
102           res = matmul(mat1, mat2)
103        
104     END SELECT
106     call cpu_time(end)
107     
108     time = end - start
109     
110   END FUNCTION measure_8
113   real FUNCTION measure_16(dim) result(time)
114     integer, intent(in) :: dim
115     real(kind=16), dimension(:,:), allocatable :: mat1, mat2, res
116     real :: start, end
118     call init_random_seed()
120     allocate(mat1(dim,dim))
121     allocate(mat2(dim,dim))
122     allocate(res(dim,dim))
124     call random_number(mat1)
125     call random_number(mat2)
127     call cpu_time(start)
129     SELECT CASE(multype)
130        CASE (1)
131           res = naivmull(mat1, mat2)
132        CASE (2)
133           res = bettmull(mat1, mat2)
134        CASE (3)
135           res = dotmull(mat1, mat2)
136        CASE default
137           res = matmul(mat1, mat2)
138        
139     END SELECT
141     call cpu_time(end)
142     
143     time = end - start
144     
145   END FUNCTION measure_16
147 END PROGRAM mul