-
Notifications
You must be signed in to change notification settings - Fork 0
/
LIF-2.cpp
executable file
·452 lines (369 loc) · 10.7 KB
/
LIF-2.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
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
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
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
#include <iostream>
#include <vector>
#include <string>
#include <unordered_map>
#include <deque>
#include <set>
#include <algorithm>
#include <cmath>
#include <stdlib.h>
#include <stdio.h>
#include <fstream>
#include <sstream>
#include <chrono>
#include <algorithm>
#include <random>
#include <tuple>
#include <cmath>
// compiled with
// "#pragma omp parallel for" in front of the main loop of ONE time step
// (YOU CAN'T PARALLELIZE CODE OVER TIME)
// g++ -std=gnu++11 -Ofast -ftree-vectorize -march=native -mavx -fopenmp LIF-2.cpp -o lif
using namespace std;
const double EPSILON = 0.001;
const double dt = 0.01;
const int N = 3000;
const double T = 100;
double refractory_period = 1.0;
int NE = int(N * 0.8);
int NI = N-NE;
vector<double> t;
vector<int> ons;
vector<double> AP;
vector<double> delayed_spike;
const double V_E = 0.0;
const double V_I = -80.0;
const double EL = -65.0;
// critical voltages:
const double Vth = -55.0; // threshold after which an AP is fired, mV
const double Vr = -70.0; // reset voltage (after an AP is fired), mV
const double Vspike = 10.0;
// define neuron types in the network:
vector<int> neur_type_mask;
vector<int> exc_id;
vector<double> tau;
const double tau_ampa = 8.0;
const double tau_nmda = 100.0;
const double tau_gaba = 8.0;
vector<vector<double>> ampa;
vector<vector<double>> nmda;
vector<vector<double>> gaba;
vector<double> in_refractory;
vector<vector<double>> VV;
vector<vector<double>> Ie;
vector<vector<vector<double>>> AMPA;
vector<vector<vector<double>>> NMDA;
vector<vector<vector<double>>> GABA;
vector<double> I_E;
vector<double> I_I;
vector<double> dV;
vector<vector<double>> w;
vector<double> V;
struct Timer
{
std::chrono::time_point<std::chrono::high_resolution_clock> start, end;
std::chrono::duration<float> duration;
Timer() {
start = std::chrono::high_resolution_clock::now();
}
/* when the function where this object is created returns,
this object must be destroyed, hence this destructor is called */
~Timer() {
end = std::chrono::high_resolution_clock::now();
duration = end - start;
float ms = duration.count() * 1000.0f;
std::cout << "Elapsed: " << ms << " ms." << std::endl;
}
};
// initialize spikes
template <class A, class B>
vector<tuple<int, int>> where (vector<vector<A>> arr, B val, int test) {
vector<tuple<int, int>> whid;
int I = arr.size();
int J = arr[0].size();
if (test == 0) {
for (int i = 0; i < I; i++) {
for (int j = 0; j < J; j++) {
if (arr[i][j] == val){
whid.push_back(make_tuple(i,j));
}
}
}
}
if (test == -1) {
for (int i = 0; i < I; i++) {
for (int j = 0; j < J; j++) {
if (arr[i][j] < val){
whid.push_back(make_tuple(i,j));
}
}
}
}
if (test == 1) {
for (int i = 0; i < I; i++) {
for (int j = 0; j < J; j++) {
if (arr[i][j] > val){
whid.push_back(make_tuple(i,j));
}
}
}
}
return whid;
}
template <class A, class B>
vector<int> where1d (vector<A> arr, B val, int test) {
vector<int> whid;
int I = arr.size();
if (test == 0) {
for (int i = 0; i < I; i++) {
if (arr[i] == val){
whid.push_back(i);
}
}
}
if (test == -1) {
for (int i = 0; i < I; i++) {
if (arr[i] < val){
whid.push_back(i);
}
}
}
if (test == 1) {
for (int i = 0; i < I; i++) {
if (arr[i] > val){
whid.push_back(i);
}
}
}
return whid;
}
template <class T>
vector<T> random_choice(vector<T> samples, int outputSize) {
vector<T> vec(outputSize);
vector<double> probabilities;
for (int i = 0; i < samples.size(); i++) {
probabilities.push_back((double) 1/samples.size());
}
std::default_random_engine generator;
std::discrete_distribution<int> distribution(probabilities.begin(), probabilities.end());
vector<int> indices(vec.size());
std::generate(indices.begin(), indices.end(), [&generator, &distribution]() { return distribution(generator); });
std::transform(indices.begin(), indices.end(), vec.begin(), [&samples](int index) { return samples[index]; });
return vec;
}
double dice() {
return rand()/(RAND_MAX + 1.0);
}
void save_spts() {
ostringstream ossw;
ossw << "spts.txt";
string fstrw = ossw.str();
ofstream ofsw;
ofsw.open( fstrw.c_str() );
for(int i = 0; i < N; i++){
for(int j = 0; j < t.size(); j++){
if (VV[i][j] > Vth + 0.1){
ofsw << i << "," << j*dt << endl;
}
}
}
}
void init() {
vector<double> row;
vector<vector<double>> rectangle;
for (int i = 0; i < N; i++) {
dV.push_back(0.0);
}
for (int i = 0; i < N; i++) {
delayed_spike.push_back(0.0);
}
for (int i = 0; i < N; i++) {
I_E.push_back(0.0);
}
for (int i = 0; i < N; i++) {
I_I.push_back(0.0);
}
// times
for (double i = 0; i < T; i += dt) {
t.push_back(i);
}
// AP vector
for (int i = 0; i < N; i++) {
AP.push_back(0.0);
}
// neur type mask
for (int i = 0; i < 10; i++) {
neur_type_mask.push_back(0);
}
for (int i = 10; i < N; i++) {
neur_type_mask.push_back(1);
}
// vector<tuple<int, int>> exc_id = where(neur_type_mask, 0, 1);
exc_id = where1d(neur_type_mask, 0, 1);
// !!!!!!!!! sample without REPLACEMENT
// ons = random_choice( exc_id, (int)(0.4 * exc_id.size()) );
ons = {63, 12, 22, 96, 97, 60, 54, 33, 92, 15, 88, 78, 91, 10, 41, 51, 45,
57, 47, 77, 70, 98, 31, 11, 93, 76, 29, 46, 49, 65, 64, 48, 18, 62,
37, 14};
for (int i: ons) {
AP[i] = 1;
}
// # taus
for (int i = 0; i < N; i++) {
if (neur_type_mask[i] == 1) {
tau.push_back(20.0);
}
else {
tau.push_back(10.0);
}
}
for (int i = 0; i < N; i++) {
V.push_back(EL);
}
// define weights:
for (int i = 0; i < N; i ++) {
w.push_back(row);
for (int j = 0; j < N; j ++) {
w[i].push_back(0.0);
}
}
// II
for (int i = 0; i < NI; i ++) {
for (int j = 0; j < NI; j ++) {
if (dice() > 0.8) {
w[i][j] = dice() * 0.3;
}
}
}
// IE
for (int i = NI; i < N; i ++) {
for (int j = 0; j < NI; j ++) {
if (dice() > 0.8) {
w[i][j] = dice() + 1.4;
}
}
}
// EI
for (int i = 0; i < NI; i ++) {
for (int j = NI; j < N; j ++) {
if (dice() > 0.8) {
w[i][j] = dice() * 1.7;
}
}
}
// EE
for (int i = NI; i < N; i++) {
for (int j = NI; j < N; j++) {
if (dice() > 0.8) {
w[i][j] = dice() + 0.1;
}
}
}
// prohibit self-connections
for (int i = 0; i < N; i++) {
w[i][i] = 0.0;
}
// define conductances:
for (int i = 0; i < N; i++) {
ampa.push_back(row);
for (int j = 0; j < N; j++) {
ampa[i].push_back(0.0);
}
}
for (int i = 0; i < N; i ++) {
nmda.push_back(row);
for (int j = 0; j < N; j ++) {
nmda[i].push_back(0.0);
}
}
for (int i = 0; i < N; i ++) {
gaba.push_back(row);
for (int j = 0; j < N; j ++) {
gaba[i].push_back(0.0);
}
}
for (int i = 0; i < N; i ++) {
in_refractory.push_back(-1.0);
}
for (int i = 0; i < N; i ++) {
VV.push_back(row);
for (int j = 0; j < t.size(); j ++) {
VV[i].push_back(0.0);
}
}
for (int i = 0; i < N; i ++) {
Ie.push_back(row);
for (int j = 0; j < t.size(); j ++) {
Ie[i].push_back(0.0);
}
}
for (int i = 0; i < N; i ++) {
rectangle.push_back(row);
for (int j = 0; j < t.size(); j ++) {
rectangle[i].push_back(0.0);
}
}
// for (int i = 0; i < N; i ++) {
// AMPA.push_back(rectangle);
// }
// for (int i = 0; i < N; i ++) {
// NMDA.push_back(rectangle);
// }
// for (int i = 0; i < N; i ++) {
// GABA.push_back(rectangle);
// }
}
int main() {
init();
auto start = std::chrono::steady_clock::now();
Timer timer;
for (int ts = 0; ts < t.size(); ts++) {
if (ts%1000 == 0){
auto end = std::chrono::steady_clock::now();
std::chrono::duration<double> elapsed_seconds = end-start;
cout << t[ts] << " " << elapsed_seconds.count() << endl;
start = std::chrono::steady_clock::now();
}
#pragma omp parallel for
for (int ii = 0; ii < N; ii++) {
if (AP[ii] == 1.0){
in_refractory[ii] = refractory_period + dice();
AP[ii] = 0.0;
}
if (abs(in_refractory[ii]) < EPSILON) {
delayed_spike[ii] = 1.0;
}
else {
delayed_spike[ii] = 0.0;
}
I_E[ii] = 0.0;
I_I[ii] = 0.0;
for (int jj = 0; jj < N; jj++) {
if (w[ii][jj] > 0) {
ampa[ii][jj] += (-ampa[ii][jj] / tau_ampa + neur_type_mask[ii] * delayed_spike[jj] * w[ii][jj]) * dt;
nmda[ii][jj] += (-nmda[ii][jj] / tau_nmda + neur_type_mask[ii] * delayed_spike[jj] * w[ii][jj]) * dt;
gaba[ii][jj] += (-gaba[ii][jj] / tau_gaba + (1.0 - neur_type_mask[ii]) * delayed_spike[jj] * w[ii][jj]) * dt;
}
I_E[ii] += -ampa[ii][jj] * (V[ii] - V_E) - 0.1 * nmda[ii][jj] * (V[ii] - V_E);
I_I[ii] += -gaba[ii][jj] * (V[ii] - V_I);
VV[ii][ts] = V[ii];
// AMPA[ii][jj][ts] = ampa[ii][jj];
// NMDA[ii][jj][ts] = nmda[ii][jj];
// GABA[ii][jj][ts] = gaba[ii][jj];
}
dV[ii] = (-(V[ii] - EL) / tau[ii] + I_E[ii] + I_I[ii] ) * dt;
if (V[ii] >= Vspike)
V[ii] = Vr;
if (in_refractory[ii] > 0)
dV[ii] = 0.0;
V[ii] += dV[ii];
if (V[ii] > Vth) {
V[ii] = Vspike;
AP[ii] = 1;
}
in_refractory[ii] -= dt;
}
}
save_spts();
return 0;
}