-
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
event handling: fix a bug and clean house #2421
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
eb6fbe7
fix multi_event_stream: sort events by time for multiple cells per ce…
boeschf cc12918
events overhaul
boeschf 34f27df
make old compilers happy
boeschf 83c4846
make old compilers more happy
boeschf 1ed1eb0
Merge remote-tracking branch 'upstream/master' into fix/event_sorting
boeschf 7c44fac
apply suggestions from review
boeschf 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
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 | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -2,10 +2,8 @@ | |||||||
|
||||||||
#include <vector> | ||||||||
|
||||||||
#include <arbor/generic_event.hpp> | ||||||||
#include <arbor/mechanism_abi.h> | ||||||||
|
||||||||
|
||||||||
#include "backends/event.hpp" | ||||||||
#include "backends/event_stream_state.hpp" | ||||||||
#include "event_lane.hpp" | ||||||||
|
@@ -18,15 +16,13 @@ namespace arb { | |||||||
|
||||||||
template <typename Event> | ||||||||
struct event_stream_base { | ||||||||
using size_type = std::size_t; | ||||||||
using event_type = Event; | ||||||||
using event_time_type = ::arb::event_time_type<Event>; | ||||||||
using event_data_type = ::arb::event_data_type<Event>; | ||||||||
using event_data_type = decltype(event_data(std::declval<Event>())); | ||||||||
|
||||||||
protected: // members | ||||||||
std::vector<event_data_type> ev_data_; | ||||||||
std::vector<std::size_t> ev_spans_ = {0}; | ||||||||
size_type index_ = 0; | ||||||||
std::size_t index_ = 0; | ||||||||
event_data_type* base_ptr_ = nullptr; | ||||||||
|
||||||||
public: | ||||||||
|
@@ -62,24 +58,32 @@ struct event_stream_base { | |||||||
index_ = 0; | ||||||||
} | ||||||||
|
||||||||
// Construct a mapping of mech_id to a stream s.t. streams are partitioned into | ||||||||
// time step buckets by `ev_span` | ||||||||
protected: | ||||||||
// backend specific initializations | ||||||||
virtual void init() = 0; | ||||||||
}; | ||||||||
|
||||||||
struct spike_event_stream_base : event_stream_base<deliverable_event> { | ||||||||
template<typename EventStream> | ||||||||
static std::enable_if_t<std::is_base_of_v<event_stream_base, EventStream>> | ||||||||
multi_event_stream(const event_lane_subrange& lanes, | ||||||||
const std::vector<target_handle>& handles, | ||||||||
const std::vector<std::size_t>& divs, | ||||||||
const timestep_range& steps, | ||||||||
std::unordered_map<unsigned, EventStream>& streams) { | ||||||||
friend void initialize(const event_lane_subrange& lanes, | ||||||||
const std::vector<target_handle>& handles, | ||||||||
const std::vector<std::size_t>& divs, | ||||||||
const timestep_range& steps, | ||||||||
std::unordered_map<unsigned, EventStream>& streams) { | ||||||||
arb_assert(lanes.size() < divs.size()); | ||||||||
|
||||||||
// reset streams and allocate sufficient space for temporaries | ||||||||
auto n_steps = steps.size(); | ||||||||
std::unordered_map<unsigned, std::vector<std::size_t>> dt_sizes; | ||||||||
for (auto& [k, v]: streams) { | ||||||||
v.clear(); | ||||||||
dt_sizes[k].resize(n_steps, 0); | ||||||||
v.spike_counter_.clear(); | ||||||||
v.spike_counter_.resize(steps.size(), 0); | ||||||||
v.spikes_.clear(); | ||||||||
// ev_data_ has been cleared during v.clear(), so we use its capacity | ||||||||
v.spikes_.reserve(v.ev_data_.capacity()); | ||||||||
} | ||||||||
|
||||||||
// loop over lanes: group events by mechanism and sort them by time | ||||||||
auto cell = 0; | ||||||||
for (const auto& lane: lanes) { | ||||||||
auto div = divs[cell]; | ||||||||
|
@@ -94,16 +98,71 @@ struct event_stream_base { | |||||||
if (step >= n_steps) break; | ||||||||
arb_assert(div + target < handles.size()); | ||||||||
const auto& handle = handles[div + target]; | ||||||||
streams[handle.mech_id].ev_data_.push_back({handle.mech_index, weight}); | ||||||||
dt_sizes[handle.mech_id][step]++; | ||||||||
auto& stream = streams[handle.mech_id]; | ||||||||
stream.spikes_.push_back(spike_data{step, handle.mech_index, time, weight}); | ||||||||
// insertion sort with last element as pivot | ||||||||
// ordering: first w.r.t. step, within a step: mech_index, within a mech_index: time | ||||||||
auto first = stream.spikes_.begin(); | ||||||||
auto last = stream.spikes_.end(); | ||||||||
auto pivot = std::prev(last, 1); | ||||||||
std::rotate(std::upper_bound(first, pivot, *pivot), pivot, last); | ||||||||
// increment count in current time interval | ||||||||
stream.spike_counter_[step]++; | ||||||||
} | ||||||||
} | ||||||||
|
||||||||
for (auto& [id, stream]: streams) { | ||||||||
util::make_partition(stream.ev_spans_, dt_sizes[id]); | ||||||||
stream.init(); | ||||||||
// copy temporary deliverable_events into stream's ev_data_ | ||||||||
stream.ev_data_.reserve(stream.spikes_.size()); | ||||||||
std::transform(stream.spikes_.begin(), stream.spikes_.end(), std::back_inserter(stream.ev_data_), | ||||||||
[](auto const& e) noexcept -> arb_deliverable_event_data { | ||||||||
return {e.mech_index, e.weight}; }); | ||||||||
// scan over spike_counter_ and written to ev_spans_ | ||||||||
util::make_partition(stream.ev_spans_, stream.spike_counter_); | ||||||||
// delegate to derived class init: static cast necessary to access protected init() | ||||||||
static_cast<spike_event_stream_base&>(stream).init(); | ||||||||
} | ||||||||
} | ||||||||
|
||||||||
protected: // members | ||||||||
struct spike_data { | ||||||||
arb_size_type step = 0; | ||||||||
cell_local_size_type mech_index = 0; | ||||||||
time_type time = 0; | ||||||||
float weight = 0; | ||||||||
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.
Suggested change
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. absolutely, thanks for pointing this out |
||||||||
auto operator<=>(spike_data const&) const noexcept = default; | ||||||||
}; | ||||||||
std::vector<spike_data> spikes_; | ||||||||
std::vector<std::size_t> spike_counter_; | ||||||||
}; | ||||||||
|
||||||||
struct sample_event_stream_base : event_stream_base<sample_event> { | ||||||||
friend void initialize(const std::vector<std::vector<sample_event>>& staged, | ||||||||
sample_event_stream_base& stream) { | ||||||||
// clear previous data | ||||||||
stream.clear(); | ||||||||
|
||||||||
// return if there are no timestep bins | ||||||||
if (!staged.size()) return; | ||||||||
|
||||||||
// return if there are no events | ||||||||
auto num_events = util::sum_by(staged, [] (const auto& v) {return v.size();}); | ||||||||
if (!num_events) return; | ||||||||
|
||||||||
// allocate space for spans and data | ||||||||
stream.ev_spans_.reserve(staged.size() + 1); | ||||||||
stream.ev_data_.reserve(num_events); | ||||||||
|
||||||||
// add event data and spans | ||||||||
for (const auto& v : staged) { | ||||||||
for (const auto& ev: v) stream.ev_data_.push_back(ev.raw); | ||||||||
stream.ev_spans_.push_back(stream.ev_data_.size()); | ||||||||
} | ||||||||
|
||||||||
arb_assert(num_events == stream.ev_data_.size()); | ||||||||
arb_assert(staged.size() + 1 == stream.ev_spans_.size()); | ||||||||
stream.init(); | ||||||||
} | ||||||||
}; | ||||||||
|
||||||||
} // namespace arb |
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
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.
We are using
capacity
here, that seems unusual.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.
this is intentional, as
ev_data_
has been cleared before. I should probably make a comment. Otherwise, I can reorder the operations, so thatv.clear()
is called afterwards.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.
I think that would be confusing, just make a note.