forked from sbromberger/LightGraphs.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
egonets.jl
63 lines (54 loc) · 1.45 KB
/
egonets.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
using LightGraphs
using BenchmarkTools
@show Threads.nthreads()
@benchgroup "parallel" begin
@benchgroup "egonet" begin
function vertex_function(g::Graph, i::Int)
a = 0
for u in neighbors(g, i)
a += degree(g, u)
end
return a
end
function twohop(g::Graph, i::Int)
a = 0
for u in neighbors(g, i)
for v in neighbors(g, u)
a += degree(g,v)
end
end
return a
end
function mapvertices(f, g::Graph)
n = nv(g)
a = zeros(Int, n)
Threads.@threads for i in 1:n
a[i] = f(g, i)
end
return a
end
function mapvertices_single(f, g)
n = nv(g)
a = zeros(Int, n)
for i in 1:n
a[i] = f(g, i)
end
return a
end
function comparison(f, g)
println("Mulithreaded on $(Threads.nthreads())")
b1 = @benchmark mapvertices($f, $g)
println(b1)
println("singlethreaded")
b2 = @benchmark mapvertices_single($f, $g)
println(b2)
println("done")
end
nv_ = 10000
g = Graph(nv_, 64*nv_)
f = vertex_function
println(g)
comparison(vertex_function, g)
comparison(twohop, g)
end
end