Commit ac910b77 authored by Peter Jentsch's avatar Peter Jentsch
Browse files

some allocation tests in /scratch_code

parent b4ace6e9
......@@ -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"
......
......@@ -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]
......
......@@ -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)
......
......@@ -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)
......
......@@ -26,7 +26,6 @@ import Pandas: read_csv
using DataFrames
using StaticArrays
import LightGraphs.neighbors
using VectorizedRNG
export intervalsmodel, hh, ws, rest, abm
......
......@@ -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
......
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
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
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment