Skip to content

Commit d7fcb78

Browse files
committed
Got rid of some shared files. Made so projections structure is similar to populations structure: KasperskyLab#121
1 parent ecaa73d commit d7fcb78

24 files changed

Lines changed: 232 additions & 279 deletions

File tree

knp/backends/cpu/cpu-library/include/knp/backends/cpu-library/impl/populations/altai/impl.h

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,20 @@
1919
*/
2020
#pragma once
2121

22+
#include <knp/core/population.h>
23+
2224
#include <limits>
2325

24-
#include "shared.h"
2526

2627
namespace knp::backends::cpu::populations::impl::altai
2728
{
2829

30+
/**
31+
* @brief AltAI neuron shortcut.
32+
*/
33+
using AltaiNeuron = knp::neuron_traits::AltAILIF;
34+
35+
2936
inline void calculate_pre_impact_single_neuron_state_impl(knp::neuron_traits::neuron_parameters<AltaiNeuron> &neuron)
3037
{
3138
neuron.potential_ = std::round(neuron.potential_);
@@ -35,6 +42,7 @@ inline void calculate_pre_impact_single_neuron_state_impl(knp::neuron_traits::ne
3542
neuron.pre_impact_potential_ = neuron.potential_;
3643
}
3744

45+
3846
inline void impact_neuron_impl(
3947
knp::neuron_traits::neuron_parameters<knp::neuron_traits::AltAILIF> &neuron,
4048
const knp::core::messaging::SynapticImpact &impact, bool is_forcing)
@@ -67,20 +75,18 @@ inline void impact_neuron_impl(
6775

6876
inline bool calculate_post_impact_single_neuron_state_impl(knp::neuron_traits::neuron_parameters<AltaiNeuron> &neuron)
6977
{
70-
{ // Potential leak.
71-
// -1 if leak_rev is true and potential < 0, 1 otherwise.
72-
const int sign = (neuron.leak_rev_ && neuron.potential_ < 0) ? -1 : 1;
73-
neuron.potential_ += neuron.potential_leak_ * sign;
74-
}
78+
// -1 if leak_rev is true and potential < 0, 1 otherwise.
79+
const int sign = (neuron.leak_rev_ && neuron.potential_ < 0) ? -1 : 1;
80+
neuron.potential_ += neuron.potential_leak_ * sign;
7581

7682
bool spiked = false;
7783
if (neuron.activity_time_ > 0)
7884
{
79-
neuron.activity_time_--;
85+
--neuron.activity_time_;
8086
}
8187
else if (neuron.activity_time_ < 0)
8288
{
83-
neuron.activity_time_++;
89+
++neuron.activity_time_;
8490
}
8591

8692
if (neuron.activity_time_ == 0)

knp/backends/cpu/cpu-library/include/knp/backends/cpu-library/impl/populations/altai/interface.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ inline void impact_neuron_interface<altai::AltaiNeuron>(
5555
altai::impact_neuron_impl(neuron, impact, is_forcing);
5656
}
5757

58+
5859
/**
5960
* @brief Calculate post impact state of single neuron.
6061
* @param neuron Neuron.
@@ -67,6 +68,7 @@ inline bool calculate_post_impact_single_neuron_state_interface<altai::AltaiNeur
6768
return altai::calculate_post_impact_single_neuron_state_impl(neuron);
6869
}
6970

71+
7072
/**
7173
* @brief Teach population.
7274
* @param population Population.
@@ -75,13 +77,14 @@ inline bool calculate_post_impact_single_neuron_state_interface<altai::AltaiNeur
7577
* @param step Step.
7678
*/
7779
template <>
78-
inline void teach_population_interface<altai::AltaiNeuron, knp::synapse_traits::DeltaSynapse>(
80+
inline void train_population_interface<altai::AltaiNeuron, knp::synapse_traits::DeltaSynapse>(
7981
knp::core::Population<altai::AltaiNeuron> &population,
8082
std::vector<knp::core::Projection<knp::synapse_traits::DeltaSynapse> *> const &projections,
8183
const knp::core::messaging::SpikeMessage &message, knp::core::Step step)
8284
{
8385
}
8486

87+
8588
/**
8689
* @brief Teach population.
8790
* @param population Population.
@@ -90,7 +93,7 @@ inline void teach_population_interface<altai::AltaiNeuron, knp::synapse_traits::
9093
* @param step Step.
9194
*/
9295
template <>
93-
inline void teach_population_interface<altai::AltaiNeuron, knp::synapse_traits::SynapticResourceSTDPDeltaSynapse>(
96+
inline void train_population_interface<altai::AltaiNeuron, knp::synapse_traits::SynapticResourceSTDPDeltaSynapse>(
9497
knp::core::Population<altai::AltaiNeuron> &population,
9598
std::vector<knp::core::Projection<knp::synapse_traits::SynapticResourceSTDPDeltaSynapse> *> const &projections,
9699
const knp::core::messaging::SpikeMessage &message, knp::core::Step step)

knp/backends/cpu/cpu-library/include/knp/backends/cpu-library/impl/populations/altai/shared.h

Lines changed: 0 additions & 32 deletions
This file was deleted.

knp/backends/cpu/cpu-library/include/knp/backends/cpu-library/impl/populations/blifat/impl.h

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
#include "stdp.h"
2525

26+
2627
namespace knp::backends::cpu::populations::impl::blifat
2728
{
2829

@@ -44,6 +45,7 @@ inline void calculate_pre_impact_single_neuron_state_impl(knp::neuron_traits::ne
4445
neuron.pre_impact_potential_ = neuron.potential_;
4546
}
4647

48+
4749
inline void calculate_pre_impact_single_neuron_state_impl(
4850
knp::neuron_traits::neuron_parameters<STDPBlifatNeuron> &neuron)
4951
{
@@ -66,6 +68,7 @@ inline void calculate_pre_impact_single_neuron_state_impl(
6668
neuron.pre_impact_potential_ = neuron.potential_;
6769
}
6870

71+
6972
inline void impact_neuron_impl(
7073
knp::neuron_traits::neuron_parameters<BlifatNeuron> &neuron, const knp::core::messaging::SynapticImpact &impact,
7174
bool is_forcing)
@@ -87,8 +90,12 @@ inline void impact_neuron_impl(
8790
case knp::synapse_traits::OutputType::BLOCKING:
8891
neuron.total_blocking_period_ = static_cast<decltype(neuron.total_blocking_period_)>(impact.impact_value_);
8992
break;
93+
default:
94+
throw std::runtime_error("Unhandled synapse type.");
9095
}
9196
}
97+
98+
9299
inline void impact_neuron_impl(
93100
knp::neuron_traits::neuron_parameters<STDPBlifatNeuron> &neuron, const knp::core::messaging::SynapticImpact &impact,
94101
bool is_forcing)
@@ -100,6 +107,7 @@ inline void impact_neuron_impl(
100107
}
101108
}
102109

110+
103111
inline bool calculate_post_impact_single_neuron_state_impl(knp::neuron_traits::neuron_parameters<BlifatNeuron> &neuron)
104112
{
105113
bool spike = false;
@@ -111,9 +119,10 @@ inline bool calculate_post_impact_single_neuron_state_impl(knp::neuron_traits::n
111119
bool was_negative = neuron.total_blocking_period_ < 0;
112120
// If it is negative, increase by 1.
113121
neuron.total_blocking_period_ += was_negative;
114-
// If it is now zero, but was negative before, increase it to max, else leave it as is.
115-
neuron.total_blocking_period_ +=
116-
std::numeric_limits<int64_t>::max() * ((neuron.total_blocking_period_ == 0) && was_negative);
122+
if ((neuron.total_blocking_period_ == 0) && was_negative)
123+
{
124+
neuron.total_blocking_period_ = std::numeric_limits<int64_t>::max();
125+
}
117126
}
118127
else
119128
{

knp/backends/cpu/cpu-library/include/knp/backends/cpu-library/impl/populations/blifat/interface.h

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
#include "impl.h"
2828

29+
2930
namespace knp::backends::cpu::populations::impl
3031
{
3132

@@ -40,6 +41,7 @@ inline void calculate_pre_impact_single_neuron_state_interface<blifat::BlifatNeu
4041
blifat::calculate_pre_impact_single_neuron_state_impl(neuron);
4142
}
4243

44+
4345
/**
4446
* @brief Calculate pre impact state of single neuron.
4547
* @param neuron Neuron.
@@ -51,6 +53,7 @@ inline void calculate_pre_impact_single_neuron_state_interface<blifat::STDPBlifa
5153
blifat::calculate_pre_impact_single_neuron_state_impl(neuron);
5254
}
5355

56+
5457
/**
5558
* @brief Impact neuron.
5659
* @param neuron Neuron.
@@ -65,6 +68,7 @@ inline void impact_neuron_interface<blifat::BlifatNeuron>(
6568
blifat::impact_neuron_impl(neuron, impact, is_forcing);
6669
}
6770

71+
6872
/**
6973
* @brief Impact neuron.
7074
* @param neuron Neuron.
@@ -79,6 +83,7 @@ inline void impact_neuron_interface<blifat::STDPBlifatNeuron>(
7983
blifat::impact_neuron_impl(neuron, impact, is_forcing);
8084
}
8185

86+
8287
/**
8388
* @brief Calculate post impact state of single neuron.
8489
* @param neuron Neuron.
@@ -91,6 +96,7 @@ inline bool calculate_post_impact_single_neuron_state_interface<blifat::BlifatNe
9196
return blifat::calculate_post_impact_single_neuron_state_impl(neuron);
9297
}
9398

99+
94100
/**
95101
* @brief Calculate post impact state of single neuron.
96102
* @param neuron Neuron.
@@ -103,6 +109,7 @@ inline bool calculate_post_impact_single_neuron_state_interface<blifat::STDPBlif
103109
return blifat::calculate_post_impact_single_neuron_state_impl(neuron);
104110
}
105111

112+
106113
/**
107114
* @brief Teach population.
108115
* @param population Population.
@@ -111,13 +118,14 @@ inline bool calculate_post_impact_single_neuron_state_interface<blifat::STDPBlif
111118
* @param step Step.
112119
*/
113120
template <>
114-
inline void teach_population_interface<blifat::BlifatNeuron, knp::synapse_traits::DeltaSynapse>(
121+
inline void train_population_interface<blifat::BlifatNeuron, knp::synapse_traits::DeltaSynapse>(
115122
knp::core::Population<blifat::BlifatNeuron> &population,
116123
std::vector<knp::core::Projection<knp::synapse_traits::DeltaSynapse> *> const &projections,
117124
const knp::core::messaging::SpikeMessage &message, knp::core::Step step)
118125
{
119126
}
120127

128+
121129
/**
122130
* @brief Teach population.
123131
* @param population Population.
@@ -126,13 +134,14 @@ inline void teach_population_interface<blifat::BlifatNeuron, knp::synapse_traits
126134
* @param step Step.
127135
*/
128136
template <>
129-
inline void teach_population_interface<blifat::BlifatNeuron, knp::synapse_traits::SynapticResourceSTDPDeltaSynapse>(
137+
inline void train_population_interface<blifat::BlifatNeuron, knp::synapse_traits::SynapticResourceSTDPDeltaSynapse>(
130138
knp::core::Population<blifat::BlifatNeuron> &population,
131139
std::vector<knp::core::Projection<knp::synapse_traits::SynapticResourceSTDPDeltaSynapse> *> const &projections,
132140
const knp::core::messaging::SpikeMessage &message, knp::core::Step step)
133141
{
134142
}
135143

144+
136145
/**
137146
* @brief Teach population.
138147
* @param population Population.
@@ -141,13 +150,12 @@ inline void teach_population_interface<blifat::BlifatNeuron, knp::synapse_traits
141150
* @param step Step.
142151
*/
143152
template <>
144-
inline void teach_population_interface<blifat::STDPBlifatNeuron, knp::synapse_traits::SynapticResourceSTDPDeltaSynapse>(
153+
inline void train_population_interface<blifat::STDPBlifatNeuron, knp::synapse_traits::SynapticResourceSTDPDeltaSynapse>(
145154
knp::core::Population<blifat::STDPBlifatNeuron> &population,
146155
std::vector<knp::core::Projection<knp::synapse_traits::SynapticResourceSTDPDeltaSynapse> *> const &projections,
147156
const knp::core::messaging::SpikeMessage &message, knp::core::Step step)
148157
{
149-
blifat::teach_population_impl(population, projections, message, step);
158+
blifat::train_population_impl(population, projections, message, step);
150159
}
151160

152-
153161
} //namespace knp::backends::cpu::populations::impl

0 commit comments

Comments
 (0)