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