-
Notifications
You must be signed in to change notification settings - Fork 61
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
Reduce transient memory allocations in integrate/pw_lin. #2208
Closed
thorstenhater
wants to merge
8
commits into
arbor-sim:master
from
thorstenhater:perf/reduce-pw-lin-memory
+248
−119
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
55f93df
Use index/get instead of std::visit.
thorstenhater adae932
Snapshot, doesnt pass
thorstenhater 4f9d5dd
Fix cases.
thorstenhater ec90987
Merge remote-tracking branch 'origin/master' into perf/reduce-pw-lin-…
thorstenhater 78f821b
Complain -- ie terminate -- on unknown cases.
thorstenhater 7ef78b6
Also add proxy for constant iterator, hoping we'll save the
thorstenhater 1fecaa1
Merge remote-tracking branch 'origin/master' into perf/reduce-pw-lin-…
thorstenhater 37b4d81
Only print profile if we can.
thorstenhater File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -91,7 +91,7 @@ struct cable_cell_impl { | |
dictionary(labels), | ||
decorations(decorations) | ||
{ | ||
init(decorations); | ||
init(); | ||
} | ||
|
||
cable_cell_impl(): cable_cell_impl({},{},{}) {} | ||
|
@@ -100,7 +100,7 @@ struct cable_cell_impl { | |
|
||
cable_cell_impl(cable_cell_impl&& other) = default; | ||
|
||
void init(const decor&); | ||
void init(); | ||
|
||
template <typename T> | ||
mlocation_map<T>& get_location_map(const T&) { | ||
|
@@ -116,12 +116,13 @@ struct cable_cell_impl { | |
} | ||
|
||
template <typename Item> | ||
void place(const locset& ls, const Item& item, const hash_type& label) { | ||
|
||
void place(const mlocation_list& ls, const Item& item, const hash_type& label) { | ||
auto& mm = get_location_map(item); | ||
cell_lid_type& lid = placed_count.get<Item>(); | ||
cell_lid_type first = lid; | ||
|
||
for (auto l: thingify(ls, provider)) { | ||
for (auto l: ls) { | ||
placed<Item> p{l, lid++, item}; | ||
mm.push_back(p); | ||
} | ||
|
@@ -160,70 +161,104 @@ struct cable_cell_impl { | |
return region_map.get<init_reversal_potential>()[init.ion]; | ||
} | ||
|
||
void paint(const region& reg, const density& prop) { | ||
this->paint(reg, scaled_mechanism<density>(prop)); | ||
void paint(const mextent& cables, const density& prop, const std::string& reg) { | ||
this->paint(cables, scaled_mechanism<density>(prop), reg); | ||
} | ||
|
||
void paint(const region& reg, const scaled_mechanism<density>& prop) { | ||
void paint(const mextent& cables, const scaled_mechanism<density>& prop, const std::string& reg) { | ||
std::unordered_map<std::string, iexpr_ptr> im; | ||
for (const auto& [fst, snd]: prop.scale_expr) { | ||
im.insert_or_assign(fst, thingify(snd, provider)); | ||
} | ||
|
||
auto& mm = get_region_map(prop.t_mech); | ||
const auto& cables = thingify(reg, provider); | ||
for (const auto& c: cables) { | ||
// Skip zero-length cables in extent: | ||
if (c.prox_pos == c.dist_pos) continue; | ||
|
||
if (!mm.insert(c, {prop.t_mech, im})) { | ||
std::stringstream rg; rg << reg; | ||
throw cable_cell_error(util::pprintf("Setting mechanism '{}' on region '{}' overpaints at cable {}", | ||
prop.t_mech.mech.name(), rg.str(), c)); | ||
prop.t_mech.mech.name(), reg, c)); | ||
} | ||
} | ||
} | ||
|
||
template <typename TaggedMech> | ||
void paint(const region& reg, const TaggedMech& prop) { | ||
mextent cables = thingify(reg, provider); | ||
void paint(const mextent& cables, const TaggedMech& prop, const std::string& reg) { | ||
auto& mm = get_region_map(prop); | ||
|
||
for (auto c: cables) { | ||
// Skip zero-length cables in extent: | ||
if (c.prox_pos==c.dist_pos) continue; | ||
|
||
if (!mm.insert(c, prop)) { | ||
std::stringstream rg; rg << reg; | ||
throw cable_cell_error(util::pprintf("Setting property '{}' on region '{}' overpaints at '{}'", show(prop), rg.str(), c)); | ||
} | ||
if (!mm.insert(c, prop)) throw cable_cell_error(util::pprintf("Setting property '{}' on region '{}' overpaints at '{}'", | ||
show(prop), reg, c)); | ||
} | ||
} | ||
|
||
mlocation_list concrete_locset(const locset& l) const { | ||
return thingify(l, provider); | ||
} | ||
|
||
mextent concrete_region(const region& r) const { | ||
return thingify(r, provider); | ||
} | ||
mlocation_list concrete_locset(const locset& l) const { return thingify(l, provider); } | ||
mextent concrete_region(const region& r) const { return thingify(r, provider); } | ||
}; | ||
|
||
using impl_ptr = std::unique_ptr<cable_cell_impl, void (*)(cable_cell_impl*)>; | ||
impl_ptr make_impl(cable_cell_impl* c) { | ||
return impl_ptr(c, [](cable_cell_impl* p){delete p;}); | ||
} | ||
|
||
void cable_cell_impl::init(const decor& d) { | ||
for (const auto& p: d.paintings()) { | ||
auto& where = p.first; | ||
std::visit([this, &where] (auto&& what) {this->paint(where, what);}, p.second); | ||
void cable_cell_impl::init() { | ||
struct rcache { | ||
std::string region = ""; | ||
mextent cables = {}; | ||
}; | ||
|
||
auto rc = rcache{}; | ||
|
||
for (const auto& [where, what]: decorations.paintings()) { | ||
auto region = util::to_string(where); | ||
// Try to cache with a lookback of one since most models paint one | ||
// region in direct succession. We also key on the stringy view of | ||
// regions since in general equality on regions is undecidable. | ||
if (rc.region != region) { | ||
rc.region = std::move(region); | ||
rc.cables = thingify(where, provider); | ||
} | ||
switch (what.index()) { | ||
case 0: { paint(rc.cables, std::get<init_membrane_potential>(what), rc.region); break; } | ||
case 1: { paint(rc.cables, std::get<axial_resistivity>(what), rc.region); break; } | ||
case 2: { paint(rc.cables, std::get<temperature>(what), rc.region); break; } | ||
case 3: { paint(rc.cables, std::get<membrane_capacitance>(what), rc.region); break; } | ||
case 4: { paint(rc.cables, std::get<ion_diffusivity>(what), rc.region); break; } | ||
case 5: { paint(rc.cables, std::get<init_int_concentration>(what), rc.region); break; } | ||
case 6: { paint(rc.cables, std::get<init_ext_concentration>(what), rc.region); break; } | ||
case 7: { paint(rc.cables, std::get<init_reversal_potential>(what), rc.region); break; } | ||
case 8: { paint(rc.cables, std::get<density>(what), rc.region); break; } | ||
case 9: { paint(rc.cables, std::get<voltage_process>(what), rc.region); break; } | ||
case 10: { paint(rc.cables, std::get<scaled_mechanism<density>>(what), rc.region); break; } | ||
default: throw arbor_internal_error{"Unknown paintable variant"}; | ||
} | ||
} | ||
for (const auto& p: d.placements()) { | ||
auto& where = std::get<0>(p); | ||
auto& label = std::get<2>(p); | ||
std::visit([this, &where, &label] (auto&& what) {return this->place(where, what, label); }, | ||
std::get<1>(p)); | ||
|
||
struct lcache { | ||
std::string locset = ""; | ||
mlocation_list places = {}; | ||
}; | ||
|
||
auto lc = lcache{}; | ||
|
||
for (const auto& [where, what, label]: decorations.placements()) { | ||
auto locset = util::to_string(where); | ||
// Try to cache with a lookback of one since most models place to one | ||
// locset in direct succession. We also key on the stringy view of | ||
// locset since in general equality on regions is undecidable. | ||
if (lc.locset != locset) { | ||
lc.locset = std::move(locset); | ||
lc.places = thingify(where, provider); | ||
} | ||
switch (what.index()) { | ||
case 0: { place(lc.places, std::get<i_clamp>(what), label); break; } | ||
case 1: { place(lc.places, std::get<threshold_detector>(what), label); break; } | ||
case 2: { place(lc.places, std::get<synapse>(what), label); break; } | ||
case 3: { place(lc.places, std::get<junction>(what), label); break; } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here as with the paintings case matching. |
||
default: throw arbor_internal_error{"Unknown placeable variant"}; | ||
} | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For new paintable types, adding it here this might get overlooked.
std::visit
would offer a way to throw a compile time error for such cases, but from the comments elsewhere, I'm assuming this was changed fromstd::visit
for performance reasons here as well. Maybe at least add a default case that throws an error at runtime for unmatched types.