Skip to content
New issue

Have a question about this project?Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of serviceand privacy statement.We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DirectCR-RSSA #346

Open
wants to merge 43 commits into
base: master
Choose a base branch
from
Open

DirectCR-RSSA #346

wants to merge 43 commits into from

Conversation

Vilin97
Copy link
Contributor

@Vilin97 Vilin97 commented Sep 11, 2023

A new spatial SSA calledDirectCRRSSAuses RSSA-style sampling to sample the site+jump. It is about 20% faster thanDirectCRDirect,at least on the problem in thetest/spatial/ABC.jl(5 sites in a line, 3 species, 2 reactions).

using BenchmarkTools
@btime solve(jump_problems[1], SSAStepper()) # NSM with a `CartesianGrid`, 52ms
@btime solve(jump_problems[2], SSAStepper()) # NSM with a `Graph`, 49ms
@btime solve(jump_problems[3], SSAStepper()) # DirectCRDirect with a `CartesianGrid`, 51ms
@btime solve(jump_problems[4], SSAStepper()) # DirectCRRSSA with a `CartesianGrid`, 42ms

@github-actions
Copy link
Contributor

Pull Request Test Coverage Report forBuild 6141985002

  • 172of181(95.03%)changed or added relevant lines in8files are covered.
  • 12unchanged lines in5files lost coverage.
  • Overall coverage decreased (-0.1%) to89.759%

Changes Missing Coverage Covered Lines Changed/Added Lines %
src/spatial/bracketing.jl 35 38 92.11%
src/spatial/rssacrdirect.jl 105 111 94.59%
Files with Coverage Reduction New Missed Lines %
src/aggregators/prioritytable.jl 1 86.86%
src/extended_jump_array.jl 1 80.52%
src/coupling.jl 2 78.18%
src/spatial/spatial_massaction_jump.jl 3 85.45%
src/spatial/directcrdirect.jl 5 87.65%
Totals Coverage Status
Change from baseBuild 6133295964: -0.1%
Covered Lines: 2419
Relevant Lines: 2695

💛 -Coveralls

src/spatial/hop_rates.jl Outdated Show resolved Hide resolved
@Vilin97 Vilin97 marked this pull request as ready for review September 12, 2023 01:29
@Vilin97 Vilin97 changed the title [WIP] CR-RSSA DirectCR-RSSA Sep 12, 2023
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

There was a very hard-to-find bug in the ordering of arguments in generate_jumps!. I fixed it here and in NSM.jl.

@coveralls
Copy link

coveralls commented Sep 12, 2023

Pull Request Test Coverage Report forBuild 6180696485

  • 155of161(96.27%)changed or added relevant lines in8files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.3%) to90.175%

Changes Missing Coverage Covered Lines Changed/Added Lines %
src/spatial/bracketing.jl 23 24 95.83%
src/spatial/directcrrssa.jl 106 111 95.5%
Totals Coverage Status
Change from baseBuild 6167199515: 0.3%
Covered Lines: 2423
Relevant Lines: 2687

💛 -Coveralls

Copy link
Member

@isaacsas isaacsas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

This needs rebasing too it appears.

Comment on lines +8 to +16
function LowHigh(low::T, high::T; do_copy = true) where {T}
if do_copy
return new{T}(deepcopy(low), deepcopy(high))
else
return new{T}(low, high)
end
end
LowHigh(pair::Tuple{T,T}; kwargs...) where {T} = LowHigh(pair[1], pair[2]; kwargs...)
LowHigh(low_and_high::T; kwargs...) where {T} = LowHigh(low_and_high, low_and_high; kwargs...)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Does switching to using a branch have performance implications? We create these structures a lot right in the scalar case right? I'm not sure if Julia will remove it during compilation.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Even if it's not optimized away, it shouldn't be expensive, right? I am not good at reading low level code but it seems that it does get optimized away in the end.

#Old struct without branching
structLowHighOld{T}
low::T
high::T

functionLowHighOld(low::T,high::T)where{T}
new{T}(deepcopy(low),deepcopy(high))
end
end

#New struct with branching
structLowHighNew{T}
low::T
high::T

functionLowHighNew(low::T,high::T;do_copy=true)where{T}
ifdo_copy
returnnew{T}(deepcopy(low),deepcopy(high))
else
returnnew{T}(low, high)
end
end
end

#Test values
low_value=10
high_value=20

#Code introspection to see if the branch is removed in scalar cases
@code_llvmdebuginfo=:noneLowHighOld(low_value, high_value)

@code_llvmdebuginfo=:noneLowHighNew(low_value, high_value; do_copy=true)

The output of the code introspection is as follows:

definevoid@julia_LowHighOld_2364([2xi64]*noaliasnocapturenoundefnonnullsret([2xi64])
align8dereferenceable(16)%0,{}* noundefnonnullreadonly%1,i64signext%2,i64signext%3) #0{
top:
%newstruct.sroa.0.0..sroa_idx=getelementptrinbounds[2xi64], [2xi64]*%0,i640,i640
storei64%2,i64*%newstruct.sroa.0.0..sroa_idx,align8
%newstruct.sroa.2.0..sroa_idx1=getelementptrinbounds[2xi64], [2xi64]*%0,i640,i641storei64%3,i64*%newstruct.sroa.2.0..sroa_idx1,align8
retvoid
}

definevoid@julia_LowHighNew_2366([2xi64]*noaliasnocapturenoundefnonnullsret([2xi64])
align8dereferenceable(16)%0,[1xi8]*nocapturenoundefnonnullreadonlyalign1dereferenceable(1)%1,{}* noundefnonnullreadonly%2,i64signext%3,i64signext%4) #0{
top:
%.sroa.025.0..sroa_idx=getelementptrinbounds[2xi64], [2xi64]*%0,i640,i640
storei64%3,i64*%.sroa.025.0..sroa_idx,align8
%.sroa.2.0..sroa_idx26=getelementptrinbounds[2xi64], [2xi64]*%0,i640,i641
storei64%4,i64*%.sroa.2.0..sroa_idx26,align8
retvoid
}

src/spatial/hop_rates.jl Outdated Show resolved Hide resolved
src/spatial/hop_rates.jl Outdated Show resolved Hide resolved
src/spatial/hop_rates.jl Outdated Show resolved Hide resolved

# SSAs
for alg in [DirectCRDirect(), DirectCRRSSA()]
push!(jump_problems, JumpProblem(prob, alg, majumps, hopping_constants = hopping_constants, spatial_system = grids[1], save_positions = (false, false), rng = rng))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Suggested change
push!(jump_problems,JumpProblem(prob, alg, majumps,hopping_constants=hopping_constants,spatial_system=grids[1], save_positions=(false,false),rng=rng))
push!(jump_problems,JumpProblem(prob, alg, majumps;hopping_constants, spatial_system=grids[1], save_positions=(false,false), rng))

src/spatial/directcrrssa.jl Outdated Show resolved Hide resolved
Comment on lines +139 to +145
jump = sample_jump_direct(rx_rates.high, hop_rates.high, site, spatial_system, rng)
time_delta += randexp(rng)
if accept_jump(p, u, jump)
p.next_jump_time = t + time_delta / groupsum(rt)
p.next_jump = jump
break
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Does this handle if there is no next jump?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

To be honest, I do not rembmer any of the logic by now. Do we have a test for what you are asking? If not, we should, for all SSAs.

if hop_rate(hop_rates.low, species, site) > acceptance_threshold
return true
else
# compute the real rate. Could have used hop_rates.high as well.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Does this mean we are storing two copies of the hopping constants for the low vs high (or is it aliased)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Aliased becausedo_copy = falsewhen initializingLowHigh(hopping_rates).Same withrx_rates.

if rx_rate(rx_rates.low, rx, site) > acceptance_threshold
return true
else
# compute the real rate. Could have used rx_rates.high as well.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Similar question to above

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Same answer -- aliased.

src/spatial/directcrrssa.jl Show resolved Hide resolved
Copy link
Contributor Author

@Vilin97 Vilin97 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Addressed comments.

@Vilin97
Copy link
Contributor Author

Vilin97 commented Aug 21, 2024

What do I need to do to get rid of the format and the spell check CI failures?@isaacsas

@isaacsas
Copy link
Member

isaacsas commented Aug 22, 2024

For spell check fix the listed spelling errors I guess. Haven’t ever used it or seen it before.

For formatting use JuliaFormatter to format the repo and add to the PR.

@isaacsas
Copy link
Member

Just FYI, I'm traveling for the next week, but will try to review once I am back in Boston.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants