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