forked from scivision/fortran2018-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
contiguous.F90
72 lines (52 loc) · 1.53 KB
/
contiguous.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
module contig
use, intrinsic:: iso_fortran_env, only: sp=>real32, dp=>real64, i64=>int64
implicit none
contains
subroutine timestwo_contig(x, contig)
real(dp), contiguous, intent(inout) :: x(:,:)
logical, intent(out) :: contig
#ifdef ISCONTIG
contig = is_contiguous(x)
#endif
x = 2*x
end subroutine timestwo_contig
subroutine timestwo(x, contig)
real(dp), intent(inout) :: x(:,:)
logical, intent(out), optional :: contig
#ifdef ISCONTIG
contig = is_contiguous(x)
#endif
x = 2*x
end subroutine timestwo
end module contig
!! The ::2 indexing is not contiguous,
!! but the contiguous parameter in timestwo_contig copies the array into contiguous temporary array,
!! which could be faster for some operations
use, intrinsic:: iso_fortran_env, only: compiler_version
use contig
implicit none
integer, parameter :: N = 1000000
real(dp) :: x(2,N) = 1.
integer(i64) :: tic, toc, rate
real(dp) :: t1, t2
logical :: iscontig1, iscontig2
!print *,compiler_version() ! bug in flang 6 and PGI 18.10
#ifdef ISCONTIG
call system_clock(tic)
call timestwo(x(:, ::2), iscontig1)
call system_clock(toc, rate)
t1 = (toc-tic) / real(rate, dp)
call system_clock(tic)
call timestwo_contig(x(:, ::2), iscontig2)
call system_clock(toc, rate)
t2 = (toc-tic) / real(rate, dp)
print '(L2,A,F7.3,A)', iscontig1,' contig: ',t1,' sec.'
print '(L2,A,F7.3,A)', iscontig2,' contig: ',t2,' sec.'
#else
call system_clock(tic)
call timestwo(x(:, ::2))
call system_clock(toc, rate)
t1 = (toc-tic) / real(rate, dp)
print '(A,F7.3,A)', 'non-contig: ',t1,' sec.'
#endif
end program