Skip to content
This repository has been archived by the owner on Jun 23, 2023. It is now read-only.

Memory saving for Q #186

Merged
merged 3 commits into from
Mar 5, 2021
Merged

Conversation

Inazuma110
Copy link
Contributor

@Inazuma110 Inazuma110 commented Feb 25, 2021

Memory saving for Q

When computing modularity using the function Q, many elements of kikj, P, and diff are useless. (Because at the end of the computation, the function takes a product with $\delta(N, L)$, a sparse matrix.)
So change the code so that the function Q works without wasted elements.

I tried to benchmark before and after the change.
Before

julia> N = convert(BipartiteNetwork, web_of_life("M_PL_015"))
666×131 (String) bipartite ecological network (L: 2933 - Bool)

julia> tmp = n_random_modules(100)(N)
(666×131 (String) bipartite ecological network (L: 2933 - Bool), Dict("Bombylius undatus
 " => 64,"Sarcopoterium spinosum" => 29,"Lasioglossum crassepunctatum" => 6,"Euodynerus
velutinus" => 16,"Leptochilus alpestris" => 71,"Nomada mavromoustakisi" => 55,"Psallus s
p1 M_PL_015" => 52,"Osmia subcornuta" => 29,"Amphicoma distincta" => 29,"Psilothrix cyan
eus " => 23))

julia> @benchmark Q(tmp...)
BenchmarkTools.Trial:
  memory estimate:  9.86 MiB
  allocs estimate:  903
  --------------
  minimum time:     5.520 ms (0.00% GC)
  median time:      6.262 ms (0.00% GC)
  mean time:        6.672 ms (5.08% GC)
  maximum time:     11.120 ms (15.41% GC)
  --------------
  samples:          749
  evals/sample:     1

After

julia> @benchmark Q(tmp...)
BenchmarkTools.Trial:
  memory estimate:  180.19 KiB
  allocs estimate:  3636
  --------------
  minimum time:     4.190 ms (0.00% GC)
  median time:      5.002 ms (0.00% GC)
  mean time:        5.251 ms (0.16% GC)
  maximum time:     9.538 ms (32.33% GC)
  --------------
  samples:          952
  evals/sample:     1

Presumably, when the number of vertices is N, the space computation has been improved from O(N^2) to O(N).
(Perhaps the amount of time computation can be improved by a constant factor.)

Related issues

Checklist

  • The code is covered by unit tests (I did not make any changes to the tests, because I did not change the function behavior.)
    • Additional cases or module in test/...
    • Relevant lines in test/runtests.jl
  • The code is documented (Likewise, there are no changes to the documentation.)
  • Docstrings written for every function
  • If needed, additional lines in docs/src/...
  • All contributors have added their name and affiliation to .zenodo.json

Pinging

Pinging @tpoisot

@tpoisot
Copy link
Member

tpoisot commented Mar 5, 2021

Really nice - I'm going to tag a new release with this, it's especially efficient now that the networks are sparse matrices!

@tpoisot tpoisot merged commit dada0d2 into PoisotLab:main Mar 5, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants