Skip to content

Commit

Permalink
Merge pull request #3326 from otcathatsya/prototype/connection_semantics
Browse files Browse the repository at this point in the history
Exabrainprep Functional Changes
  • Loading branch information
heplesser authored Oct 10, 2024
2 parents 93a4d33 + dce3e21 commit 132c2cc
Show file tree
Hide file tree
Showing 8 changed files with 188 additions and 186 deletions.
310 changes: 155 additions & 155 deletions CMakeLists.txt

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions nestkernel/conn_builder_factory.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class GenericConnBuilderFactory
virtual ~GenericConnBuilderFactory()
{
}
virtual ConnBuilder* create( NodeCollectionPTR,
virtual std::unique_ptr< ConnBuilder > create( NodeCollectionPTR,
NodeCollectionPTR,
const DictionaryDatum&,
const std::vector< DictionaryDatum >& ) const = 0;
Expand All @@ -65,13 +65,13 @@ class ConnBuilderFactory : public GenericConnBuilderFactory

public:
//! create conn builder
ConnBuilder*
std::unique_ptr< ConnBuilder >
create( NodeCollectionPTR sources,
NodeCollectionPTR targets,
const DictionaryDatum& conn_spec,
const std::vector< DictionaryDatum >& syn_specs ) const override
{
return new ConnBuilderType( sources, targets, conn_spec, syn_specs );
return std::make_unique< ConnBuilderType >( sources, targets, conn_spec, syn_specs );
}
};

Expand Down
5 changes: 3 additions & 2 deletions nestkernel/connection_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -350,15 +350,16 @@ nest::ConnectionManager::get_user_set_delay_extrema() const
return user_set_delay_extrema;
}

nest::ConnBuilder*
std::unique_ptr< nest::ConnBuilder >
nest::ConnectionManager::get_conn_builder( const std::string& name,
NodeCollectionPTR sources,
NodeCollectionPTR targets,
const DictionaryDatum& conn_spec,
const std::vector< DictionaryDatum >& syn_specs )
{
const size_t rule_id = connruledict_->lookup( name );
ConnBuilder* cb = connbuilder_factories_.at( rule_id )->create( sources, targets, conn_spec, syn_specs );
std::unique_ptr< ConnBuilder > cb =
connbuilder_factories_.at( rule_id )->create( sources, targets, conn_spec, syn_specs );
assert( cb );
return cb;
}
Expand Down
2 changes: 1 addition & 1 deletion nestkernel/connection_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class ConnectionManager : public ManagerInterface
template < typename ConnBuilder >
void register_conn_builder( const std::string& name );

ConnBuilder* get_conn_builder( const std::string& name,
std::unique_ptr< ConnBuilder > get_conn_builder( const std::string& name,
NodeCollectionPTR sources,
NodeCollectionPTR targets,
const DictionaryDatum& conn_spec,
Expand Down
15 changes: 7 additions & 8 deletions nestkernel/projection_collection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ ProjectionCollection::ProjectionCollection( const ArrayDatum& projections )
auto conn_spec = getValue< DictionaryDatum >( projection_array[ 2 ] );
if ( is_spatial )
{
projections_.emplace_back( new ConnectionClassWrapper_::SpatialBuilderWrapper_( sources, targets, conn_spec ) );
// TODO: delete builder in destructor, or put it in smart pointer
projections_.emplace_back(
std::make_unique< ConnectionClassWrapper_::SpatialBuilderWrapper_ >( sources, targets, conn_spec ) );
post_spatial_connector_creation_checks( conn_spec ); // checks of dictionary access flags
}
else
Expand All @@ -57,13 +57,11 @@ ProjectionCollection::ProjectionCollection( const ArrayDatum& projections )
synapse_params.begin(),
// Lambda expression that handles the conversion of each element.
[]( Token& token ) -> DictionaryDatum { return getValue< DictionaryDatum >( token ); } );

// Need to do the same checks of arguments as in ConnectionManager::connect().
pre_connector_creation_checks( sources, targets, conn_spec, synapse_params );
const auto rule_name = static_cast< const std::string >( ( *conn_spec )[ names::rule ] );
projections_.emplace_back(
kernel().connection_manager.get_conn_builder( rule_name, sources, targets, conn_spec, synapse_params ) );
// TODO: delete builder in destructor, or put it in smart pointer
post_connector_creation_checks( conn_spec, synapse_params ); // checks of dictionary access flags
}
}
Expand Down Expand Up @@ -160,15 +158,16 @@ ProjectionCollection::post_spatial_connector_creation_checks( DictionaryDatum& c
}


ProjectionCollection::ConnectionClassWrapper_::ConnectionClassWrapper_( ConnBuilder* const conn_builder )
: conn_builder_( conn_builder )
ProjectionCollection::ConnectionClassWrapper_::ConnectionClassWrapper_( std::unique_ptr< ConnBuilder > conn_builder )
: conn_builder_( std::move( conn_builder ) )
, spatial_conn_creator_( nullptr )
{
}

ProjectionCollection::ConnectionClassWrapper_::ConnectionClassWrapper_( SpatialBuilderWrapper_* const spatial_builder )
ProjectionCollection::ConnectionClassWrapper_::ConnectionClassWrapper_(
std::unique_ptr< SpatialBuilderWrapper_ > spatial_builder )
: conn_builder_( nullptr )
, spatial_conn_creator_( spatial_builder )
, spatial_conn_creator_( std::move( spatial_builder ) )
{
}

Expand Down
12 changes: 7 additions & 5 deletions nestkernel/projection_collection.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@

#include <vector>

#include "connection_creator.h"
#include "dictutils.h"
#include "nest_datums.h"
#include "connection_creator.h"
#include "spatial.h"


Expand Down Expand Up @@ -59,18 +59,20 @@ class ProjectionCollection
ConnectionCreator spatial_builder;
};
ConnectionClassWrapper_() = delete;
ConnectionClassWrapper_( const ConnectionClassWrapper_& ) = delete;
ConnectionClassWrapper_( ConnectionClassWrapper_&& ) = default;
// Regular connections
ConnectionClassWrapper_( ConnBuilder* const );
ConnectionClassWrapper_( std::unique_ptr< ConnBuilder > );
// Spatial connections
ConnectionClassWrapper_( SpatialBuilderWrapper_* const );
ConnectionClassWrapper_( std::unique_ptr< SpatialBuilderWrapper_ > );

void connect();

private:
// Regular connections
ConnBuilder* const conn_builder_;
std::unique_ptr< ConnBuilder > conn_builder_;
// Spatial connections
SpatialBuilderWrapper_* const spatial_conn_creator_;
std::unique_ptr< SpatialBuilderWrapper_ > spatial_conn_creator_;
};

std::vector< ConnectionClassWrapper_ > projections_;
Expand Down
4 changes: 1 addition & 3 deletions nestkernel/sp_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ SPManager::disconnect( NodeCollectionPTR sources,
}
}

ConnBuilder* cb = nullptr;
std::unique_ptr< ConnBuilder > cb = nullptr;
conn_spec->clear_access_flags();
syn_spec->clear_access_flags();

Expand Down Expand Up @@ -275,8 +275,6 @@ SPManager::disconnect( NodeCollectionPTR sources,
// Set flag before calling cb->disconnect() in case exception is thrown after some connections have been removed.
kernel().connection_manager.set_connections_have_changed();
cb->disconnect();

delete cb;
}

void
Expand Down
20 changes: 11 additions & 9 deletions pynest/examples/Connection_semantics_prototype_examples.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,7 @@
"metadata": {},
"source": [
"### Generate projections iteratively, with duplicated projections\n",
"By cloning a Projection, one can iteratively generate similar projections, where only a few parameters are changing."
"To iteratively modify a Projection, ideally wrap a projection with `itertools.partial`."
]
},
{
Expand Down Expand Up @@ -575,19 +575,21 @@
}
],
"source": [
"import itertools\n",
"from functools import partial\n",
"\n",
"nest.ResetKernel()\n",
"n1 = nest.Create('iaf_psc_alpha', 3)\n",
"n2 = nest.Create('iaf_psc_exp', 3)\n",
"\n",
"synapse = nest.synapsemodels.static(weight=0.5, delay=0.7)\n",
"base_projection = nest.FixedTotalNumber(source=n1, target=n2,\n",
" N=5,\n",
" syn_spec=synapse)\n",
"for s, t in ((n1, n2), (n1, n1), (n2, n2), (n2, n1)):\n",
" p = base_projection.clone() # Create a copy of the base Projection.\n",
" p.source = s # Change some parameters\n",
" p.target = t # of the copied Projection.\n",
" nest.Connect(p) # Add the copy to the buffer.\n",
"base_projection = nest.FixedTotalNumber(N=5, syn_spec=synapse)\n",
"create_projection = partial(nest.FixedTotalNumber, N=5, syn_spec=synapse) # create prototype\n",
"\n",
"for source, target in itertools.product((n1, n2), repeat=2): # itertools.product to map out all combinations\n",
" projection = create_projection(source=source, target=target) # modify a few parameters each time\n",
" nest.Connect(projection)\n",
"\n",
"nest.BuildNetwork()\n",
"\n",
"conns = nest.GetConnections()\n",
Expand Down

0 comments on commit 132c2cc

Please sign in to comment.