Skip to content

Commit

Permalink
Avoid oversized arrays in scaling tests
Browse files Browse the repository at this point in the history
Arrays for testing the scaling functions with random matrices were only
allocated once with the maximum dimensions of all possibly generated
matrices. That makes it simple, but unfortunately also has the potential
to hide possible bugs, as seen in #211 and #213. To improve the chance
to detect such bugs, this reallocates the arrays to the exact dimensions
for each current test case.
  • Loading branch information
mjacobse authored and jfowkes committed Jun 19, 2024
1 parent 1e06b9b commit f0a82e6
Showing 1 changed file with 54 additions and 18 deletions.
72 changes: 54 additions & 18 deletions tests/scaling.f90
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,10 @@ subroutine test_auction_sym_random
write(*, "(a)") "Testing auction_scaling_sym() with random matrices"
write(*, "(a)") "=================================================="

allocate(a%ptr(maxn+1))
allocate(a%row(2*maxnz), a%val(2*maxnz))
allocate(scaling(maxn), match(maxn), rmax(maxn), cnt(maxn))
! dummy allocations to simplify reallocating specific test case size in loop
allocate(a%ptr(0))
allocate(a%row(0), a%val(0))
allocate(scaling(0), match(0), rmax(0), cnt(0))

prblm_loop: &
do prblm = 1, nprob
Expand All @@ -88,6 +89,11 @@ subroutine test_auction_sym_random
cycle
endif

deallocate(a%ptr, a%row, a%val, scaling, match, rmax, cnt)
allocate(a%ptr(a%n+1))
allocate(a%row(nza), a%val(nza))
allocate(scaling(a%n), match(a%n), rmax(a%n), cnt(a%n))

call gen_random_sym(a, nza, state)
!print *, "n = ", a%n
!do i = 1, a%n
Expand Down Expand Up @@ -212,9 +218,10 @@ subroutine test_auction_unsym_random
write(*, "(a)") "Testing auction_scaling_unsym() with random matrices"
write(*, "(a)") "===================================================="

allocate(a%ptr(maxn+1))
allocate(a%row(2*maxnz), a%val(2*maxnz))
allocate(rscaling(maxn), cscaling(maxn), match(maxn), rmax(maxn), cnt(maxn))
! dummy allocations to simplify reallocating specific test case size in loop
allocate(a%ptr(0))
allocate(a%row(0), a%val(0))
allocate(rscaling(0), cscaling(0), match(0), rmax(0), cnt(0))

prblm_loop: &
do prblm = 1, nprob
Expand All @@ -241,6 +248,11 @@ subroutine test_auction_unsym_random
! cycle
!endif

deallocate(a%ptr, a%row, a%val, rscaling, cscaling, match, rmax, cnt)
allocate(a%ptr(a%n+1))
allocate(a%row(nza), a%val(nza))
allocate(rscaling(a%m), cscaling(a%n), match(a%m), rmax(a%m), cnt(a%n))

call gen_random_unsym(a, nza, state)
!print *, "n = ", a%n
!do i = 1, a%n
Expand Down Expand Up @@ -388,9 +400,10 @@ subroutine test_equilib_sym_random
write(*, "(a)") "Testing equilib_scaling_sym() with random matrices"
write(*, "(a)") "=================================================="

allocate(a%ptr(maxn+1))
allocate(a%row(2*maxnz), a%val(2*maxnz))
allocate(scaling(maxn), rinf(maxn))
! dummy allocations to simplify reallocating specific test case size in loop
allocate(a%ptr(0))
allocate(a%row(0), a%val(0))
allocate(scaling(0), rinf(0))

prblm_loop: &
do prblm = 1, nprob
Expand All @@ -412,6 +425,11 @@ subroutine test_equilib_sym_random
cycle
endif

deallocate(a%ptr, a%row, a%val, scaling, rinf)
allocate(a%ptr(a%n+1))
allocate(a%row(nza), a%val(nza))
allocate(scaling(a%n), rinf(a%n))

call gen_random_sym(a, nza, state)
!print *, "n = ", a%n
!do i = 1, a%n
Expand Down Expand Up @@ -480,9 +498,10 @@ subroutine test_equilib_unsym_random
write(*, "(a)") "Testing equilib_scaling_unsym() with random matrices"
write(*, "(a)") "===================================================="

allocate(a%ptr(maxn+1))
allocate(a%row(2*maxnz), a%val(2*maxnz))
allocate(rscaling(maxn), cscaling(maxn), rinf(maxn))
! dummy allocations to simplify reallocating specific test case size in loop
allocate(a%ptr(0))
allocate(a%row(0), a%val(0))
allocate(rscaling(0), cscaling(0), rinf(0))

prblm_loop: &
do prblm = 1, nprob
Expand Down Expand Up @@ -510,6 +529,11 @@ subroutine test_equilib_unsym_random
cycle
endif

deallocate(a%ptr, a%row, a%val, rscaling, cscaling, rinf)
allocate(a%ptr(a%n+1))
allocate(a%row(nza), a%val(nza))
allocate(rscaling(a%m), cscaling(a%n), rinf(a%m))

call gen_random_unsym(a, nza, state)
!print *, "n = ", a%n
!do i = 1, a%n
Expand Down Expand Up @@ -594,9 +618,10 @@ subroutine test_hungarian_sym_random
write(*, "(a)") "Testing hungarian_scaling_sym() with random matrices"
write(*, "(a)") "==================================================="

allocate(a%ptr(maxn+1))
allocate(a%row(2*maxnz), a%val(2*maxnz))
allocate(scaling(maxn), match(maxn), rmax(maxn), cnt(maxn))
! dummy allocations to simplify reallocating specific test case size in loop
allocate(a%ptr(0))
allocate(a%row(0), a%val(0))
allocate(scaling(0), match(0), rmax(0), cnt(0))

prblm_loop: &
do prblm = 1, nprob
Expand All @@ -618,6 +643,11 @@ subroutine test_hungarian_sym_random
cycle
endif

deallocate(a%ptr, a%row, a%val, scaling, match, rmax, cnt)
allocate(a%ptr(a%n+1))
allocate(a%row(nza), a%val(nza))
allocate(scaling(a%n), match(a%n), rmax(a%n), cnt(a%n))

call gen_random_sym(a, nza, state)
!print *, "n = ", a%n
!do i = 1, a%n
Expand Down Expand Up @@ -726,9 +756,10 @@ subroutine test_hungarian_unsym_random
write(*, "(a)") "Testing hungarian_scaling_unsym() with random matrices"
write(*, "(a)") "======================================================"

allocate(a%ptr(maxn+1))
allocate(a%row(2*maxnz), a%val(2*maxnz))
allocate(rscaling(maxn), cscaling(maxn), match(maxn), rmax(maxn), cnt(maxn))
! dummy allocations to simplify reallocating specific test case size in loop
allocate(a%ptr(0))
allocate(a%row(0), a%val(0))
allocate(rscaling(0), cscaling(0), match(0), rmax(0), cnt(0))

prblm_loop: &
do prblm = 1, nprob
Expand All @@ -748,6 +779,11 @@ subroutine test_hungarian_unsym_random
write(*, "(a, i3, a, i5, a, i5, a, i7, a, i2, a)",advance="no") &
" - no. ", prblm, " m = ", a%m, " n = ", a%n, " nza = ", nza, "..."

deallocate(a%ptr, a%row, a%val, rscaling, cscaling, match, rmax, cnt)
allocate(a%ptr(a%n+1))
allocate(a%row(nza), a%val(nza))
allocate(rscaling(a%m), cscaling(a%n), match(a%m), rmax(a%m), cnt(a%n))

if(nza.gt.maxnz .or. a%n.gt.maxn .or. a%m.gt.maxn) then
write(*, "(a)") "bad random matrix."
write(*, "(a,i5,a,i5,a,i5)") "m = ", a%m, "n = ", a%n, &
Expand Down

0 comments on commit f0a82e6

Please sign in to comment.