Skip to content

Commit c3b3205

Browse files
authored
Merge pull request #53 from JuliaGraphs/dev
Add benchmark and remove warning for integer labels
2 parents e599c71 + 5e7fa03 commit c3b3205

File tree

8 files changed

+148
-7
lines changed

8 files changed

+148
-7
lines changed

Project.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,14 @@ julia = "1.6"
1515

1616
[extras]
1717
Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595"
18+
BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf"
1819
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
20+
Graphs = "86223c79-3864-5bf0-83f7-82e725a168b6"
21+
InteractiveUtils = "b77e0a4c-d291-57a0-90e8-8db25a27a240"
1922
JuliaFormatter = "98e50ef6-434e-11e9-1051-2b60c6c9e899"
2023
MetaGraphs = "626554b9-1ddb-594c-aa3c-2596fe9399a5"
24+
SimpleTraits = "699a6c99-e7fa-54fc-8d76-47d257e15c1d"
2125
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
2226

2327
[targets]
24-
test = ["Aqua", "Documenter", "JuliaFormatter", "MetaGraphs", "Test"]
28+
test = ["Aqua", "BenchmarkTools", "Documenter", "Graphs", "InteractiveUtils", "JuliaFormatter", "MetaGraphs", "SimpleTraits", "Test"]

docs/Manifest.toml

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
julia_version = "1.8.5"
44
manifest_format = "2.0"
5-
project_hash = "580b7ffaff126fd1040a2c45d9e0166b2dd82fd9"
5+
project_hash = "087b8a4b471c678bd9311435ec9ae9ba1d7d5947"
66

77
[[deps.ANSIColoredPrinters]]
88
git-tree-sha1 = "574baf8110975760d391c710b6341da1afa48d8c"
@@ -25,6 +25,12 @@ uuid = "56f22d72-fd6d-98f1-02f0-08ddc0907c33"
2525
[[deps.Base64]]
2626
uuid = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"
2727

28+
[[deps.BenchmarkTools]]
29+
deps = ["JSON", "Logging", "Printf", "Profile", "Statistics", "UUIDs"]
30+
git-tree-sha1 = "d9a9701b899b30332bbcb3e1679c41cce81fb0e8"
31+
uuid = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf"
32+
version = "1.3.2"
33+
2834
[[deps.Compat]]
2935
deps = ["Dates", "LinearAlgebra", "UUIDs"]
3036
git-tree-sha1 = "61fdd77467a5c3ad071ef8277ac6bd6af7dd4c04"
@@ -213,6 +219,10 @@ version = "1.3.0"
213219
deps = ["Unicode"]
214220
uuid = "de0858da-6303-5e67-8744-51eddeeeb8d7"
215221

222+
[[deps.Profile]]
223+
deps = ["Printf"]
224+
uuid = "9abbd945-dff8-562f-b5e8-e1ebf5ef1b79"
225+
216226
[[deps.REPL]]
217227
deps = ["InteractiveUtils", "Markdown", "Sockets", "Unicode"]
218228
uuid = "3fa0cd96-eef1-5676-8a61-b3b8758bbffb"

docs/Project.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
[deps]
2+
BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf"
23
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
34
Graphs = "86223c79-3864-5bf0-83f7-82e725a168b6"
5+
InteractiveUtils = "b77e0a4c-d291-57a0-90e8-8db25a27a240"
46
Literate = "98b081ad-f1c9-55d3-8b20-4c87d4299306"
57
MetaGraphs = "626554b9-1ddb-594c-aa3c-2596fe9399a5"
68
MetaGraphsNext = "fa8bd995-216d-47f1-8a91-f3b68fbeb377"

src/MetaGraphsNext.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
"""
2+
MetaGraphsNext
3+
4+
A package for graphs with vertex labels and metadata in Julia. Its main export is the [`MetaGraph`](@ref) type.
5+
"""
16
module MetaGraphsNext
27

38
using JLD2

src/metagraph.jl

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
A graph type with custom vertex labels containing vertex-, edge- and graph-level metadata.
1414
1515
Vertex labels have type `Label`, while vertex (resp. edge, resp. graph) metadata has type `VertexData` (resp. `EdgeData`, resp. `GraphData`).
16-
It is recommended not to set `Label` to an integer type, so as to avoid confusion between vertex labels and vertex codes (which have type `Code<:Integer`).
16+
It is recommended not to set `Label` to an integer type, so as to avoid confusion between vertex labels (which do not change as the graph evolves) and vertex codes (which have type `Code<:Integer` and can change as the graph evolves).
1717
1818
# Fields
1919
- `graph::Graph`: underlying, data-less graph with vertex codes of type `Code`
@@ -74,9 +74,6 @@ function MetaGraph(
7474
),
7575
)
7676
end
77-
if Label <: Integer
78-
@warn "Constructing a MetaGraph with integer labels is not advised."
79-
end
8077
vertex_labels = Dict{Code,Label}()
8178
vertex_properties = Dict{Label,Tuple{Code,VertexData}}()
8279
edge_data = Dict{Tuple{Label,Label},EdgeData}()

test/runtests.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ using Documenter
33
using Graphs
44
using JuliaFormatter
55
using MetaGraphsNext
6-
using SimpleTraits
76
using Test
87

98
DocMeta.setdocmeta!(MetaGraphsNext, :DocTestSetup, :(using MetaGraphsNext); recursive=true)
@@ -31,6 +30,9 @@ DocMeta.setdocmeta!(MetaGraphsNext, :DocTestSetup, :(using MetaGraphsNext); recu
3130
@testset verbose = true "Type stability" begin
3231
include(joinpath("tutorial", "4_type_stability.jl"))
3332
end
33+
@testset verbose = true "Benchmark" begin
34+
include(joinpath("tutorial", "5_benchmark.jl"))
35+
end
3436
end
3537

3638
@testset verbose = true "Miscellaneous" begin

test/tutorial/2_graphs.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
using Graphs
44
using MetaGraphsNext
5+
using SimpleTraits #src
56
using Test #src
67

78
# `MetaGraph`s inherit many methods from Graphs.jl. In general, inherited methods refer to vertices by codes, not labels, for compatibility with the `AbstractGraph` interface.

test/tutorial/5_benchmark.jl

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
# # Benchmark
2+
3+
# Here we compare the performance of MetaGraphsNext.jl with its predecessor MetaGraphs.jl.
4+
5+
using BenchmarkTools
6+
using Graphs
7+
using InteractiveUtils
8+
using MetaGraphs: MetaGraphs
9+
using MetaGraphsNext: MetaGraphsNext
10+
using Test #src
11+
12+
#=
13+
The benchmarking task is two-fold:
14+
1. Build a complete graph with random boolean metadata on the vertices (`active`) and float metadata on the edges (`distance`)
15+
2. Compute the sum of distances for all edges whose endpoints are both active.
16+
=#
17+
18+
# ## Graph construction
19+
20+
function build_incremental_metagraphsnext(n)
21+
g = Graph(0)
22+
mg = MetaGraphsNext.MetaGraph(
23+
g;
24+
label_type=Int, # this will throw a warning
25+
vertex_data_type=Bool,
26+
edge_data_type=Float64,
27+
)
28+
for li in 1:n
29+
mg[li] = rand(Bool)
30+
end
31+
for li in 1:n, lj in 1:(li - 1)
32+
mg[li, lj] = rand(Float64)
33+
end
34+
return mg
35+
end;
36+
37+
#-
38+
39+
function build_bulk_metagraphsnext(n)
40+
g = complete_graph(n)
41+
vertices_description = [li => rand(Bool) for li in 1:n]
42+
edges_description = [(li, lj) => rand(Float64) for li in 1:n for lj in 1:(li - 1)]
43+
mg = MetaGraphsNext.MetaGraph(g, vertices_description, edges_description;)
44+
return mg
45+
end;
46+
47+
#-
48+
49+
function build_metagraphs(n)
50+
g = complete_graph(n)
51+
mg = MetaGraphs.MetaGraph(g)
52+
for i in 1:n
53+
MetaGraphs.set_prop!(mg, i, :active, rand(Bool))
54+
end
55+
for i in 1:n, j in 1:(i - 1)
56+
MetaGraphs.set_prop!(mg, i, j, :distance, rand(Float64))
57+
end
58+
return mg
59+
end;
60+
61+
#-
62+
63+
@btime build_incremental_metagraphsnext(100);
64+
65+
#-
66+
67+
@btime build_bulk_metagraphsnext(100);
68+
69+
#-
70+
71+
@btime build_metagraphs(100);
72+
73+
# ## Graph exploitation
74+
75+
function sum_active_edges_metagraphsnext(mg)
76+
S = 0.0
77+
for (li, lj) in MetaGraphsNext.edge_labels(mg)
78+
active_i = mg[li]
79+
active_j = mg[lj]
80+
distance_ij = mg[li, lj]
81+
if active_i && active_j
82+
S += distance_ij
83+
end
84+
end
85+
return S
86+
end
87+
88+
#-
89+
90+
function sum_active_edges_metagraphs(mg)
91+
S = 0.0
92+
for e in edges(mg)
93+
i, j = src(e), dst(e)
94+
active_i = MetaGraphs.get_prop(mg, i, :active)
95+
active_j = MetaGraphs.get_prop(mg, j, :active)
96+
distance_ij = MetaGraphs.get_prop(mg, i, j, :distance)
97+
if active_i && active_j
98+
S += distance_ij
99+
end
100+
end
101+
return S
102+
end
103+
104+
#-
105+
106+
mg1 = build_incremental_metagraphsnext(100);
107+
@btime sum_active_edges_metagraphsnext($mg1);
108+
109+
#-
110+
111+
mg2 = build_metagraphs(100);
112+
@btime sum_active_edges_metagraphs($mg2);
113+
114+
# The difference in performance can be explained by type instability.
115+
116+
@code_warntype sum_active_edges_metagraphsnext(mg1);
117+
118+
#-
119+
120+
@code_warntype sum_active_edges_metagraphs(mg2);

0 commit comments

Comments
 (0)