CARLsim  6.1.0
CARLsim: a GPU-accelerated SNN simulator
periodic_spikegen.cpp
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 #include <periodic_spikegen.h>
52 
53 #include <user_errors.h> // fancy error messages
54 #include <algorithm> // std::find
55 #include <vector> // std::vector
56 #include <cassert> // assert
57 
58 PeriodicSpikeGenerator::PeriodicSpikeGenerator(float rate, bool spikeAtZero) {
59  assert(rate>0);
60  // FIX: LN 20202002 does not work : assertion seems not to work from different context (compiler flag!?)
61  // assert(rate>.0f);
62  //}
63  rate_ = rate; // spike rate
64  // ISSUE: warning C4244: '=': conversion from 'float' to 'int', possible loss of data
65  // ISSUE: :\test\github\carlsim4\tools\spike_generators\periodic_spikegen.cpp(62): warning C4723: potential divide by 0
66  //isi_ = 1000/rate; // inter-spike interval in ms
67  // FIX1: LN20201002 explicit cast (trunc) (int)(float expression)
68  // FIX2: LN20201002 solve numerical
69 #ifndef INT_MAX // LN2021 g++
70 #define INT_MAX 2147483647
71 #endif
72  isi_ = std::abs(rate)<0.00001f ? INT_MAX : int(1000.f/rate); // inter-spike interval in ms
73 
74  spikeAtZero_ = spikeAtZero;
75 
76  checkFiringRate();
77 }
78 
79 int PeriodicSpikeGenerator::nextSpikeTime(CARLsim* sim, int grpId, int nid, int currentTime, int lastScheduledSpikeTime, int endOfTimeSlice) {
80 // fprintf(stderr,"currentTime: %u lastScheduled: %u\n",currentTime,lastScheduledSpikeTime);
81 
82  if (spikeAtZero_) {
83  // insert spike at t=0 for each neuron (keep track of neuron IDs to avoid getting stuck in infinite loop)
84  if (std::find(nIdFiredAtZero_.begin(), nIdFiredAtZero_.end(), nid)==nIdFiredAtZero_.end()) {
85  // spike at t=0 has not been scheduled yet for this neuron
86  nIdFiredAtZero_.push_back(nid);
87  return 0;
88  }
89  }
90 
91  // periodic spiking according to ISI
92  return lastScheduledSpikeTime+isi_;
93 }
94 
95 void PeriodicSpikeGenerator::checkFiringRate() {
96  UserErrors::assertTrue(rate_>0, UserErrors::MUST_BE_POSITIVE, "PeriodicSpikeGenerator", "Firing rate");
97 }
CARLsim User Interface This class provides a user interface to the public sections of CARLsimCore sou...
Definition: carlsim.h:142
static void assertTrue(bool statement, errorType errorIfAssertionFails, std::string errorFunc, std::string errorMsgPrefix="", std::string errorMsgSuffix="")
simple wrapper for assert statement
Definition: user_errors.cpp:15
#define INT_MAX
parameter must have positive value
Definition: user_errors.h:55
PeriodicSpikeGenerator(float rate, bool spikeAtZero=true)
PeriodicSpikeGenerator constructor.
int nextSpikeTime(CARLsim *sim, int grpId, int nid, int currentTime, int lastScheduledSpikeTime, int endOfTimeSlice)
schedules the next spike time