-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathactivitySimulator.cpp
195 lines (155 loc) · 5.63 KB
/
activitySimulator.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*
* ***
* Activity simulator for ActSim
***********************************/
#include "activitySimulator.hpp"
#include <boost/format.hpp>
#include <boost/python/make_constructor.hpp>
#include <boost/python/raw_function.hpp>
#include <math.h>
#include <random>
#include <vexcl/vexcl.hpp>
#include <vexcl/spmat.hpp>
namespace py = boost::python;
/*
***
* Simulator class
************************/
/* *********** *
* Constructor *
* *********** */
Simulator::Simulator() {
m_bRunning = false;
}
Simulator::Simulator(int numNeurons, std::vector<size_t> vecIndPtr, std::vector<int> vecIndices, std::vector<double> vecData, std::map<std::string, var> mapParam):
m_nNeurons(numNeurons), m_vecIndPtr(vecIndPtr), m_vecIndices(vecIndices), m_vecData(vecData), m_mapParam(mapParam) {
m_bRunning = false;
}
Simulator::~Simulator() {}
/* ************** *
* Set parameters *
* ************** */
void Simulator::setParam() {
// load parameters
for(const auto& iter : m_mapParam) {
if (iter.first == "Threshold") m_rThreshold = boost::get<double>(iter.second);
else if (iter.first == "Leak") m_rLeak = boost::get<double>(iter.second);
else if (iter.first == "Refrac") m_rRefrac = boost::get<double>(iter.second);
else if (iter.first == "SimulTime") m_rSimulTime = boost::get<double>(iter.second);
else if (iter.first == "TimeStep") m_rTimeStep = boost::get<double>(iter.second);
else if (iter.first == "NoiseStdDev") m_rNoiseStdDev = boost::get<double>(iter.second);
}
// compute remaining values
m_nTotStep = static_cast<int>(floor(m_rSimulTime / m_rTimeStep));
m_nRefrac = static_cast<int>(floor(m_rRefrac / m_rTimeStep));
m_rSqrtStep = sqrt(m_rTimeStep);
}
/* ********** *
* Simulation *
* ********** */
/* start */
void Simulator::start() {
if (!m_bRunning) {
m_bRunning = true;
std::cout << "Starting run" << std:: endl;
runSimulation();
}
}
/* running the simulation */
void Simulator::runSimulation() {
// initiate openCL
vex::Context ctx( vex::Filter::Type(CL_DEVICE_TYPE_GPU) && vex::Filter::DoublePrecision );
if (!ctx) throw std::runtime_error("No devices available.");
// cpu random number generator
std::default_random_engine generator;
std::normal_distribution<double> distribution(0.0,1.0);
// cpu vectors
std::vector<double> vecPotential = initPotential();
std::vector<double> vecRestPotential = initRestPotential();
std::vector<double> vecThreshold(m_nNeurons, m_rThreshold);
std::vector<int> vecZeros(m_nNeurons, 0);
//~ std::vector<double> vecZeros(m_nNeurons, 0.);
std::cout << "CPU vec initialized" << std::endl;
// GPU constants
//~ double rTimeStep = m_rTimeStep;
VEX_CONSTANT(ts, 0.001);
VEX_CONSTANT(rts, 0.01);
VEX_CONSTANT(l, 0.1);
// random noise generator
vex::RandomNormal<double> randNorm;
// GPU vectors
vex::vector<double> d_vecPotential(ctx, vecPotential);
vex::vector<double> d_vecRestPotential(ctx, vecRestPotential);
vex::vector<double> d_vecThreshold(ctx, vecThreshold);
vex::vector<int> d_vecActive(ctx, vecZeros);
vex::vector<int> d_vecRefractory(ctx, vecZeros);
// GPU matrices
vex::SpMat<double, int, size_t> d_matConnect(ctx.queue(), static_cast<size_t>(m_nNeurons), static_cast<size_t>(m_nNeurons), m_vecIndPtr.data(), m_vecIndices.data(), m_vecData.data());
vex::SpMat<double, int, size_t> d_matActionPotentials;
// run
for (int i = 0; i<m_nTotStep; ++i) {
std::cout << boost::format("Current step: %1%") % i << std::endl;
d_vecPotential += ( (d_vecRestPotential - d_vecPotential) * m_rTimeStep + randNorm(vex::element_index(0,m_nNeurons), std::rand()) * m_rSqrtStep ) / m_rLeak;
//~ d_vecPotential += d_vecRestPotential;
//~ for (int j=0; j<vecPotential.size(); ++j) {
//~ vecPotential[j] += ( (vecRestPotential[j] - vecPotential[j]) * 0.001 + distribution(generator) *0.01 ) / 0.1;
//~ }
}
}
/* action potential gestion */
VEX_FUNCTION(int, apGestion, (vex::SpMat<double, int, size_t>, d_matActionPotentials),
double sum = 0;
double myval = val[i];
for(size_t j = 0; j < n; ++j)
if (j != i) sum += fabs(val[j] - myval);
return sum;
);
/* ************** *
* Initialization *
* ************** */
/* Initialize the potentials */
std::vector<double> Simulator::initPotential() {
std::vector<double> vecPotential(m_nNeurons);
return vecPotential;
}
std::vector<double> Simulator::initRestPotential() {
std::vector<double> vecRestPotential(m_nNeurons);
return vecRestPotential;
}
/* ************* *
* Communication *
* ************* */
/* Send the results to DataProcessor */
py::object Simulator::get_results() {
py::object results;
return results;
}
/*
***
* Constructor (interface with Python)
********************************************/
std::shared_ptr<Simulator> Simulator_py(py::list lstCSR, py::object xmlRoot) {
// create the convertor and create c++ arguments
Convertor convertor = Convertor();
int numNeurons = convertor.getNumNeurons(lstCSR);
std::vector<double> vecData = convertor.getDataConnectMat(lstCSR);
std::vector<size_t> vecIndPtr = convertor.getIndPtrConnectMat(lstCSR);
std::vector<int> vecIndices = convertor.getIndicesConnectMat(lstCSR);
std::map<std::string, var> mapParam = convertor.convertParam(xmlRoot);
// call the constructor with the converted arguments
return std::shared_ptr<Simulator>(
new Simulator(numNeurons, vecIndPtr, vecIndices, vecData, mapParam)
);
}
/*
***
* Python module declaration
********************************************/
BOOST_PYTHON_MODULE(libsimulator) {
// using no_init postpones defining __init__ function
py::class_<Simulator>("Simulator", py::no_init)
.def("__init__", py::make_constructor(&Simulator_py))
.def("setParam", &Simulator::setParam)
.def("start", &Simulator::start)
;
}