-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
1818 lines (1634 loc) · 88.2 KB
/
main.c
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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "GeneralIncludes.h"
#include "cl_LIFNeuron.h"
#include "cl_Synapse.h"
#include "HandleOpenCL.h"
#include "NumericalTools.h"
#include "DataReporters.h"
//#define PI (atan(1.)*4.)
void freeMemory(cl_LIFNeuron *lif_p, cl_Synapse *syn_p, FixedSynapse *fixed_syn_p, SpikeQueue *spike_queue_p);
void updateEventBasedSynapse(cl_Synapse *syn, SynapseConsts *syn_const, int syn_id, int current_time);
unsigned int generateNetwork(cl_LIFNeuron *lif_p, cl_Synapse *syn_p, FixedSynapse *fixed_syn_p){
//clock_t start, finish;
//double totaltime;
float p = CONNECTIVITY_PROBABILITY;
long network_seed = NETWORK_SEED;
unsigned int total_ee_synapses = 0;
unsigned int total_fixed_synapses = 0;
unsigned int mean_ee_synapses_per_neuron = (int)((NO_EXC)*(CONNECTIVITY_PROBABILITY));
unsigned int mean_total_synapses_per_neuron = (int)((NO_LIFS)*(CONNECTIVITY_PROBABILITY));
//CONSIDER: widening the margin of error on the following two estimates
unsigned int estimated_total_ee_synapses = (NO_EXC * (mean_ee_synapses_per_neuron+100)) + (int)(NO_EXC / 10) + 100; // mean + wide margin + constant (for small nets)
unsigned int estimated_total_synapses = (NO_LIFS * (mean_total_synapses_per_neuron+100)) + (int)(NO_LIFS / 10) + 100; // mean + wide margin + constant (for small nets)
unsigned int estimated_ee_synapses_per_neuron = (mean_ee_synapses_per_neuron) + (int)(mean_ee_synapses_per_neuron/2) + 1000; // mean + wide margin + constant (for small nets)
unsigned int estimated_total_synapses_per_neuron = (mean_total_synapses_per_neuron) + (int)(mean_total_synapses_per_neuron/2) + 2000; // mean + wide margin + constant (for small nets)
float delta_spike_modifier = (*lif_p).tau_m / (*lif_p).dt;
//printf("DEBUG: delta_spike_modifier %f\n", delta_spike_modifier);
(*syn_p).pre_lif = calloc(estimated_total_ee_synapses, sizeof(signed int));
(*syn_p).post_lif = calloc(estimated_total_ee_synapses, sizeof(signed int));
(*fixed_syn_p).Jx = calloc(estimated_total_synapses, sizeof(float));
(*fixed_syn_p).post_lif = calloc(estimated_total_synapses, sizeof(signed int));
// Setup population which will be manipulated
//no_injection_lifs = 0; // required for calculating firing rates
//(*lif_p).subpopulation_flag = calloc(NO_LIFS, sizeof(unsigned char));
//(*syn_p).receives_stimulation_flag = calloc(estimated_total_synapses, sizeof(unsigned char));
(*syn_p).initially_UP = calloc(estimated_total_synapses, sizeof(unsigned char));
printf("Space allocation, mean_ee_syn_per_neuron: %d, est_ee_syn_per_neuron: %d, est_total_ee_synapses: %d, est_total_syn_per_neuron: %d\n", mean_ee_synapses_per_neuron, estimated_ee_synapses_per_neuron, estimated_total_ee_synapses, estimated_total_synapses_per_neuron);
printf("Generating network structure...\n");
//start = clock();
// Assign basic memory requirements for keeping track of pre and post neuronal synapses
(*lif_p).no_outgoing_synapses = calloc(NO_LIFS, sizeof(unsigned int));
(*lif_p).no_outgoing_ee_synapses = calloc(NO_LIFS, sizeof(unsigned int));
(*lif_p).outgoing_synapse_index = malloc(sizeof(signed int *) * NO_LIFS);
(*lif_p).no_incoming_synapses = calloc(NO_LIFS, sizeof(unsigned int));
(*lif_p).incoming_synapse_index = malloc(sizeof(signed int *) * NO_LIFS);
// Assign (hopefully) overly large memory for recording ids of pre and post neuronal synapses
for(int i = 0; i < NO_LIFS; i++){
(*lif_p).incoming_synapse_index[i] = malloc(sizeof(signed int) * estimated_total_synapses_per_neuron);
(*lif_p).outgoing_synapse_index[i] = malloc(sizeof(signed int) * estimated_total_synapses_per_neuron);
}
// Generate Synapses randomly, telling each synapse its pre and post synaptic neuron ids and each lif its pre or post neuronal synapse ids
for(int i = 0; i < NO_EXC; i++){
// By generating EE synapses first we can assume that all synapses with array address < total_ee_synapses are EE synapses
for(int j = 0; j < NO_EXC; j++){
// EXC -> EXC synapses
if(i != j){ // Disallow autapses
if ((ran2(&network_seed)) < p){
// A new synapse is formed
//printf("synapse(%d) ", total_synapses);
// Assign indices of pre and post synaptic neurons to the new synapse
(*syn_p).pre_lif[total_ee_synapses] = i;
(*syn_p).post_lif[total_ee_synapses] = j;
//printf("pre_lif: %d, post_lif: %d, ", (*syn_p).pre_lif[total_synapses], (*syn_p).post_lif[total_synapses]);
// Update pre-synaptic neuron relationship with synapse
(*lif_p).outgoing_synapse_index[i][(*lif_p).no_outgoing_synapses[i]] = (int)total_ee_synapses; //synaptic id
(*lif_p).no_outgoing_synapses[i]++;// could be added to array lookup in previous line
(*lif_p).no_outgoing_ee_synapses[i]++;
//printf("out_id: %d, no_out: %d ", (*lif_p).outgoing_synapse_index[i][(*lif_p).no_outgoing_synapses[i]-1], (*lif_p).no_outgoing_synapses[i]);
// Update post-synaptic neuron relationship with synapse
(*lif_p).incoming_synapse_index[j][(*lif_p).no_incoming_synapses[j]] = (int)total_ee_synapses; //syn id
(*lif_p).no_incoming_synapses[j]++;
//printf("in_id: %d, no_in: %d \n", (*lif_p).incoming_synapse_index[j][(*lif_p).no_incoming_synapses[j]-1], (*lif_p).no_incoming_synapses[j]);
// Add a small subset of LIFs to manipulation list
/*if((total_ee_synapses % RECORDER_MULTI_SYNAPSE_SKIP) == RECORDER_SYNAPSE_ID){
(*lif_p).subpopulation_flag[i] = 1;
(*lif_p).subpopulation_flag[j] = 1;
no_injection_lifs += 2;
//(*syn_p).receives_stimulation_flag[total_ee_synapses] = 1;
//printf("i %d, j %d\n", i, j);
}*/
total_ee_synapses++;
#ifdef DEBUG_MODE_NETWORK
lif_mean_destination[i] += j;
lif_mean_dest_EE[i] += j;
lif_debug_no_EE[i]++;
lif_in_EE[j]++;
#endif /* DEBUG_MODE_NETWORK */
}
#ifdef SYN_MAINTAIN_FF_CONNECTIVITY
else if ( (i+1) == j ){
// A new synapse is formed
//printf("synapse(%d) ", total_synapses);
// Assign indices of pre and post synaptic neurons to the new synapse
(*syn_p).pre_lif[total_ee_synapses] = i;
(*syn_p).post_lif[total_ee_synapses] = j;
//printf("pre_lif: %d, post_lif: %d, ", (*syn_p).pre_lif[total_synapses], (*syn_p).post_lif[total_synapses]);
// Update pre-synaptic neuron relationship with synapse
(*lif_p).outgoing_synapse_index[i][(*lif_p).no_outgoing_synapses[i]] = (int)total_ee_synapses; //synaptic id
(*lif_p).no_outgoing_synapses[i]++;// could be added to array lookup in previous line
(*lif_p).no_outgoing_ee_synapses[i]++;
//printf("out_id: %d, no_out: %d ", (*lif_p).outgoing_synapse_index[i][(*lif_p).no_outgoing_synapses[i]-1], (*lif_p).no_outgoing_synapses[i]);
// Update post-synaptic neuron relationship with synapse
(*lif_p).incoming_synapse_index[j][(*lif_p).no_incoming_synapses[j]] = (int)total_ee_synapses; //syn id
(*lif_p).no_incoming_synapses[j]++;
//printf("in_id: %d, no_in: %d \n", (*lif_p).incoming_synapse_index[j][(*lif_p).no_incoming_synapses[j]-1], (*lif_p).no_incoming_synapses[j]);
// Add a small subset of LIFs to manipulation list
/*if((total_ee_synapses % RECORDER_MULTI_SYNAPSE_SKIP) == RECORDER_SYNAPSE_ID){
(*lif_p).subpopulation_flag[i] = 1;
(*lif_p).subpopulation_flag[j] = 1;
no_injection_lifs += 2;
//(*syn_p).receives_stimulation_flag[total_ee_synapses] = 1;
//printf("i %d, j %d\n", i, j);
}*/
total_ee_synapses++;
#ifdef DEBUG_MODE_NETWORK
lif_mean_destination[i] += j;
lif_mean_dest_EE[i] += j;
lif_debug_no_EE[i]++;
lif_in_EE[j]++;
#endif /* DEBUG_MODE_NETWORK */
}
#endif /* SYN_MAINTAIN_FF_CONNECTIVITY */
}
}
}
for(int i = 0; i < NO_EXC; i++){
for(int j = NO_EXC; j < NO_LIFS; j++){
// EXC -> INH synapses
if(i != j){ //TODO: test not strictly necessary here
if((ran2(&network_seed)) < p){
// A new synapse
// by not having a pre_lif variable we save on checks when backpropagating spikes
(*fixed_syn_p).post_lif[total_fixed_synapses] = j;
(*fixed_syn_p).Jx[total_fixed_synapses] = delta_spike_modifier * J_IE;
(*lif_p).outgoing_synapse_index[i][(*lif_p).no_outgoing_synapses[i]] = (int)(total_fixed_synapses + total_ee_synapses); //synaptic id
(*lif_p).no_outgoing_synapses[i]++;
total_fixed_synapses++;
#ifdef DEBUG_MODE_NETWORK
lif_mean_destination[i] += j;
lif_mean_dest_IE[i] += j;
lif_debug_no_IE[i]++;
lif_in_IE[j]++;
#endif /* DEBUG_MODE_NETWORK */
}
}
}
}
for(int i = NO_EXC; i < NO_LIFS; i++){
for(int j = 0; j < NO_EXC; j++){
// INH -> EXC synapses
if(i != j){ //TODO: test not strictly necessary here
if((ran2(&network_seed)) < p){
// A new synapse
(*fixed_syn_p).post_lif[total_fixed_synapses] = j;
(*fixed_syn_p).Jx[total_fixed_synapses] = delta_spike_modifier * J_EI;
(*lif_p).outgoing_synapse_index[i][(*lif_p).no_outgoing_synapses[i]] = (int)(total_fixed_synapses + total_ee_synapses); //synaptic id
(*lif_p).no_outgoing_synapses[i]++;
total_fixed_synapses++;
#ifdef DEBUG_MODE_NETWORK
lif_mean_destination[i] += j;
lif_mean_dest_EI[i] += j;
lif_debug_no_EI[i]++;
lif_in_EI[j]++;
#endif /* DEBUG_MODE_NETWORK */
}
}
}
for(int j = NO_EXC; j < NO_LIFS; j++){
// INH -> INH synapses
if(i != j){
if((ran2(&network_seed)) < p){
// A new synapse
(*fixed_syn_p).post_lif[total_fixed_synapses] = j;
(*fixed_syn_p).Jx[total_fixed_synapses] = delta_spike_modifier * J_II;
(*lif_p).outgoing_synapse_index[i][(*lif_p).no_outgoing_synapses[i]] = (int)(total_fixed_synapses + total_ee_synapses); //synaptic id
(*lif_p).no_outgoing_synapses[i]++;
total_fixed_synapses++;
#ifdef DEBUG_MODE_NETWORK
lif_mean_destination[i] += j;
lif_mean_dest_II[i] += j;
lif_debug_no_II[i]++;
lif_in_II[j]++;
#endif /* DEBUG_MODE_NETWORK */
}
}
}
}
//finish = clock();
//totaltime = (double)(finish - start)/CLOCKS_PER_SEC;
//printf("Time taken: %f, ", totaltime);
printf("Total EE synapses: %d, total fixed synapses: %d\n", total_ee_synapses, total_fixed_synapses);
/*printf("No of LIFs to be manipulated: %d, removing duplicates...", no_injection_lifs);
// Remove duplicates from list of LIFs to be manipulated
for(int i = 0; i < no_injection_lifs; i++){
for(int j = i+1; j < no_injection_lifs; j++){
if(lif_injection_list[i] == lif_injection_list[j]){
lif_injection_list[j] = lif_injection_list[no_injection_lifs-1];
no_injection_lifs--;
//printf("DEBUG: no_injection_lifs %d, reading i %d, removing j %d, value %d, new_value %d\n", no_injection_lifs, i, j, lif_injection_list[i], lif_injection_list[j]);
}
}
//printf("%d %d\n", i, lif_injection_list[i]);
}
// Resize list of manipulation LIFs
lif_injection_list = realloc(lif_injection_list, sizeof(unsigned int) * no_injection_lifs);
*/
/*printf("Presumed no of current manipulation LIFs (may double count): %d\n", no_injection_lifs);
no_injection_lifs = 0;
for(int i = 0; i < NO_LIFS; i++){
if((*lif_p).subpopulation_flag[i] == 1){
//printf("DEBUG: i: %d\n", i);
no_injection_lifs++;
}
}
printf("Actual no of current manipulation LIFs: %d\n", no_injection_lifs);*/
// Shrink memory reserved to required sizes
(*syn_p).pre_lif = realloc((*syn_p).pre_lif, sizeof(signed int) * total_ee_synapses);
(*syn_p).post_lif = realloc((*syn_p).post_lif, sizeof(signed int) * total_ee_synapses);
(*fixed_syn_p).Jx = realloc((*fixed_syn_p).Jx, sizeof(float) * total_fixed_synapses);
(*fixed_syn_p).post_lif = realloc((*fixed_syn_p).post_lif, sizeof(signed int) * total_fixed_synapses);
//(*syn_p).receives_stimulation_flag = realloc((*syn_p).receives_stimulation_flag, sizeof(unsigned char) * total_ee_synapses);
(*syn_p).initially_UP = realloc((*syn_p).initially_UP, sizeof(unsigned char) * total_ee_synapses);
for(int i = 0; i < NO_LIFS; i++){
(*lif_p).incoming_synapse_index[i] = realloc((*lif_p).incoming_synapse_index[i], sizeof(signed int) * (*lif_p).no_incoming_synapses[i]);
(*lif_p).outgoing_synapse_index[i] = realloc((*lif_p).outgoing_synapse_index[i], sizeof(signed int) * (*lif_p).no_outgoing_synapses[i]);
#ifdef DEBUG_MODE_NETWORK
lif_mean_destination[i] /= (*lif_p).no_outgoing_synapses[i];
lif_mean_dest_EE[i] /= lif_debug_no_EE[i];
lif_mean_dest_IE[i] /= lif_debug_no_IE[i];
lif_mean_dest_EI[i] /= lif_debug_no_EI[i];
lif_mean_dest_II[i] /= lif_debug_no_II[i];
#endif /* DEBUG_MODE_NETWORK */
}
(*fixed_syn_p).total_fixed_synapses = total_fixed_synapses;
return total_ee_synapses;
}
int main (int argc, const char * argv[]) {
int i, j, k;
int offset;
float isi; // new ISI variable (local to main sim loop)
#ifdef SYN_USE_RAND_UNIFORM_INITIALISATION
long uniform_synaptic_seed = UNIFORM_SYNAPTIC_SEED;
#endif
#ifdef SYN_USE_INVIVO_DOUBLE_WELL_INITIALISATION
long uniform_synaptic_seed = UNIFORM_SYNAPTIC_SEED;
#endif
//long gaussian_lif_seed = (GAUSSIAN_SYNAPTIC_SEED - 1);
#ifdef LEARNING_INDEP_POISSON_STIM
// Variables added for Poisson patterned stimulation
int pattern_time;
int *time_to_next_stim_spike;
long seed_resettable_pattern = RAN2_RESETTABLE_SEED;
#endif /* LEARNING_INDEP_POISSON_STIM */
#ifdef LEARNING_REPEATED_PATTERNED_STIM
// Variables added for regular patterned stimulation
//int pattern_time[NO_STIM_SUBSETS]; // moved to struct
//int *time_to_next_stim_spike;
//long seed_resettable_pattern = RAN2_RESETTABLE_SEED;
typedef struct patterned_stim_params {
// per stimulus subpopulation, variables and constants
int pattern_time; // internal clock for pattern. Giving a separate clock per pattern is overkill in the current implementation but will allow for different on and off times of sub-pops if that is ever required
int *time_to_next_stim_spike; // clock ticks until next induced spike in this subpopulation (for this stimulus)
int start_time_delay; // don't necessarily stimulate all subpops from STIM_ON but rather from (STIM_ON + start_time_delay)
int pattern_duration; // Takes per population STIM_PATTERN_DURATION value
int pattern_pause_duration; // not strictly necessary, but it is currently used to set the full cycle duration
int pattern_full_cycle_duration; // saves on re-calculating multiple times
int inter_neuron_fixed_stepping_isi; // this is the offset between adjacent neurons in the pattern
int per_neuron_isi; // this is the per neuron frequency (via ISI)
int stim_id_offset; // id of initial neuron is stimulus subset
} patterned_stim_params;
patterned_stim_params patterned_stim_parameters[NO_STIM_SUBSETS];
#endif /* LEARNING_REPEATED_PATTERNED_STIM */
clock_t start_t,finish_t;
double totaltime;
//Setup output files
reporters_setup();
char *KernelSource = readKernelSource("kernel.cl");
// LIF compute kernel
CL cl_lif;
CL *cl_lif_p = &cl_lif;
cl_LIFNeuron lif;
cl_LIFNeuron *lif_p = &lif;
//These guys get initialise as maps to device memory now (below)
//(*lif_p).V = malloc(sizeof(float) * NO_LIFS);
//(*lif_p).I = malloc(sizeof(float) * NO_LIFS);
//(*lif_p).gauss = calloc(NO_LIFS, sizeof(float));
//(*lif_p).time_since_spike = calloc(NO_LIFS, sizeof(unsigned int));
(*lif_p).time_of_last_spike = calloc(NO_LIFS, sizeof(unsigned int));
(*lif_p).v_rest = LIF_V_REST;
(*lif_p).v_reset = LIF_V_RESET;
(*lif_p).v_threshold = LIF_V_THRESHOLD;
(*lif_p).tau_m = (LIF_RM * LIF_CM);
//(*lif_p).r_m = LIF_RM;
//(*lif_p).c_m = LIF_CM;
(*lif_p).sigma = LIF_SIGMA; //5;
(*lif_p).refrac_time = LIF_REFRAC_TIME; //20;
(*lif_p).dt = LIF_DT;
(*lif_p).no_lifs = NO_LIFS;
(*lif_p).time_step = 0;
(*lif_p).random123_seed = PARALLEL_SEED;
(*cl_lif_p).job_size = (*lif_p).no_lifs;
// Setup external and synaptic voltages/currents
double external_voltage = J_EXT;
// Syanptic currents must be modified by (tau_m/dt) as they are delta current spikes
double delta_spike_modifier = (*lif_p).tau_m / (*lif_p).dt;
// When STIM current is a delta spike it must be modified by (tau_m/dt)
double stim_voltage = J_STIM;
stim_voltage *= delta_spike_modifier;
// This implementation uses delta-spike model for spike transfer
double transfer_voltage = J_EE;
transfer_voltage *= delta_spike_modifier;
//printf("DEBUG: delta_spike_modifier %f, transfer_voltage %f\n", delta_spike_modifier, transfer_voltage);
char *k_name_lif = "lif";
// Synapse compute kernel
CL cl_syn;
CL *cl_syn_p = &cl_syn;
cl_Synapse syn;
cl_Synapse *syn_p = &syn;
SynapseConsts syn_const;
SynapseConsts *syn_const_p = &syn_const;
// Fixed strength excitatory and inhibitory synapses
FixedSynapse fixed_syn;
FixedSynapse *fixed_syn_p = &fixed_syn;
// Network generation
start_t = clock();
(*syn_const_p).no_syns = generateNetwork(lif_p, syn_p, fixed_syn_p);
finish_t = clock();
totaltime = (double)(finish_t - start_t)/CLOCKS_PER_SEC;
//(*syn_const_p).no_syns = NO_SYNS;
(*cl_syn_p).job_size = (*syn_const_p).no_syns;
printf("Network generated, jobsize: %d, generation time: %f\n", (*cl_syn_p).job_size, totaltime);
//DEBUGGING code: print network description using different mapping approaches
// Traverse pre-synaptic neurons
/*for(int i = 0; i < NO_LIFS; i++){
for(int j = 0; j < (*lif_p).no_outgoing_ee_synapses[i]; j++){
printf("LIF(%d) -> LIF(%d), via Synapse(%d), no outgoing %d, no EE outgoing: %d\n", i, (*syn_p).post_lif[(*lif_p).outgoing_synapse_index[i][j]], (*lif_p).outgoing_synapse_index[i][j], (*lif_p).no_outgoing_synapses[i], (*lif_p).no_outgoing_ee_synapses[i]);
}
for(int j = (*lif_p).no_outgoing_ee_synapses[i]; j < (*lif_p).no_outgoing_synapses[i]; j++){
printf("LIF(%d) -> LIF(%d), via fixed Synapse(%d), no outgoing %d, no EE outgoing: %d\n", i, (*fixed_syn_p).post_lif[(*lif_p).outgoing_synapse_index[i][j]-(*syn_const_p).no_syns], ((*lif_p).outgoing_synapse_index[i][j] - (*syn_const_p).no_syns), (*lif_p).no_outgoing_synapses[i], (*lif_p).no_outgoing_ee_synapses[i]);
}
} */ /*
printf("---------------\n");
// Traverse post-synaptic neurons
for(int i = 0; i < NO_LIFS; i++){
for(int j = 0; j < (*lif_p).no_incoming_synapses[i]; j++){
printf("LIF(%d) <- LIF(%d), via Synapse(%d), no incoming %d\n", i, (*syn_p).pre_lif[(*lif_p).incoming_synapse_index[i][j]], (*lif_p).incoming_synapse_index[i][j], (*lif_p).no_incoming_synapses[i]);
}
} */ /*
printf("---------------\n");
// Traverse synapses
for(int i = 0; i < (*syn_const_p).no_syns; i++){
printf("Synapse(%d) links LIF(%d) -> LIF(%d)\n", i, (*syn_p).pre_lif[i], (*syn_p).post_lif[i]);
}
for(int i = 0; i < (*fixed_syn_p).total_fixed_synapses; i++){
printf("Fixed Synapse(%d) links to LIF(%d)\n", i, (*fixed_syn_p).post_lif[i]);
}*/
//End DEBUGGING code
//End network generation
(*syn_p).rho = malloc(sizeof(float) * (*syn_const_p).no_syns);
(*syn_p).rho_initial = malloc(sizeof(float) * (*syn_const_p).no_syns);
(*syn_p).ca = malloc(sizeof(float) * (*syn_const_p).no_syns);
(*syn_p).gauss = calloc((*syn_const_p).no_syns, sizeof(float));
(*syn_const_p).delay = SYN_CALCIUM_DELAY; // measured in multiples of dt
(*syn_p).time_of_last_update = calloc((*syn_const_p).no_syns, sizeof(unsigned int));
(*syn_p).preT = calloc((*syn_const_p).no_syns, sizeof(unsigned int));
(*syn_p).postT = calloc((*syn_const_p).no_syns, sizeof(unsigned int));
// Event queue for delayed propagation of pre-synaptic spikes to synaptic calcium buffer
SpikeQueue spike_queue;
SpikeQueue *spike_queue_p = &spike_queue;
(*spike_queue_p).neuron_id = malloc((*syn_const_p).delay * sizeof(int *));
(*spike_queue_p).no_events = calloc((*syn_const_p).delay, sizeof(int));
for(i = 0; i < (*syn_const_p).delay; i++){
(*spike_queue_p).neuron_id[i] = malloc((*lif_p).no_lifs * sizeof(int));
}
(*syn_const_p).gamma_p = SYN_GAMMA_P;
(*syn_const_p).gamma_d = SYN_GAMMA_D;
(*syn_const_p).theta_p = SYN_THETA_P;
(*syn_const_p).theta_d = SYN_THETA_D;
(*syn_const_p).sigma = SYN_SIGMA;
(*syn_const_p).tau = SYN_TAU;
(*syn_const_p).tau_ca = SYN_TAU_CA;
(*syn_const_p).c_pre = SYN_C_PRE;
(*syn_const_p).c_post = SYN_C_POST;
(*syn_const_p).dt = SYN_DT;
//char *k_name_syn = "synapse";
// Random number generator streams
//for LIF
cl_MarsagliaStruct rnd_lif;
cl_MarsagliaStruct *rnd_lif_p = &rnd_lif;
/*(*rnd_lif_p).d_z = malloc((*lif_p).no_lifs * sizeof(unsigned int));
(*rnd_lif_p).d_w = malloc((*lif_p).no_lifs * sizeof(unsigned int));
(*rnd_lif_p).d_jsr = malloc((*lif_p).no_lifs * sizeof(unsigned int));
(*rnd_lif_p).d_jcong = malloc((*lif_p).no_lifs * sizeof(unsigned int));*/
//for Synapse
cl_MarsagliaStruct rnd_syn;
cl_MarsagliaStruct *rnd_syn_p = &rnd_syn;
(*rnd_syn_p).d_z = malloc((*syn_const_p).no_syns * sizeof(unsigned int));
(*rnd_syn_p).d_w = malloc((*syn_const_p).no_syns * sizeof(unsigned int));
(*rnd_syn_p).d_jsr = malloc((*syn_const_p).no_syns * sizeof(unsigned int));
(*rnd_syn_p).d_jcong = malloc((*syn_const_p).no_syns * sizeof(unsigned int));
// OpenCL functions
if( setupCL(cl_lif_p) == EXIT_FAILURE){
return EXIT_FAILURE;
}
/*if( setupCL(cl_syn_p) == EXIT_FAILURE){
return EXIT_FAILURE;
}*/
if( makeProgram(cl_lif_p, KernelSource, k_name_lif) == EXIT_FAILURE){
return EXIT_FAILURE;
}
/*if( makeProgram(cl_syn_p, KernelSource, k_name_syn) == EXIT_FAILURE){
return EXIT_FAILURE;
}*/
// OpenCL data IO
if( createLifIObufs(cl_lif_p) == EXIT_FAILURE){
return EXIT_FAILURE;
}
/*if( createSynIObufs(cl_syn_p) == EXIT_FAILURE){
return EXIT_FAILURE;
}*/
// Mapped memory is pinned (prevented from being swapped) hence faster (on occasion)
// so our buffers are now in mapped memory
if( mapLifIObufs(cl_lif_p, lif_p) == EXIT_FAILURE){
return EXIT_FAILURE;
}
// Prepopulate data set, including with random values
//
printf("initialising data...\n");
for ( i = 0; i < (*lif_p).no_lifs; i++){
//CONSIDER: initialising V and time_since_spike to random values (within reasonable ranges)
(*lif_p).V[i] = (float)LIF_V_INITIAL;
(*lif_p).I[i] = external_voltage;
(*lif_p).time_since_spike[i] = (*lif_p).refrac_time;
/*(*rnd_lif_p).d_z[i] = 362436069 + i + 1 + PARALLEL_SEED;
(*rnd_lif_p).d_w[i] = 521288629 + i + 1 + PARALLEL_SEED;
(*rnd_lif_p).d_jsr[i] = 123456789 + i + 1 + PARALLEL_SEED;
(*rnd_lif_p).d_jcong[i] = 380116160 + i + 1 + PARALLEL_SEED;*/
//(*lif_p).gauss[i] = gasdev(&gaussian_lif_seed);
}
for( i = 0; i < (*syn_const_p).no_syns; i++){
//TODO: set rho_initial here
#ifdef SYN_USE_CONST_INITIALISATION
(*syn_p).rho[i] = (*syn_p).rho_initial[i] = SYN_RHO_INITIAL; //ran2(&uniform_synaptic_seed);//0.377491; //
#endif
#ifdef SYN_USE_RAND_UNIFORM_INITIALISATION
(*syn_p).rho[i] = (*syn_p).rho_initial[i] = ran2(&uniform_synaptic_seed);
#endif
#ifdef SYN_USE_INVIVO_DOUBLE_WELL_INITIALISATION
(*syn_p).rho[i] = (*syn_p).rho_initial[i] = invivo_double_well_distribution(&uniform_synaptic_seed);
#endif
//}
#ifdef SYN_POTENTIATE_SUBSET_OF_SYNS
// Set a subset of synapses to UP initially
if(ran2(&uniform_synaptic_seed) < 0.05){
//TODO: why did I deliberately break the initialisation here?
//(*syn_p).rho[i] = (*syn_p).rho_initial[i] = 1; //0.85;
(*syn_p).initially_UP[i] = 1;
}
#endif /* SYN_POTENTIATE_SUBSET_OF_SYNS */
#ifdef LEARNING_REPEATED_PATTERNED_STIM
#ifdef MONITOR_UP_DOWN_POPS
//CONSIDER: different options for who to monitor in two pop monitoring
//if (i % 2 == 0){
//if ( (*syn_p).post_lif[i] > (*syn_p).pre_lif[i] ){ // this is pointing forwards in the protocol direction (very rough estimator)
if ( (*syn_p).post_lif[i] == ((*syn_p).pre_lif[i] + 1) ){ // next neighbour in protocol direction
(*syn_p).initially_UP[i] = 1;
//(*syn_p).rho[i] = 0.544;
//(*syn_p).rho_initial[i] = 0.544;
}
#endif /* MONITOR_UP_DOWN_POPS */
#endif /* LEARNING_REPEATED_PATTERNED_STIM */
(*syn_p).ca[i] = SYN_CA_INITIAL;
(*rnd_syn_p).d_z[i] = 362436069 - i + PARALLEL_SEED;
(*rnd_syn_p).d_w[i] = 521288629 - i + PARALLEL_SEED;
(*rnd_syn_p).d_jsr[i] = 123456789 - i + PARALLEL_SEED;
(*rnd_syn_p).d_jcong[i] = 380116160 - i + PARALLEL_SEED;
}
printf("DEBUG: final contents of V[0]: %f\n", (*lif_p).V[0]);
if( enqueueLifInputBuf(cl_lif_p, lif_p, rnd_lif_p) == EXIT_FAILURE){
return EXIT_FAILURE;
}
/*if( enqueueSynInputBuf(cl_syn_p, syn_p, syn_const_p, rnd_syn_p) == EXIT_FAILURE){
return EXIT_FAILURE;
}*/
if( setLifKernelArgs(cl_lif_p, lif_p) == EXIT_FAILURE){
return EXIT_FAILURE;
}
/*if( setSynKernelArgs(cl_syn_p, syn_p, syn_const_p) == EXIT_FAILURE){
return EXIT_FAILURE;
}*/
if( getMaxWorkSize(cl_lif_p) == EXIT_FAILURE){
return EXIT_FAILURE;
}
/*if( getMaxWorkSize(cl_syn_p) == EXIT_FAILURE){
return EXIT_FAILURE;
}*/
#ifdef LEARNING_INDEP_POISSON_STIM
// Variables added for Poisson patterned stimulation
pattern_time = STIM_PATTERN_DURATION + 1; // force initialisation of interstim spike times upon first run
time_to_next_stim_spike = calloc(NO_STIM_LIFS, sizeof(int));
#endif /* LEARNING_INDEP_POISSON_STIM */
#ifdef LEARNING_REPEATED_PATTERNED_STIM
// Variables added for Poisson patterned stimulation
#ifdef USE_SEPARATE_SUBPOP_PARAMS
// Manually change subpopulation parameters
// FIRST SUBPOPULATION
patterned_stim_parameters[0].pattern_duration = STIM_PATTERN_DURATION_1;
patterned_stim_parameters[0].pattern_pause_duration = STIM_PATTERN_PAUSE_DURATION_1;
patterned_stim_parameters[0].start_time_delay = STIM_PATTERN_START_DELAY_1;
patterned_stim_parameters[0].inter_neuron_fixed_stepping_isi = (int) STIM_FIXED_OFFSET_ISI_1; // just use a fixed offset between neurons, if the end neurons are outside stim window then they just don't get stimulated
patterned_stim_parameters[0].per_neuron_isi = (int) ( (1.0 / (STIM_PATTERN_AV_RATE_1 * (*lif_p).dt) ) + EPSILLON); // timesteps between stimuli, on each neuron
patterned_stim_parameters[0].stim_id_offset = STIM_OFFSET_1;
if (NO_STIM_SUBSETS > 1){
// SECOND SUBPOPULATION
patterned_stim_parameters[1].pattern_duration = STIM_PATTERN_DURATION_2;
patterned_stim_parameters[1].pattern_pause_duration = STIM_PATTERN_PAUSE_DURATION_2;
patterned_stim_parameters[1].start_time_delay = STIM_PATTERN_START_DELAY_2;
patterned_stim_parameters[1].inter_neuron_fixed_stepping_isi = (int) STIM_FIXED_OFFSET_ISI_2; // just use a fixed offset between neurons, if the end neurons are outside stim window then they just don't get stimulated
patterned_stim_parameters[1].per_neuron_isi = (int) ( (1.0 / (STIM_PATTERN_AV_RATE_2 * (*lif_p).dt) ) + EPSILLON); // timesteps between stimuli, on each neuron
patterned_stim_parameters[1].stim_id_offset = STIM_OFFSET_2;
}
if (NO_STIM_SUBSETS > 2){
// THIRD SUBPOPULATION
patterned_stim_parameters[2].pattern_duration = STIM_PATTERN_DURATION_3;
patterned_stim_parameters[2].pattern_pause_duration = STIM_PATTERN_PAUSE_DURATION_3;
patterned_stim_parameters[2].start_time_delay = STIM_PATTERN_START_DELAY_3;
patterned_stim_parameters[2].inter_neuron_fixed_stepping_isi = (int) STIM_FIXED_OFFSET_ISI_3; // just use a fixed offset between neurons, if the end neurons are outside stim window then they just don't get stimulated
patterned_stim_parameters[2].per_neuron_isi = (int) ( (1.0 / (STIM_PATTERN_AV_RATE_3 * (*lif_p).dt) ) + EPSILLON); // timesteps between stimuli, on each neuron
patterned_stim_parameters[2].stim_id_offset = STIM_OFFSET_3;
}
#endif
for( int stim_pop_id = 0; stim_pop_id < NO_STIM_SUBSETS; stim_pop_id++){
#ifndef USE_SEPARATE_SUBPOP_PARAMS
// This is the place to implement different pattern durations for each stimulus subpopulation
patterned_stim_parameters[stim_pop_id].pattern_duration = STIM_PATTERN_DURATION;
patterned_stim_parameters[stim_pop_id].pattern_pause_duration = STIM_PATTERN_PAUSE_DURATION;
patterned_stim_parameters[stim_pop_id].start_time_delay = STIM_PATTERN_START_DELAY;
patterned_stim_parameters[stim_pop_id].inter_neuron_fixed_stepping_isi = (int) STIM_FIXED_OFFSET_ISI; // just use a fixed offset between neurons, if the end neurons are outside stim window then they just don't get stimulated
patterned_stim_parameters[stim_pop_id].per_neuron_isi = (int) ( (1.0 / (STIM_PATTERN_AV_RATE * (*lif_p).dt) ) + EPSILLON); // timesteps between stimuli, on each neuron
patterned_stim_parameters[stim_pop_id].stim_id_offset = STIM_OFFSET;
#endif
// Other stimulus subpop specific initialisation (which is not user modifiable)
patterned_stim_parameters[stim_pop_id].pattern_full_cycle_duration = patterned_stim_parameters[stim_pop_id].pattern_duration + patterned_stim_parameters[stim_pop_id].pattern_pause_duration;
patterned_stim_parameters[stim_pop_id].pattern_time = (patterned_stim_parameters[stim_pop_id].pattern_full_cycle_duration + 1); // force initialisation of interstim spike times upon first run
patterned_stim_parameters[stim_pop_id].time_to_next_stim_spike = calloc(NO_STIM_LIFS, sizeof(int));
}
// The following have all been moved inside the struct
//time_to_next_stim_spike = calloc( (NO_STIM_LIFS * NO_STIM_SUBSETS), sizeof(int));
//int stim_isi = (int) ( (1.0 / (STIM_PATTERN_AV_RATE * (*lif_p).dt) ) + EPSILLON); // timesteps between stimuli, on each neuron
//int inter_neuron_offset = (int) ((float)stim_isi / (float)NO_STIM_LIFS); // used for a sort of sliding repeated stim within pattern window, which rescales so that all neurons get a stimulation
//int inter_neuron_offset = (int) STIM_FIXED_OFFSET_ISI; // just use a fixed offset between neurons, if the end neurons are outside stim window then they just don't get stimulated
#endif /* LEARNING_REPEATED_PATTERNED_STIM */
// Do the OpenCL processing
j = 0;
offset = 0;
//clock_t start,finish;
//double totaltime;
printf("Go\n");
start_t = clock();
// Print initial state of a single recorder synapse
print_synapse_activity(j, syn_p);
while(j < MAX_TIME_STEPS){
// Kernel args need to be set on each time step in order to update index of RND
//TODO: remove setKernelArgs before this loop?
if( setLifKernelArgs(cl_lif_p, lif_p) == EXIT_FAILURE){
printf("Error on time step %d setting kernel arguments\n", j);
return EXIT_FAILURE;
}
// -----Process LIF Kernel-------
if( enqueueLifKernel(cl_lif_p) == EXIT_FAILURE){
return EXIT_FAILURE;
}
//Re-enabled waitForKernel() every 10^8 timesteps in the hope that this will free Nvidia memory store,
// it didn't!
//if((j % 100000000) == 0){
/*if((j % 1000000) == 0){
printf("DEBUG: calling clFinish() on the command queue, timestep: %d\n", j);
fflush(stdout);
if( waitForKernel(cl_lif_p) == EXIT_FAILURE){
return EXIT_FAILURE;
}
}*/
// Read the OpenCL output
if( enqueueLifOutputBuf(cl_lif_p, lif_p, rnd_lif_p) == EXIT_FAILURE){
return EXIT_FAILURE;
}
/*
// -----Process Synapse Kernel-----
if( enqueueSynKernel(cl_syn_p) == EXIT_FAILURE){
return EXIT_FAILURE;
}
waitForKernel(cl_syn_p);
// Read the OpenCL output
if( enqueueSynOutputBuf(cl_syn_p, syn_p, syn_const_p, rnd_syn_p) == EXIT_FAILURE){
return EXIT_FAILURE;
}
*/
// Output results
#ifdef DEBUG_MODE_MAIN
printf("V(%d): %f, time_since_spike(%d): %d, gauss: %f\n", j, (*lif_p).V[RECORDER_NEURON_ID], j, (*lif_p).time_since_spike[RECORDER_NEURON_ID], (*lif_p).gauss[RECORDER_NEURON_ID]);
printf("rho(%d): %f, ca(%d): %f, preT(%d): %d, postT(%d): %d, gauss: %f\n", j, (*syn_p).rho[RECORDER_NEURON_ID], j, (*syn_p).ca[RECORDER_NEURON_ID], j, (*syn_p).preT[RECORDER_NEURON_ID], j, (*syn_p).postT[RECORDER_NEURON_ID], (*syn_p).gauss[RECORDER_NEURON_ID]);
#endif /* DEBUG_MODE_MAIN */
// ---- Prepare next run ----
// Event-based 1 (Update synapse: Delayed forward propagation)
// Transfer delayed pre-synaptic spikes to EE synapses
for( i = 0; i < (*spike_queue_p).no_events[offset]; i++){
//printf("number of events: %d\n", (*spike_queue_p).no_events[offset]);
//printf("No outgoing EE synapses for neuron(%d): %d\n", (*spike_queue_p).neuron_id[offset][i], (*lif_p).no_outgoing_ee_synapses[(*spike_queue_p).neuron_id[offset][i]]);
// Process each neuron which spiked (delay timesteps ago)
for ( k = 0; k < (*lif_p).no_outgoing_ee_synapses[ (*spike_queue_p).neuron_id[offset][i] ]; k++){
//if(i==0){
// printf("transferring delayed pre-synaptic spike to synapse(%d)\n", (*lif_p).outgoing_synapse_index[ (*spike_queue_p).neuron_id[offset][i] ][k]);
//}
(*syn_p).preT[ (*lif_p).outgoing_synapse_index[ (*spike_queue_p).neuron_id[offset][i] ][k] ] = 1;
#ifdef DEBUG_MODE_SPIKES
printf("Pre Spike (LIF %d) \n", (*spike_queue_p).neuron_id[offset][i]);
#endif /* DEBUG_MODE_SPIKES */
#ifdef ENABLE_SYNAPSE_UPDATES
//TODO: reenable updateEventBasedSynapse here
updateEventBasedSynapse(syn_p, syn_const_p, (*lif_p).outgoing_synapse_index[ (*spike_queue_p).neuron_id[offset][i] ][k], j);
#endif /* ENABLE_SYNAPSE_UPDATES */
}
}
// Event-based 2 (Reset delayed event queue)
// Reset delayed event queue
(*spike_queue_p).no_events[offset] = 0;
// Apply external voltage (this cannot be done in same loop as spike detection/propagation)
for( i = 0; i < (*lif_p).no_lifs; i++){
// Fixed external current
(*lif_p).I[i] = external_voltage;
#ifdef DEBUG_MODE_NETWORK
lif_gauss_totals[i] += (*lif_p).gauss[i];
#endif /* DEBUG_MODE_NETWORK */
//TODO: apply serialised external noise here
//(*lif_p).gauss[i] = gasdev(&gaussian_lif_seed);
}
// For a brief period apply stimulation to a subset of neurons
#ifdef LEARNING_EXTERN_CONST_STIM
// Using constant external voltage as stimulus
if((STIM_ON < (j * LIF_DT)) && ((j * LIF_DT) < STIM_OFF)){
for( i = 0; i < NO_STIM_LIFS; i++){
// The old constant stim code
(*lif_p).I[i + STIM_OFFSET] = J_STIM;
//printf("DEBUG: (j*LIF_DT) %f, i %d\n", (j*LIF_DT), i);
}
}
#endif /* LEARNING_EXTERN_CONST_STIM */
#ifdef LEARNING_INDEP_POISSON_STIM
// Using a repeated Poisson pattern as stimulus
if((STIM_ON < (j * LIF_DT)) && ((j * LIF_DT) < STIM_OFF)){
float wait_as_float;
int timesteps_to_next_spike;
pattern_time++;
// There are multiple stimulation subsets
for( k = 0; k < NO_STIM_SUBSETS; k++){
// Loop over the neurons within a subset
for( i = 0; i < NO_STIM_LIFS; i++){
if(pattern_time > STIM_PATTERN_DURATION){
// Pattern duration exceeded, restart the pattern
if(i == 0){
// RESET RND generator
do{
wait_as_float = expdev_resettable(&seed_resettable_pattern, 1, RAN2_RESETTABLE_SEED);
timesteps_to_next_spike = (int)(wait_as_float / (STIM_PATTERN_AV_RATE * (*lif_p).dt) + EPSILLON);
time_to_next_stim_spike[i] = timesteps_to_next_spike;
//printf("DEBUG1: time to next stim spike: %d, i: %d, j: %d\n", time_to_next_stim_spike[i], i, j);
} while (timesteps_to_next_spike <= 0);
}
else{
// Redraw wait times for all stim neurons
do{
wait_as_float = expdev_resettable(&seed_resettable_pattern, 0, RAN2_RESETTABLE_SEED);
timesteps_to_next_spike = (int)(wait_as_float / (STIM_PATTERN_AV_RATE * (*lif_p).dt) + EPSILLON);
time_to_next_stim_spike[i] = timesteps_to_next_spike;
//printf("DEBUG2: time to next stim spike: %d, i: %d, j: %d\n", time_to_next_stim_spike[i], i, j);
} while (timesteps_to_next_spike <= 0);
}
}
if(time_to_next_stim_spike[i] == 0){
// It's time for a spike, do it then draw waiting time until next one
(*lif_p).I[i + STIM_OFFSET] = stim_voltage;
do{
wait_as_float = expdev_resettable(&seed_resettable_pattern, 0, RAN2_RESETTABLE_SEED);
timesteps_to_next_spike = (int)(wait_as_float / (STIM_PATTERN_AV_RATE * (*lif_p).dt) + EPSILLON);
time_to_next_stim_spike[i] = timesteps_to_next_spike;
//printf("DEBUG3: time to next stim spike: %d, i: %d, j: %d\n", time_to_next_stim_spike[i], i, j);
} while (timesteps_to_next_spike <= 0);
}
else{
// No spike this time
time_to_next_stim_spike[i]--;
}
}
}// end of stim-subset loop
if ( pattern_time > STIM_PATTERN_DURATION){
pattern_time = 0;
}
}
#endif /* LEARNING_INDEP_POISSON_STIM */
#ifdef LEARNING_REPEATED_PATTERNED_STIM
// Using a repeated regular pattern as stimulus
if((STIM_ON < (j * LIF_DT)) && ((j * LIF_DT) < STIM_OFF)){
// There are multiple stimulation subsets, loop over them
for( int stim_pop_id = 0; stim_pop_id < NO_STIM_SUBSETS; stim_pop_id++){
patterned_stim_parameters[stim_pop_id].pattern_time++;
int stim_sub_pop_offset_from_zero = patterned_stim_parameters[stim_pop_id].stim_id_offset;
if (patterned_stim_parameters[stim_pop_id].pattern_time < patterned_stim_parameters[stim_pop_id].pattern_duration){
// Follow the pattern stimulation and generation protocol
for( i = 0; i < NO_STIM_LIFS; i++){
// indexing of time_to_next_stim_spike[] is per neuron in a particular stim sub-population
if(patterned_stim_parameters[stim_pop_id].time_to_next_stim_spike[i] == 0){
// It's time for a spike, do it then draw waiting time until next one
//(*lif_p).I[i + (STIM_OFFSET * stim_pop_id)] = stim_voltage;
(*lif_p).I[i + stim_sub_pop_offset_from_zero] = stim_voltage;
patterned_stim_parameters[stim_pop_id].time_to_next_stim_spike[i] = patterned_stim_parameters[stim_pop_id].per_neuron_isi;
//printf("DEBUG: spike, j = %d, i = %d, stim_pop_id = %d\n", j, i, stim_pop_id);
}
else{
// No spike this time
patterned_stim_parameters[stim_pop_id].time_to_next_stim_spike[i]--;
}
}
}
else if (patterned_stim_parameters[stim_pop_id].pattern_time < (patterned_stim_parameters[stim_pop_id].pattern_full_cycle_duration) ){
// Follow the pause protocol, ie don't stimulate
//printf("DEBUG: pause in pattern, j = %d\n", j);
}
else{
//printf("DEBUG: resetting pattern, j = %d\n", j);
// Pattern plus pause duration exceeded, restart the pattern
for ( i = 0; i < NO_STIM_LIFS; i++){
patterned_stim_parameters[stim_pop_id].time_to_next_stim_spike[i] = patterned_stim_parameters[stim_pop_id].inter_neuron_fixed_stepping_isi * i + patterned_stim_parameters[stim_pop_id].start_time_delay;
}
patterned_stim_parameters[stim_pop_id].pattern_time = 0;
}
} // end of stim-subset loop
}
#endif /* LEARNING_REPEATED_PATTERNED_STIM */
// Print to intracellular recorder file
// print: time, voltage, input current
fprintf(intracellular_output, "%d %f %d %f %f ", j, (*lif_p).V[RECORDER_NEURON_ID], (*lif_p).time_since_spike[RECORDER_NEURON_ID], (*lif_p).I[RECORDER_NEURON_ID], (*lif_p).gauss[RECORDER_NEURON_ID]);
int local_count = 0;
// Update LIFs: spike detection/propagation to post-synaptic lifs as well as pre- and post-lif neurons
for ( i = 0; i < (*lif_p).no_lifs; i++){
if((*lif_p).time_since_spike[i] == 0){
// New, ISI calculation code
isi = ( j - (*lif_p).time_of_last_spike[i] ) * (*lif_p).dt;
(*lif_p).time_of_last_spike[i] = j;
//CONSIDER: using local variables to point to I[], post_lif[], Jx[], etc. it cuts down on dereferencing!
//TODO: strongly consider implementing parallel spike transfer system
/*if(i==0){
printf("%d spiked\n", i);
}*/
// Event-based 3 (Update synapse: Backpropagation to synapse)
// Post-synaptic spike should backpropagate to its synapses with no delay
for ( k = 0; k < (*lif_p).no_incoming_synapses[i]; k++){
// as non EE based lifs are not added to incoming_synapses lists this is safe
(*syn_p).postT[(*lif_p).incoming_synapse_index[i][k]] = 1;
#ifdef DEBUG_MODE_SPIKES
printf("Post Spike (LIF %d) \n", i);
#endif /* DEBUG_MODE_SPIKES */
#ifdef ENABLE_SYNAPSE_UPDATES
//TODO: reenable updateEventBasedSynapse here
updateEventBasedSynapse(syn_p, syn_const_p, (*lif_p).incoming_synapse_index[i][k], j);
//if(i==0){
// printf("backprop to pre-lif synapse(%d)\n", (*lif_p).incoming_synapse_index[i][k]);
//}
#endif /* ENABLE_SYNAPSE_UPDATES */
}
// Transfer voltage change to post-synaptic neurons
for ( k = 0; k < (*lif_p).no_outgoing_ee_synapses[i]; k++){
// across plastic synapses
#ifdef ENABLE_SYNAPSE_UPDATES
// Event-based 4 (Update synapse: Update in advance of current transfer)
//TODO: reenable updateEventBasedSynapse here
updateEventBasedSynapse(syn_p, syn_const_p, (*lif_p).outgoing_synapse_index[i][k], j);
#endif /* ENABLE_SYNAPSE_UPDATES */
//#ifdef DEBUG_MODE_NETWORK
//Debug code
if ((*syn_p).post_lif[(*lif_p).outgoing_synapse_index[i][k]] == RECORDER_NEURON_ID){
//local_count++;
//TODO: change plastic versus fixed transfer voltage here
#ifndef ENABLE_FIXED_TRANSFERS
lif_currents_EE[j] += transfer_voltage * (*syn_p).rho[(*lif_p).outgoing_synapse_index[i][k]];
#endif /* ENABLE_FIXED_TRANSFERS */
#ifdef ENABLE_FIXED_TRANSFERS
#ifndef ENABLE_TRANSFER_RHO_INITIAL
lif_currents_EE[j] += transfer_voltage * SYN_RHO_FIXED; //(*syn_p).rho[(*lif_p).outgoing_synapse_index[i][k]];
#endif /* ENABLE_TRANSFER_RHO_INITIAL */
#ifdef ENABLE_TRANSFER_RHO_INITIAL
lif_currents_EE[j] += transfer_voltage * (*syn_p).rho_initial[(*lif_p).outgoing_synapse_index[i][k]];
#endif /* ENABLE_TRANSFER_RHO_INITIAL */
#endif /* ENABLE_FIXED_TRANSFERS */
//printf("DEBUG: synaptic transfer voltage: %f, rho: %f, transfer voltage: %fc\n", (transfer_voltage * (*syn_p).rho[(*lif_p).outgoing_synapse_index[i][k]]), (*syn_p).rho[(*lif_p).outgoing_synapse_index[i][k]], transfer_voltage);
//printf("DEBUG: total transfer voltage: %f, time: %f\n", lif_currents_EE[j], (j * LIF_DT));
local_count++;
}
//#endif /* DEBUG_MODE_NETWORK */
#ifdef DEBUG_MODE_SPIKES
printf("Spike transfer (LIF %d) \n", i);
#endif /* DEBUG_MODE_SPIKES */
//TODO: change plastic versus fixed transfer voltage here
#ifndef ENABLE_FIXED_TRANSFERS
(*lif_p).I[(*syn_p).post_lif[(*lif_p).outgoing_synapse_index[i][k]]] += transfer_voltage * (*syn_p).rho[(*lif_p).outgoing_synapse_index[i][k]];
#endif /* ENABLE_FIXED_TRANSFERS */
#ifdef ENABLE_FIXED_TRANSFERS
#ifndef ENABLE_TRANSFER_RHO_INITIAL
(*lif_p).I[(*syn_p).post_lif[(*lif_p).outgoing_synapse_index[i][k]]] += transfer_voltage * SYN_RHO_FIXED; //(*syn_p).rho[(*lif_p).outgoing_synapse_index[i][k]];
#endif /* ENABLE_TRANSFER_RHO_INITIAL */
#ifdef ENABLE_TRANSFER_RHO_INITIAL
(*lif_p).I[(*syn_p).post_lif[(*lif_p).outgoing_synapse_index[i][k]]] += transfer_voltage * (*syn_p).rho_initial[(*lif_p).outgoing_synapse_index[i][k]];
#endif /* ENABLE_TRANSFER_RHO_INITIAL */
#endif /* ENABLE_FIXED_TRANSFERS */
/*if(i==0){
printf("current transfer, I: %f, to post-synaptic neuron(%d)\n", (transfer_voltage * (*syn_p).rho[(*lif_p).outgoing_synapse_index[i][k]]), (*syn_p).post_lif[(*lif_p).outgoing_synapse_index[i][k]]);
}*/
}
for ( k = (*lif_p).no_outgoing_ee_synapses[i]; k < (*lif_p).no_outgoing_synapses[i]; k++){
// across fixed synapses
//#ifdef DEBUG_MODE_NETWORK
//Debug code
if((*fixed_syn_p).post_lif[ ((*lif_p).outgoing_synapse_index[i][k] - (*syn_const_p).no_syns) ] == RECORDER_NEURON_ID){
//local_count--;
if((i < NO_EXC) && (RECORDER_NEURON_ID >= NO_EXC)){ //E->I
lif_currents_IE[j] += (*fixed_syn_p).Jx[ ((*lif_p).outgoing_synapse_index[i][k] - (*syn_const_p).no_syns) ];
}
else if((i >= NO_EXC) && (RECORDER_NEURON_ID >= NO_EXC)){ //I->I
lif_currents_II[j] += (*fixed_syn_p).Jx[ ((*lif_p).outgoing_synapse_index[i][k] - (*syn_const_p).no_syns) ];
}
else if((i >= NO_EXC) && (RECORDER_NEURON_ID < NO_EXC)){ //I->E
lif_currents_EI[j] += (*fixed_syn_p).Jx[ ((*lif_p).outgoing_synapse_index[i][k] - (*syn_const_p).no_syns) ];
}
}
//#endif /* DEBUG_MODE_NETWORK */
// Alternative dereferencing for fixed synapses
(*lif_p).I[(*fixed_syn_p).post_lif[ ((*lif_p).outgoing_synapse_index[i][k] - (*syn_const_p).no_syns) ] ] += (*fixed_syn_p).Jx[ ((*lif_p).outgoing_synapse_index[i][k] - (*syn_const_p).no_syns) ];
#ifdef DEBUG_MODE_SPIKES
if(i==RECORDER_NEURON_ID){
printf("current transfer, I: %f, via fixed syn(%d) to post-synaptic neuron(%d)\n", ((*fixed_syn_p).Jx[ ((*lif_p).outgoing_synapse_index[i][k] - (*syn_const_p).no_syns) ]), (*lif_p).outgoing_synapse_index[i][k]-(*syn_const_p).no_syns, (*fixed_syn_p).post_lif[(*lif_p).outgoing_synapse_index[i][k] - (*syn_const_p).no_syns]);
}
#endif /* DEBUG_MODE_SPIKES */
}
// Event-based 5 (Add spike to delayed processing event queue)
// Add to pre-spike event queue
//CONSIDER: don't add non EE events to event queue (relative efficiencies depend on NO_INH<<NO_EXC and nu_i>nu_e)
(*spike_queue_p).neuron_id[offset][(*spike_queue_p).no_events[offset]] = i;
(*spike_queue_p).no_events[offset]++;
//Print to raster file
print_raster_spike(j, i, isi);
// Add to average spiking activity bins
/*if( (i < (NO_STIM_LIFS + STIM_OFFSET)) && (i > (STIM_OFFSET-1) ) ){ //Stim pop
lif_injection_spikes[(int) ( ( (*lif_p).dt / BIN_SIZE) * j + EPSILLON)]++;
}
else if(i < NO_EXC){ //Non-stim pop
summary_exc_spikes[(int)( ( (*lif_p).dt / BIN_SIZE ) * j + EPSILLON)]++;
}
else{ //INH pop
summary_inh_spikes[(int)( ( (*lif_p).dt / BIN_SIZE ) * j + EPSILLON)]++;
}*/
//CONSIDER: using Kahan formula for addition here to improve accuracy
// for now double is sufficient, it works to greater than 1 billion spikes per bin
#ifndef USE_SEPARATE_SUBPOP_PARAMS
if( i > (NO_EXC - 1)){ //INH pop
summary_inh_spikes[(int)( ( (*lif_p).dt / BIN_SIZE ) * j + EPSILLON)]++;
}
else if( (i > (NO_STIM_LIFS + STIM_OFFSET - 1)) || (i < (STIM_OFFSET) ) ){ //EXC pop
summary_exc_spikes[(int)( ( (*lif_p).dt / BIN_SIZE ) * j + EPSILLON)]++;
}
else { //Stim pop
lif_injection_spikes[(int) ( ( (*lif_p).dt / BIN_SIZE) * j + EPSILLON)]++;
}
#else
if( i > (NO_EXC - 1)){ //INH pop
summary_inh_spikes[(int)( ( (*lif_p).dt / BIN_SIZE ) * j + EPSILLON)]++;
}
else{ //EXC pop
int flag_stim_spike_recorder = 0;
int stim_sub_pop_offset_from_zero;
for( int stim_pop_id = 0; stim_pop_id < NO_STIM_SUBSETS; stim_pop_id++){
stim_sub_pop_offset_from_zero = patterned_stim_parameters[stim_pop_id].stim_id_offset;