Sparse Data Patterns
Nonzero Sum
Compact Stored Values
Sparse data often stores only nonzero entries and summarizes those compact values.
Program
Play the program to include a different number of stored nonzero values.
nonzero_sum.f90
Replay: real traced execution (multi-file project)
program nonzero_sum_demo
implicit none
integer :: indices(4)
integer :: values(4)
integer :: active_count
integer :: i
integer :: total
indices = [2, 5, 9, 12]
values = [4, 7, 3, 6]
active_count = 3
total = 0
do i = 1, active_count
total = total + values(i)
end do
print '(I0, 1X, I0)', active_count, total
end program nonzero_sum_demo
program nonzero_sum_demo
implicit none
integer :: indices(4)
integer :: values(4)
integer :: active_count
integer :: i
integer :: total
indices = [2, 5, 9, 12]
values = [4, 7, 3, 6]
active_count = 2
total = 0
do i = 1, active_count
total = total + values(i)
end do
print '(I0, 1X, I0)', active_count, total
end program nonzero_sum_demo
program nonzero_sum_demo
implicit none
integer :: indices(4)
integer :: values(4)
integer :: active_count
integer :: i
integer :: total
indices = [2, 5, 9, 12]
values = [4, 7, 3, 6]
active_count = 4
total = 0
do i = 1, active_count
total = total + values(i)
end do
print '(I0, 1X, I0)', active_count, total
end program nonzero_sum_demo
indices ← [2, 5, 9, 12]
9indices = [2, 5, 9, 12]10values = [4, 7, 3, 6]values this step[2, 5, 9, 12]indicesvalues ← [4, 7, 3, 6]
9indices = [2, 5, 9, 12]10values = [4, 7, 3, 6]11active_count = 3values this step[4, 7, 3, 6]valuesactive_count ← 3
10values = [4, 7, 3, 6]11active_count = 312total = 0values this step3active_counttotal ← 0
11active_count = 312total = 013do i = 1, active_countvalues this step0totali ← 1
12total = 013do i = 1, active_count14 total = total + values(i)values this step1itotal ← 4
13do i = 1, active_count14 total = total + values(i)15end dovalues this step0 → 4total4values(1)i ← 2
12total = 013do i = 1, active_count14 total = total + values(i)values this step2itotal ← 11
13do i = 1, active_count14 total = total + values(i)15end dovalues this step4 → 11total7values(2)i ← 3
12total = 013do i = 1, active_count14 total = total + values(i)values this step3itotal ← 14
13do i = 1, active_count14 total = total + values(i)15end dovalues this step11 → 14total3values(3)print '(I0, 1X, I0)', active_count, total
15 end do16 print '(I0, 1X, I0)', active_count, total17end program nonzero_sum_demooutput3 14values this step3active_count14total
indices ← [2, 5, 9, 12]
9indices = [2, 5, 9, 12]10values = [4, 7, 3, 6]values this step[2, 5, 9, 12]indicesvalues ← [4, 7, 3, 6]
9indices = [2, 5, 9, 12]10values = [4, 7, 3, 6]11active_count = 2values this step[4, 7, 3, 6]valuesactive_count ← 2
10values = [4, 7, 3, 6]11active_count = 212total = 0values this step2active_counttotal ← 0
11active_count = 212total = 013do i = 1, active_countvalues this step0totali ← 1
12total = 013do i = 1, active_count14 total = total + values(i)values this step1itotal ← 4
13do i = 1, active_count14 total = total + values(i)15end dovalues this step0 → 4total4values(1)i ← 2
12total = 013do i = 1, active_count14 total = total + values(i)values this step2itotal ← 11
13do i = 1, active_count14 total = total + values(i)15end dovalues this step4 → 11total7values(2)print '(I0, 1X, I0)', active_count, total
15 end do16 print '(I0, 1X, I0)', active_count, total17end program nonzero_sum_demooutput2 11values this step2active_count11total
indices ← [2, 5, 9, 12]
9indices = [2, 5, 9, 12]10values = [4, 7, 3, 6]values this step[2, 5, 9, 12]indicesvalues ← [4, 7, 3, 6]
9indices = [2, 5, 9, 12]10values = [4, 7, 3, 6]11active_count = 4values this step[4, 7, 3, 6]valuesactive_count ← 4
10values = [4, 7, 3, 6]11active_count = 412total = 0values this step4active_counttotal ← 0
11active_count = 412total = 013do i = 1, active_countvalues this step0totali ← 1
12total = 013do i = 1, active_count14 total = total + values(i)values this step1itotal ← 4
13do i = 1, active_count14 total = total + values(i)15end dovalues this step0 → 4total4values(1)i ← 2
12total = 013do i = 1, active_count14 total = total + values(i)values this step2itotal ← 11
13do i = 1, active_count14 total = total + values(i)15end dovalues this step4 → 11total7values(2)i ← 3
12total = 013do i = 1, active_count14 total = total + values(i)values this step3itotal ← 14
13do i = 1, active_count14 total = total + values(i)15end dovalues this step11 → 14total3values(3)i ← 4
12total = 013do i = 1, active_count14 total = total + values(i)values this step4itotal ← 20
13do i = 1, active_count14 total = total + values(i)15end dovalues this step14 → 20total6values(4)print '(I0, 1X, I0)', active_count, total
15 end do16 print '(I0, 1X, I0)', active_count, total17end program nonzero_sum_demooutput4 20values this step4active_count20total
sparse storage
`indices` records where the stored values belong in the full shape.
active entries
`active_count` chooses how many compact entries are present.
compact sum
The summary loops over stored values, not over every possible position.