Iterator for NodeCollections. More...
#include <node_collection.h>
Public Types | |
| enum class | NCIteratorKind { GLOBAL , RANK_LOCAL , THREAD_LOCAL , END } |
| Markers for kind of iterator, required by composite_update_indices_(). More... | |
| using | iterator_category = std::forward_iterator_tag |
| using | difference_type = long |
| using | value_type = NodeIDTriple |
| using | pointer = NodeIDTriple* |
| using | reference = NodeIDTriple& |
Public Member Functions | |
| nc_const_iterator (const nc_const_iterator &nci)=default | |
| std::pair< size_t, size_t > | get_part_offset () const |
| NodeIDTriple | operator* () const |
| bool | operator== (const nc_const_iterator &rhs) const |
| bool | operator!= (const nc_const_iterator &rhs) const |
| bool | operator< (const nc_const_iterator &rhs) const |
| bool | operator<= (const nc_const_iterator &rhs) const |
| bool | operator> (const nc_const_iterator &rhs) const |
| bool | operator>= (const nc_const_iterator &rhs) const |
| nc_const_iterator & | operator++ () |
| nc_const_iterator | operator++ (int) |
| nc_const_iterator & | operator+= (const size_t) |
| nc_const_iterator | operator+ (const size_t) const |
| size_t | get_step_size () const |
| Return step size of iterator. | |
Private Member Functions | |
| nc_const_iterator (NodeCollectionPTR collection_ptr, const NodeCollectionPrimitive &collection, size_t offset, size_t stride, NCIteratorKind kind=NCIteratorKind::GLOBAL) | |
| Create safe iterator for NodeCollectionPrimitive. | |
| nc_const_iterator (NodeCollectionPTR collection_ptr, const NodeCollectionComposite &collection, size_t part, size_t offset, size_t stride, NCIteratorKind kind=NCIteratorKind::GLOBAL) | |
| Create safe iterator for NodeCollectionComposite. | |
| size_t | find_next_within_part_ (size_t n) const |
| Return element_idx_ for next element if within part. | |
| void | advance_global_iter_to_new_part_ (size_t n) |
| Advance composite GLOBAL iterator by n elements, taking stride into account. | |
| void | advance_local_iter_to_new_part_ (size_t n) |
| Advance composite {THREAD,RANK}_LOCAL iterator by n elements, taking stride into account. | |
Private Attributes | |
| NodeCollectionPTR | coll_ptr_ |
| pointer to keep node collection alive, see note | |
| size_t | element_idx_ |
| index into (current) primitive node collection | |
| size_t | part_idx_ |
| index into parts vector of composite collection | |
| size_t | step_ |
| internal step also accounting for stepping over rank/thread | |
| const NCIteratorKind | kind_ |
| whether to iterate over all elements or rank/thread specific | |
| const size_t | rank_or_vp_ |
| rank or vp iterator is bound to | |
| NodeCollectionPrimitive const *const | primitive_collection_ |
| Pointer to primitive collection to iterate over. Zero if iterator is for composite collection. | |
| NodeCollectionComposite const *const | composite_collection_ |
| Pointer to composite collection to iterate over. Zero if iterator is for primitive collection. | |
Friends | |
| class | NodeCollectionPrimitive |
| class | NodeCollectionComposite |
Iterator for NodeCollections.
This iterator can iterate over primitive and composite NodeCollections. Behavior is determined by the constructor used to create the iterator.
In the following discussion, we use the following terms:
Note further that
Constructing the end() iterator is costly, especially for composite NCs. In classic for loops including ... ; it < nc->end() ; ..., the end() iterator is constructed anew for every iteration, even if nc is const (tested with AppleClang 15 and GCC 13, -std=c++17). Therefore, either construct the end() iterator first
const auto end_it = nc->end(); for ( auto it = nc->thread_local_begin() ; it < end_it ; ++it )
or use range iteration (global iteration only, uses != to compare iterators)
for ( const auto& nc_elem : nc )
For NodeCollectionPrimitive, creating a rank/thread-local begin() iterator and stepping is straightforward:
Upon construction of a NodeCollectionComposite instance, we need to determine its first and last entries. We can distinguish three different cases, mapping to three different constructors.
If nc is a primivtive collection and we slice as nc[start:end:stride], we only have a single part and
We receive a list of parts, which are all primitive collections. The new collection consists of all elements of all parts.
The collection now begins with the first element of the first part and ends with the last element of the last part, i.e.,
Here, we have two subcases:
If nc is already sliced, we can only pick a single element given by start and end==start+1. We proceed as follows:
We slice as nc[start:end:stride] but are guaranteed that all elements in parts_ are in nc and all parts have stride 1. We thus proceed as follows:
We further construct a vector of the cumulative sizes of all parts and a vector containing for each part the part-local index to the first element of each part that belongs to the node collection, or invalid_index if there is no element in the part. Note: The cumulative sizes include all elements of the parts, including elements before first_elem_ or after last_elem_, but they do not include elements in parts before first_part_ or after last_part_.
Thus, if a composite NC is sliced, the same stride applies to all parts; by definition, also the same period applies to all parts
We now need to distinguish between global and local iteration.
For global iteration, we can ignore phase relations. We need to take into account slicing and possible gaps between parts, as well as the possibility, for stride > 1, that parts contain no elements. Consider the following node collection with several parts; vertical bars indicate borders between parts ( to construct such a node collection, join collections with different neuron models). In the table, (PartIdx, ElemIdx) show the iterator values for iterators pointing to the corresponding element in the collection, while PythonIdx is the index that applies for slicing from the Python level. Different slices are shown in the final lines of the table, with asterisks marking the elements belonging to the sliced collection. Note that the second slice does not contain any elements from the first and third parts.
GID 1 2 | 3 4 5 | 6 7 8 | 9 10 11 PartIdx 0 0 | 1 1 1 | 2 2 2 | 3 3 3 ElemIdx 0 1 | 0 1 2 | 0 1 2 | 0 1 2 ---------------------------------------- PythonIdx 0 1 | 2 3 4 | 5 6 7 | 8 9 10 ---------------------------------------- [::3] * | * | * | * [4::5] | * | | * [1:11:3] * | * | * |
The begin() iterator is given by
Rank-local and thread-local iteration work in exactly the same way, just that the period and phase are in one case given by the ranks and in the other by the threads. Thus, we discuss the algorithms only once.
For period > 1, the relation of phase to position in a given part can differ from part to part. Consider the following node collection in a simulation with four threads (one MPI process). The table shows the GID of the neuron, its thread (phase), the part_idx and the element_idx of a iterator pointing to the element. Finally, elements that belong to thread 1 and 2 respectively, are marked with an asterisk (only 11 elements shown for brevity):
GID 1 2 3 4 5 6 7 8 9 10 11 PartIdx 0 0 0 0 0 0 0 0 0 0 0 ElemIdx 0 1 2 3 4 5 6 7 8 9 10 Phase 1 2 3 0 1 2 3 0 1 2 3 ------------------------------------------- On thr 1 * * * On thr 2 * * *
The phase relation here is phase == element_idx % period + 1, where 1 is the phase of the node with GID 1.
Now consider a new node collection constructed by
nc2 = nc[:5] + nc[7:]
This yields a node collection with two parts, marked with the vertical line (note that nodes 6 and 7 are not included). We consider it once it its entirety and once sliced as nc2[::3]. The 1stInPt line marks the elements indexed by the first_in_part_ vector.
GID 1 2 3 4 5 | 8 9 10 11 12 13 14 15 16 17 18 19 20 21 PartIdx 0 0 0 0 0 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ElemIdx 0 1 2 3 4 | 0 1 2 3 4 5 6 7 8 9 10 11 12 13 Phase 1 2 3 0 1 | 0 1 2 3 0 1 2 3 0 1 2 3 0 1 ------------------------------------------------------------------------------------- All 1stInPt # | # All thr 0 * | * * * * All thr 1 * * | * * * * All thr 2 * | * * * All thr 3 * | * * * ------------------------------------------------------------------------------------- [::3] 1stInPt # | # [::3] thr 0 * | * [::3] thr 1 * | * * [::3] thr 2 | * [::3] thr 3 | *
The phase relation is the same as above for the first part, but for the second part, the phase relation is phase == element_idx % period + 0, where 0 is the phase of the node with GID 8, the first node in the second part.
Here, we may need to move through several parts to find the first valid entry. We proceed as follows:
We further have
| using nest::nc_const_iterator::difference_type = long |
| using nest::nc_const_iterator::iterator_category = std::forward_iterator_tag |
|
strong |
|
explicitprivate |
Create safe iterator for NodeCollectionPrimitive.
| collection_ptr | smart pointer to collection to keep collection alive |
| collection | Collection to iterate over |
| offset | Index of collection element iterator points to |
| stride | Step for skipping due to e.g. slicing; does NOT include stepping over rank/thread |
References coll_ptr_, composite_collection_, element_idx_, FULL_LOGGING_ONLY, nest::invalid_thread, nest::kernel(), kind_, NodeCollectionPrimitive, part_idx_, primitive_collection_, rank_or_vp_, nest::NodeCollectionPrimitive::size(), and step_.
Referenced by nc_const_iterator(), operator!=(), operator+(), operator++(), operator<(), operator<=(), operator==(), operator>(), and operator>=().
|
explicitprivate |
Create safe iterator for NodeCollectionComposite.
| collection_ptr | smart pointer to collection to keep collection alive |
| collection | Collection to iterate over |
| part | Index of part of collection iterator points to |
| offset | Index of element in NC part that iterator points to |
| stride | Step for skipping due to e.g. slicing; does NOT include stepping over rank/thread |
References coll_ptr_, composite_collection_, element_idx_, FULL_LOGGING_ONLY, nest::invalid_thread, nest::kernel(), kind_, NodeCollectionComposite, part_idx_, nest::NodeCollectionComposite::parts_, primitive_collection_, rank_or_vp_, and step_.
|
default |
|
private |
Advance composite GLOBAL iterator by n elements, taking stride into account.
References composite_collection_, element_idx_, and part_idx_.
Referenced by operator+=().
|
private |
Advance composite {THREAD,RANK}_LOCAL iterator by n elements, taking stride into account.
References composite_collection_, element_idx_, FULL_LOGGING_ONLY, nest::MPIManager::get_num_processes(), nest::VPManager::get_num_virtual_processes(), nest::MPIManager::get_rank(), nest::NodeCollectionComposite::gid_to_rank_(), nest::NodeCollectionComposite::gid_to_vp_(), nest::kernel(), kind_, nest::KernelManager::mpi_manager, part_idx_, RANK_LOCAL, THREAD_LOCAL, nest::VPManager::thread_to_vp(), and nest::KernelManager::vp_manager.
Referenced by operator+=().
|
private |
Return element_idx_ for next element if within part.
Returns current element_idx_ otherwise.
References composite_collection_, element_idx_, part_idx_, primitive_collection_, and step_.
Referenced by operator+=().
|
inline |
References element_idx_, and part_idx_.
|
inline |
Return step size of iterator.
For thread- and rank-local iterators, this takes into account stepping over all VPs / ranks. For stepped node collections, this takes also stepping into account. Thus if we have a thread-local iterator in a simulation with 4 VPs and a node-collection step of 3, then the iterator's step is 12.
References step_.
|
inline |
| NodeIDTriple nest::nc_const_iterator::operator* | ( | ) | const |
References composite_collection_, element_idx_, FULL_LOGGING_ONLY, nest::kernel(), nest::NodeIDTriple::model_id, nest::NodeIDTriple::nc_index, nest::NodeIDTriple::node_id, part_idx_, and primitive_collection_.
|
inline |
|
inline |
|
inline |
|
inline |
References advance_global_iter_to_new_part_(), advance_local_iter_to_new_part_(), element_idx_, END, find_next_within_part_(), GLOBAL, kind_, and primitive_collection_.
|
inline |
References element_idx_, nc_const_iterator(), and part_idx_.
|
inline |
|
inline |
References element_idx_, nc_const_iterator(), and part_idx_.
|
inline |
|
inline |
|
friend |
References NodeCollectionComposite.
Referenced by nc_const_iterator(), and NodeCollectionComposite.
|
friend |
References NodeCollectionPrimitive.
Referenced by nc_const_iterator(), and NodeCollectionPrimitive.
|
private |
pointer to keep node collection alive, see note
Referenced by nc_const_iterator(), and nc_const_iterator().
|
private |
Pointer to composite collection to iterate over. Zero if iterator is for primitive collection.
Referenced by advance_global_iter_to_new_part_(), advance_local_iter_to_new_part_(), find_next_within_part_(), nc_const_iterator(), nc_const_iterator(), and operator*().
|
private |
index into (current) primitive node collection
Referenced by advance_global_iter_to_new_part_(), advance_local_iter_to_new_part_(), find_next_within_part_(), get_part_offset(), nc_const_iterator(), nc_const_iterator(), operator*(), operator+=(), operator<(), and operator==().
|
private |
whether to iterate over all elements or rank/thread specific
Referenced by advance_local_iter_to_new_part_(), nc_const_iterator(), nc_const_iterator(), and operator+=().
|
private |
index into parts vector of composite collection
Referenced by advance_global_iter_to_new_part_(), advance_local_iter_to_new_part_(), find_next_within_part_(), get_part_offset(), nc_const_iterator(), nc_const_iterator(), operator*(), operator<(), and operator==().
|
private |
Pointer to primitive collection to iterate over. Zero if iterator is for composite collection.
Referenced by find_next_within_part_(), nc_const_iterator(), nc_const_iterator(), operator*(), and operator+=().
|
private |
rank or vp iterator is bound to
Referenced by nc_const_iterator(), and nc_const_iterator().
|
private |
internal step also accounting for stepping over rank/thread
Referenced by find_next_within_part_(), get_step_size(), nc_const_iterator(), and nc_const_iterator().