Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Open sidebar
Peter Jentsch
CovidAlertABM
Commits
ac910b77
Commit
ac910b77
authored
Jun 14, 2021
by
Peter Jentsch
Browse files
some allocation tests in /scratch_code
parent
b4ace6e9
Changes
8
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
132 additions
and
19 deletions
+132
-19
CovidAlertVaccinationModel/Manifest.toml
CovidAlertVaccinationModel/Manifest.toml
+0
-8
CovidAlertVaccinationModel/Project.toml
CovidAlertVaccinationModel/Project.toml
+0
-1
CovidAlertVaccinationModel/src/ABM/contact_vectors.jl
CovidAlertVaccinationModel/src/ABM/contact_vectors.jl
+4
-5
CovidAlertVaccinationModel/src/ABM/mixing_graphs.jl
CovidAlertVaccinationModel/src/ABM/mixing_graphs.jl
+4
-4
CovidAlertVaccinationModel/src/CovidAlertVaccinationModel.jl
CovidAlertVaccinationModel/src/CovidAlertVaccinationModel.jl
+0
-1
CovidAlertVaccinationModel/test/ABM/mixing_test.jl
CovidAlertVaccinationModel/test/ABM/mixing_test.jl
+1
-0
scratch_code/alloc_test.jl
scratch_code/alloc_test.jl
+24
-0
scratch_code/alloc_test2.jl
scratch_code/alloc_test2.jl
+99
-0
No files found.
CovidAlertVaccinationModel/Manifest.toml
View file @
ac910b77
...
...
@@ -1184,14 +1184,6 @@ git-tree-sha1 = "7c8974c7de377a2dc67e778017c78f96fc8f0fc6"
uuid
=
"3d5dd08c-fd9d-11e8-17fa-ed2836048c2f"
version
=
"0.20.16"
[[VectorizedRNG]]
deps
=
[
"Distributed"
,
"Random"
,
"UnPack"
,
"VectorizationBase"
]
git-tree-sha1
=
"0a65169fbbf9c94d2136ba75946e73a88af3b491"
repo-rev
=
"master"
repo-url
=
"https://github.com/JuliaSIMD/VectorizedRNG.jl.git"
uuid
=
"33b4df10-0173-11e9-2a0c-851a7edac40e"
version
=
"0.2.11"
[[VersionParsing]]
git-tree-sha1
=
"80229be1f670524750d905f8fc8148e5a8c4537f"
uuid
=
"81def892-9a0e-5fdd-b105-ffc91e053289"
...
...
CovidAlertVaccinationModel/Project.toml
View file @
ac910b77
...
...
@@ -43,7 +43,6 @@ Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
StatsBase
=
"2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91"
ThreadsX
=
"ac1d9e8a-700a-412c-b207-f0111f4b6c0d"
UnPack
=
"3a884ed6-31ef-47d7-9d2a-63182c4928ed"
VectorizedRNG
=
"33b4df10-0173-11e9-2a0c-851a7edac40e"
ZeroWeightedDistributions
=
"24733ad3-391a-4e41-8839-c7177de7dea4"
[compat]
...
...
CovidAlertVaccinationModel/src/ABM/contact_vectors.jl
View file @
ac910b77
...
...
@@ -24,7 +24,6 @@ end
return
csum
end
"""
generate_contact_vectors!(ij_dist<:Distribution,ji_dist<:Distribution,i_to_j_contacts::Vector{T}, j_to_i_contacts::Vector{T})
...
...
@@ -42,10 +41,10 @@ function generate_contact_vectors!(ij_dist,ji_dist,i_to_j_contacts::AbstractVect
csum
=
sum
(
i_to_j_contacts
)
-
sum
(
j_to_i_contacts
)
inner_iter
=
10
index_list_i
=
@MVector
zeros
(
Int
,
inner_iter
)
index_list_j
=
similar
(
index_list_i
)
sample_list_i
=
@MVector
zeros
(
T
,
inner_iter
)
sample_list_j
=
similar
(
sample_list_i
)
index_list_i
=
MArray
{
Tuple
{
10
,},
Int
,
1
,
10
}(
undef
)
index_list_j
=
MArray
{
Tuple
{
10
,},
Int
,
1
,
10
}(
undef
)
sample_list_i
=
MArray
{
Tuple
{
10
,},
Int
,
1
,
10
}(
undef
)
sample_list_j
=
MArray
{
Tuple
{
10
,},
Int
,
1
,
10
}(
undef
)
while
csum
!=
0
sample!
(
Random
.
default_rng
(
Threads
.
threadid
()),
1
:
l_i
,
index_list_i
)
...
...
CovidAlertVaccinationModel/src/ABM/mixing_graphs.jl
View file @
ac910b77
...
...
@@ -84,16 +84,16 @@ function time_dep_mixing_graphs(len,base_network,demographics,index_vectors,ws_m
ws_static_edges
=
WeightedGraph
(
demographics
,
index_vectors
,
ws_matrix_tuple
.
daily
,
contact_time_distributions
.
ws
)
ws_weekly_edges
=
WeightedGraph
(
demographics
,
index_vectors
,
ws_matrix_tuple
.
twice_a_week
,
contact_time_distributions
.
ws
)
ws_
daily
_edges
=
WeightedGraph
(
demographics
,
index_vectors
,
ws_matrix_tuple
.
otherwise
,
contact_time_distributions
.
ws
)
ws_
justonce
_edges
=
WeightedGraph
(
demographics
,
index_vectors
,
ws_matrix_tuple
.
otherwise
,
contact_time_distributions
.
ws
)
rest_static_edges
=
WeightedGraph
(
demographics
,
index_vectors
,
rest_matrix_tuple
.
daily
,
contact_time_distributions
.
rest
)
rest_weekly_edges
=
WeightedGraph
(
demographics
,
index_vectors
,
rest_matrix_tuple
.
twice_a_week
,
contact_time_distributions
.
rest
)
rest_
daily
_edges
=
WeightedGraph
(
demographics
,
index_vectors
,
rest_matrix_tuple
.
otherwise
,
contact_time_distributions
.
rest
)
rest_
justonce
_edges
=
WeightedGraph
(
demographics
,
index_vectors
,
rest_matrix_tuple
.
otherwise
,
contact_time_distributions
.
rest
)
inf_network_list
=
[
home_static_edges
,
rest_static_edges
,
ws_
daily
_edges
,
rest_
daily
_edges
]
inf_network_list
=
[
home_static_edges
,
rest_static_edges
,
ws_
justonce
_edges
,
rest_
justonce
_edges
]
soc_network_list
=
[
home_static_edges
,
rest_static_edges
,
ws_static_edges
]
remade_graphs
=
(
ws_
daily
_edges
,
rest_
daily
_edges
)
remade_graphs
=
(
ws_
justonce
_edges
,
rest_
justonce
_edges
)
resampled_graphs
=
(
home_static_edges
,
rest_static_edges
,
ws_static_edges
,
rest_weekly_edges
,
ws_weekly_edges
)
infected_mixing_graph
=
TimeDepMixingGraph
(
len
,
remade_graphs
,
resampled_graphs
,
inf_network_list
)
...
...
CovidAlertVaccinationModel/src/CovidAlertVaccinationModel.jl
View file @
ac910b77
...
...
@@ -26,7 +26,6 @@ import Pandas: read_csv
using
DataFrames
using
StaticArrays
import
LightGraphs
.
neighbors
using
VectorizedRNG
export
intervalsmodel
,
hh
,
ws
,
rest
,
abm
...
...
CovidAlertVaccinationModel/test/ABM/mixing_test.jl
View file @
ac910b77
...
...
@@ -131,6 +131,7 @@ end
fit!
(
mixing_dist
[
Int
(
demo_v
),
j
],
d
)
end
end
display
(
mean
.
(
mixing_dist
))
for
i
in
eachindex
(
mixing_dist
)
@test
mean
(
mixing_dist
[
i
])
≈
mean
(
dist
[
i
])
atol
=
0.2
end
...
...
scratch_code/alloc_test.jl
0 → 100644
View file @
ac910b77
using
StaticArrays
using
StrideArrays
using
BenchmarkTools
using
UnsafeArrays
function
g
()
#would like to reduce allocations here
l
=
rand
(
9000
:
10_000
)
#large random integer
arr
=
Vector
{
Int
}(
undef
,
l
)
#array of length l
#do some stuff with arr
accum
=
0
arr
.=
1
for
k
in
arr
accum
+=
k
+
f
(
arr
)
end
return
accum
#discard arr
end
function
f
(
arr
)
return
arr
[
1
]
*
2
end
@allocated
g
()
\ No newline at end of file
scratch_code/alloc_test2.jl
0 → 100644
View file @
ac910b77
using
CovidAlertVaccinationModel
using
CovidAlertVaccinationModel
:
ModelSolution
using
UnPack
using
StrideArrays
using
Random
,
StatsBase
using
StaticArrays
using
BenchmarkTools
"""
Called in the inner loop of `generate_contact_vectors!`, this function removes the degree corresponding to kth entry of index_lists, replaces it with another randomly sampled degree, and returns `csum` adjusted to the new samples.
We repeat this function until `csum == 0`.
"""
@inline
function
reindex!
(
k
,
csum
,
index_list_i
,
index_list_j
,
j_to_i_contacts
,
i_to_j_contacts
,
sample_list_i
,
sample_list_j
)
i_index
=
index_list_i
[
k
]
j_index
=
index_list_j
[
k
]
csum
+=
j_to_i_contacts
[
j_index
]
-
i_to_j_contacts
[
i_index
]
+
sample_list_i
[
k
]
-
sample_list_j
[
k
]
i_to_j_contacts
[
i_index
]
=
sample_list_i
[
k
]
j_to_i_contacts
[
j_index
]
=
sample_list_j
[
k
]
return
csum
end
"""
generate_contact_vectors!(ij_dist<:Distribution,ji_dist<:Distribution,i_to_j_contacts::Vector{T}, j_to_i_contacts::Vector{T})
Fills i_to_j_contacts and j_to_i_contacts with degrees sampled from ij_dist and ji_dist, such that i_to_j_contacts and j_to_i_contacts have equal sum.
Given `μz_i = mean(ij_dist)` and `μ_j = mean(ji_dist)`, these must satisfy `μ_i* length(i_to_j_contacts) == μ_j* length(j_to_i_contacts)`
"""
function
generate_contact_vectors!
(
ij_dist
,
ji_dist
,
i_to_j_contacts
::
AbstractVector
{
T
},
j_to_i_contacts
::
AbstractVector
{
T
})
where
T
rand!
(
Random
.
default_rng
(
Threads
.
threadid
()),
ij_dist
,
i_to_j_contacts
)
rand!
(
Random
.
default_rng
(
Threads
.
threadid
()),
ji_dist
,
j_to_i_contacts
)
l_i
=
length
(
i_to_j_contacts
)
l_j
=
length
(
j_to_i_contacts
)
csum
=
sum
(
i_to_j_contacts
)
-
sum
(
j_to_i_contacts
)
inner_iter
=
10
index_list_i
=
MArray
{
Tuple
{
10
,},
Int
,
1
,
10
}(
undef
)
index_list_j
=
MArray
{
Tuple
{
10
,},
Int
,
1
,
10
}(
undef
)
sample_list_i
=
MArray
{
Tuple
{
10
,},
Int
,
1
,
10
}(
undef
)
sample_list_j
=
MArray
{
Tuple
{
10
,},
Int
,
1
,
10
}(
undef
)
while
csum
!=
0
GC
.
@preserve
index_list_i
index_list_j
begin
sample!
(
Random
.
default_rng
(
Threads
.
threadid
()),
1
:
l_i
,
index_list_i
)
sample!
(
Random
.
default_rng
(
Threads
.
threadid
()),
1
:
l_j
,
index_list_j
)
end
rand!
(
Random
.
default_rng
(
Threads
.
threadid
()),
ij_dist
,
sample_list_i
)
rand!
(
Random
.
default_rng
(
Threads
.
threadid
()),
ji_dist
,
sample_list_j
)
@inbounds
for
i
=
1
:
inner_iter
if
csum
!=
0
csum
=
reindex!
(
i
,
csum
,
index_list_i
,
index_list_j
,
j_to_i_contacts
,
i_to_j_contacts
,
sample_list_i
,
sample_list_j
)
end
end
end
return
nothing
end
using
CovidAlertVaccinationModel
:
GraphEdge
function
alloc_test
()
model
=
ModelSolution
(
1
,
get_parameters
(),
5000
)
@unpack
demographics
,
index_vectors
,
rest_matrix_tuple
,
ws_matrix_tuple
=
model
dist
=
ws_matrix_tuple
.
daily
contact_array
=
[
sizehint!
(
Vector
{
GraphEdge
}(),
length
(
index_vectors
[
i
])
*
2
)
for
i
in
1
:
length
(
index_vectors
),
j
in
1
:
length
(
index_vectors
)]
fast_chung_lu!
(
index_vectors
,
contact_array
,
dist
)
return
contact_array
end
function
fast_chung_lu!
(
index_vectors
,
contact_array
,
mixing_matrix
)
tot
=
0
num_degrees_alloc_ij
=
Array
{
Int
}(
undef
,
maximum
(
length
.
(
index_vectors
)))
num_degrees_alloc_ji
=
similar
(
num_degrees_alloc_ij
)
stubs_alloc_i
=
similar
(
num_degrees_alloc_ij
)
stubs_alloc_j
=
similar
(
num_degrees_alloc_ij
)
for
i
in
1
:
3
,
j
in
1
:
i
#diagonal
if
i
!=
j
num_degrees_ij
=
view
(
num_degrees_alloc_ij
,
1
:
length
(
index_vectors
[
i
]))
num_degrees_ji
=
view
(
num_degrees_alloc_ji
,
1
:
length
(
index_vectors
[
j
]))
generate_contact_vectors!
(
mixing_matrix
[
i
,
j
],
mixing_matrix
[
j
,
i
],
num_degrees_ij
,
num_degrees_ji
)
num_edges
=
sum
(
num_degrees_ij
)
stubs_i
=
view
(
stubs_alloc_i
,
1
:
num_edges
)
stubs_j
=
view
(
stubs_alloc_i
,
1
:
num_edges
)
if
num_edges
>
0
sample!
(
Random
.
default_rng
(
Threads
.
threadid
()),
index_vectors
[
i
],
Weights
(
num_degrees_ij
./
num_edges
),
stubs_i
)
sample!
(
Random
.
default_rng
(
Threads
.
threadid
()),
index_vectors
[
j
],
Weights
(
num_degrees_ji
./
num_edges
),
stubs_j
)
tot
+=
num_edges
end
contact_array
[
j
,
i
]
=
GraphEdge
.
(
stubs_i
,
stubs_j
)
end
end
return
tot
end
@btime
alloc_test
()
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment