Parameter Sweeps
Nested Sweep Area
Two Parameters
Nested loops evaluate every pair from two parameter lists and keep the best combination.
Program
Play the program to include more width choices in the sweep.
nested_sweep_area.f90
Replay: real traced execution (multi-file project)
program nested_sweep_area_demo
implicit none
integer :: widths(3)
integer :: heights(2)
integer :: width_count
integer :: height_count
integer :: wi, hi
integer :: width, height, area
integer :: best_width, best_height, best_area
widths = [2, 4, 6]
heights = [3, 5]
width_count = 2
height_count = 2
best_area = -1
best_width = 0
best_height = 0
do wi = 1, width_count
width = widths(wi)
do hi = 1, height_count
height = heights(hi)
area = width * height
if (area > best_area) then
best_area = area
best_width = width
best_height = height
end if
end do
end do
print '(I0, 1X, I0, 1X, I0)', best_width, best_height, best_area
end program nested_sweep_area_demo
program nested_sweep_area_demo
implicit none
integer :: widths(3)
integer :: heights(2)
integer :: width_count
integer :: height_count
integer :: wi, hi
integer :: width, height, area
integer :: best_width, best_height, best_area
widths = [2, 4, 6]
heights = [3, 5]
width_count = 1
height_count = 2
best_area = -1
best_width = 0
best_height = 0
do wi = 1, width_count
width = widths(wi)
do hi = 1, height_count
height = heights(hi)
area = width * height
if (area > best_area) then
best_area = area
best_width = width
best_height = height
end if
end do
end do
print '(I0, 1X, I0, 1X, I0)', best_width, best_height, best_area
end program nested_sweep_area_demo
program nested_sweep_area_demo
implicit none
integer :: widths(3)
integer :: heights(2)
integer :: width_count
integer :: height_count
integer :: wi, hi
integer :: width, height, area
integer :: best_width, best_height, best_area
widths = [2, 4, 6]
heights = [3, 5]
width_count = 3
height_count = 2
best_area = -1
best_width = 0
best_height = 0
do wi = 1, width_count
width = widths(wi)
do hi = 1, height_count
height = heights(hi)
area = width * height
if (area > best_area) then
best_area = area
best_width = width
best_height = height
end if
end do
end do
print '(I0, 1X, I0, 1X, I0)', best_width, best_height, best_area
end program nested_sweep_area_demo
widths ← [2, 4, 6]
11widths = [2, 4, 6]12heights = [3, 5]values this step[2, 4, 6]widthsheights ← [3, 5]
11widths = [2, 4, 6]12heights = [3, 5]13width_count = 2values this step[3, 5]heightswidth_count ← 2
12heights = [3, 5]13width_count = 214height_count = 2values this step2width_countheight_count ← 2
13width_count = 214height_count = 215best_area = -1values this step2height_countbest_area ← -1
14height_count = 215best_area = -116best_width = 0values this step-1best_areabest_width ← 0
15best_area = -116best_width = 017best_height = 0values this step0best_widthbest_height ← 0
16best_width = 017best_height = 018do wi = 1, width_countvalues this step0best_heightwi ← 1
17best_height = 018do wi = 1, width_count19 width = widths(wi)values this step1wiwidth ← 2
18do wi = 1, width_count19 width = widths(wi)20 do hi = 1, height_countvalues this step2widthhi ← 1
19width = widths(wi)20do hi = 1, height_count21 height = heights(hi)values this step1hiheight ← 3
20do hi = 1, height_count21 height = heights(hi)22 area = width * heightvalues this step3heightarea ← 6
21height = heights(hi)22area = width * height23if (area > best_area) thenvalues this step6area2width3heightif (area > best_area) then
22area = width * height23if (area > best_area) then24 best_area = areavalues this step.true.area > best_areabest_area ← 6
23if (area > best_area) then24 best_area = area25 best_width = widthvalues this step6best_areabest_width ← 2
24best_area = area25best_width = width26best_height = heightvalues this step2best_widthbest_height ← 3
25 best_width = width26 best_height = height27end ifvalues this step3best_heighthi ← 2
19width = widths(wi)20do hi = 1, height_count21 height = heights(hi)values this step2hiheight ← 5
20do hi = 1, height_count21 height = heights(hi)22 area = width * heightvalues this step5heightarea ← 10
21height = heights(hi)22area = width * height23if (area > best_area) thenvalues this step10area2width5heightif (area > best_area) then
22area = width * height23if (area > best_area) then24 best_area = areavalues this step.true.area > best_areabest_area ← 10
23if (area > best_area) then24 best_area = area25 best_width = widthvalues this step10best_areabest_width ← 2
24best_area = area25best_width = width26best_height = heightvalues this step2best_widthbest_height ← 5
25 best_width = width26 best_height = height27end ifvalues this step5best_heightwi ← 2
17best_height = 018do wi = 1, width_count19 width = widths(wi)values this step2wiwidth ← 4
18do wi = 1, width_count19 width = widths(wi)20 do hi = 1, height_countvalues this step4widthhi ← 1
19width = widths(wi)20do hi = 1, height_count21 height = heights(hi)values this step1hiheight ← 3
20do hi = 1, height_count21 height = heights(hi)22 area = width * heightvalues this step3heightarea ← 12
21height = heights(hi)22area = width * height23if (area > best_area) thenvalues this step12area4width3heightif (area > best_area) then
22area = width * height23if (area > best_area) then24 best_area = areavalues this step.true.area > best_areabest_area ← 12
23if (area > best_area) then24 best_area = area25 best_width = widthvalues this step12best_areabest_width ← 4
24best_area = area25best_width = width26best_height = heightvalues this step4best_widthbest_height ← 3
25 best_width = width26 best_height = height27end ifvalues this step3best_heighthi ← 2
19width = widths(wi)20do hi = 1, height_count21 height = heights(hi)values this step2hiheight ← 5
20do hi = 1, height_count21 height = heights(hi)22 area = width * heightvalues this step5heightarea ← 20
21height = heights(hi)22area = width * height23if (area > best_area) thenvalues this step20area4width5heightif (area > best_area) then
22area = width * height23if (area > best_area) then24 best_area = areavalues this step.true.area > best_areabest_area ← 20
23if (area > best_area) then24 best_area = area25 best_width = widthvalues this step20best_areabest_width ← 4
24best_area = area25best_width = width26best_height = heightvalues this step4best_widthbest_height ← 5
25 best_width = width26 best_height = height27end ifvalues this step5best_heightprint '(I0, 1X, I0, 1X, I0)', best_width, best_height, best_area
29 end do30 print '(I0, 1X, I0, 1X, I0)', best_width, best_height, best_area31end program nested_sweep_area_demooutput4 5 20values this step4best_width5best_height20best_area
widths ← [2, 4, 6]
11widths = [2, 4, 6]12heights = [3, 5]values this step[2, 4, 6]widthsheights ← [3, 5]
11widths = [2, 4, 6]12heights = [3, 5]13width_count = 1values this step[3, 5]heightswidth_count ← 1
12heights = [3, 5]13width_count = 114height_count = 2values this step1width_countheight_count ← 2
13width_count = 114height_count = 215best_area = -1values this step2height_countbest_area ← -1
14height_count = 215best_area = -116best_width = 0values this step-1best_areabest_width ← 0
15best_area = -116best_width = 017best_height = 0values this step0best_widthbest_height ← 0
16best_width = 017best_height = 018do wi = 1, width_countvalues this step0best_heightwi ← 1
17best_height = 018do wi = 1, width_count19 width = widths(wi)values this step1wiwidth ← 2
18do wi = 1, width_count19 width = widths(wi)20 do hi = 1, height_countvalues this step2widthhi ← 1
19width = widths(wi)20do hi = 1, height_count21 height = heights(hi)values this step1hiheight ← 3
20do hi = 1, height_count21 height = heights(hi)22 area = width * heightvalues this step3heightarea ← 6
21height = heights(hi)22area = width * height23if (area > best_area) thenvalues this step6area2width3heightif (area > best_area) then
22area = width * height23if (area > best_area) then24 best_area = areavalues this step.true.area > best_areabest_area ← 6
23if (area > best_area) then24 best_area = area25 best_width = widthvalues this step6best_areabest_width ← 2
24best_area = area25best_width = width26best_height = heightvalues this step2best_widthbest_height ← 3
25 best_width = width26 best_height = height27end ifvalues this step3best_heighthi ← 2
19width = widths(wi)20do hi = 1, height_count21 height = heights(hi)values this step2hiheight ← 5
20do hi = 1, height_count21 height = heights(hi)22 area = width * heightvalues this step5heightarea ← 10
21height = heights(hi)22area = width * height23if (area > best_area) thenvalues this step10area2width5heightif (area > best_area) then
22area = width * height23if (area > best_area) then24 best_area = areavalues this step.true.area > best_areabest_area ← 10
23if (area > best_area) then24 best_area = area25 best_width = widthvalues this step10best_areabest_width ← 2
24best_area = area25best_width = width26best_height = heightvalues this step2best_widthbest_height ← 5
25 best_width = width26 best_height = height27end ifvalues this step5best_heightprint '(I0, 1X, I0, 1X, I0)', best_width, best_height, best_area
29 end do30 print '(I0, 1X, I0, 1X, I0)', best_width, best_height, best_area31end program nested_sweep_area_demooutput2 5 10values this step2best_width5best_height10best_area
widths ← [2, 4, 6]
11widths = [2, 4, 6]12heights = [3, 5]values this step[2, 4, 6]widthsheights ← [3, 5]
11widths = [2, 4, 6]12heights = [3, 5]13width_count = 3values this step[3, 5]heightswidth_count ← 3
12heights = [3, 5]13width_count = 314height_count = 2values this step3width_countheight_count ← 2
13width_count = 314height_count = 215best_area = -1values this step2height_countbest_area ← -1
14height_count = 215best_area = -116best_width = 0values this step-1best_areabest_width ← 0
15best_area = -116best_width = 017best_height = 0values this step0best_widthbest_height ← 0
16best_width = 017best_height = 018do wi = 1, width_countvalues this step0best_heightwi ← 1
17best_height = 018do wi = 1, width_count19 width = widths(wi)values this step1wiwidth ← 2
18do wi = 1, width_count19 width = widths(wi)20 do hi = 1, height_countvalues this step2widthhi ← 1
19width = widths(wi)20do hi = 1, height_count21 height = heights(hi)values this step1hiheight ← 3
20do hi = 1, height_count21 height = heights(hi)22 area = width * heightvalues this step3heightarea ← 6
21height = heights(hi)22area = width * height23if (area > best_area) thenvalues this step6area2width3heightif (area > best_area) then
22area = width * height23if (area > best_area) then24 best_area = areavalues this step.true.area > best_areabest_area ← 6
23if (area > best_area) then24 best_area = area25 best_width = widthvalues this step6best_areabest_width ← 2
24best_area = area25best_width = width26best_height = heightvalues this step2best_widthbest_height ← 3
25 best_width = width26 best_height = height27end ifvalues this step3best_heighthi ← 2
19width = widths(wi)20do hi = 1, height_count21 height = heights(hi)values this step2hiheight ← 5
20do hi = 1, height_count21 height = heights(hi)22 area = width * heightvalues this step5heightarea ← 10
21height = heights(hi)22area = width * height23if (area > best_area) thenvalues this step10area2width5heightif (area > best_area) then
22area = width * height23if (area > best_area) then24 best_area = areavalues this step.true.area > best_areabest_area ← 10
23if (area > best_area) then24 best_area = area25 best_width = widthvalues this step10best_areabest_width ← 2
24best_area = area25best_width = width26best_height = heightvalues this step2best_widthbest_height ← 5
25 best_width = width26 best_height = height27end ifvalues this step5best_heightwi ← 2
17best_height = 018do wi = 1, width_count19 width = widths(wi)values this step2wiwidth ← 4
18do wi = 1, width_count19 width = widths(wi)20 do hi = 1, height_countvalues this step4widthhi ← 1
19width = widths(wi)20do hi = 1, height_count21 height = heights(hi)values this step1hiheight ← 3
20do hi = 1, height_count21 height = heights(hi)22 area = width * heightvalues this step3heightarea ← 12
21height = heights(hi)22area = width * height23if (area > best_area) thenvalues this step12area4width3heightif (area > best_area) then
22area = width * height23if (area > best_area) then24 best_area = areavalues this step.true.area > best_areabest_area ← 12
23if (area > best_area) then24 best_area = area25 best_width = widthvalues this step12best_areabest_width ← 4
24best_area = area25best_width = width26best_height = heightvalues this step4best_widthbest_height ← 3
25 best_width = width26 best_height = height27end ifvalues this step3best_heighthi ← 2
19width = widths(wi)20do hi = 1, height_count21 height = heights(hi)values this step2hiheight ← 5
20do hi = 1, height_count21 height = heights(hi)22 area = width * heightvalues this step5heightarea ← 20
21height = heights(hi)22area = width * height23if (area > best_area) thenvalues this step20area4width5heightif (area > best_area) then
22area = width * height23if (area > best_area) then24 best_area = areavalues this step.true.area > best_areabest_area ← 20
23if (area > best_area) then24 best_area = area25 best_width = widthvalues this step20best_areabest_width ← 4
24best_area = area25best_width = width26best_height = heightvalues this step4best_widthbest_height ← 5
25 best_width = width26 best_height = height27end ifvalues this step5best_heightwi ← 3
17best_height = 018do wi = 1, width_count19 width = widths(wi)values this step3wiwidth ← 6
18do wi = 1, width_count19 width = widths(wi)20 do hi = 1, height_countvalues this step6widthhi ← 1
19width = widths(wi)20do hi = 1, height_count21 height = heights(hi)values this step1hiheight ← 3
20do hi = 1, height_count21 height = heights(hi)22 area = width * heightvalues this step3heightarea ← 18
21height = heights(hi)22area = width * height23if (area > best_area) thenvalues this step18area6width3heightif (area > best_area) then
22area = width * height23if (area > best_area) then24 best_area = areavalues this step.false.area > best_areahi ← 2
19width = widths(wi)20do hi = 1, height_count21 height = heights(hi)values this step2hiheight ← 5
20do hi = 1, height_count21 height = heights(hi)22 area = width * heightvalues this step5heightarea ← 30
21height = heights(hi)22area = width * height23if (area > best_area) thenvalues this step30area6width5heightif (area > best_area) then
22area = width * height23if (area > best_area) then24 best_area = areavalues this step.true.area > best_areabest_area ← 30
23if (area > best_area) then24 best_area = area25 best_width = widthvalues this step30best_areabest_width ← 6
24best_area = area25best_width = width26best_height = heightvalues this step6best_widthbest_height ← 5
25 best_width = width26 best_height = height27end ifvalues this step5best_heightprint '(I0, 1X, I0, 1X, I0)', best_width, best_height, best_area
29 end do30 print '(I0, 1X, I0, 1X, I0)', best_width, best_height, best_area31end program nested_sweep_area_demooutput6 5 30values this step6best_width5best_height30best_area
nested loop
The outer loop picks one width and the inner loop tries every height.
combination
Each `(width, height)` pair is one sweep case.
best combination
The current best parameter pair is updated when its area is larger.