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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Meta ticket: Simplify and speed up graph backends #28895

Open
kliem opened this issue Dec 18, 2019 · 25 comments
Open

Meta ticket: Simplify and speed up graph backends #28895

kliem opened this issue Dec 18, 2019 · 25 comments

Comments

@kliem
Copy link
Contributor

kliem commented Dec 18, 2019

This ticket collects tickets simplifying and improving the graph backends.

It is motivated by the following post:

https://groups.google.com/d/msg/sage-devel/JhirgrbcFMc/CVfHhPCdAgAJ

Possibly related tickets:

Here are some timings we try to improve along the way:

sage: import itertools
sage: def test_speed(n):
....:     for struct in ('dense', 'sparse'):
....:         print("\nnow doing {}".format(struct))
....:         G = Graph(n, data_structure=struct)
....:         edges = itertools.combinations(range(n), 2)
....:         print("adding edges")
....:         %time G.add_edges(edges)
....:         print("copy to dense")
....:         %time _ = copy(G)
....:         print("copy to sparse")
....:         %time _ = G.copy(sparse=True)
....:         print("copy to statice sparse")
....:         %time H = G.copy(immutable=True)
....:         edges = itertools.combinations(range(n), 2)
....:         print("delete edges")
....:         %time G.delete_edges(edges)
....:     
....:         print("copying static sparse to {}".format(struct))
....:         %time _ = H.copy(data_structure=struct)
....:   
sage: test_speed(1000)

now doing dense
adding edges
CPU times: user 169 ms, sys: 4 µs, total: 169 ms
Wall time: 169 ms
copy to dense
CPU times: user 2.14 s, sys: 0 ns, total: 2.14 s
Wall time: 2.14 s
copy to sparse
CPU times: user 2.44 s, sys: 0 ns, total: 2.44 s
Wall time: 2.44 s
copy to statice sparse
CPU times: user 2.75 s, sys: 3.61 ms, total: 2.75 s
Wall time: 2.75 s
delete edges
CPU times: user 613 ms, sys: 0 ns, total: 613 ms
Wall time: 613 ms
copying static sparse to dense
CPU times: user 282 ms, sys: 0 ns, total: 282 ms
Wall time: 282 ms

now doing sparse
adding edges
CPU times: user 434 ms, sys: 0 ns, total: 434 ms
Wall time: 435 ms
copy to dense
CPU times: user 619 ms, sys: 0 ns, total: 619 ms
Wall time: 619 ms
copy to sparse
CPU times: user 747 ms, sys: 3.84 ms, total: 750 ms
Wall time: 750 ms
copy to statice sparse
CPU times: user 997 ms, sys: 0 ns, total: 997 ms
Wall time: 997 ms
delete edges
CPU times: user 896 ms, sys: 0 ns, total: 896 ms
Wall time: 896 ms
copying static sparse to sparse
CPU times: user 582 ms, sys: 0 ns, total: 582 ms
Wall time: 582 ms
sage: test_speed(2000)

now doing dense
adding edges
CPU times: user 672 ms, sys: 0 ns, total: 672 ms
Wall time: 671 ms
copy to dense
CPU times: user 16.1 s, sys: 0 ns, total: 16.1 s
Wall time: 16.1 s
copy to sparse
CPU times: user 17.4 s, sys: 34.7 ms, total: 17.5 s
Wall time: 17.5 s
copy to statice sparse
CPU times: user 18.9 s, sys: 51.6 ms, total: 18.9 s
Wall time: 18.9 s
delete edges
CPU times: user 2.49 s, sys: 0 ns, total: 2.49 s
Wall time: 2.49 s
copying static sparse to dense
CPU times: user 1.18 s, sys: 0 ns, total: 1.18 s
Wall time: 1.18 s

now doing sparse
adding edges
CPU times: user 1.87 s, sys: 0 ns, total: 1.87 s
Wall time: 1.87 s
copy to dense
CPU times: user 2.64 s, sys: 0 ns, total: 2.64 s
Wall time: 2.64 s
copy to sparse
CPU times: user 3.21 s, sys: 47.8 ms, total: 3.26 s
Wall time: 3.26 s
copy to statice sparse
CPU times: user 4.25 s, sys: 36 ms, total: 4.28 s
Wall time: 4.28 s
delete edges
CPU times: user 3.86 s, sys: 0 ns, total: 3.86 s
Wall time: 3.86 s
copying static sparse to sparse
CPU times: user 2.55 s, sys: 0 ns, total: 2.55 s
Wall time: 2.55 s

CC: @dcoudert @kliem

Component: graph theory

Keywords: backends

Issue created by migration from https://trac.sagemath.org/ticket/28895

@kliem kliem added this to the sage-9.0 milestone Dec 18, 2019
@kliem

This comment has been minimized.

@kliem
Copy link
Contributor Author

kliem commented Dec 18, 2019

comment:2

The idea is that in the end, most top level function should be handled in c_graph.pyx.
If possible, only the low level functions are done separate for each backend.

Once things are unified (as good as different backends allow), one should speed up things. E.g. the following is extremely slow:

sage: import itertools
sage: def test_speed(n):
....:     G = Graph(n, data_structure='dense')
....:     edges = itertools.combinations(range(n), 2)
....:     %time G.add_edges(edges)
....:     edges = itertools.combinations(range(n), 2)
....:     %time G.delete_edges(edges)
sage: test_speed(1000)
CPU times: user 175 ms, sys: 8 µs, total: 175 ms
Wall time: 175 ms
CPU times: user 574 ms, sys: 0 ns, total: 574 ms
Wall time: 574 ms

I got both things down to about 80 ms by calling add_edges on the top level and then actually using the unsafe methods wherever possible (if we already obtained the indices safely, there should be nothing preventing as from using unsafe methods). Similar for delete_edges.

@kliem
Copy link
Contributor Author

kliem commented Dec 18, 2019

comment:3

Adding this, because I missed it on my first try to optimize add_edges. This would have created a mistake in BipartiteGraph without a doctest catching on.

@kliem

This comment has been minimized.

@kliem

This comment has been minimized.

@kliem

This comment has been minimized.

@dcoudert

This comment has been minimized.

@dcoudert
Copy link
Contributor

comment:6

just mentioned that #25465, #28865 are for the same issue.

@kliem

This comment has been minimized.

@embray
Copy link
Contributor

embray commented Jan 6, 2020

comment:8

Ticket retargeted after milestone closed

@embray embray modified the milestones: sage-9.0, sage-9.1 Jan 6, 2020
@mkoeppe
Copy link
Contributor

mkoeppe commented Apr 14, 2020

comment:9

Batch modifying tickets that will likely not be ready for 9.1, based on a review of the ticket title, branch/review status, and last modification date.

@mkoeppe mkoeppe modified the milestones: sage-9.1, sage-9.2 Apr 14, 2020
@dcoudert
Copy link
Contributor

comment:10

#22349 could also be part of this task.

@kliem

This comment has been minimized.

@kliem

This comment has been minimized.

@mkoeppe mkoeppe modified the milestones: sage-9.2, sage-9.3 Aug 29, 2020
@kliem

This comment has been minimized.

@kliem

This comment has been minimized.

@kliem

This comment has been minimized.

@dcoudert

This comment has been minimized.

@kliem

This comment has been minimized.

@kliem
Copy link
Contributor Author

kliem commented Oct 16, 2020

comment:19

With #30776 and #30777 things are looking much better:

sage: test_speed(1000)                                                                                                                                                                                                                                                                                                                                                     

now doing dense
adding edges
CPU times: user 81.9 ms, sys: 0 ns, total: 81.9 ms
Wall time: 81.9 ms
copy to dense
CPU times: user 10.2 ms, sys: 0 ns, total: 10.2 ms
Wall time: 10.2 ms
copy to sparse
CPU times: user 89.5 ms, sys: 7.87 ms, total: 97.4 ms
Wall time: 97.1 ms
copy to statice sparse
CPU times: user 423 ms, sys: 11.9 ms, total: 435 ms
Wall time: 435 ms
delete edges
CPU times: user 79.1 ms, sys: 0 ns, total: 79.1 ms
Wall time: 79.1 ms
copying static sparse to dense
CPU times: user 81.3 ms, sys: 51 µs, total: 81.4 ms
Wall time: 81.4 ms

now doing sparse
adding edges
CPU times: user 169 ms, sys: 0 ns, total: 169 ms
Wall time: 169 ms
copy to dense
CPU times: user 158 ms, sys: 0 ns, total: 158 ms
Wall time: 158 ms
copy to sparse
CPU times: user 290 ms, sys: 0 ns, total: 290 ms
Wall time: 290 ms
copy to statice sparse
CPU times: user 544 ms, sys: 0 ns, total: 544 ms
Wall time: 544 ms
delete edges
CPU times: user 166 ms, sys: 0 ns, total: 166 ms
Wall time: 166 ms
copying static sparse to sparse
CPU times: user 187 ms, sys: 0 ns, total: 187 ms
Wall time: 187 ms
sage: test_speed(2000)                                                                                                                                                                                                                                                                                                                                                     

now doing dense
adding edges
CPU times: user 340 ms, sys: 0 ns, total: 340 ms
Wall time: 340 ms
copy to dense
CPU times: user 40.9 ms, sys: 0 ns, total: 40.9 ms
Wall time: 40.8 ms
copy to sparse
CPU times: user 396 ms, sys: 3.93 ms, total: 400 ms
Wall time: 400 ms
copy to statice sparse
CPU times: user 2 s, sys: 68 ms, total: 2.07 s
Wall time: 2.07 s
delete edges
CPU times: user 321 ms, sys: 32 µs, total: 321 ms
Wall time: 321 ms
copying static sparse to dense
CPU times: user 396 ms, sys: 0 ns, total: 396 ms
Wall time: 396 ms

now doing sparse
adding edges
CPU times: user 768 ms, sys: 0 ns, total: 768 ms
Wall time: 768 ms
copy to dense
CPU times: user 791 ms, sys: 0 ns, total: 791 ms
Wall time: 791 ms
copy to sparse
CPU times: user 1.32 s, sys: 48 ms, total: 1.36 s
Wall time: 1.36 s
copy to statice sparse
CPU times: user 2.48 s, sys: 0 ns, total: 2.48 s
Wall time: 2.48 s
delete edges
CPU times: user 825 ms, sys: 0 ns, total: 825 ms
Wall time: 825 ms
copying static sparse to sparse
CPU times: user 876 ms, sys: 0 ns, total: 876 ms
Wall time: 876 ms

@dcoudert

This comment has been minimized.

@dcoudert

This comment has been minimized.

@kliem

This comment has been minimized.

@mkoeppe
Copy link
Contributor

mkoeppe commented Mar 24, 2021

comment:23

Sage development has entered the release candidate phase for 9.3. Setting a new milestone for this ticket based on a cursory review of ticket status, priority, and last modification date.

@mkoeppe mkoeppe modified the milestones: sage-9.3, sage-9.4 Mar 24, 2021
@mkoeppe mkoeppe modified the milestones: sage-9.4, sage-9.5 Aug 9, 2021
@dcoudert

This comment has been minimized.

@mkoeppe mkoeppe modified the milestones: sage-9.5, sage-9.6 Dec 18, 2021
@mkoeppe mkoeppe modified the milestones: sage-9.6, sage-9.7 Apr 1, 2022
@mkoeppe mkoeppe modified the milestones: sage-9.7, sage-9.8 Aug 31, 2022
@mkoeppe mkoeppe removed this from the sage-9.8 milestone Jan 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants