From 51d09f7d1efa390484ef721008ba21d9c9485dcd Mon Sep 17 00:00:00 2001 From: Ben Cumming <louncharf@gmail.com> Date: Mon, 15 Jan 2018 10:49:59 +0100 Subject: [PATCH] Make cell type copyable by default (#430) * Enable `cell` copy constructor, remove special tag type used to guard `cell` cloning. * Provide sane defaults for `recipe` methods. --- example/miniapp/miniapp_recipes.cpp | 4 --- src/cell.hpp | 27 +++++++++---------- src/common_types.hpp | 1 + src/common_types_io.cpp | 13 +++++++++ src/fvm_multicell.hpp | 2 +- src/recipe.hpp | 20 +++++++++----- .../test_communicator.cpp | 16 ----------- .../test_domain_decomposition.cpp | 3 --- tests/simple_recipes.hpp | 17 +++--------- tests/unit/test_cell.cpp | 2 +- tests/unit/test_domain_decomposition.cpp | 16 ----------- tests/validation/validate_ball_and_stick.cpp | 1 - .../validate_compartment_policy.cpp | 1 - tests/validation/validate_kinetic.cpp | 1 - tests/validation/validate_soma.cpp | 1 - tests/validation/validate_synapses.cpp | 1 - 16 files changed, 45 insertions(+), 81 deletions(-) diff --git a/example/miniapp/miniapp_recipes.cpp b/example/miniapp/miniapp_recipes.cpp index 017236d4..69690387 100644 --- a/example/miniapp/miniapp_recipes.cpp +++ b/example/miniapp/miniapp_recipes.cpp @@ -176,10 +176,6 @@ public: } } - std::vector<event_generator_ptr> event_generators(cell_gid_type) const override { - return {}; - } - protected: template <typename RNG> cell_connection draw_connection_params(RNG& rng) const { diff --git a/src/cell.hpp b/src/cell.hpp index 200691b7..a4d81592 100644 --- a/src/cell.hpp +++ b/src/cell.hpp @@ -54,10 +54,6 @@ struct cell_global_properties { std::map<std::string, specialized_mechanism> special_mechs; }; -// used in constructor below -struct clone_cell_t {}; -constexpr clone_cell_t clone_cell{}; - /// high-level abstract representation of a cell and its segments class cell { public: @@ -81,22 +77,25 @@ public: double threshold; }; - // constructor + /// Default constructor cell(); - // Sometimes we really do want a copy (pending big morphology refactor). - cell(clone_cell_t, const cell& other): + /// Copy constructor + cell(const cell& other): parents_(other.parents_), stimuli_(other.stimuli_), synapses_(other.synapses_), spike_detectors_(other.spike_detectors_) - { - // unique_ptr's cannot be copy constructed, do a manual assignment - segments_.reserve(other.segments_.size()); - for (const auto& s: other.segments_) { - segments_.push_back(s->clone()); - } - } + { + // unique_ptr's cannot be copy constructed, do a manual assignment + segments_.reserve(other.segments_.size()); + for (const auto& s: other.segments_) { + segments_.push_back(s->clone()); + } + } + + /// Move constructor + cell(cell&& other) = default; /// Return the kind of cell, used for grouping into cell_groups cell_kind get_cell_kind() const { diff --git a/src/common_types.hpp b/src/common_types.hpp index 2cc32034..19a5aacf 100644 --- a/src/common_types.hpp +++ b/src/common_types.hpp @@ -76,6 +76,7 @@ enum cell_kind { } // namespace arb std::ostream& operator<<(std::ostream& O, arb::cell_member_type m); +std::ostream& operator<<(std::ostream& O, arb::cell_kind k); namespace std { template <> struct hash<arb::cell_member_type> { diff --git a/src/common_types_io.cpp b/src/common_types_io.cpp index 4850d5d8..9e478115 100644 --- a/src/common_types_io.cpp +++ b/src/common_types_io.cpp @@ -6,3 +6,16 @@ std::ostream& operator<<(std::ostream& O, arb::cell_member_type m) { return O << m.gid << ':' << m.index; } +std::ostream& operator<<(std::ostream& o, arb::cell_kind k) { + o << "cell_kind::"; + switch (k) { + case arb::cell_kind::regular_spike_source: + return o << "regular_spike_source"; + case arb::cell_kind::cable1d_neuron: + return o << "cable1d_neuron"; + case arb::cell_kind::data_spike_source: + return o << "data_spike_source"; + } + return o; +} + diff --git a/src/fvm_multicell.hpp b/src/fvm_multicell.hpp index 3eb1fb1a..aac92841 100644 --- a/src/fvm_multicell.hpp +++ b/src/fvm_multicell.hpp @@ -638,7 +638,7 @@ void fvm_multicell<Backend>::initialize( std::vector<cell> cells; cells.reserve(gids.size()); for (auto gid: gids) { - cells.push_back(any_cast<cell>(rec.get_cell_description(gid))); + cells.push_back(std::move(any_cast<cell>(rec.get_cell_description(gid)))); } auto cell_num_compartments = diff --git a/src/recipe.hpp b/src/recipe.hpp index 54ba235d..a8981dbc 100644 --- a/src/recipe.hpp +++ b/src/recipe.hpp @@ -60,14 +60,20 @@ public: virtual util::unique_any get_cell_description(cell_gid_type gid) const = 0; virtual cell_kind get_cell_kind(cell_gid_type) const = 0; - virtual cell_size_type num_sources(cell_gid_type) const = 0; - virtual cell_size_type num_targets(cell_gid_type) const = 0; - virtual cell_size_type num_probes(cell_gid_type) const = 0; + virtual cell_size_type num_sources(cell_gid_type) const { return 0; } + virtual cell_size_type num_targets(cell_gid_type) const { return 0; } + virtual cell_size_type num_probes(cell_gid_type) const { return 0; } + + virtual std::vector<event_generator_ptr> event_generators(cell_gid_type) const { + return {}; + } + virtual std::vector<cell_connection> connections_on(cell_gid_type) const { + return {}; + } + virtual probe_info get_probe(cell_member_type) const { + throw std::logic_error("no probes"); + } - virtual std::vector<event_generator_ptr> event_generators(cell_gid_type) const = 0; - - virtual std::vector<cell_connection> connections_on(cell_gid_type) const = 0; - virtual probe_info get_probe(cell_member_type probe_id) const = 0; // Global property type will be specific to given cell kind. virtual util::any get_global_properties(cell_kind) const { return util::any{}; }; diff --git a/tests/global_communication/test_communicator.cpp b/tests/global_communication/test_communicator.cpp index 21d7be0e..2a5238a4 100644 --- a/tests/global_communication/test_communicator.cpp +++ b/tests/global_communication/test_communicator.cpp @@ -213,14 +213,6 @@ namespace { 1.0f)}; // delay } - std::vector<event_generator_ptr> event_generators(cell_gid_type) const override { - return {}; - } - - probe_info get_probe(cell_member_type) const override { - throw std::logic_error("no probes"); - } - private: cell_size_type size_; cell_size_type ranks_; @@ -286,14 +278,6 @@ namespace { return cons; } - std::vector<event_generator_ptr> event_generators(cell_gid_type) const override { - return {}; - } - - probe_info get_probe(cell_member_type) const override { - throw std::logic_error("no probes"); - } - private: cell_size_type size_; cell_size_type ranks_; diff --git a/tests/global_communication/test_domain_decomposition.cpp b/tests/global_communication/test_domain_decomposition.cpp index 119dded9..d6cef200 100644 --- a/tests/global_communication/test_domain_decomposition.cpp +++ b/tests/global_communication/test_domain_decomposition.cpp @@ -58,9 +58,6 @@ namespace { return {}; } - probe_info get_probe(cell_member_type) const override { - throw std::logic_error("no probes"); - } private: cell_size_type size_; diff --git a/tests/simple_recipes.hpp b/tests/simple_recipes.hpp index 509f6dab..8e7218c0 100644 --- a/tests/simple_recipes.hpp +++ b/tests/simple_recipes.hpp @@ -20,14 +20,6 @@ public: return probes_.count(i)? probes_.at(i).size(): 0; } - std::vector<event_generator_ptr> event_generators(cell_gid_type) const override { - return {}; - } - - std::vector<cell_connection> connections_on(cell_gid_type) const override { - return {}; - } - virtual probe_info get_probe(cell_member_type probe_id) const override { return probes_.at(probe_id.gid).at(probe_id.index); } @@ -76,9 +68,6 @@ public: cell_size_type num_cells() const override { return n_; } cell_kind get_cell_kind(cell_gid_type) const override { return Kind; } - cell_size_type num_sources(cell_gid_type) const override { return 0; } - cell_size_type num_targets(cell_gid_type) const override { return 0; } - util::unique_any get_cell_description(cell_gid_type) const override { return util::make_unique_any<Description>(desc_); } @@ -98,13 +87,13 @@ public: template <typename Seq> explicit cable1d_recipe(const Seq& cells) { for (const auto& c: cells) { - cells_.emplace_back(clone_cell, c); + cells_.emplace_back(c); } } explicit cable1d_recipe(const cell& c) { cells_.reserve(1); - cells_.emplace_back(clone_cell, c); + cells_.emplace_back(c); } cell_size_type num_cells() const override { return cells_.size(); } @@ -119,7 +108,7 @@ public: } util::unique_any get_cell_description(cell_gid_type i) const override { - return util::make_unique_any<cell>(clone_cell, cells_[i]); + return util::make_unique_any<cell>(cells_[i]); } protected: diff --git a/tests/unit/test_cell.cpp b/tests/unit/test_cell.cpp index 587f8fa9..58981bfa 100644 --- a/tests/unit/test_cell.cpp +++ b/tests/unit/test_cell.cpp @@ -224,7 +224,7 @@ TEST(cell, clone) // make clone - cell d(clone_cell, c); + cell d(c); // check equality diff --git a/tests/unit/test_domain_decomposition.cpp b/tests/unit/test_domain_decomposition.cpp index c6cf69fa..2bafbf40 100644 --- a/tests/unit/test_domain_decomposition.cpp +++ b/tests/unit/test_domain_decomposition.cpp @@ -38,22 +38,6 @@ namespace { cell_kind::cable1d_neuron; } - cell_size_type num_sources(cell_gid_type) const override { return 0; } - cell_size_type num_targets(cell_gid_type) const override { return 0; } - cell_size_type num_probes(cell_gid_type) const override { return 0; } - - std::vector<cell_connection> connections_on(cell_gid_type) const override { - return {}; - } - - std::vector<event_generator_ptr> event_generators(cell_gid_type) const override { - return {}; - } - - probe_info get_probe(cell_member_type) const override { - throw std::logic_error("no probes"); - } - private: cell_size_type size_; }; diff --git a/tests/validation/validate_ball_and_stick.cpp b/tests/validation/validate_ball_and_stick.cpp index b5fd2290..7f804268 100644 --- a/tests/validation/validate_ball_and_stick.cpp +++ b/tests/validation/validate_ball_and_stick.cpp @@ -2,7 +2,6 @@ #include <cell.hpp> #include <common_types.hpp> -#include <fvm_multicell.hpp> #include <load_balance.hpp> #include <hardware/node_info.hpp> #include <hardware/gpu.hpp> diff --git a/tests/validation/validate_compartment_policy.cpp b/tests/validation/validate_compartment_policy.cpp index 75d5ee0d..c9ee4d36 100644 --- a/tests/validation/validate_compartment_policy.cpp +++ b/tests/validation/validate_compartment_policy.cpp @@ -5,7 +5,6 @@ #include <common_types.hpp> #include <cell.hpp> -#include <fvm_multicell.hpp> #include <model.hpp> #include <recipe.hpp> #include <simple_sampler.hpp> diff --git a/tests/validation/validate_kinetic.cpp b/tests/validation/validate_kinetic.cpp index b0077df1..83cc8d18 100644 --- a/tests/validation/validate_kinetic.cpp +++ b/tests/validation/validate_kinetic.cpp @@ -4,7 +4,6 @@ #include <common_types.hpp> #include <cell.hpp> -#include <fvm_multicell.hpp> #include <hardware/node_info.hpp> #include <hardware/gpu.hpp> #include <load_balance.hpp> diff --git a/tests/validation/validate_soma.cpp b/tests/validation/validate_soma.cpp index 86f8dfd1..8d0e3e8e 100644 --- a/tests/validation/validate_soma.cpp +++ b/tests/validation/validate_soma.cpp @@ -2,7 +2,6 @@ #include <common_types.hpp> #include <cell.hpp> -#include <fvm_multicell.hpp> #include <hardware/gpu.hpp> #include <hardware/node_info.hpp> #include <load_balance.hpp> diff --git a/tests/validation/validate_synapses.cpp b/tests/validation/validate_synapses.cpp index 7b611ca9..31a7bf3f 100644 --- a/tests/validation/validate_synapses.cpp +++ b/tests/validation/validate_synapses.cpp @@ -1,6 +1,5 @@ #include <cell.hpp> #include <cell_group.hpp> -#include <fvm_multicell.hpp> #include <hardware/node_info.hpp> #include <hardware/gpu.hpp> #include <json/json.hpp> -- GitLab