benchmark_exactitude_aqss.cpp 14.2 KB
Newer Older
1
2
3
4
// Copyright (c) 2020 INRA Distributed under the Boost Software License,
// Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)

5
6
#include <hayai.hpp>

7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <irritator/core.hpp>

#include <boost/ut.hpp>

#include <fmt/format.h>

#include <cstdio>

#include <fstream>

using namespace std;

struct file_output
{
21
22
    std::FILE* os = nullptr;
    std::string filename;
23

24
25
26
27
28
29
30
    file_output(const std::string_view name)
      : filename(name)
    {
        os = std::fopen(filename.c_str(), "w");
    }

    ~file_output()
31
32
33
34
35
    {
        if (os)
            std::fclose(os);
    }

36
    void operator()(const irt::observer& obs,
37
                    const irt::dynamics_type /*type*/,
38
                    const irt::time t,
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
                    const irt::observer::status s) noexcept
    {
        switch (s) {
        case irt::observer::status::initialize:
            fmt::print(os, "t,{}\n", obs.name.c_str());
            break;

        case irt::observer::status::run:
            fmt::print(os, "{},{}\n", t, obs.msg.real[0]);
            break;

        case irt::observer::status::finalize:
            break;
        }
    }
};
55

Gauthier Quesnel's avatar
Gauthier Quesnel committed
56
57
58
59
60
61
62
63
struct neuron
{
    irt::dynamics_id sum;
    irt::dynamics_id integrator;
    irt::dynamics_id quantifier;
    irt::dynamics_id constant;
    irt::dynamics_id cross;
    irt::dynamics_id constant_cross;
64
65
};

Gauthier Quesnel's avatar
Gauthier Quesnel committed
66
67
struct neuron
make_neuron(irt::simulation* sim, double quantum) noexcept
68
{
Gauthier Quesnel's avatar
Gauthier Quesnel committed
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
    using namespace boost::ut;
    double tau_lif = 10;
    double Vr_lif = 0.0;
    double Vt_lif = 10.0;

    auto& sum_lif = sim->adder_2_models.alloc();
    auto& integrator_lif = sim->integrator_models.alloc();
    auto& quantifier_lif = sim->quantifier_models.alloc();
    auto& constant_lif = sim->constant_models.alloc();
    auto& constant_cross_lif = sim->constant_models.alloc();
    auto& cross_lif = sim->cross_models.alloc();

    sum_lif.default_input_coeffs[0] = -1.0 / tau_lif;
    sum_lif.default_input_coeffs[1] = 20.0 / tau_lif;

    constant_lif.default_value = 1.0;
    constant_cross_lif.default_value = Vr_lif;

    integrator_lif.default_current_value = 0.0;

    quantifier_lif.default_adapt_state = irt::quantifier::adapt_state::possible;
    quantifier_lif.default_zero_init_offset = true;
    quantifier_lif.default_step_size = quantum;
    quantifier_lif.default_past_length = 3;

    cross_lif.default_threshold = Vt_lif;

    sim->alloc(sum_lif, sim->adder_2_models.get_id(sum_lif));
    sim->alloc(integrator_lif, sim->integrator_models.get_id(integrator_lif));
    sim->alloc(quantifier_lif, sim->quantifier_models.get_id(quantifier_lif));
    sim->alloc(constant_lif, sim->constant_models.get_id(constant_lif));
    sim->alloc(cross_lif, sim->cross_models.get_id(cross_lif));
    sim->alloc(constant_cross_lif,
               sim->constant_models.get_id(constant_cross_lif));

    struct neuron neuron_model = {
        sim->adder_2_models.get_id(sum_lif),
        sim->integrator_models.get_id(integrator_lif),
        sim->quantifier_models.get_id(quantifier_lif),
        sim->constant_models.get_id(constant_lif),
        sim->cross_models.get_id(cross_lif),
        sim->constant_models.get_id(constant_cross_lif),
    };

    // Connections
    expect(sim->connect(quantifier_lif.y[0], integrator_lif.x[0]) ==
           irt::status::success);
    expect(sim->connect(sum_lif.y[0], integrator_lif.x[1]) ==
           irt::status::success);
    expect(sim->connect(cross_lif.y[0], integrator_lif.x[2]) ==
           irt::status::success);
    expect(sim->connect(cross_lif.y[0], quantifier_lif.x[0]) ==
           irt::status::success);
    expect(sim->connect(cross_lif.y[0], sum_lif.x[0]) == irt::status::success);
    expect(sim->connect(integrator_lif.y[0], cross_lif.x[0]) ==
           irt::status::success);
    expect(sim->connect(integrator_lif.y[0], cross_lif.x[2]) ==
           irt::status::success);
    expect(sim->connect(constant_cross_lif.y[0], cross_lif.x[1]) ==
           irt::status::success);
    expect(sim->connect(constant_lif.y[0], sum_lif.x[1]) ==
           irt::status::success);
    return neuron_model;
132
133
}

Gauthier Quesnel's avatar
Gauthier Quesnel committed
134
135
void
lif_benchmark(double simulation_duration, double quantum)
136
{
Gauthier Quesnel's avatar
Gauthier Quesnel committed
137
    using namespace boost::ut;
138
139
140
141

    irt::simulation sim;
    expect(irt::is_success(sim.init(2600lu, 40000lu)));

Gauthier Quesnel's avatar
Gauthier Quesnel committed
142
    struct neuron neuron_model = make_neuron(&sim, quantum);
143
144

    irt::time t = 0.0;
Gauthier Quesnel's avatar
Gauthier Quesnel committed
145
146
147
    std::string file_name = "output_lif_aqss_sd_" +
                            std::to_string(simulation_duration) + "_q_" +
                            std::to_string(quantum) + ".csv";
148
149
150
    file_output fo_a(file_name.c_str());
    expect(fo_a.os != nullptr);

151
    auto& obs_a = sim.observers.alloc("A", fo_a);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
152
153
154
    sim.observe(sim.models.get(
                  sim.qss2_integrator_models.get(neuron_model.integrator).id),
                obs_a);
155
156
157
158
159
160
161
162
163
164

    expect(irt::status::success == sim.initialize(t));

    do {

        irt::status st = sim.run(t);
        expect(st == irt::status::success);

    } while (t < simulation_duration);
}
Gauthier Quesnel's avatar
Gauthier Quesnel committed
165
166
167
168
169
170
171
172
173
void
izhikevich_benchmark(double simulation_duration,
                     double quantum,
                     double a,
                     double b,
                     double c,
                     double d,
                     double I,
                     double vini)
174
{
Gauthier Quesnel's avatar
Gauthier Quesnel committed
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
    using namespace boost::ut;
    irt::simulation sim;

    expect(irt::is_success(sim.init(1000lu, 1000lu)));
    expect(sim.constant_models.can_alloc(3));
    expect(sim.adder_2_models.can_alloc(3));
    expect(sim.adder_4_models.can_alloc(1));
    expect(sim.mult_2_models.can_alloc(1));
    expect(sim.integrator_models.can_alloc(2));
    expect(sim.quantifier_models.can_alloc(2));
    expect(sim.cross_models.can_alloc(2));

    auto& constant = sim.constant_models.alloc();
    auto& constant2 = sim.constant_models.alloc();
    auto& constant3 = sim.constant_models.alloc();
    auto& sum_a = sim.adder_2_models.alloc();
    auto& sum_b = sim.adder_2_models.alloc();
    auto& sum_c = sim.adder_4_models.alloc();
    auto& sum_d = sim.adder_2_models.alloc();
    auto& product = sim.mult_2_models.alloc();
    auto& integrator_a = sim.integrator_models.alloc();
    auto& integrator_b = sim.integrator_models.alloc();
    auto& quantifier_a = sim.quantifier_models.alloc();
    auto& quantifier_b = sim.quantifier_models.alloc();
    auto& cross = sim.cross_models.alloc();
    auto& cross2 = sim.cross_models.alloc();

    double vt = 30.0;

    constant.default_value = 1.0;
    constant2.default_value = c;
    constant3.default_value = I;

    cross.default_threshold = vt;
    cross2.default_threshold = vt;

    integrator_a.default_current_value = vini;

    quantifier_a.default_adapt_state = irt::quantifier::adapt_state::possible;
    quantifier_a.default_zero_init_offset = true;
    quantifier_a.default_step_size = quantum;
    quantifier_a.default_past_length = 3;

    integrator_b.default_current_value = 0.0;

    quantifier_b.default_adapt_state = irt::quantifier::adapt_state::possible;
    quantifier_b.default_zero_init_offset = true;
    quantifier_b.default_step_size = quantum;
    quantifier_b.default_past_length = 3;

    product.default_input_coeffs[0] = 1.0;
    product.default_input_coeffs[1] = 1.0;

    sum_a.default_input_coeffs[0] = 1.0;
    sum_a.default_input_coeffs[1] = -1.0;
    sum_b.default_input_coeffs[0] = -a;
    sum_b.default_input_coeffs[1] = a * b;
    sum_c.default_input_coeffs[0] = 0.04;
    sum_c.default_input_coeffs[1] = 5.0;
    sum_c.default_input_coeffs[2] = 140.0;
    sum_c.default_input_coeffs[3] = 1.0;
    sum_d.default_input_coeffs[0] = 1.0;
    sum_d.default_input_coeffs[1] = d;

    expect(sim.models.can_alloc(14));
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
    expect((irt::is_success(
      sim.alloc(constant3, sim.constant_models.get_id(constant3)))) >> fatal);
    expect((irt::is_success(
      sim.alloc(constant, sim.constant_models.get_id(constant)))) >> fatal);
    expect((irt::is_success(
      sim.alloc(constant2, sim.constant_models.get_id(constant2)))) >> fatal);

    expect((
      irt::is_success(sim.alloc(sum_a, sim.adder_2_models.get_id(sum_a)))) >> fatal);
    expect((
      irt::is_success(sim.alloc(sum_b, sim.adder_2_models.get_id(sum_b)))) >> fatal);
    expect((
      irt::is_success(sim.alloc(sum_c, sim.adder_4_models.get_id(sum_c)))) >> fatal);
    expect((
      irt::is_success(sim.alloc(sum_d, sim.adder_2_models.get_id(sum_d)))) >> fatal);

    expect((
      irt::is_success(sim.alloc(product, sim.mult_2_models.get_id(product)))) >> fatal);
    expect((irt::is_success(
      sim.alloc(integrator_a, sim.integrator_models.get_id(integrator_a)))) >> fatal);
    expect((irt::is_success(
      sim.alloc(integrator_b, sim.integrator_models.get_id(integrator_b)))) >> fatal);
    expect((irt::is_success(
      sim.alloc(quantifier_a, sim.quantifier_models.get_id(quantifier_a)))) >> fatal);
    expect((irt::is_success(
      sim.alloc(quantifier_b, sim.quantifier_models.get_id(quantifier_b)))) >> fatal);
    expect((irt::is_success(sim.alloc(cross, sim.cross_models.get_id(cross)))) >> fatal);
    expect((
      irt::is_success(sim.alloc(cross2, sim.cross_models.get_id(cross2)))) >> fatal);

    expect((sim.models.size() == 14_ul) >> fatal);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314

    expect(sim.connect(integrator_a.y[0], cross.x[0]) == irt::status::success);
    expect(sim.connect(constant2.y[0], cross.x[1]) == irt::status::success);
    expect(sim.connect(integrator_a.y[0], cross.x[2]) == irt::status::success);

    expect(sim.connect(cross.y[0], quantifier_a.x[0]) == irt::status::success);
    expect(sim.connect(cross.y[0], product.x[0]) == irt::status::success);
    expect(sim.connect(cross.y[0], product.x[1]) == irt::status::success);
    expect(sim.connect(product.y[0], sum_c.x[0]) == irt::status::success);
    expect(sim.connect(cross.y[0], sum_c.x[1]) == irt::status::success);
    expect(sim.connect(cross.y[0], sum_b.x[1]) == irt::status::success);

    expect(sim.connect(constant.y[0], sum_c.x[2]) == irt::status::success);
    expect(sim.connect(constant3.y[0], sum_c.x[3]) == irt::status::success);

    expect(sim.connect(sum_c.y[0], sum_a.x[0]) == irt::status::success);
    expect(sim.connect(integrator_b.y[0], sum_a.x[1]) == irt::status::success);
    expect(sim.connect(cross2.y[0], sum_a.x[1]) == irt::status::success);
    expect(sim.connect(sum_a.y[0], integrator_a.x[1]) == irt::status::success);
    expect(sim.connect(cross.y[0], integrator_a.x[2]) == irt::status::success);
    expect(sim.connect(quantifier_a.y[0], integrator_a.x[0]) ==
           irt::status::success);

    expect(sim.connect(cross2.y[0], quantifier_b.x[0]) == irt::status::success);
    expect(sim.connect(cross2.y[0], sum_b.x[0]) == irt::status::success);
    expect(sim.connect(quantifier_b.y[0], integrator_b.x[0]) ==
           irt::status::success);
    expect(sim.connect(sum_b.y[0], integrator_b.x[1]) == irt::status::success);

    expect(sim.connect(cross2.y[0], integrator_b.x[2]) == irt::status::success);
    expect(sim.connect(integrator_a.y[0], cross2.x[0]) == irt::status::success);
    expect(sim.connect(integrator_b.y[0], cross2.x[2]) == irt::status::success);
    expect(sim.connect(sum_d.y[0], cross2.x[1]) == irt::status::success);
    expect(sim.connect(integrator_b.y[0], sum_d.x[0]) == irt::status::success);
    expect(sim.connect(constant.y[0], sum_d.x[1]) == irt::status::success);

    std::string file_name =
      "output_izhikevitch_aqss_a_sd_" + std::to_string(simulation_duration) +
      "_q_" + std::to_string(quantum) + "_a_" + std::to_string(a) + "_b_" +
      std::to_string(b) + "_c_" + std::to_string(c) + "_d_" +
      std::to_string(d) + ".csv";
    file_output fo_a(file_name.c_str());
    expect(fo_a.os != nullptr);

315
    auto& obs_a = sim.observers.alloc("A", fo_a);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
316
317
318
319
320
321
322
    file_name = "output_izhikevitch_aqss_b_sd_" +
                std::to_string(simulation_duration) + "_q_" +
                std::to_string(quantum) + "_a_" + std::to_string(a) + "_b_" +
                std::to_string(b) + "_c_" + std::to_string(c) + "_d_" +
                std::to_string(d) + ".csv";
    file_output fo_b(file_name.c_str());
    expect(fo_b.os != nullptr);
323
    auto& obs_b = sim.observers.alloc("B", fo_b);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
324
325
326
327
328
329
330

    sim.observe(sim.models.get(integrator_a.id), obs_a);
    sim.observe(sim.models.get(integrator_b.id), obs_b);

    irt::time t = 0.0;

    expect(irt::status::success == sim.initialize(t));
331
    expect(sim.sched.size() == 14_ul);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
332
333
334
335
336

    do {
        irt::status st = sim.run(t);
        expect(st == irt::status::success);
    } while (t < simulation_duration);
337
};
Gauthier Quesnel's avatar
Gauthier Quesnel committed
338
BENCHMARK_P(LIF, AQSS, 10, 1, (double simulation_duration, double quantum))
339
{
Gauthier Quesnel's avatar
Gauthier Quesnel committed
340
    lif_benchmark(simulation_duration, quantum);
341
}
Gauthier Quesnel's avatar
Gauthier Quesnel committed
342
343
344
345
346
347
348
349
350
351
352
353
BENCHMARK_P(Izhikevich,
            AQSS,
            1,
            1,
            (double simulation_duration,
             double quantum,
             double a,
             double b,
             double c,
             double d,
             double I,
             double vini))
354
{
Gauthier Quesnel's avatar
Gauthier Quesnel committed
355
    izhikevich_benchmark(simulation_duration, quantum, a, b, c, d, I, vini);
356
357
}

Gauthier Quesnel's avatar
Gauthier Quesnel committed
358
BENCHMARK_P_INSTANCE(LIF, AQSS, (1000, 1e-2));
359
// Regular spiking (RS)
Gauthier Quesnel's avatar
Gauthier Quesnel committed
360
361
362
BENCHMARK_P_INSTANCE(Izhikevich,
                     AQSS,
                     (1000, 1e-2, 0.02, 0.2, -65.0, 8.0, 10.0, 0.0));
363
// Intrinsical bursting (IB)
Gauthier Quesnel's avatar
Gauthier Quesnel committed
364
365
366
BENCHMARK_P_INSTANCE(Izhikevich,
                     AQSS,
                     (1000, 1e-2, 0.02, 0.2, -55.0, 4.0, 10.0, 0.0));
367
// Chattering spiking (CH)
Gauthier Quesnel's avatar
Gauthier Quesnel committed
368
369
370
BENCHMARK_P_INSTANCE(Izhikevich,
                     AQSS,
                     (1000, 1e-2, 0.02, 0.2, -50.0, 2.0, 10.0, 0.0));
371
// Fast spiking (FS)
Gauthier Quesnel's avatar
Gauthier Quesnel committed
372
373
374
BENCHMARK_P_INSTANCE(Izhikevich,
                     AQSS,
                     (1000, 1e-2, 0.1, 0.2, -65.0, 2.0, 10.0, 0.0));
375
// Thalamo-Cortical (TC)
Gauthier Quesnel's avatar
Gauthier Quesnel committed
376
377
378
BENCHMARK_P_INSTANCE(Izhikevich,
                     AQSS,
                     (1000, 1e-2, 0.02, 0.25, -65.0, 0.05, 10.0, -87.0));
379
// Rezonator (RZ)
Gauthier Quesnel's avatar
Gauthier Quesnel committed
380
381
382
BENCHMARK_P_INSTANCE(Izhikevich,
                     AQSS,
                     (1000, 1e-2, 0.1, 0.26, -65.0, 2.0, 10.0, -63.0));
383
// Low-threshold spiking (LTS)
Gauthier Quesnel's avatar
Gauthier Quesnel committed
384
385
386
BENCHMARK_P_INSTANCE(Izhikevich,
                     AQSS,
                     (1000, 1e-2, 0.02, 0.25, -65.0, 2.0, 10.0, -63.0));
387
// Problematic (P)
Gauthier Quesnel's avatar
Gauthier Quesnel committed
388
389
390
BENCHMARK_P_INSTANCE(Izhikevich,
                     AQSS,
                     (1000, 1e-2, 0.2, 2, -56.0, -16.0, -99.0, 0.0));
391
392
393
394

int
main()
{
Gauthier Quesnel's avatar
Gauthier Quesnel committed
395
    hayai::ConsoleOutputter consoleOutputter;
396

Gauthier Quesnel's avatar
Gauthier Quesnel committed
397
398
    hayai::Benchmarker::AddOutputter(consoleOutputter);
    hayai::Benchmarker::RunAllTests();
399
}