55 #if defined(WIN32) || defined(WIN64) || defined(__APPLE__)
56 void SNN::spikeGeneratorUpdate_CPU(
int netId) {
58 void* SNN::spikeGeneratorUpdate_CPU(
int netId) {
60 assert(runtimeData[netId].allocated);
61 assert(runtimeData[netId].memType ==
CPU_MEM);
65 for (
int poisN = 0; poisN < networkConfigs[netId].
numNPois; poisN++) {
67 runtimeData[netId].
randNum[poisN] = drand48();
71 if (networkConfigs[netId].numNSpikeGen > 0) {
75 memset(managerRuntimeData.
spikeGenBits, 0,
sizeof(
int) * (networkConfigs[netId].numNSpikeGen / 32 + 1));
78 fillSpikeGenBits(netId);
81 memcpy(runtimeData[netId].spikeGenBits, managerRuntimeData.
spikeGenBits,
sizeof(
int) * (networkConfigs[netId].numNSpikeGen / 32 + 1));
85 #if !defined(WIN32) && !defined(WIN64) && !defined(__APPLE__) // Linux or MAC
87 void* SNN::helperSpikeGeneratorUpdate_CPU(
void* arguments) {
95 #if defined(WIN32) || defined(WIN64) || defined(__APPLE__)
96 void SNN::updateTimingTable_CPU(
int netId) {
98 void* SNN::updateTimingTable_CPU(
int netId) {
100 assert(runtimeData[netId].memType ==
CPU_MEM);
106 #if !defined(WIN32) && !defined(WIN64) && !defined(__APPLE__) // Linux or MAC
108 void* SNN::helperUpdateTimingTable_CPU(
void* arguments) {
231 #if defined(WIN32) || defined(WIN64) || defined(__APPLE__)
232 void SNN::convertExtSpikesD2_CPU(
int netId,
int startIdx,
int endIdx,
int GtoLOffset) {
234 void* SNN::convertExtSpikesD2_CPU(
int netId,
int startIdx,
int endIdx,
int GtoLOffset) {
236 int spikeCountExtRx = endIdx - startIdx;
244 for (
int extIdx = startIdx; extIdx < endIdx; extIdx++)
245 runtimeData[netId].firingTableD2[extIdx] += GtoLOffset;
248 #if !defined(WIN32) && !defined(WIN64) && !defined(__APPLE__) // Linux or MAC
250 void* SNN::helperConvertExtSpikesD2_CPU(
void* arguments) {
258 #if defined(WIN32) || defined(WIN64) || defined(__APPLE__)
259 void SNN::convertExtSpikesD1_CPU(
int netId,
int startIdx,
int endIdx,
int GtoLOffset) {
261 void* SNN::convertExtSpikesD1_CPU(
int netId,
int startIdx,
int endIdx,
int GtoLOffset) {
263 int spikeCountExtRx = endIdx - startIdx;
270 for (
int extIdx = startIdx; extIdx < endIdx; extIdx++)
271 runtimeData[netId].firingTableD1[extIdx] += GtoLOffset;
274 #if !defined(WIN32) && !defined(WIN64) && !defined(__APPLE__) // Linux or MAC
276 void* SNN::helperConvertExtSpikesD1_CPU(
void* arguments) {
284 #if defined(WIN32) || defined(WIN64) || defined(__APPLE__)
285 void SNN::clearExtFiringTable_CPU(
int netId) {
287 void* SNN::clearExtFiringTable_CPU(
int netId) {
289 assert(runtimeData[netId].memType ==
CPU_MEM);
291 memset(runtimeData[netId].extFiringTableEndIdxD1, 0,
sizeof(
int) * networkConfigs[netId].numGroups);
292 memset(runtimeData[netId].extFiringTableEndIdxD2, 0,
sizeof(
int) * networkConfigs[netId].numGroups);
295 #if !defined(WIN32) && !defined(WIN64) && !defined(__APPLE__) // Linux or MAC
297 void* SNN::helperClearExtFiringTable_CPU(
void* arguments) {
305 void SNN::copyTimeTable(
int netId,
bool toManager) {
309 memcpy(managerRuntimeData.
timeTableD2, runtimeData[netId].timeTableD2,
sizeof(
int) * (1000 + glbNetworkConfig.
maxDelay + 1));
310 memcpy(managerRuntimeData.
timeTableD1, runtimeData[netId].timeTableD1,
sizeof(
int) * (1000 + glbNetworkConfig.
maxDelay + 1));
312 memcpy(runtimeData[netId].timeTableD2, managerRuntimeData.
timeTableD2,
sizeof(
int)*(1000 + glbNetworkConfig.
maxDelay + 1));
313 memcpy(runtimeData[netId].timeTableD1, managerRuntimeData.
timeTableD1,
sizeof(
int)*(1000 + glbNetworkConfig.
maxDelay + 1));
317 void SNN::copyExtFiringTable(
int netId) {
320 memcpy(managerRuntimeData.
extFiringTableEndIdxD2, runtimeData[netId].extFiringTableEndIdxD2,
sizeof(
int) * networkConfigs[netId].numGroups);
321 memcpy(managerRuntimeData.
extFiringTableEndIdxD1, runtimeData[netId].extFiringTableEndIdxD1,
sizeof(
int) * networkConfigs[netId].numGroups);
322 memcpy(managerRuntimeData.
extFiringTableD2, runtimeData[netId].extFiringTableD2,
sizeof(
int*) * networkConfigs[netId].numGroups);
323 memcpy(managerRuntimeData.
extFiringTableD1, runtimeData[netId].extFiringTableD1,
sizeof(
int*) * networkConfigs[netId].numGroups);
331 #if defined(WIN32) || defined(WIN64) || defined(__APPLE__)
332 void SNN::resetSpikeCnt_CPU(
int netId,
int lGrpId) {
334 void* SNN::resetSpikeCnt_CPU(
int netId,
int lGrpId) {
336 assert(runtimeData[netId].memType ==
CPU_MEM);
339 memset(runtimeData[netId].nSpikeCnt, 0,
sizeof(
int) * networkConfigs[netId].numN);
341 int lStartN = groupConfigs[netId][lGrpId].
lStartN;
342 int numN = groupConfigs[netId][lGrpId].
numN;
343 memset(runtimeData[netId].nSpikeCnt + lStartN, 0,
sizeof(
int) * numN);
347 #if !defined(WIN32) && !defined(WIN64) && !defined(__APPLE__) // Linux or MAC
349 void* SNN::helperResetSpikeCnt_CPU(
void* arguments) {
359 #if defined(WIN32) || defined(WIN64) || defined(__APPLE__)
360 void SNN::doCurrentUpdateD1_CPU(
int netId) {
362 void* SNN::doCurrentUpdateD1_CPU(
int netId) {
364 assert(runtimeData[netId].memType ==
CPU_MEM);
366 int k = runtimeData[netId].
timeTableD1[simTimeMs + networkConfigs[netId].
maxDelay + 1] - 1;
367 int k_end = runtimeData[netId].
timeTableD1[simTimeMs + networkConfigs[netId].maxDelay];
369 while((k >= k_end) && (k >= 0)) {
382 assert(postNId < networkConfigs[netId].numNAssigned);
385 assert(synId < (runtimeData[netId].Npre[postNId]));
387 if (postNId < networkConfigs[netId].numN)
388 generatePostSynapticSpike(lNId , postNId, synId, 0, netId);
395 #if !defined(WIN32) && !defined(WIN64) && !defined(__APPLE__) // Linux or MAC
397 void* SNN::helperDoCurrentUpdateD1_CPU(
void* arguments) {
407 #if defined(WIN32) || defined(WIN64) || defined(__APPLE__)
408 void SNN::doCurrentUpdateD2_CPU(
int netId) {
410 void* SNN::doCurrentUpdateD2_CPU(
int netId) {
412 assert(runtimeData[netId].memType ==
CPU_MEM);
414 if (networkConfigs[netId].maxDelay > 1) {
415 int k = runtimeData[netId].
timeTableD2[simTimeMs + 1 + networkConfigs[netId].maxDelay] - 1;
416 int k_end = runtimeData[netId].
timeTableD2[simTimeMs + 1];
417 int t_pos = simTimeMs;
419 while ((k >= k_end) && (k >= 0)) {
424 while (!((k >= runtimeData[netId].timeTableD2[t_pos + networkConfigs[netId].maxDelay]) && (k < runtimeData[netId].timeTableD2[t_pos + networkConfigs[netId].maxDelay + 1]))) {
426 assert((t_pos + networkConfigs[netId].maxDelay - 1) >= 0);
431 int tD = simTimeMs - t_pos;
433 assert((tD < networkConfigs[netId].maxDelay) && (tD >= 0));
446 assert(postNId < networkConfigs[netId].numNAssigned);
449 assert(synId < (runtimeData[netId].Npre[postNId]));
451 if (postNId < networkConfigs[netId].numN)
452 generatePostSynapticSpike(lNId , postNId, synId, tD, netId);
460 #if !defined(WIN32) && !defined(WIN64) && !defined(__APPLE__) // Linux or MAC
462 void* SNN::helperDoCurrentUpdateD2_CPU(
void* arguments) {
470 #if defined(WIN32) || defined(WIN64) || defined(__APPLE__)
471 void SNN::doSTPUpdateAndDecayCond_CPU(
int netId) {
473 void* SNN::doSTPUpdateAndDecayCond_CPU(
int netId) {
475 assert(runtimeData[netId].memType ==
CPU_MEM);
478 for (
int lGrpId = 0; lGrpId < networkConfigs[netId].numGroups; lGrpId++) {
479 for(
int lNId = groupConfigs[netId][lGrpId].lStartN; lNId <= groupConfigs[netId][lGrpId].
lEndN; lNId++) {
480 if (groupConfigs[netId][lGrpId].WithSTP) {
483 runtimeData[netId].
stpu[ind_plus] = runtimeData[netId].
stpu[ind_minus] * (1.0f - groupConfigs[netId][lGrpId].
STP_tau_u_inv);
484 runtimeData[netId].
stpx[ind_plus] = runtimeData[netId].
stpx[ind_minus] + (1.0f - runtimeData[netId].
stpx[ind_minus]) * groupConfigs[netId][lGrpId].STP_tau_x_inv;
488 if (networkConfigs[netId].sim_with_conductances &&
IS_REGULAR_NEURON(lNId, networkConfigs[netId].numNReg, networkConfigs[netId].numNPois)) {
489 runtimeData[netId].
gAMPA[lNId] *= dAMPA;
490 if (sim_with_NMDA_rise) {
491 runtimeData[netId].
gNMDA_r[lNId] *= rNMDA;
492 runtimeData[netId].
gNMDA_d[lNId] *= dNMDA;
494 runtimeData[netId].
gNMDA[lNId] *= dNMDA;
497 runtimeData[netId].
gGABAa[lNId] *= dGABAa;
498 if (sim_with_GABAb_rise) {
499 runtimeData[netId].
gGABAb_r[lNId] *= rGABAb;
500 runtimeData[netId].
gGABAb_d[lNId] *= dGABAb;
502 runtimeData[netId].
gGABAb[lNId] *= dGABAb;
509 #if !defined(WIN32) && !defined(WIN64) && !defined(__APPLE__) // Linux or MAC
511 void* SNN::helperDoSTPUpdateAndDecayCond_CPU(
void* arguments) {
519 #if defined(WIN32) || defined(WIN64) || defined(__APPLE__)
520 void SNN::findFiring_CPU(
int netId) {
522 void* SNN::findFiring_CPU(
int netId) {
524 assert(runtimeData[netId].memType ==
CPU_MEM);
526 for(
int lGrpId = 0; lGrpId < networkConfigs[netId].numGroups; lGrpId++) {
527 for (
int lNId = groupConfigs[netId][lGrpId].lStartN; lNId <= groupConfigs[netId][lGrpId].
lEndN; lNId++) {
528 bool needToWrite =
false;
531 if(groupConfigs[netId][lGrpId].isSpikeGenFunc) {
532 unsigned int offset = lNId - groupConfigs[netId][lGrpId].
lStartN + groupConfigs[netId][lGrpId].
Noffset;
533 needToWrite = getSpikeGenBit(offset, netId);
535 needToWrite = getPoissonSpike(lNId, netId);
541 if (runtimeData[netId].curSpike[lNId]) {
542 runtimeData[netId].
curSpike[lNId] =
false;
547 if (networkConfigs[netId].sim_with_nm && lNId - groupConfigs[netId][lGrpId].lStartN <
MAX_NEURON_MON_GRP_SZIE) {
549 runtimeData[netId].
nVBuffer[idxBase + lNId - groupConfigs[netId][lGrpId].
lStartN] = runtimeData[netId].
voltage[lNId];
550 runtimeData[netId].
nUBuffer[idxBase + lNId - groupConfigs[netId][lGrpId].lStartN] = runtimeData[netId].
recovery[lNId];
557 bool hasSpace =
false;
561 if (groupConfigs[netId][lGrpId].MaxDelay == 1)
563 if (runtimeData[netId].spikeCountD1Sec + 1 < networkConfigs[netId].maxSpikesD1) {
568 if (runtimeData[netId].spikeCountD2Sec + runtimeData[netId].spikeCountLastSecLeftD2 + 1 < networkConfigs[netId].maxSpikesD2) {
578 if (groupConfigs[netId][lGrpId].MaxDelay == 1) {
585 if (groupConfigs[netId][lGrpId].hasExternalConnect) {
587 if (groupConfigs[netId][lGrpId].MaxDelay == 1) {
594 assert(extFireId != -1);
598 if (groupConfigs[netId][lGrpId].WithSTP) {
599 firingUpdateSTP(lNId, lGrpId, netId);
605 if (
IS_REGULAR_NEURON(lNId, networkConfigs[netId].numNReg, networkConfigs[netId].numNPois))
606 resetFiredNeuron(lNId, lGrpId, netId);
609 if (!sim_in_testing && groupConfigs[netId][lGrpId].WithSTDP) {
610 updateLTP(lNId, lGrpId, netId);
617 #if !defined(WIN32) && !defined(WIN64) && !defined(__APPLE__) // Linux or MAC
619 void* SNN::helperFindFiring_CPU(
void* arguments) {
628 void SNN::updateLTP(
int lNId,
int lGrpId,
int netId) {
629 unsigned int pos_ij = runtimeData[netId].
cumulativePre[lNId];
630 for(
int j = 0; j < runtimeData[netId].
Npre_plastic[lNId]; pos_ij++, j++) {
631 int stdp_tDiff = (simTime - runtimeData[netId].
synSpikeTime[pos_ij]);
632 assert(!((stdp_tDiff < 0) && (runtimeData[netId].synSpikeTime[pos_ij] !=
MAX_SIMULATION_TIME)));
634 if (stdp_tDiff > 0) {
636 if (groupConfigs[netId][lGrpId].WithESTDP && runtimeData[netId].maxSynWt[pos_ij] >= 0) {
638 switch (groupConfigs[netId][lGrpId].WithESTDPcurve) {
640 if (stdp_tDiff * groupConfigs[netId][lGrpId].TAU_PLUS_INV_EXC < 25)
641 runtimeData[netId].
wtChange[pos_ij] +=
STDP(stdp_tDiff, groupConfigs[netId][lGrpId].ALPHA_PLUS_EXC, groupConfigs[netId][lGrpId].TAU_PLUS_INV_EXC);
644 if (stdp_tDiff * groupConfigs[netId][lGrpId].TAU_PLUS_INV_EXC < 25) {
645 if (stdp_tDiff <= groupConfigs[netId][lGrpId].GAMMA)
646 runtimeData[netId].
wtChange[pos_ij] += groupConfigs[netId][lGrpId].
OMEGA + groupConfigs[netId][lGrpId].
KAPPA *
STDP(stdp_tDiff, groupConfigs[netId][lGrpId].ALPHA_PLUS_EXC, groupConfigs[netId][lGrpId].TAU_PLUS_INV_EXC);
648 runtimeData[netId].
wtChange[pos_ij] -=
STDP(stdp_tDiff, groupConfigs[netId][lGrpId].ALPHA_PLUS_EXC, groupConfigs[netId][lGrpId].TAU_PLUS_INV_EXC);
655 }
else if (groupConfigs[netId][lGrpId].WithISTDP && runtimeData[netId].maxSynWt[pos_ij] < 0) {
657 switch (groupConfigs[netId][lGrpId].WithISTDPcurve) {
659 if (stdp_tDiff * groupConfigs[netId][lGrpId].TAU_PLUS_INV_INB < 25) {
660 runtimeData[netId].
wtChange[pos_ij] -=
STDP(stdp_tDiff, groupConfigs[netId][lGrpId].ALPHA_PLUS_INB, groupConfigs[netId][lGrpId].TAU_PLUS_INV_INB);
664 if (stdp_tDiff <= groupConfigs[netId][lGrpId].LAMBDA) {
665 runtimeData[netId].
wtChange[pos_ij] -= groupConfigs[netId][lGrpId].
BETA_LTP;
667 }
else if (stdp_tDiff <= groupConfigs[netId][lGrpId].DELTA) {
668 runtimeData[netId].
wtChange[pos_ij] -= groupConfigs[netId][lGrpId].
BETA_LTD;
681 void SNN::firingUpdateSTP(
int lNId,
int lGrpId,
int netId) {
684 int ind_plus =
STP_BUF_POS(lNId, simTime, networkConfigs[netId].maxDelay);
685 int ind_minus =
STP_BUF_POS(lNId, (simTime - 1), networkConfigs[netId].maxDelay);
688 runtimeData[netId].
stpu[ind_plus] += groupConfigs[netId][lGrpId].
STP_U * (1.0f - runtimeData[netId].
stpu[ind_minus]);
691 runtimeData[netId].
stpx[ind_plus] -= runtimeData[netId].
stpu[ind_plus] * runtimeData[netId].
stpx[ind_minus];
694 void SNN::resetFiredNeuron(
int lNId,
short int lGrpId,
int netId) {
695 if (groupConfigs[netId][lGrpId].WithSTDP)
698 if (networkConfigs[netId].sim_with_homeostasis) {
704 bool SNN::getPoissonSpike(
int lNId,
int netId) {
708 return runtimeData[netId].
randNum[lNId - networkConfigs[netId].numNReg] * 1000.0f
709 < runtimeData[netId].
poissonFireRate[lNId - networkConfigs[netId].numNReg];
712 bool SNN::getSpikeGenBit(
unsigned int nIdPos,
int netId) {
713 const int nIdBitPos = nIdPos % 32;
714 const int nIdIndex = nIdPos / 32;
715 return ((runtimeData[netId].spikeGenBits[nIdIndex] >> nIdBitPos) & 0x1);
731 void SNN::generatePostSynapticSpike(
int preNId,
int postNId,
int synId,
int tD,
int netId) {
733 unsigned int pos = runtimeData[netId].
cumulativePre[postNId] + synId;
734 assert(postNId < networkConfigs[netId].numNReg);
737 short int post_grpId = runtimeData[netId].
grpIds[postNId];
738 short int pre_grpId = runtimeData[netId].
grpIds[preNId];
740 unsigned int pre_type = groupConfigs[netId][pre_grpId].
Type;
747 assert(mulIndex >= 0 && mulIndex < numConnections);
752 float change = runtimeData[netId].
wt[pos];
755 if (groupConfigs[netId][pre_grpId].WithSTP) {
762 int ind_minus =
STP_BUF_POS(preNId, (simTime-tD-1), networkConfigs[netId].maxDelay);
763 int ind_plus =
STP_BUF_POS(preNId, (simTime-tD), networkConfigs[netId].maxDelay);
765 change *= groupConfigs[netId][pre_grpId].
STP_A * runtimeData[netId].
stpu[ind_plus] * runtimeData[netId].
stpx[ind_minus];
778 if (sim_with_conductances) {
780 runtimeData[netId].
gAMPA [postNId] += change * mulSynFast[mulIndex];
782 if (sim_with_NMDA_rise) {
783 runtimeData[netId].
gNMDA_r[postNId] += change * sNMDA * mulSynSlow[mulIndex];
784 runtimeData[netId].
gNMDA_d[postNId] += change * sNMDA * mulSynSlow[mulIndex];
786 runtimeData[netId].
gNMDA [postNId] += change * mulSynSlow[mulIndex];
790 runtimeData[netId].
gGABAa[postNId] -= change * mulSynFast[mulIndex];
792 if (sim_with_GABAb_rise) {
793 runtimeData[netId].
gGABAb_r[postNId] -= change * sGABAb * mulSynSlow[mulIndex];
794 runtimeData[netId].
gGABAb_d[postNId] -= change * sGABAb * mulSynSlow[mulIndex];
796 runtimeData[netId].
gGABAb[postNId] -= change * mulSynSlow[mulIndex];
800 runtimeData[netId].
current[postNId] += change;
809 runtimeData[netId].
grpDA[post_grpId] += 0.04;
814 if (!sim_in_testing && groupConfigs[netId][post_grpId].WithSTDP) {
815 int stdp_tDiff = (simTime - runtimeData[netId].
lastSpikeTime[postNId]);
817 if (stdp_tDiff >= 0) {
820 switch (groupConfigs[netId][post_grpId].WithISTDPcurve) {
822 if (stdp_tDiff * groupConfigs[netId][post_grpId].TAU_MINUS_INV_INB < 25) {
823 runtimeData[netId].
wtChange[pos] -=
STDP(stdp_tDiff, groupConfigs[netId][post_grpId].ALPHA_MINUS_INB, groupConfigs[netId][post_grpId].TAU_MINUS_INV_INB);
827 if (stdp_tDiff <= groupConfigs[netId][post_grpId].LAMBDA) {
828 runtimeData[netId].
wtChange[pos] -= groupConfigs[netId][post_grpId].
BETA_LTP;
829 }
else if (stdp_tDiff <= groupConfigs[netId][post_grpId].DELTA) {
830 runtimeData[netId].
wtChange[pos] -= groupConfigs[netId][post_grpId].
BETA_LTD;
837 }
else if (groupConfigs[netId][post_grpId].WithESTDP && ((pre_type &
TARGET_AMPA) || (pre_type &
TARGET_NMDA))) {
839 switch (groupConfigs[netId][post_grpId].WithESTDPcurve) {
842 if (stdp_tDiff * groupConfigs[netId][post_grpId].TAU_MINUS_INV_EXC < 25)
843 runtimeData[netId].
wtChange[pos] +=
STDP(stdp_tDiff, groupConfigs[netId][post_grpId].ALPHA_MINUS_EXC, groupConfigs[netId][post_grpId].TAU_MINUS_INV_EXC);
851 assert(!((stdp_tDiff < 0) && (runtimeData[netId].lastSpikeTime[postNId] !=
MAX_SIMULATION_TIME)));
857 float dvdtIzhikevich4(
float volt,
float recov,
float totalCurrent,
float timeStep = 1.0f) {
858 return (((0.04f * volt + 5.0f) * volt + 140.0f - recov + totalCurrent) * timeStep);
863 float dudtIzhikevich4(
float volt,
float recov,
float izhA,
float izhB,
float timeStep = 1.0f) {
864 return (izhA * (izhB * volt - recov) * timeStep);
869 float dvdtIzhikevich9(
float volt,
float recov,
float invCapac,
float izhK,
float voltRest,
870 float voltInst,
float totalCurrent,
float timeStep = 1.0f)
872 return ((izhK * (volt - voltRest) * (volt - voltInst) - recov + totalCurrent) * invCapac * timeStep);
877 float dudtIzhikevich9(
float volt,
float recov,
float voltRest,
float izhA,
float izhB,
float timeStep = 1.0f) {
878 return (izhA * (izhB * (volt - voltRest) - recov) * timeStep);
883 float dvdtLIF(
float volt,
float lif_vReset,
float lif_gain,
float lif_bias,
int lif_tau_m,
float totalCurrent,
float timeStep = 1.0f) {
884 return ((lif_vReset -volt + ((totalCurrent * lif_gain) + lif_bias))/ (
float) lif_tau_m) * timeStep;
887 float SNN::getCompCurrent(
int netid,
int lGrpId,
int lneurId,
float const0,
float const1) {
888 float compCurrent = 0.0f;
892 int lGrpIdOther = groupConfigs[netid][lGrpId].
compNeighbors[k];
893 int lneurIdOther = lneurId - groupConfigs[netid][lGrpId].
lStartN + groupConfigs[netid][lGrpIdOther].
lStartN;
894 compCurrent += groupConfigs[netid][lGrpId].
compCoupling[k] * ((runtimeData[netid].
voltage[lneurIdOther] + const1)
895 - (runtimeData[netid].voltage[lneurId] + const0));
901 #if defined(WIN32) || defined(WIN64) || defined(__APPLE__)
902 void SNN::globalStateUpdate_CPU(
int netId) {
904 void* SNN::globalStateUpdate_CPU(
int netId) {
906 assert(runtimeData[netId].memType ==
CPU_MEM);
908 float timeStep = networkConfigs[netId].timeStep;
911 for (
int j = 1; j <= networkConfigs[netId].simNumStepsPerMs; j++) {
912 bool lastIter = (j == networkConfigs[netId].simNumStepsPerMs);
913 for (
int lGrpId = 0; lGrpId < networkConfigs[netId].numGroups; lGrpId++) {
915 if (groupConfigs[netId][lGrpId].WithHomeostasis & (lastIter)) {
916 for (
int lNId = groupConfigs[netId][lGrpId].lStartN; lNId <= groupConfigs[netId][lGrpId].
lEndN; lNId++)
917 runtimeData[netId].avgFiring[lNId] *= groupConfigs[netId][lGrpId].avgTimeScale_decay;
922 for (
int lNId = groupConfigs[netId][lGrpId].lStartN; lNId <= groupConfigs[netId][lGrpId].
lEndN; lNId++) {
923 assert(lNId < networkConfigs[netId].numNReg);
927 float v = runtimeData[netId].
voltage[lNId];
928 float v_next = runtimeData[netId].
nextVoltage[lNId];
929 float u = runtimeData[netId].
recovery[lNId];
930 float I_sum, NMDAtmp;
934 float k = runtimeData[netId].
Izh_k[lNId];
935 float vr = runtimeData[netId].
Izh_vr[lNId];
936 float vt = runtimeData[netId].
Izh_vt[lNId];
937 float inverse_C = 1.0f / runtimeData[netId].
Izh_C[lNId];
938 float vpeak = runtimeData[netId].
Izh_vpeak[lNId];
939 float a = runtimeData[netId].
Izh_a[lNId];
940 float b = runtimeData[netId].
Izh_b[lNId];
943 int lif_tau_m = runtimeData[netId].
lif_tau_m[lNId];
944 int lif_tau_ref = runtimeData[netId].
lif_tau_ref[lNId];
946 float lif_vTh = runtimeData[netId].
lif_vTh[lNId];
947 float lif_vReset = runtimeData[netId].
lif_vReset[lNId];
948 float lif_gain = runtimeData[netId].
lif_gain[lNId];
949 float lif_bias = runtimeData[netId].
lif_bias[lNId];
951 float totalCurrent = runtimeData[netId].
extCurrent[lNId];
953 if (networkConfigs[netId].sim_with_conductances) {
954 NMDAtmp = (v + 80.0f) * (v + 80.0f) / 60.0f / 60.0f;
955 gNMDA = (networkConfigs[netId].sim_with_NMDA_rise) ? (runtimeData[netId].gNMDA_d[lNId] - runtimeData[netId].gNMDA_r[lNId]) : runtimeData[netId].
gNMDA[lNId];
956 gGABAb = (networkConfigs[netId].sim_with_GABAb_rise) ? (runtimeData[netId].gGABAb_d[lNId] - runtimeData[netId].gGABAb_r[lNId]) : runtimeData[netId].
gGABAb[lNId];
958 I_sum = -(runtimeData[netId].
gAMPA[lNId] * (v - 0.0f)
959 + gNMDA * NMDAtmp / (1.0f + NMDAtmp) * (v - 0.0f)
960 + runtimeData[netId].gGABAa[lNId] * (v + 70.0f)
961 + gGABAb * (v + 90.0f));
963 totalCurrent += I_sum;
966 totalCurrent += runtimeData[netId].
current[lNId];
968 if (groupConfigs[netId][lGrpId].withCompartments) {
969 totalCurrent += getCompCurrent(netId, lGrpId, lNId);
972 switch (networkConfigs[netId].simIntegrationMethod) {
974 if (!groupConfigs[netId][lGrpId].withParamModel_9 && !groupConfigs[netId][lGrpId].isLIF)
978 if (v_next > 30.0f) {
980 runtimeData[netId].
curSpike[lNId] =
true;
981 v_next = runtimeData[netId].
Izh_c[lNId];
982 u += runtimeData[netId].
Izh_d[lNId];
985 else if (!groupConfigs[netId][lGrpId].isLIF)
988 v_next = v +
dvdtIzhikevich9(v, u, inverse_C, k, vr, vt, totalCurrent, timeStep);
989 if (v_next > vpeak) {
991 runtimeData[netId].
curSpike[lNId] =
true;
992 v_next = runtimeData[netId].
Izh_c[lNId];
993 u += runtimeData[netId].
Izh_d[lNId];
998 if (lif_tau_ref_c > 0){
1001 v_next = lif_vReset;
1005 if (v_next > lif_vTh) {
1006 runtimeData[netId].
curSpike[lNId] =
true;
1007 v_next = lif_vReset;
1017 v_next = v +
dvdtLIF(v, lif_vReset, lif_gain, lif_bias, lif_tau_m, totalCurrent, timeStep);
1022 if (groupConfigs[netId][lGrpId].isLIF){
1023 if (v_next < lif_vReset) v_next = lif_vReset;
1026 if (v_next < -90.0f) v_next = -90.0f;
1028 if (!groupConfigs[netId][lGrpId].withParamModel_9)
1041 if (!groupConfigs[netId][lGrpId].withParamModel_9 && !groupConfigs[netId][lGrpId].isLIF) {
1046 float k2 =
dvdtIzhikevich4(v + k1 / 2.0f, u + l1 / 2.0f, totalCurrent,
1048 float l2 =
dudtIzhikevich4(v + k1 / 2.0f, u + l1 / 2.0f, a, b, timeStep);
1050 float k3 =
dvdtIzhikevich4(v + k2 / 2.0f, u + l2 / 2.0f, totalCurrent,
1052 float l3 =
dudtIzhikevich4(v + k2 / 2.0f, u + l2 / 2.0f, a, b, timeStep);
1056 v_next = v + (1.0f / 6.0f) * (k1 + 2.0f * k2 + 2.0f * k3 + k4);
1057 if (v_next > 30.0f) {
1059 runtimeData[netId].
curSpike[lNId] =
true;
1060 v_next = runtimeData[netId].
Izh_c[lNId];
1061 u += runtimeData[netId].
Izh_d[lNId];
1063 if (v_next < -90.0f) v_next = -90.0f;
1065 u += (1.0f / 6.0f) * (l1 + 2.0f * l2 + 2.0f * l3 + l4);
1067 else if(!groupConfigs[netId][lGrpId].isLIF){
1073 float k2 =
dvdtIzhikevich9(v + k1 / 2.0f, u + l1 / 2.0f, inverse_C, k, vr, vt,
1074 totalCurrent, timeStep);
1075 float l2 =
dudtIzhikevich9(v + k1 / 2.0f, u + l1 / 2.0f, vr, a, b, timeStep);
1077 float k3 =
dvdtIzhikevich9(v + k2 / 2.0f, u + l2 / 2.0f, inverse_C, k, vr, vt,
1078 totalCurrent, timeStep);
1079 float l3 =
dudtIzhikevich9(v + k2 / 2.0f, u + l2 / 2.0f, vr, a, b, timeStep);
1082 totalCurrent, timeStep);
1085 v_next = v + (1.0f / 6.0f) * (k1 + 2.0f * k2 + 2.0f * k3 + k4);
1087 if (v_next > vpeak) {
1089 runtimeData[netId].
curSpike[lNId] =
true;
1090 v_next = runtimeData[netId].
Izh_c[lNId];
1091 u += runtimeData[netId].
Izh_d[lNId];
1094 if (v_next < -90.0f) v_next = -90.0f;
1096 u += (1.0f / 6.0f) * (l1 + 2.0f * l2 + 2.0f * l3 + l4);
1100 if (lif_tau_ref_c > 0){
1103 v_next = lif_vReset;
1107 if (v_next > lif_vTh) {
1108 runtimeData[netId].
curSpike[lNId] =
true;
1109 v_next = lif_vReset;
1119 v_next = v +
dvdtLIF(v, lif_vReset, lif_gain, lif_bias, lif_tau_m, totalCurrent, timeStep);
1122 if (v_next < lif_vReset) v_next = lif_vReset;
1131 runtimeData[netId].
recovery[lNId] = u;
1136 if (networkConfigs[netId].sim_with_conductances) {
1137 runtimeData[netId].
current[lNId] = I_sum;
1141 runtimeData[netId].
current[lNId] = 0.0f;
1146 if (groupConfigs[netId][lGrpId].WithHomeostasis)
1150 if (networkConfigs[netId].sim_with_nm && lNId - groupConfigs[netId][lGrpId].lStartN <
MAX_NEURON_MON_GRP_SZIE) {
1152 runtimeData[netId].
nIBuffer[idxBase + lNId - groupConfigs[netId][lGrpId].
lStartN] = totalCurrent;
1162 if ((groupConfigs[netId][lGrpId].WithESTDPtype ==
DA_MOD || groupConfigs[netId][lGrpId].WithISTDP ==
DA_MOD) && runtimeData[netId].grpDA[lGrpId] > groupConfigs[netId][lGrpId].baseDP) {
1163 runtimeData[netId].
grpDA[lGrpId] *= groupConfigs[netId][lGrpId].
decayDP;
1165 runtimeData[netId].
grpDABuffer[lGrpId * 1000 + simTimeMs] = runtimeData[netId].
grpDA[lGrpId];
1172 memcpy(runtimeData[netId].voltage, runtimeData[netId].nextVoltage,
sizeof(
float)*networkConfigs[netId].numNReg);
1177 #if !defined(WIN32) && !defined(WIN64) && !defined(__APPLE__) // Linux or MAC
1179 void* SNN::helperGlobalStateUpdate_CPU(
void* arguments) {
1188 #if defined(WIN32) || defined(WIN64) || defined(__APPLE__)
1189 void SNN::updateWeights_CPU(
int netId) {
1191 void* SNN::updateWeights_CPU(
int netId) {
1194 assert(sim_in_testing==
false);
1195 assert(sim_with_fixedwts==
false);
1196 assert(runtimeData[netId].memType ==
CPU_MEM);
1199 for (
int lGrpId = 0; lGrpId < networkConfigs[netId].numGroups; lGrpId++) {
1201 if (groupConfigs[netId][lGrpId].FixedInputWts || !(groupConfigs[netId][lGrpId].WithSTDP))
1204 for (
int lNId = groupConfigs[netId][lGrpId].lStartN; lNId <= groupConfigs[netId][lGrpId].
lEndN; lNId++) {
1205 assert(lNId < networkConfigs[netId].numNReg);
1206 unsigned int offset = runtimeData[netId].
cumulativePre[lNId];
1207 float diff_firing = 0.0;
1208 float homeostasisScale = 1.0;
1210 if (groupConfigs[netId][lGrpId].WithHomeostasis) {
1211 assert(runtimeData[netId].baseFiring[lNId] > 0);
1212 diff_firing = 1 - runtimeData[netId].
avgFiring[lNId] / runtimeData[netId].
baseFiring[lNId];
1216 if (lNId == groupConfigs[netId][lGrpId].lStartN)
1217 KERNEL_DEBUG(
"Weights, Change at %d (diff_firing: %f)", simTimeSec, diff_firing);
1219 for (
int j = 0; j < runtimeData[netId].
Npre_plastic[lNId]; j++) {
1222 float effectiveWtChange = stdpScaleFactor_ * runtimeData[netId].
wtChange[offset + j];
1228 switch (groupConfigs[netId][lGrpId].WithESTDPtype) {
1230 if (groupConfigs[netId][lGrpId].WithHomeostasis) {
1231 runtimeData[netId].
wt[offset + j] += (diff_firing*runtimeData[netId].
wt[offset + j] * homeostasisScale + runtimeData[netId].
wtChange[offset + j])*runtimeData[netId].baseFiring[lNId] / groupConfigs[netId][lGrpId].avgTimeScale / (1 + fabs(diff_firing) * 50);
1234 runtimeData[netId].
wt[offset + j] += effectiveWtChange;
1238 if (groupConfigs[netId][lGrpId].WithHomeostasis) {
1239 effectiveWtChange = runtimeData[netId].
grpDA[lGrpId] * effectiveWtChange;
1240 runtimeData[netId].
wt[offset + j] += (diff_firing*runtimeData[netId].
wt[offset + j] * homeostasisScale + effectiveWtChange)*runtimeData[netId].baseFiring[lNId] / groupConfigs[netId][lGrpId].avgTimeScale / (1 + fabs(diff_firing) * 50);
1242 runtimeData[netId].
wt[offset + j] += runtimeData[netId].
grpDA[lGrpId] * effectiveWtChange;
1251 switch (groupConfigs[netId][lGrpId].WithISTDPtype) {
1253 if (groupConfigs[netId][lGrpId].WithHomeostasis) {
1254 runtimeData[netId].
wt[offset + j] += (diff_firing*runtimeData[netId].
wt[offset + j] * homeostasisScale + runtimeData[netId].
wtChange[offset + j])*runtimeData[netId].baseFiring[lNId] / groupConfigs[netId][lGrpId].avgTimeScale / (1 + fabs(diff_firing) * 50);
1257 runtimeData[netId].
wt[offset + j] += effectiveWtChange;
1261 if (groupConfigs[netId][lGrpId].WithHomeostasis) {
1262 effectiveWtChange = runtimeData[netId].
grpDA[lGrpId] * effectiveWtChange;
1263 runtimeData[netId].
wt[offset + j] += (diff_firing*runtimeData[netId].
wt[offset + j] * homeostasisScale + effectiveWtChange)*runtimeData[netId].baseFiring[lNId] / groupConfigs[netId][lGrpId].avgTimeScale / (1 + fabs(diff_firing) * 50);
1265 runtimeData[netId].
wt[offset + j] += runtimeData[netId].
grpDA[lGrpId] * effectiveWtChange;
1276 runtimeData[netId].
wtChange[offset + j] *= wtChangeDecay_;
1279 if (runtimeData[netId].maxSynWt[offset + j] >= 0) {
1280 if (runtimeData[netId].wt[offset + j] >= runtimeData[netId].maxSynWt[offset + j])
1281 runtimeData[netId].
wt[offset + j] = runtimeData[netId].
maxSynWt[offset + j];
1282 if (runtimeData[netId].wt[offset + j] < 0)
1283 runtimeData[netId].
wt[offset + j] = 0.0;
1286 if (runtimeData[netId].wt[offset + j] <= runtimeData[netId].maxSynWt[offset + j])
1287 runtimeData[netId].
wt[offset + j] = runtimeData[netId].
maxSynWt[offset + j];
1288 if (runtimeData[netId].wt[offset + j] > 0)
1289 runtimeData[netId].
wt[offset + j] = 0.0;
1296 #if !defined(WIN32) && !defined(WIN64) && !defined(__APPLE__) // Linux or MAC
1298 void* SNN::helperUpdateWeights_CPU(
void* arguments) {
1310 #if defined(WIN32) || defined(WIN64) || defined(__APPLE__)
1311 void SNN::shiftSpikeTables_CPU(
int netId) {
1313 void* SNN::shiftSpikeTables_CPU(
int netId) {
1315 assert(runtimeData[netId].memType ==
CPU_MEM);
1318 for(
int p = runtimeData[netId].timeTableD2[999], k = 0; p < runtimeData[netId].
timeTableD2[999 + networkConfigs[netId].maxDelay + 1]; p++, k++) {
1322 for(
int i = 0; i < networkConfigs[netId].maxDelay; i++) {
1327 runtimeData[netId].
timeTableD1[networkConfigs[netId].maxDelay] = 0;
1340 #if !defined(WIN32) && !defined(WIN64) && !defined(__APPLE__) // Linux or MAC
1342 void* SNN::helperShiftSpikeTables_CPU(
void* arguments) {
1350 void SNN::allocateSNN_CPU(
int netId) {
1363 runtimeData[netId].
randNum =
new float[networkConfigs[netId].numNPois];
1371 copyPreConnectionInfo(netId,
ALL, &runtimeData[netId], &managerRuntimeData,
true);
1372 copyPostConnectionInfo(netId,
ALL, &runtimeData[netId], &managerRuntimeData,
true);
1377 copySynapseState(netId, &runtimeData[netId], &managerRuntimeData,
true);
1387 copyNeuronState(netId,
ALL, &runtimeData[netId],
true);
1392 copySTPState(netId,
ALL, &runtimeData[netId], &managerRuntimeData,
true);
1399 copyGroupState(netId,
ALL, &runtimeData[netId], &managerRuntimeData,
true);
1408 copyAuxiliaryData(netId,
ALL, &runtimeData[netId],
true);
1418 for (
int lGrpId = 0; lGrpId < networkConfigs[netId].numGroupsAssigned; lGrpId++) {
1419 KERNEL_DEBUG(
"Settings for Group %s:", groupConfigMap[groupConfigs[netId][lGrpId].gGrpId].grpName.c_str());
1421 KERNEL_DEBUG(
"\tType: %d",(
int)groupConfigs[netId][lGrpId].Type);
1422 KERNEL_DEBUG(
"\tNumN: %d",groupConfigs[netId][lGrpId].numN);
1423 KERNEL_DEBUG(
"\tM: %d",groupConfigs[netId][lGrpId].numPostSynapses);
1424 KERNEL_DEBUG(
"\tPreM: %d",groupConfigs[netId][lGrpId].numPreSynapses);
1425 KERNEL_DEBUG(
"\tspikeGenerator: %d",(
int)groupConfigs[netId][lGrpId].isSpikeGenerator);
1426 KERNEL_DEBUG(
"\tFixedInputWts: %d",(
int)groupConfigs[netId][lGrpId].FixedInputWts);
1427 KERNEL_DEBUG(
"\tMaxDelay: %d",(
int)groupConfigs[netId][lGrpId].MaxDelay);
1428 KERNEL_DEBUG(
"\tWithSTDP: %d",(
int)groupConfigs[netId][lGrpId].WithSTDP);
1429 if (groupConfigs[netId][lGrpId].WithSTDP) {
1431 KERNEL_DEBUG(
"\t\tTAU_PLUS_INV_EXC: %f",groupConfigs[netId][lGrpId].TAU_PLUS_INV_EXC);
1432 KERNEL_DEBUG(
"\t\tTAU_MINUS_INV_EXC: %f",groupConfigs[netId][lGrpId].TAU_MINUS_INV_EXC);
1433 KERNEL_DEBUG(
"\t\tALPHA_PLUS_EXC: %f",groupConfigs[netId][lGrpId].ALPHA_PLUS_EXC);
1434 KERNEL_DEBUG(
"\t\tALPHA_MINUS_EXC: %f",groupConfigs[netId][lGrpId].ALPHA_MINUS_EXC);
1436 KERNEL_DEBUG(
"\t\tTAU_PLUS_INV_INB: %f",groupConfigs[netId][lGrpId].TAU_PLUS_INV_INB);
1437 KERNEL_DEBUG(
"\t\tTAU_MINUS_INV_INB: %f",groupConfigs[netId][lGrpId].TAU_MINUS_INV_INB);
1438 KERNEL_DEBUG(
"\t\tALPHA_PLUS_INB: %f",groupConfigs[netId][lGrpId].ALPHA_PLUS_INB);
1439 KERNEL_DEBUG(
"\t\tALPHA_MINUS_INB: %f",groupConfigs[netId][lGrpId].ALPHA_MINUS_INB);
1440 KERNEL_DEBUG(
"\t\tLAMBDA: %f",groupConfigs[netId][lGrpId].LAMBDA);
1441 KERNEL_DEBUG(
"\t\tDELTA: %f",groupConfigs[netId][lGrpId].DELTA);
1442 KERNEL_DEBUG(
"\t\tBETA_LTP: %f",groupConfigs[netId][lGrpId].BETA_LTP);
1443 KERNEL_DEBUG(
"\t\tBETA_LTD: %f",groupConfigs[netId][lGrpId].BETA_LTD);
1445 KERNEL_DEBUG(
"\tWithSTP: %d",(
int)groupConfigs[netId][lGrpId].WithSTP);
1446 if (groupConfigs[netId][lGrpId].WithSTP) {
1447 KERNEL_DEBUG(
"\t\tSTP_U: %f", groupConfigs[netId][lGrpId].STP_U);
1451 KERNEL_DEBUG(
"\tspikeGen: %s", groupConfigs[netId][lGrpId].isSpikeGenFunc?
"is Set" :
"is not set ");
1476 void SNN::copyPreConnectionInfo(
int netId,
int lGrpId,
RuntimeData* dest,
RuntimeData* src,
bool allocateMem) {
1477 int lengthN, lengthSyn, posN, posSyn;
1479 if (lGrpId ==
ALL) {
1480 lengthN = networkConfigs[netId].numNAssigned;
1483 lengthN = groupConfigs[netId][lGrpId].
numN;
1484 posN = groupConfigs[netId][lGrpId].
lStartN;
1489 dest->
Npre =
new unsigned short[networkConfigs[netId].numNAssigned];
1490 memcpy(&dest->
Npre[posN], &src->
Npre[posN],
sizeof(
short) * lengthN);
1493 if (!sim_with_fixedwts) {
1496 dest->
Npre_plastic =
new unsigned short[networkConfigs[netId].numNAssigned];
1501 float* Npre_plasticInv =
new float[networkConfigs[netId].numNAssigned];
1503 for (
int i = 0; i < networkConfigs[netId].numNAssigned; i++)
1504 Npre_plasticInv[i] = 1.0f / managerRuntimeData.
Npre_plastic[i];
1506 dest->
Npre_plasticInv =
new float[networkConfigs[netId].numNAssigned];
1507 memcpy(dest->
Npre_plasticInv, Npre_plasticInv,
sizeof(
float) * networkConfigs[netId].numNAssigned);
1509 delete[] Npre_plasticInv;
1515 dest->
cumulativePre =
new unsigned int[networkConfigs[netId].numNAssigned];
1519 if (lGrpId ==
ALL) {
1520 lengthSyn = networkConfigs[netId].numPreSynNet;
1524 for (
int lNId = groupConfigs[netId][lGrpId].lStartN; lNId <= groupConfigs[netId][lGrpId].
lEndN; lNId++)
1525 lengthSyn += dest->
Npre[lNId];
1527 posSyn = dest->
cumulativePre[groupConfigs[netId][lGrpId].lStartN];
1551 void SNN::copyPostConnectionInfo(
int netId,
int lGrpId,
RuntimeData* dest,
RuntimeData* src,
bool allocateMem) {
1552 int lengthN, lengthSyn, posN, posSyn;
1554 if (lGrpId ==
ALL) {
1555 lengthN = networkConfigs[netId].numNAssigned;
1558 lengthN = groupConfigs[netId][lGrpId].
numN;
1559 posN = groupConfigs[netId][lGrpId].
lStartN;
1564 dest->
Npost =
new unsigned short[networkConfigs[netId].numNAssigned];
1565 memcpy(&dest->
Npost[posN], &src->
Npost[posN],
sizeof(
short) * lengthN);
1569 dest->
cumulativePost =
new unsigned int[networkConfigs[netId].numNAssigned];
1574 if (lGrpId ==
ALL) {
1575 lengthSyn = networkConfigs[netId].numPostSynNet;
1579 for (
int lNId = groupConfigs[netId][lGrpId].lStartN; lNId <= groupConfigs[netId][lGrpId].
lEndN; lNId++)
1580 lengthSyn += dest->
Npost[lNId];
1582 posSyn = dest->
cumulativePost[groupConfigs[netId][lGrpId].lStartN];
1612 assert(networkConfigs[netId].numPreSynNet > 0);
1616 dest->
wt =
new float[networkConfigs[netId].numPreSynNet];
1617 memcpy(dest->
wt, src->
wt,
sizeof(
float) * networkConfigs[netId].numPreSynNet);
1622 if (!sim_with_fixedwts) {
1625 dest->
wtChange =
new float[networkConfigs[netId].numPreSynNet];
1626 memcpy(dest->
wtChange, src->
wtChange,
sizeof(
float) * networkConfigs[netId].numPreSynNet);
1630 dest->
maxSynWt =
new float[networkConfigs[netId].numPreSynNet];
1631 memcpy(dest->
maxSynWt, src->
maxSynWt,
sizeof(
float) * networkConfigs[netId].numPreSynNet);
1651 void SNN::copyNeuronState(
int netId,
int lGrpId,
RuntimeData* dest,
bool allocateMem) {
1656 length = networkConfigs[netId].numNReg;
1659 ptrPos = groupConfigs[netId][lGrpId].
lStartN;
1660 length = groupConfigs[netId][lGrpId].
numN;
1663 assert(length <= networkConfigs[netId].numNReg);
1668 if(!allocateMem && groupConfigs[netId][lGrpId].Type &
POISSON_NEURON)
1672 dest->
recovery =
new float[length];
1673 memcpy(&dest->
recovery[ptrPos], &managerRuntimeData.
recovery[ptrPos],
sizeof(
float) * length);
1676 dest->
voltage =
new float[length];
1677 memcpy(&dest->
voltage[ptrPos], &managerRuntimeData.
voltage[ptrPos],
sizeof(
float) * length);
1685 dest->
current =
new float[length];
1686 memcpy(&dest->
current[ptrPos], &managerRuntimeData.
current[ptrPos],
sizeof(
float) * length);
1688 if (sim_with_conductances) {
1690 copyConductanceAMPA(netId, lGrpId, dest, &managerRuntimeData, allocateMem, 0);
1691 copyConductanceNMDA(netId, lGrpId, dest, &managerRuntimeData, allocateMem, 0);
1692 copyConductanceGABAa(netId, lGrpId, dest, &managerRuntimeData, allocateMem, 0);
1693 copyConductanceGABAb(netId, lGrpId, dest, &managerRuntimeData, allocateMem, 0);
1698 copyExternalCurrent(netId, lGrpId, dest, allocateMem);
1702 memcpy(&dest->
curSpike[ptrPos], &managerRuntimeData.
curSpike[ptrPos],
sizeof(
bool) * length);
1704 copyNeuronParameters(netId, lGrpId, dest, allocateMem);
1706 if (networkConfigs[netId].sim_with_nm)
1707 copyNeuronStateBuffer(netId, lGrpId, dest, &managerRuntimeData, allocateMem);
1709 if (sim_with_homeostasis) {
1714 memcpy(&dest->
avgFiring[ptrPos], &managerRuntimeData.
avgFiring[ptrPos],
sizeof(
float) * length);
1736 void SNN::copyConductanceAMPA(
int netId,
int lGrpId,
RuntimeData* dest,
RuntimeData* src,
bool allocateMem,
int destOffset) {
1743 length = networkConfigs[netId].numNReg;
1745 ptrPos = groupConfigs[netId][lGrpId].
lStartN;
1746 length = groupConfigs[netId][lGrpId].
numN;
1748 assert(length <= networkConfigs[netId].numNReg);
1752 assert(src->
gAMPA != NULL);
1754 dest->
gAMPA =
new float[length];
1755 memcpy(&dest->
gAMPA[ptrPos + destOffset], &src->
gAMPA[ptrPos],
sizeof(
float) * length);
1776 void SNN::copyConductanceNMDA(
int netId,
int lGrpId,
RuntimeData* dest,
RuntimeData* src,
bool allocateMem,
int destOffset) {
1783 length = networkConfigs[netId].numNReg;
1785 ptrPos = groupConfigs[netId][lGrpId].
lStartN;
1786 length = groupConfigs[netId][lGrpId].
numN;
1788 assert(length <= networkConfigs[netId].numNReg);
1794 dest->
gNMDA_r =
new float[length];
1795 memcpy(&dest->
gNMDA_r[ptrPos], &src->
gNMDA_r[ptrPos],
sizeof(
float) * length);
1799 dest->
gNMDA_d =
new float[length];
1800 memcpy(&dest->
gNMDA_d[ptrPos], &src->
gNMDA_d[ptrPos],
sizeof(
float) * length);
1802 assert(src->
gNMDA != NULL);
1804 dest->
gNMDA =
new float[length];
1805 memcpy(&dest->
gNMDA[ptrPos + destOffset], &src->
gNMDA[ptrPos],
sizeof(
float) * length);
1827 void SNN::copyConductanceGABAa(
int netId,
int lGrpId,
RuntimeData* dest,
RuntimeData* src,
bool allocateMem,
int destOffset) {
1834 length = networkConfigs[netId].numNReg;
1836 ptrPos = groupConfigs[netId][lGrpId].
lStartN;
1837 length = groupConfigs[netId][lGrpId].
numN;
1839 assert(length <= networkConfigs[netId].numNReg);
1842 assert(src->
gGABAa != NULL);
1844 dest->
gGABAa =
new float[length];
1845 memcpy(&dest->
gGABAa[ptrPos + destOffset], &src->
gGABAa[ptrPos],
sizeof(
float) * length);
1866 void SNN::copyConductanceGABAb(
int netId,
int lGrpId,
RuntimeData* dest,
RuntimeData* src,
bool allocateMem,
int destOffset) {
1871 if (lGrpId ==
ALL) {
1873 length = networkConfigs[netId].numNReg;
1875 ptrPos = groupConfigs[netId][lGrpId].
lStartN;
1876 length = groupConfigs[netId][lGrpId].
numN;
1878 assert(length <= networkConfigs[netId].numNReg);
1884 dest->
gGABAb_r =
new float[length];
1885 memcpy(&dest->
gGABAb_r[ptrPos], &src->
gGABAb_r[ptrPos],
sizeof(
float) * length);
1889 dest->
gGABAb_d =
new float[length];
1890 memcpy(&dest->
gGABAb_d[ptrPos], &src->
gGABAb_d[ptrPos],
sizeof(
float) * length);
1892 assert(src->
gGABAb != NULL);
1894 dest->
gGABAb =
new float[length];
1895 memcpy(&dest->
gGABAb[ptrPos + destOffset], &src->
gGABAb[ptrPos],
sizeof(
float) * length);
1916 void SNN::copyNeuronStateBuffer(
int netId,
int lGrpId,
RuntimeData* dest,
RuntimeData* src,
bool allocateMem) {
1928 if (lGrpId ==
ALL) {
1933 memcpy(&dest->
nVBuffer[ptrPos], &src->
nVBuffer[ptrPos],
sizeof(
float) * length);
1934 memcpy(&dest->
nUBuffer[ptrPos], &src->
nUBuffer[ptrPos],
sizeof(
float) * length);
1935 memcpy(&dest->
nIBuffer[ptrPos], &src->
nIBuffer[ptrPos],
sizeof(
float) * length);
1938 for (
int t = 0; t < 1000; t++) {
1946 memcpy(&dest->
nVBuffer[ptrPos], &src->
nVBuffer[ptrPos],
sizeof(
float) * length);
1947 memcpy(&dest->
nUBuffer[ptrPos], &src->
nUBuffer[ptrPos],
sizeof(
float) * length);
1948 memcpy(&dest->
nIBuffer[ptrPos], &src->
nIBuffer[ptrPos],
sizeof(
float) * length);
1971 void SNN::copyExternalCurrent(
int netId,
int lGrpId,
RuntimeData* dest,
bool allocateMem) {
1976 lengthN = networkConfigs[netId].numNReg;
1978 assert(lGrpId >= 0);
1979 posN = groupConfigs[netId][lGrpId].
lStartN;
1980 lengthN = groupConfigs[netId][lGrpId].
numN;
1982 assert(lengthN >= 0 && lengthN <= networkConfigs[netId].numNReg);
1984 KERNEL_DEBUG(
"copyExternalCurrent: lGrpId=%d, ptrPos=%d, length=%d, allocate=%s", lGrpId, posN, lengthN, allocateMem?
"y":
"n");
1988 memcpy(&(dest->
extCurrent[posN]), &(managerRuntimeData.
extCurrent[posN]),
sizeof(
float) * lengthN);
2009 void SNN::copyNeuronParameters(
int netId,
int lGrpId,
RuntimeData* dest,
bool allocateMem) {
2014 assert(lGrpId ==
ALL);
2015 assert(dest->
Izh_a == NULL);
2016 assert(dest->
Izh_b == NULL);
2017 assert(dest->
Izh_c == NULL);
2018 assert(dest->
Izh_d == NULL);
2019 assert(dest->
Izh_C == NULL);
2020 assert(dest->
Izh_k == NULL);
2021 assert(dest->
Izh_vr == NULL);
2022 assert(dest->
Izh_vt == NULL);
2027 assert(dest->
lif_vTh == NULL);
2035 length = networkConfigs[netId].numNReg;
2038 ptrPos = groupConfigs[netId][lGrpId].
lStartN;
2039 length = groupConfigs[netId][lGrpId].
numN;
2043 dest->
Izh_a =
new float[length];
2044 memcpy(&dest->
Izh_a[ptrPos], &(managerRuntimeData.
Izh_a[ptrPos]),
sizeof(
float) * length);
2047 dest->
Izh_b =
new float[length];
2048 memcpy(&dest->
Izh_b[ptrPos], &(managerRuntimeData.
Izh_b[ptrPos]),
sizeof(
float) * length);
2051 dest->
Izh_c =
new float[length];
2052 memcpy(&dest->
Izh_c[ptrPos], &(managerRuntimeData.
Izh_c[ptrPos]),
sizeof(
float) * length);
2055 dest->
Izh_d =
new float[length];
2056 memcpy(&dest->
Izh_d[ptrPos], &(managerRuntimeData.
Izh_d[ptrPos]),
sizeof(
float) * length);
2059 dest->
Izh_C =
new float[length];
2060 memcpy(&dest->
Izh_C[ptrPos], &(managerRuntimeData.
Izh_C[ptrPos]),
sizeof(
float) * length);
2063 dest->
Izh_k =
new float[length];
2064 memcpy(&dest->
Izh_k[ptrPos], &(managerRuntimeData.
Izh_k[ptrPos]),
sizeof(
float) * length);
2067 dest->
Izh_vr =
new float[length];
2068 memcpy(&dest->
Izh_vr[ptrPos], &(managerRuntimeData.
Izh_vr[ptrPos]),
sizeof(
float) * length);
2071 dest->
Izh_vt =
new float[length];
2072 memcpy(&dest->
Izh_vt[ptrPos], &(managerRuntimeData.
Izh_vt[ptrPos]),
sizeof(
float) * length);
2076 memcpy(&dest->
Izh_vpeak[ptrPos], &(managerRuntimeData.
Izh_vpeak[ptrPos]),
sizeof(
float) * length);
2081 memcpy(&dest->
lif_tau_m[ptrPos], &(managerRuntimeData.
lif_tau_m[ptrPos]),
sizeof(
int) * length);
2092 dest->
lif_vTh =
new float[length];
2093 memcpy(&dest->
lif_vTh[ptrPos], &(managerRuntimeData.
lif_vTh[ptrPos]),
sizeof(
float) * length);
2097 memcpy(&dest->
lif_vReset[ptrPos], &(managerRuntimeData.
lif_vReset[ptrPos]),
sizeof(
float) * length);
2100 dest->
lif_gain =
new float[length];
2101 memcpy(&dest->
lif_gain[ptrPos], &(managerRuntimeData.
lif_gain[ptrPos]),
sizeof(
float) * length);
2104 dest->
lif_bias =
new float[length];
2105 memcpy(&dest->
lif_bias[ptrPos], &(managerRuntimeData.
lif_bias[ptrPos]),
sizeof(
float) * length);
2108 if (sim_with_homeostasis) {
2109 float* baseFiringInv =
new float[length];
2110 for(
int nid = 0; nid < length; nid++) {
2111 if (managerRuntimeData.
baseFiring[nid] != 0.0f)
2112 baseFiringInv[nid] = 1.0f / managerRuntimeData.
baseFiring[ptrPos + nid];
2114 baseFiringInv[nid] = 0.0;
2119 memcpy(&dest->
baseFiringInv[ptrPos], baseFiringInv,
sizeof(
float) * length);
2125 delete [] baseFiringInv;
2150 assert(dest->
stpu == NULL);
2151 assert(dest->
stpx == NULL);
2153 assert(dest->
stpu != NULL);
2154 assert(dest->
stpx != NULL);
2156 assert(src->
stpu != NULL); assert(src->
stpx != NULL);
2159 dest->
stpu =
new float[networkConfigs[netId].numN * (networkConfigs[netId].maxDelay + 1)];
2160 memcpy(dest->
stpu, src->
stpu,
sizeof(
float) * networkConfigs[netId].numN * (networkConfigs[netId].maxDelay + 1));
2163 dest->
stpx =
new float[networkConfigs[netId].numN * (networkConfigs[netId].maxDelay + 1)];
2164 memcpy(dest->
stpx, src->
stpx,
sizeof(
float) * networkConfigs[netId].numN * (networkConfigs[netId].maxDelay + 1));
2188 dest->
grpDA =
new float[networkConfigs[netId].numGroups];
2189 dest->
grp5HT =
new float[networkConfigs[netId].numGroups];
2190 dest->
grpACh =
new float[networkConfigs[netId].numGroups];
2191 dest->
grpNE =
new float[networkConfigs[netId].numGroups];
2193 memcpy(dest->
grpDA, src->
grpDA,
sizeof(
float) * networkConfigs[netId].numGroups);
2194 memcpy(dest->
grp5HT, src->
grp5HT,
sizeof(
float) * networkConfigs[netId].numGroups);
2195 memcpy(dest->
grpACh, src->
grpACh,
sizeof(
float) * networkConfigs[netId].numGroups);
2196 memcpy(dest->
grpNE, src->
grpNE,
sizeof(
float) * networkConfigs[netId].numGroups);
2198 if (lGrpId ==
ALL) {
2201 dest->
grpDABuffer =
new float[1000 * networkConfigs[netId].numGroups];
2202 dest->
grp5HTBuffer =
new float[1000 * networkConfigs[netId].numGroups];
2203 dest->
grpAChBuffer =
new float[1000 * networkConfigs[netId].numGroups];
2204 dest->
grpNEBuffer =
new float[1000 * networkConfigs[netId].numGroups];
2211 assert(!allocateMem);
2239 void SNN::copyAuxiliaryData(
int netId,
int lGrpId,
RuntimeData* dest,
bool allocateMem) {
2240 assert(networkConfigs[netId].numN > 0);
2243 dest->
spikeGenBits =
new unsigned int[networkConfigs[netId].numNSpikeGen / 32 + 1];
2244 memset(dest->
spikeGenBits, 0,
sizeof(
int) * (networkConfigs[netId].numNSpikeGen / 32 + 1));
2249 memset(dest->
poissonFireRate, 0,
sizeof(
float) * networkConfigs[netId].numNPois);
2254 networkConfigs[netId].I_setLength = ceil(((networkConfigs[netId].maxNumPreSynN) / 32.0f));
2255 dest->
I_set =
new int[networkConfigs[netId].numNReg * networkConfigs[netId].I_setLength];
2257 assert(networkConfigs[netId].maxNumPreSynN >= 0);
2258 memset(dest->
I_set, 0,
sizeof(
int) * networkConfigs[netId].numNReg * networkConfigs[netId].I_setLength);
2262 dest->
synSpikeTime =
new int[networkConfigs[netId].numPreSynNet];
2269 dest->
lastSpikeTime =
new int[networkConfigs[netId].numNAssigned];
2273 copyNeuronSpikeCount(netId, lGrpId, dest, &managerRuntimeData,
true, 0);
2277 dest->
grpIds =
new short int[networkConfigs[netId].numNAssigned];
2278 memcpy(dest->
grpIds, managerRuntimeData.
grpIds,
sizeof(
short int) * networkConfigs[netId].numNAssigned);
2282 dest->
connIdsPreIdx =
new short int[networkConfigs[netId].numPreSynNet];
2323 dest->
firingTableD1 =
new int[networkConfigs[netId].maxSpikesD1];
2324 if (networkConfigs[netId].maxSpikesD1 > 0)
2329 dest->
firingTableD2 =
new int[networkConfigs[netId].maxSpikesD2];
2330 if (networkConfigs[netId].maxSpikesD2 > 0)
2336 memset(dest->
extFiringTableD1, 0 ,
sizeof(
int*) * networkConfigs[netId].numGroups);
2337 for (
int lGrpId = 0; lGrpId < networkConfigs[netId].numGroups; lGrpId++) {
2338 if (groupConfigs[netId][lGrpId].hasExternalConnect) {
2348 memset(dest->
extFiringTableD2, 0 ,
sizeof(
int*) * networkConfigs[netId].numGroups);
2349 for (
int lGrpId = 0; lGrpId < networkConfigs[netId].numGroups; lGrpId++) {
2350 if (groupConfigs[netId][lGrpId].hasExternalConnect) {
2387 void SNN::copyNeuronSpikeCount(
int netId,
int lGrpId,
RuntimeData* dest,
RuntimeData* src,
bool allocateMem,
int destOffset) {
2392 lengthN = networkConfigs[netId].numN;
2394 posN = groupConfigs[netId][lGrpId].
lStartN;
2395 lengthN = groupConfigs[netId][lGrpId].
numN;
2397 assert(lengthN > 0 && lengthN <= networkConfigs[netId].numN);
2402 memcpy(&dest->
nSpikeCnt[posN + destOffset], &src->
nSpikeCnt[posN],
sizeof(
int) * lengthN);
2406 #if defined(WIN32) || defined(WIN64) || defined(__APPLE__)
2407 void SNN::assignPoissonFiringRate_CPU(
int netId) {
2409 void* SNN::assignPoissonFiringRate_CPU(
int netId) {
2411 assert(runtimeData[netId].memType ==
CPU_MEM);
2413 for (
int lGrpId = 0; lGrpId < networkConfigs[netId].numGroups; lGrpId++) {
2415 if (groupConfigs[netId][lGrpId].isSpikeGenerator) {
2416 int lNId = groupConfigs[netId][lGrpId].
lStartN;
2417 int gGrpId = groupConfigs[netId][lGrpId].
gGrpId;
2418 PoissonRate* rate = groupConfigMDMap[gGrpId].ratePtr;
2421 if (groupConfigMap[gGrpId].spikeGenFunc || rate == NULL)
2424 assert(runtimeData[netId].poissonFireRate != NULL);
2425 assert(rate->
isOnGPU() ==
false);
2427 memcpy(&runtimeData[netId].poissonFireRate[lNId - networkConfigs[netId].numNReg], rate->
getRatePtrCPU(),
2433 #if !defined(WIN32) && !defined(WIN64) && !defined(__APPLE__) // Linux or MAC
2435 void* SNN::helperAssignPoissonFiringRate_CPU(
void* arguments) {
2457 void SNN::copyWeightState(
int netId,
int lGrpId) {
2458 int lengthSyn, posSyn;
2461 copyPreConnectionInfo(netId, lGrpId, &managerRuntimeData, &runtimeData[netId],
false);
2463 if (lGrpId ==
ALL) {
2464 lengthSyn = networkConfigs[netId].numPreSynNet;
2469 for (
int lNId = groupConfigs[netId][lGrpId].lStartN; lNId <= groupConfigs[netId][lGrpId].
lEndN; lNId++)
2470 lengthSyn += managerRuntimeData.
Npre[lNId];
2472 posSyn = managerRuntimeData.
cumulativePre[groupConfigs[netId][lGrpId].lStartN];
2475 assert(posSyn < networkConfigs[netId].numPreSynNet || networkConfigs[netId].numPreSynNet == 0);
2476 assert(lengthSyn <= networkConfigs[netId].numPreSynNet);
2478 memcpy(&managerRuntimeData.
wt[posSyn], &runtimeData[netId].wt[posSyn],
sizeof(
float) * lengthSyn);
2483 if ((!sim_with_fixedwts) || sim_with_stdp) {
2485 memcpy(&managerRuntimeData.
wtChange[posSyn], &runtimeData[netId].wtChange[posSyn],
sizeof(
float) * lengthSyn);
2489 void SNN::copyNetworkConfig(
int netId) {
2493 void SNN::copyGrpIdsLookupArray(
int netId) {
2494 memcpy(managerRuntimeData.
grpIds, runtimeData[netId].grpIds,
sizeof(
short int) * networkConfigs[netId].numNAssigned);
2497 void SNN::copyConnIdsLookupArray(
int netId) {
2498 memcpy(managerRuntimeData.
connIdsPreIdx, runtimeData[netId].connIdsPreIdx,
sizeof(
short int) * networkConfigs[netId].numPreSynNet);
2501 void SNN::copyLastSpikeTime(
int netId) {
2502 memcpy(managerRuntimeData.
lastSpikeTime, runtimeData[netId].lastSpikeTime,
sizeof(
int) * networkConfigs[netId].numN);
2508 void SNN::copyNetworkSpikeCount(
int netId,
2509 unsigned int* spikeCountD1,
unsigned int* spikeCountD2,
2510 unsigned int* spikeCountExtD1,
unsigned int* spikeCountExtD2) {
2523 void SNN::copySpikeTables(
int netId) {
2524 unsigned int spikeCountD1Sec, spikeCountD2Sec, spikeCountLastSecLeftD2;
2529 memcpy(managerRuntimeData.
firingTableD2, runtimeData[netId].firingTableD2,
sizeof(
int) * (spikeCountD2Sec + spikeCountLastSecLeftD2));
2530 memcpy(managerRuntimeData.
firingTableD1, runtimeData[netId].firingTableD1,
sizeof(
int) * spikeCountD1Sec);
2531 memcpy(managerRuntimeData.
timeTableD2, runtimeData[netId].timeTableD2,
sizeof(
int) * (1000 + networkConfigs[netId].maxDelay + 1));
2532 memcpy(managerRuntimeData.
timeTableD1, runtimeData[netId].timeTableD1,
sizeof(
int) * (1000 + networkConfigs[netId].maxDelay + 1));
2535 #if defined(WIN32) || defined(WIN64) || defined(__APPLE__)
2536 void SNN::deleteRuntimeData_CPU(
int netId) {
2538 void* SNN::deleteRuntimeData_CPU(
int netId) {
2540 assert(runtimeData[netId].memType ==
CPU_MEM);
2542 delete [] runtimeData[netId].
voltage;
2544 delete [] runtimeData[netId].
recovery;
2545 delete [] runtimeData[netId].
current;
2547 delete [] runtimeData[netId].
curSpike;
2548 delete [] runtimeData[netId].
Npre;
2551 delete [] runtimeData[netId].
Npost;
2555 delete [] runtimeData[netId].
wt;
2556 delete [] runtimeData[netId].
wtChange;
2557 delete [] runtimeData[netId].
maxSynWt;
2563 delete [] runtimeData[netId].
grpDA;
2564 delete [] runtimeData[netId].
grp5HT;
2565 delete [] runtimeData[netId].
grpACh;
2566 delete [] runtimeData[netId].
grpNE;
2573 if (networkConfigs[netId].sim_with_nm) {
2574 delete[] runtimeData[netId].
nVBuffer;
2575 delete[] runtimeData[netId].
nUBuffer;
2576 delete[] runtimeData[netId].
nIBuffer;
2579 delete [] runtimeData[netId].
grpIds;
2581 delete [] runtimeData[netId].
Izh_a;
2582 delete [] runtimeData[netId].
Izh_b;
2583 delete [] runtimeData[netId].
Izh_c;
2584 delete [] runtimeData[netId].
Izh_d;
2585 delete [] runtimeData[netId].
Izh_C;
2586 delete [] runtimeData[netId].
Izh_k;
2587 delete [] runtimeData[netId].
Izh_vr;
2588 delete [] runtimeData[netId].
Izh_vt;
2594 delete [] runtimeData[netId].
lif_vTh;
2596 delete [] runtimeData[netId].
lif_gain;
2597 delete [] runtimeData[netId].
lif_bias;
2599 delete [] runtimeData[netId].
gAMPA;
2600 if (sim_with_NMDA_rise) {
2601 delete [] runtimeData[netId].
gNMDA_r;
2602 delete [] runtimeData[netId].
gNMDA_d;
2605 delete [] runtimeData[netId].
gNMDA;
2607 delete [] runtimeData[netId].
gGABAa;
2608 if (sim_with_GABAb_rise) {
2609 delete [] runtimeData[netId].
gGABAb_r;
2610 delete [] runtimeData[netId].
gGABAb_d;
2613 delete [] runtimeData[netId].
gGABAb;
2616 delete [] runtimeData[netId].
stpu;
2617 delete [] runtimeData[netId].
stpx;
2624 delete [] runtimeData[netId].
I_set;
2636 tempPtrs =
new int*[networkConfigs[netId].numGroups];
2639 memcpy(tempPtrs, runtimeData[netId].extFiringTableD2,
sizeof(
int*) * networkConfigs[netId].numGroups);
2640 for (
int i = 0; i < networkConfigs[netId].numGroups; i++)
2641 delete [] tempPtrs[i];
2645 memcpy(tempPtrs, runtimeData[netId].extFiringTableD1,
sizeof(
int*) * networkConfigs[netId].numGroups);
2646 for (
int i = 0; i < networkConfigs[netId].numGroups; i++)
2647 delete [] tempPtrs[i];
2655 if (runtimeData[netId].randNum != NULL)
delete [] runtimeData[netId].
randNum;
2656 runtimeData[netId].
randNum = NULL;
2659 #if !defined(WIN32) && !defined(WIN64) && !defined(__APPLE__) // Linux or MAC
2661 void* SNN::helperDeleteRuntimeData_CPU(
void* arguments) {