CARLsim  6.1.0
CARLsim: a GPU-accelerated SNN simulator
carlsim.h
Go to the documentation of this file.
1 /* * Copyright (c) 2016 Regents of the University of California. All rights reserved.
2 *
3 * Redistribution and use in source and binary forms, with or without
4 * modification, are permitted provided that the following conditions
5 * are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * 3. The names of its contributors may not be used to endorse or promote
15 * products derived from this software without specific prior written
16 * permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
22 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
25 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
26 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 *
30 * *********************************************************************************************** *
31 * CARLsim
32 * created by: (MDR) Micah Richert, (JN) Jayram M. Nageswaran
33 * maintained by:
34 * (MA) Mike Avery <averym@uci.edu>
35 * (MB) Michael Beyeler <mbeyeler@uci.edu>,
36 * (KDC) Kristofor Carlson <kdcarlso@uci.edu>
37 * (TSC) Ting-Shuo Chou <tingshuc@uci.edu>
38 * (HK) Hirak J Kashyap <kashyaph@uci.edu>
39 *
40 * CARLsim v1.0: JM, MDR
41 * CARLsim v2.0/v2.1/v2.2: JM, MDR, MA, MB, KDC
42 * CARLsim3: MB, KDC, TSC
43 * CARLsim4: TSC, HK
44 * CARLsim5: HK, JX, KC
45 *
46 * CARLsim available from http://socsci.uci.edu/~jkrichma/CARLsim/
47 * Ver 12/31/2016
48 */
49 
50 #ifndef _CARLSIM_H_
51 #define _CARLSIM_H_
52 
53 #include <stdint.h> // uint64_t, uint32_t, etc.
54 #include <string> // std::string
55 #include <vector> // std::vector
56 #include <algorithm>
57 
58 #include <carlsim_definitions.h>
59 #include <carlsim_datastructures.h>
60 #include <carlsim_api.h>
61 
62 // include the following core functionalities instead of forward-declaring, so that the user only needs to include
63 // carlsim.h
64 #include <callback.h>
65 #include <poisson_rate.h>
66 #include <spike_monitor.h>
67 #include <neuron_monitor.h>
68 #include <connection_monitor.h>
69 #include <group_monitor.h>
70 #include <linear_algebra.h>
71 
72 class GroupMonitor;
73 class ConnectionMonitor;
74 class SpikeMonitor;
75 class SpikeGenerator;
76 
77 
78 
79 // Cross-platform definition (Linux, Windows)
80 #if defined(WIN32) || defined(WIN64)
81 #include <Windows.h>
82 
83 #include <float.h>
84 #include <time.h>
85 
86 //#ifndef isnan
87 //#define isnan(x) _isnan(x)
88 //#endif
89 //
90 //#ifndef isinf
91 //#define isinf(x) (!_finite(x))
92 //#endif
93 #ifndef srand48
94 #define srand48(x) srand(x)
95 #endif
96 
97 #ifndef drand48
98 #define drand48() (double(rand())/RAND_MAX)
99 #endif
100 
101 #else
102 
103 #include <pthread.h> // pthread
104 #include <sys/stat.h> // mkdir
105 #include <unistd.h> //unix thread affinity macros
106 
107 #endif
108 
109 //set by cMake option CARLSIM_LN_FIRING
110 //{ LN feat for CARLsim6
111 //#define LN_GET_FIRING
112 //#define LN_GET_FIRING_MT
113 //}
114 
115 
143 public:
144  // +++++ PUBLIC METHODS: CONSTRUCTOR / DESTRUCTOR +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ //
145 
179  CARLsim(const std::string& netName = "SNN", SimMode preferredSimMode = CPU_MODE, LoggerMode loggerMode = USER, int ithGPUs = 0, int randSeed = -1);
180  ~CARLsim();
181 
182 
183 
184  // +++++ PUBLIC METHODS: SETTING UP A SIMULATION ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ //
185 
251  short int connect(int grpId1, int grpId2, const std::string& connType, const RangeWeight& wt, float connProb,
252  const RangeDelay& delay=RangeDelay(1), const RadiusRF& radRF=RadiusRF(-1.0), bool synWtType=SYN_FIXED,
253  float mulSynFast=1.0f, float mulSynSlow=1.0f);
254 
262  short int connect(int grpId1, int grpId2, ConnectionGenerator* conn, bool synWtType=SYN_FIXED);
263 
270  short int connect(int grpId1, int grpId2, ConnectionGenerator* conn, float mulSynFast, float mulSynSlow,
271  bool synWtType=SYN_FIXED);
272 
281  short int connectCompartments(int grpIdLower, int grpIdUpper);
282 
283 
289  int createGroup(const std::string& grpName, int nNeur, int neurType, int preferredPartition = ANY, ComputingBackend preferredBackend = CPU_CORES);
290 
296  int createGroupLIF(const std::string& grpName, int nNeur, int neurType, int preferredPartition = ANY, ComputingBackend preferredBackend = CPU_CORES);
297 
319  int createGroup(const std::string& grpName, const Grid3D& grid, int neurType, int preferredPartition = ANY, ComputingBackend preferredBackend = CPU_CORES);
320 
331  int createGroupLIF(const std::string& grpName, const Grid3D& grid, int neurType, int preferredPartition = ANY, ComputingBackend preferredBackend = CPU_CORES);
332 
338  int createSpikeGeneratorGroup(const std::string& grpName, int nNeur, int neurType, int preferredPartition = ANY, ComputingBackend preferredBackend = CPU_CORES);
339 
356  int createSpikeGeneratorGroup(const std::string& grpName, const Grid3D& grid, int neurType, int preferredPartition = ANY, ComputingBackend preferredBackend = CPU_CORES);
357 
358 
372  void setConductances(bool isSet);
373 
390  void setConductances(bool isSet, int tdAMPA, int tdNMDA, int tdGABAa, int tdGABAb);
391 
412  void setConductances(bool isSet, int tdAMPA, int trNMDA, int tdNMDA, int tdGABAa, int trGABAb, int tdGABAb);
413 
414 #ifdef LN_I_CALC_TYPES
415 
427  void setConductances(int grpId, bool isSet);
428 
441  void setConductances(int grpId, bool isSet, int tdAMPA, int tdNMDA, int tdGABAa, int tdGABAb);
442 
457  void setConductances(int grpId, bool isSet, int tdAMPA, int trNMDA, int tdNMDA, int tdGABAa, int trGABAb, int tdGABAb);
458 
459 
469  void setACNE12(int grpId);
470 
471 
480  void setNM4weighted(int grpId, IcalcType type, float wDA=1.f, float w5HT=1.f, float wACh=1.f, float wNE=1.f, float wNorm=4.f, float wBase=1.0f);
481 
482 #endif
483 
512  void setHomeostasis(int grpId, bool isSet, float homeoScale, float avgTimeScale);
513 
528  void setHomeostasis(int grpId, bool isSet);
529 
547  void setHomeoBaseFiringRate(int grpId, float baseFiring, float baseFiringSD=0.0f);
548 
549  /*
550  * \brief Sets the integration method for the simulation
551  *
552  * This function specifies the integration method for the simulation. Currently, the chosen integration method
553  * will apply to all neurons in the network.
554  *
555  * The basic simulation time step is 1ms, meaning that spike times cannot be retrieved with sub-millisecond
556  * precision. However, the integration time step can be lower than 1ms, which is specified by numStepsPerMs.
557  * A numStepsPerMs set to 10 would take 10 integration steps per 1ms simulation time step.
558  *
559  * By default, the simulation will use Forward-Euler with an integration step of 0.5ms (i.e.,
560  * <tt>numStepsPerMs</tt>=2).
561  *
562  * Currently CARLsim supports the following integration methods:
563  * - FORWARD_EULER: The most basic, forward-Euler method. Suggested value for <tt>numStepsPerMs</tt>: >= 2.
564  * - RUNGE_KUTTA4: Fourth-order Runge-Kutta (aka classical Runge-Kutta, aka RK4).
565  * Suggested value for <tt>numStepsPerMs</tt>: >= 10.
566  *
567  * \STATE ::CONFIG_STATE
568  * \param[in] method the integration method to use
569  * \param[in] numStepsPerMs the number of integration steps per 1ms simulation time step
570  *
571  * \note Note that the higher numStepsPerMs the slower the simulation may be, due to increased computational load.
572  * \since v3.1
573  */
574  void setIntegrationMethod(integrationMethod_t method, int numStepsPerMs);
575 
582  void setNeuronParameters(int grpId, float izh_a, float izh_a_sd, float izh_b, float izh_b_sd,
583  float izh_c, float izh_c_sd, float izh_d, float izh_d_sd);
584 
591  void setNeuronParameters(int grpId, float izh_a, float izh_b, float izh_c, float izh_d);
592 
612  void setNeuronParameters(int grpId, float izh_C, float izh_k, float izh_vr, float izh_vt,
613  float izh_a, float izh_b, float izh_vpeak, float izh_c, float izh_d);
614 
644  void setNeuronParameters(int grpId, float izh_C, float izh_C_sd, float izh_k, float izh_k_sd,
645  float izh_vr, float izh_vr_sd, float izh_vt, float izh_vt_sd,
646  float izh_a, float izh_a_sd, float izh_b, float izh_b_sd,
647  float izh_vpeak, float izh_vpeak_sd, float izh_c, float izh_c_sd,
648  float izh_d, float izh_d_sd);
649 
661  void setNeuronParametersLIF(int grpId, int tau_m, int tau_ref=0, float vTh=1.0f, float vReset=0.0f, const RangeRmem& rMem = RangeRmem(1.0f));
662 
672  void setCompartmentParameters(int grpId, float couplingUp, float couplingDown);
673 
689  void setNeuromodulator(int grpId, float baseDP, float tauDP, float base5HT, float tau5HT,
690  float baseACh, float tauACh, float baseNE, float tauNE);
691 
692  // new
693  void setNeuromodulator(int grpId,
694  float baseDP, float tauDP, float releaseDP, bool activeDP,
695  float base5HT, float tau5HT, float release5HT, bool active5HT,
696  float baseACh, float tauACh, float releaseAch, bool activeACh,
697  float baseNE, float tauNE, float releaseNE, bool activeNE);
698 
707  void setNeuromodulator(int grpId, float tauDP = 100.0f, float tau5HT = 100.0f,
708  float tauACh = 100.0f, float tauNE = 100.0f);
709 
719  void setSTDP(int preGrpId, int postGrpId, bool isSet);
720 
730  void setSTDP(int preGrpId, int postGrpId, bool isSet, STDPType type, float alphaPlus, float tauPlus, float alphaMinus, float tauMinus);
731 
741  void setESTDP(int preGrpId, int postGrpId, bool isSet);
742 
756  void setESTDP(int preGrpId, int postGrpId, bool isSet, STDPType type, ExpCurve curve);
757 
758 #ifdef LN_I_CALC_TYPES
759 
769  void setESTDP(int preGrpId, int postGrpId, bool isSet, STDPType type, ExpCurve curve, PkaPlcModulation modulation);
770 
771  void setConnectionModulation(int preGrpId, int postGrpId, IcalcType icalcType);
772 
773 #endif
774 
775 
789  void setESTDP(int preGrpId, int postGrpId, bool isSet, STDPType type, TimingBasedCurve curve);
790 
791 
801  void setISTDP(int preGrpId, int postGrpId, bool isSet);
802 
816  void setISTDP(int preGrpId, int postGrpId, bool isSet, STDPType type, ExpCurve curve);
817 
831  void setISTDP(int preGrpId, int postGrpId, bool isSet, STDPType type, PulseCurve curve);
832 
862  void setSTP(int grpId, bool isSet, float STP_U, float STP_tau_u, float STP_tau_x);
863 
885  void setSTP(int grpId, bool isSet);
886 
887 #ifdef LN_I_CALC_TYPES
888 
931  void setNM4STP(int grpId, float wSTP_U[], float wSTP_tau_u[], float wSTP_tau_x[]);
932 #endif
933 
942  void setWeightAndWeightChangeUpdate(UpdateInterval wtANDwtChangeUpdateInterval, bool enableWtChangeDecay, float wtChangeDecay=0.9f);
943 
944 
945  // +++++ PUBLIC METHODS: RUNNING A SIMULATION ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ //
946 
955  int runNetwork(int nSec, int nMsec=0, bool printRunSummary=true);
956 
962  void setupNetwork();
963 
965  void setupNetworkMT();
966 
967  // +++++ PUBLIC METHODS: LOGGING / PLOTTING +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ //
968 
969  const FILE* getLogFpInf();
970  const FILE* getLogFpErr();
971  const FILE* getLogFpDeb();
972  const FILE* getLogFpLog();
973 
991  void saveSimulation(const std::string& fileName, bool saveSynapseInfo=true);
992 
1007  void setLogFile(const std::string& fileName);
1008 
1027  void setLogsFpCustom(FILE* fpInf=NULL, FILE* fpErr=NULL, FILE* fpDeb=NULL, FILE* fpLog=NULL);
1028 
1029 
1030 
1031  // +++++ PUBLIC METHODS: INTERACTING WITH A SIMULATION ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ //
1032 
1055  void biasWeights(short int connId, float bias, bool updateWeightRange=false);
1056 
1076  void loadSimulation(FILE* fid);
1077 
1095  //void resetSpikeCounter(int grpId);
1096 
1119  void scaleWeights(short int connId, float scale, bool updateWeightRange=false);
1120 
1153  ConnectionMonitor* setConnectionMonitor(int grpIdPre, int grpIdPost, const std::string& fname);
1154 
1192  void setExternalCurrent(int grpId, const std::vector<float>& current);
1193 
1222  void setExternalCurrent(int grpId, float current);
1223 
1232  GroupMonitor* setGroupMonitor(int grpId, const std::string& fname, int mode=0);
1233 
1258  //void setSpikeCounter(int grpId, int recordDur=-1);
1259 
1279  void setSpikeGenerator(int grpId, SpikeGenerator* spikeGenFunc);
1280 
1323  SpikeMonitor* setSpikeMonitor(int grpId, const std::string& fileName);
1324 
1367  NeuronMonitor* setNeuronMonitor(int grpId, const std::string& fileName);
1368 
1386  void setSpikeRate(int grpId, PoissonRate* spikeRate, int refPeriod=1);
1387 
1415  void setWeight(short int connId, int neurIdPre, int neurIdPost, float weight, bool updateWeightRange=false);
1416 
1438  void startTesting(bool updateWeights=true);
1439 
1452  void stopTesting();
1453 
1460  //void writePopWeights(std::string fname, int gIDpre, int gIDpost);
1461 
1462 
1463 
1464  // +++++ PUBLIC METHODS: GETTERS / SETTERS +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ //
1465 
1481  CARLsimState getCARLsimState();
1482 
1483 
1484 #ifdef LN_GET_FIRING
1485 
1493  void getFiring(std::vector<bool>& firing, int netId=-1);
1494 #endif
1495 
1496 #ifdef LN_GET_FIRING_MT
1497  void getFiringMT(std::vector<bool>& firing, int netId);
1498 #endif
1499 
1500 #ifdef LN_AXON_PLAST
1501 
1508  void findWavefrontPath(std::vector<int>& path, std::vector<float>& eligibility, int netId, int grpId, int startNId, int goalNId);
1509 
1521  bool updateDelays(int gGrpIdPre, int gGrpIdPost, std::vector<std::tuple<int, int, uint8_t>> connDelays);
1522 
1523 
1535  void printEntrails(char* buffer, unsigned length, int gGrpIdPre, int gGrpIdPost);
1536 
1537 #endif
1538 
1539 
1547  std::vector<float> getConductanceAMPA(int grpId);
1548 
1556  std::vector<float> getConductanceNMDA(int grpId);
1557 
1565  std::vector<float> getConductanceGABAa(int grpId);
1566 
1574  std::vector<float> getConductanceGABAb(int grpId);
1575 
1587  RangeDelay getDelayRange(short int connId);
1588 
1595  uint8_t* getDelays(int gIDpre, int gIDpost, int& Npre, int& Npost);
1596 
1597  // FIXME: This function is called in SNN::connect() at CONFIG_STATE, which violate the restriction
1610  Grid3D getGroupGrid3D(int grpId);
1611 
1618  int getGroupId(std::string grpName);
1619 
1626  std::string getGroupName(int grpId);
1627 
1646  Point3D getNeuronLocation3D(int neurId);
1647 
1654  int getMaxNumCompConnections();
1655 
1677  Point3D getNeuronLocation3D(int grpId, int relNeurId);
1678 
1687  int getNeuronId(int grpId, Point3D location);
1688 
1689 
1690 
1700  int getNumConnections();
1701 
1708  int getNumSynapticConnections(short int connectionId);
1709 
1717  int getNumGroups();
1718 
1726  int getNumNeurons();
1727 
1735  int getNumNeuronsReg();
1736 
1744  int getNumNeuronsRegExc();
1745 
1753  int getNumNeuronsRegInh();
1754 
1762  int getNumNeuronsGen();
1763 
1771  int getNumNeuronsGenExc();
1772 
1780  int getNumNeuronsGenInh();
1781 
1788  int getNumSynapses();
1789 
1796  int getGroupStartNeuronId(int grpId);
1797 
1804  int getGroupEndNeuronId(int grpId);
1805 
1812  int getGroupNumNeurons(int grpId);
1813 
1821  ConnSTDPInfo getConnSTDPInfo(int grpId);
1822 
1831  GroupNeuromodulatorInfo getGroupNeuromodulatorInfo(int grpId);
1832 
1839  int getSimTime();
1840 
1847  int getSimTimeSec();
1848 
1855  int getSimTimeMsec();
1856 
1897  //int* getSpikeCounter(int grpId);
1898 
1910  SpikeMonitor* getSpikeMonitor(int grpId);
1911 
1923  RangeWeight getWeightRange(short int connId);
1924 
1934  bool isConnectionPlastic(short int connId);
1935 
1945  bool isGroupWithHomeostasis(int grpId);
1946 
1947 
1948 
1958  bool isSimulationWithCOBA();
1959 
1970  bool isSimulationWithCUBA();
1971 
1972 #ifdef LN_I_CALC_TYPES
1973 
1974  bool isGroupWith(int grpId, IcalcType icalcType);
1975 
1976  bool isGroupWithCOBA(int grpId);
1977 
1978  bool isGroupWithCUBA(int grpId);
1979 
1980 
1990  IcalcType getIcalcType(int grpId);
1991 
1992  bool getConductanceConfig(int grpId, float& dAMPA, float& rNMDA, float& dNMDA, float& dGABAa, float& rGABAb, float& dGABAb);
1993 
1994  bool getConductanceConfig(int grpId, int& tdAMPA, int& trNMDA, int& tdNMDA, int& tdGABAa, int& trGABAb, int& tdGABAb);
1995 
1996 #endif
1997 
2004  bool isExcitatoryGroup(int grpId);
2005 
2012  bool isInhibitoryGroup(int grpId);
2013 
2020  bool isPoissonGroup(int grpId);
2021 
2022  // +++++ PUBLIC METHODS: SET DEFAULTS +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ //
2023 
2035  void setDefaultConductanceTimeConstants(int tdAMPA, int trNMDA, int tdNMDA, int tdGABAa, int trGABAb, int tdGABAb);
2036 
2043  void setDefaultHomeostasisParams(float homeoScale, float avgTimeScale);
2044 
2051  void setDefaultSaveOptions(std::string fileName, bool saveSynapseInfo);
2052 
2061  void setDefaultSTDPparams(float alphaPlus, float tauPlus, float alphaMinus, float tauMinus, STDPType stdpType);
2062 
2071  void setDefaultESTDPparams(float alphaPlus, float tauPlus, float alphaMinus, float tauMinus, STDPType stdpType);
2072 
2081  void setDefaultISTDPparams(float betaLTP, float betaLTD, float lambda, float delta, STDPType stdpType);
2082 
2104  void setDefaultSTPparams(int neurType, float STP_U, float STP_tau_u, float STP_tau_x);
2105 
2106 
2110  static int cudaDeviceCount();
2111 
2115  static void cudaDeviceDescription(unsigned ithGPU, const char **desc);
2116 
2117 
2118 
2119 private:
2120  // This class provides a pImpl for the CARLsim User API.
2121  // \see https://marcmutz.wordpress.com/translated-articles/pimp-my-pimpl/
2122  class Impl;
2123  Impl* _impl;
2124 };
2125 #endif
Class for generating Poisson spike trains.
Definition: poisson_rate.h:88
x86/x64 Multi Core Processor (LN20201016)
A struct for retrieving STDP related information of a connection.
CARLsim User Interface This class provides a user interface to the public sections of CARLsimCore sou...
Definition: carlsim.h:142
A struct to assign a timing-based E-STDP curve.
UpdateInterval
Update frequency for weights.
#define CARLSIM_API
Definition: carlsim_api.h:14
#define SYN_FIXED
a point in 3D space
STDPType
STDP flavors.
Class SpikeMonitor.
LoggerMode
Logger modes.
SimMode
simulation mode
A struct to arrange neurons on a 3D grid (a primitive cubic Bravais lattice with cubic side length 1)...
struct to assign a pulse I-STDP curve
Struct defines the minimum and maximum membrane resisatnces of the LIF neuron group.
User mode, for experiment-oriented simulations.
Class GroupMonitor.
IcalcType
input current calculation
integrationMethod_t
Integration methods.
a range struct for synaptic delays
A struct for retrieving neuromodulator information of a group.
A struct to assign exponential STDP curves.
Class ConnectionMonitor.
a range struct for synaptic weight magnitudes
ComputingBackend
computing backend
#define ANY
used for create* method to specify any GPU or a specific GPU
CARLsimState
CARLsim states.
model is run on CPU core(s)
A struct to specify the receptive field (RF) radius in 3 dimensions.