File: openmp_67.f90

package info (click to toggle)
lfortran 0.60.0-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 58,416 kB
  • sloc: cpp: 173,406; f90: 80,491; python: 17,586; ansic: 9,610; yacc: 2,356; sh: 1,401; fortran: 895; makefile: 38; javascript: 15
file content (121 lines) | stat: -rw-r--r-- 3,571 bytes parent folder | download
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
program openmp_67
    use omp_lib
    implicit none
    integer, parameter :: n = 10000
    integer :: i, j
    real :: static_time, dynamic_time, guided_time
    double precision :: start_time
    real :: a(n), b(n), c(n)
    
    call omp_set_num_threads(4)
    
    print *, "=== Schedule Comparison Test ==="
    print *, "Comparing performance of different schedules"
    print *, "Array size:", n, "Threads:", omp_get_max_threads()
    
    ! Initialize arrays
    ! Alternative: Initialize arrays with deterministic values
    do i = 1, n
        b(i) = real(i)
        c(i) = real(n - i + 1)
    end do
    
    ! Test STATIC
    start_time = omp_get_wtime()
    !$omp parallel do schedule(static)
    do i = 1, n
        a(i) = sqrt(b(i)) + log(abs(c(i)) + 1.0)
    end do
    !$omp end parallel do
    static_time = omp_get_wtime() - start_time
    
    ! Test DYNAMIC
    start_time = omp_get_wtime()
    !$omp parallel do schedule(dynamic)
    do i = 1, n
        a(i) = sqrt(b(i)) + log(abs(c(i)) + 1.0)
    end do
    !$omp end parallel do
    dynamic_time = omp_get_wtime() - start_time
    
    ! Test GUIDED
    start_time = omp_get_wtime()
    !$omp parallel do schedule(guided)
    do i = 1, n
        a(i) = sqrt(b(i)) + log(abs(c(i)) + 1.0)
    end do
    !$omp end parallel do
    guided_time = omp_get_wtime() - start_time
    
    ! Print results
    print '(A,F8.6,A)', "STATIC  time: ", static_time, " seconds"
    print '(A,F8.6,A)', "DYNAMIC time: ", dynamic_time, " seconds"
    print '(A,F8.6,A)', "GUIDED  time: ", guided_time, " seconds"
    
    ! Analysis
    print *, ""
    print *, "Analysis:"
    
    ! For uniform workload, static should be fastest
    if (static_time < dynamic_time * 0.9 .and. static_time < guided_time * 0.9) then
        print *, "✓ STATIC is fastest for uniform workload"
    else if (dynamic_time < static_time * 0.9) then
        print *, "! DYNAMIC is fastest"
    else if (guided_time < static_time * 0.9) then
        print *, "! GUIDED is fastest"
    else
        print *, "- All schedules perform similarly"
    end if
    
    ! Test with non-uniform workload
    print *, ""
    print *, "Testing with non-uniform workload..."
    
    ! Make some iterations heavier
    do i = 1, n
        if (mod(i, 10) == 0) then
            b(i) = b(i) * 1000.0  ! Make every 10th iteration heavier
        end if
    end do
    
    ! Test STATIC with non-uniform
    start_time = omp_get_wtime()
    !$omp parallel do schedule(static)
    do i = 1, n
        if (mod(i, 10) == 0) then
            ! Heavy computation
            a(i) = 0.0
            do j = 1, 100
                a(i) = a(i) + sqrt(b(i)) + log(abs(c(i)) + 1.0)
            end do
        else
            a(i) = sqrt(b(i)) + log(abs(c(i)) + 1.0)
        end if
    end do
    !$omp end parallel do
    static_time = omp_get_wtime() - start_time
    
    ! Test DYNAMIC with non-uniform
    start_time = omp_get_wtime()
    !$omp parallel do schedule(dynamic)
    do i = 1, n
        if (mod(i, 10) == 0) then
            a(i) = 0.0
            do j = 1, 100
                a(i) = a(i) + sqrt(b(i)) + log(abs(c(i)) + 1.0)
            end do
        else
            a(i) = sqrt(b(i)) + log(abs(c(i)) + 1.0)
        end if
    end do
    !$omp end parallel do
    dynamic_time = omp_get_wtime() - start_time
    
    print *, ""
    print *, "Non-uniform workload results:"
    print '(A,F8.6,A)', "STATIC  time: ", static_time, " seconds"
    print '(A,F8.6,A)', "DYNAMIC time: ", dynamic_time, " seconds"
    

    
end program openmp_67