CARLsim  6.1.0
CARLsim: a GPU-accelerated SNN simulator
snn_definitions.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 * CARLsim6: LN, JX, KC, KW
46 *
47 * CARLsim available from http://socsci.uci.edu/~jkrichma/CARLsim/
48 * Ver 12/31/2016
49 */
50 
51 #ifndef _SNN_DEFINITIONS_H_
52 #define _SNN_DEFINITIONS_H_
53 
54 
55 // TODO: as Kris put it, this should really be called something like
56 // some_random_macros_and_hardware_limitation_dependent_param_checks.h ... for example, the MAX_... defines
57 // should really be private members of SNN. These ranges are limited by the data structures that implement
58 // the corresponding functionality. For example, you can't just set MAX_CONN_PER_SNN > 32768, because connIds
59 // are stored as short int.
60 
61 // NEURON ORGANIZATION/ARRANGEMENT MAP
62 // <--- Excitatory --> | <-------- Inhibitory REGION ----------> | <-- Excitatory --> | <--------- EXTERNAL NEURONS --------->
63 // Excitatory-Regular | Inhibitory-Regular | Inhibitory-Poisson | Excitatory-Poisson |
64 // <--- numNExcReg --> | <-- numNInhReg --> | <-- numNInhPois -> | <---numNExcPois--> |
65 // <------REGULAR NEURON REGION ----------> | <----- POISSON NEURON REGION ---------> |
66 // <----numNReg=(numNExcReg+numNInhReg)---> | <--numNPois=(numNInhPois+numNExcPois)-> | <------------ numNExtern ------------>
67 // <------------------ ALL LOCAL NEURONS (numN=numNReg+numNPois) -------------------> | <- ALL EXTERNAL NEURONS (numNExtern)->
68 // <-------------------------------- ALL ASSIGNED NEURONS (numNAssigned=numN+numNExtern) ------------------------------------>
69 // Note: this organization scheme is only used/needed for the gpu_static code.
70 #define IS_POISSON_NEURON(nid, numNReg, numNPois) ((nid) >= (numNReg) && ((nid) < (numNReg + numNPois)))
71 #define IS_REGULAR_NEURON(nid, numNReg, numNPois) (((nid) < (numNReg)) && ((nid) < (numNReg + numNPois)))
72 #define IS_INHIBITORY(nid, numNInhPois, numNReg, numNExcReg, numN) (((nid) >= (numNExcReg)) && ((nid) < (numNReg + numNInhPois)))
73 #define IS_EXCITATORY(nid, numNInhPois, numNReg, numNExcReg, numN) (((nid) < (numNReg)) && (((nid) < (numNExcReg)) || ((nid) >= (numNReg + numNInhPois))))
74 #define IS_LOCAL_NEURON(nid, numN, numNAssigned) ((nid) < (numN))
75 #define IS_EXTERNAL_NEURON(nid, numN, numNAssigned) ((nid) >= (numN) && (nid) < (numNAssigned))
76 
77 #define STATIC_LOAD_START(n) (n.x)
78 #define STATIC_LOAD_GROUP(n) (n.y & 0xff)
79 #define STATIC_LOAD_SIZE(n) ((n.y >> 16) & 0xff)
80 
81 //#define MAX_NUMBER_OF_NEURONS_BITS (20)
82 //#define MAX_NUMBER_OF_GROUPS_BITS (32 - MAX_NUMBER_OF_NEURONS_BITS)
83 //#define MAX_NUMBER_OF_NEURONS_MASK ((1 << MAX_NUMBER_OF_NEURONS_BITS) - 1)
84 //#define MAX_NUMBER_OF_GROUPS_MASK ((1 << MAX_NUMBER_OF_GROUPS_BITS) - 1)
85 //#define SET_FIRING_TABLE(nid, gid) (((gid) << MAX_NUMBER_OF_NEURONS_BITS) | (nid))
86 //#define GET_FIRING_TABLE_NID(val) ((val) & MAX_NUMBER_OF_NEURONS_MASK)
87 //#define GET_FIRING_TABLE_GID(val) (((val) >> MAX_NUMBER_OF_NEURONS_BITS) & MAX_NUMBER_OF_GROUPS_MASK)
88 
90 #define CHECK_CONNECTION_ID(n,total) { assert(n >= 0); assert(n < total); }
91 
92 // Macros for STP
93 // we keep a history of STP values to compute resource change over time
94 // there are two problems to solve:
95 // 1) parallelism. we update postsynaptic current changes in synapse parallelism, but stpu and stpx need to be updated
96 // only once for each pre-neuron (in neuron parallelism)
97 // 2) non-zero delays. as a post-neuron you want the spike to be weighted by what the utility and resource
98 // variables were when pre spiked, not from the time at which the spike arrived at post.
99 // the macro is slightly faster than an inline function, but we should consider changing it anyway because
100 // it's unsafe
101 //#define STP_BUF_SIZE 32
102 // \FIXME D is the SNN member variable for the max delay in the network, give it a better name dammit!!
103 // we actually need D+1 entries. Say D=1ms. Then to update the current we need u^+ (right after the pre-spike, so
104 // at t) and x^- (right before the spike, so at t-1).
105 #define STP_BUF_POS(nid, t, maxDelay) (nid * (maxDelay + 1) + ((t + 1) % (maxDelay + 1)))
106 
107 // use these macros for logging / error printing
108 // every message will be printed to one of fpOut_, fpErr_, fpDeb_ depending on the nature of the message
109 // Additionally, every message gets printed to some log file fpLog_. This is different from fpDeb_ for
110 // the case in which you want the two to be different (e.g., developer mode, in which you would like to
111 // see all debug info (stdout) but also have it saved to a file
112 #define KERNEL_ERROR(formatc, ...) { KERNEL_ERROR_PRINT(fpErr_,formatc,##__VA_ARGS__); \
113  KERNEL_DEBUG_PRINT(fpLog_,"ERROR",formatc,##__VA_ARGS__); }
114 #define KERNEL_WARN(formatc, ...) { KERNEL_WARN_PRINT(fpErr_,formatc,##__VA_ARGS__); \
115  KERNEL_DEBUG_PRINT(fpLog_,"WARN",formatc,##__VA_ARGS__); }
116 #define KERNEL_INFO(formatc, ...) { KERNEL_INFO_PRINT(fpInf_,formatc,##__VA_ARGS__); \
117  KERNEL_DEBUG_PRINT(fpLog_,"INFO",formatc,##__VA_ARGS__); }
118 #define KERNEL_DEBUG(formatc, ...) { KERNEL_DEBUG_PRINT(fpDeb_,"DEBUG",formatc,##__VA_ARGS__); \
119  KERNEL_DEBUG_PRINT(fpLog_,"DEBUG",formatc,##__VA_ARGS__); }
120 
121 // cast to FILE* in case we're getting a const FILE* in
122 #define KERNEL_ERROR_PRINT(fp, formatc, ...) fprintf((FILE*)fp,"\033[31;1m[ERROR %s:%d] " formatc "\033[0m \n",__FILE__,__LINE__,##__VA_ARGS__)
123 #define KERNEL_WARN_PRINT(fp, formatc, ...) fprintf((FILE*)fp,"\033[33;1m[WARNING %s:%d] " formatc "\033[0m \n",__FILE__,__LINE__,##__VA_ARGS__)
124 #define KERNEL_INFO_PRINT(fp, formatc, ...) fprintf((FILE*)fp,formatc "\n",##__VA_ARGS__)
125 #define KERNEL_DEBUG_PRINT(fp, type, formatc, ...) fprintf((FILE*)fp,"[" type " %s:%d] " formatc "\n",__FILE__,__LINE__,##__VA_ARGS__)
126 
127 
128 #define MAX_NUM_POST_SYN 100000
129 #define MAX_NUM_PRE_SYN 200000
130 #define MAX_SYN_DELAY 20
131 
132 // increasing the following numbers will increase the load on constant memory
133 // until a hard limit is reached, which is given by the datatype of the variable
134 #ifdef LN_I_CALC_TYPES
135  // Fix issue: File uses too much global constant data (0x12140 bytes, 0x10000 max)
136 #define MAX_CONN_PER_SNN 128 // hard limit: 2^16
137 #define MAX_GRP_PER_SNN 96 // hard limit: 2^16
138 #else
139 #define MAX_CONN_PER_SNN 256 // hard limit: 2^16
140 #define MAX_GRP_PER_SNN 128 // hard limit: 2^16
141 #endif
142 #define MAX_NET_PER_SNN 32 // the maximum number of local networks in a simulation
143 
144 #ifdef __NO_CUDA__
145  #define CPU_RUNTIME_BASE 0
146 #else
147  #define CPU_RUNTIME_BASE 8
148 #endif
149 
150 #define NUM_CPU_CORES sysconf(_SC_NPROCESSORS_ONLN)
151 
152 #define GPU_RUNTIME_BASE 0
153 
154 #define COND_INTEGRATION_SCALE 2
155 
156 #define NEURON_MAX_FIRING_RATE 500
157 
158 #define STDP(t,a,b) ((a)*exp(-(t)*(b))) // consider to use __expf(), which is accelerated by GPU hardware
159 #ifdef LN_GPU_STDP
160  #define STDPf(t,a,b) ((a)*(__expf(-(t)*(b)))) // simple precision float device fuction that is faster on most GPUs
161 #else
162  #define STDPf(t,a,b) ((a)*exp(-(t)*(b)))
163 #endif
164 
165 #define MAX_TIME_SLICE 1000
166 #define MAX_SIMULATION_TIME INT_MAX
167 #define LARGE_NEGATIVE_VALUE (-(1 << 30))
168 
169 #define TIMING_COUNT 1024 // (1000+maxDelay_) rounded to multiple 128
170 
171 
172 #define MAX_SPIKE_MON_BUFFER_SIZE 52428800 // about 50 MB. size is in bytes. Max size of reduced AER vector in spikeMonitorCore objects.
173 #define LONG_SPIKE_MON_DURATION 600000 // about 10 minutes
174 #define LARGE_SPIKE_MON_GRP_SIZE 5000 // about 10 minutes
175 
176 #define MAX_NEURON_MON_BUFFER_SIZE 524288000 // about 500 MB. size is in bytes. (???)
177 #define LONG_NEURON_MON_DURATION 100000 // about 100 seconds
178 #define MAX_NEURON_MON_GRP_SZIE 128
179 
180 // This flag is used when having a common poisson generator for both CPU and GPU simulation
181 // We basically use the CPU poisson generator. Evaluate if there is any firing due to the
182 // poisson neuron. Copy that curFiring status to the GPU which uses that for evaluation
183 // of poisson firing
184 #define TESTING_CPU_GPU_POISSON (0)
185 
186 #define MAX_GRPS_PER_BLOCK 100
187 #define MAX_BLOCKS 120
188 
189 //#define CONN_SYN_NEURON_BITS 20 //!< last 20 bit denote neuron id. 1 Million neuron possible
190 //#define CONN_SYN_BITS (32 - CONN_SYN_NEURON_BITS) //!< remaining 12 bits denote connection id
191 //#define CONN_SYN_NEURON_MASK ((1 << CONN_SYN_NEURON_BITS) - 1)
192 //#define CONN_SYN_MASK ((1 << CONN_SYN_BITS) - 1)
193 //#define GET_CONN_NEURON_ID(a) (((unsigned int)a.postId) & CONN_SYN_NEURON_MASK)
194 //#define GET_CONN_SYN_ID(b) (((unsigned int)b.postId) >> CONN_SYN_NEURON_BITS)
195 //#define GET_CONN_GRP_ID(c) (c.grpId)
196 //#define SET_CONN_ID(a,b) ((b) > CONN_SYN_MASK) ? (fprintf(stderr, "Error: Syn Id exceeds maximum limit (%d)\n", CONN_SYN_MASK)): (((b)<<CONN_SYN_NEURON_BITS)+((a)&CONN_SYN_NEURON_MASK))
197 
198 #define GROUP_ID_MASK 0x0000ffff
199 #define SYNAPSE_ID_MASK 0x0000ffff
200 #define MAX_SYN_PER_NEURON 65535
201 #define NUM_SYNAPSE_BITS (16)
202 
203 #define GET_CONN_NEURON_ID(val) (val.nId)
204 #define GET_CONN_SYN_ID(val) (val.gsId & SYNAPSE_ID_MASK)
205 #define GET_CONN_GRP_ID(val) ((val.gsId >> NUM_SYNAPSE_BITS) & GROUP_ID_MASK)
206 
207 #define CONNECTION_INITWTS_RANDOM 0
208 #define CONNECTION_CONN_PRESENT 1
209 #define CONNECTION_FIXED_PLASTIC 2
210 #define CONNECTION_INITWTS_RAMPUP 3
211 #define CONNECTION_INITWTS_RAMPDOWN 4
212 
213 #define SET_INITWTS_RANDOM(a) ((a & 1) << CONNECTION_INITWTS_RANDOM)
214 #define SET_CONN_PRESENT(a) ((a & 1) << CONNECTION_CONN_PRESENT)
215 #define SET_FIXED_PLASTIC(a) ((a & 1) << CONNECTION_FIXED_PLASTIC)
216 #define SET_INITWTS_RAMPUP(a) ((a & 1) << CONNECTION_INITWTS_RAMPUP)
217 #define SET_INITWTS_RAMPDOWN(a) ((a & 1) << CONNECTION_INITWTS_RAMPDOWN)
218 
219 #define GET_INITWTS_RANDOM(a) (((a) >> CONNECTION_INITWTS_RANDOM) & 1)
220 #define GET_CONN_PRESENT(a) (((a) >> CONNECTION_CONN_PRESENT) & 1)
221 #define GET_FIXED_PLASTIC(a) (((a) >> CONNECTION_FIXED_PLASTIC) & 1)
222 #define GET_INITWTS_RAMPUP(a) (((a) >> CONNECTION_INITWTS_RAMPUP) & 1)
223 #define GET_INITWTS_RAMPDOWN(a) (((a) >> CONNECTION_INITWTS_RAMPDOWN) & 1)
224 
225 #endif