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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
|
#include "online_crossbar.h"
#include "crossbar_generator.h"
#include "read_crossbar.h"
#include <ctype.h>
#include <math.h>
#include <ngspice/sharedspice.h>
#include <pthread.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Online_Crossbar {
Online_Crossbar_Config config;
double *resistances;
conductance_mapping mapping;
double *state_ring;
double *results;
size_t published_count;
size_t consumed_count;
size_t result_count;
int started;
int simulation_done;
int failed;
int mutex_initialized;
int state_condition_initialized;
int progress_condition_initialized;
pthread_mutex_t mutex;
pthread_cond_t state_available;
pthread_cond_t progress;
};
static int callback_text(char *text, int ident, void *user_data)
{
(void)ident;
(void)user_data;
if (text &&
(strncmp(text, "stderr", 6) == 0 || strstr(text, "Error") != NULL ||
strstr(text, "error") != NULL))
fprintf(stderr, "ngspice: %s\n", text);
return 0;
}
static int callback_init_data(pvecinfoall values, int ident, void *user_data)
{
(void)values;
(void)ident;
(void)user_data;
return 0;
}
static void fail_locked(Online_Crossbar *crossbar)
{
crossbar->failed = 1;
pthread_cond_broadcast(&crossbar->state_available);
pthread_cond_broadcast(&crossbar->progress);
}
static int callback_exit(int status, NG_BOOL immediate, NG_BOOL quit_exit,
int ident, void *user_data)
{
(void)status;
(void)immediate;
(void)quit_exit;
(void)ident;
Online_Crossbar *crossbar = user_data;
pthread_mutex_lock(&crossbar->mutex);
fail_locked(crossbar);
pthread_mutex_unlock(&crossbar->mutex);
return 0;
}
static int callback_background(NG_BOOL running, int ident, void *user_data)
{
(void)ident;
Online_Crossbar *crossbar = user_data;
/* libngspice passes false at worker start and true at worker exit. */
if (!running)
return 0;
pthread_mutex_lock(&crossbar->mutex);
crossbar->simulation_done = 1;
if (crossbar->result_count < crossbar->config.num_timesteps)
fail_locked(crossbar);
pthread_cond_broadcast(&crossbar->progress);
pthread_cond_broadcast(&crossbar->state_available);
pthread_mutex_unlock(&crossbar->mutex);
return 0;
}
static int row_from_name(const char *name, size_t *row_out)
{
const char *digits = name + strlen(name);
while (digits > name && isdigit((unsigned char)digits[-1]))
digits--;
if (*digits == '\0')
return -1;
char *end = NULL;
unsigned long value = strtoul(digits, &end, 10);
if (end == digits || *end != '\0')
return -1;
*row_out = (size_t)value;
return 0;
}
static size_t timestep_for_time(const Online_Crossbar *crossbar, double time)
{
if (time <= 0.0)
return 0;
double scaled = time / crossbar->config.time_step;
/* Hold state[t] over the full interval (t*dt, (t+1)*dt]. */
double interval = ceil(scaled - 1.0e-9);
size_t timestep = interval <= 1.0 ? 0 : (size_t)interval - 1;
if (timestep >= crossbar->config.num_timesteps)
timestep = crossbar->config.num_timesteps - 1;
return timestep;
}
static int callback_voltage(double *voltage, double time, char *name, int ident,
void *user_data)
{
(void)ident;
Online_Crossbar *crossbar = user_data;
size_t row;
if (!name || row_from_name(name, &row) != 0 ||
row >= crossbar->config.num_neurons) {
fprintf(stderr, "ngspice: unrecognized external source %s\n",
name ? name : "(null)");
return 1;
}
size_t timestep = timestep_for_time(crossbar, time);
pthread_mutex_lock(&crossbar->mutex);
while (!crossbar->failed && crossbar->published_count <= timestep)
pthread_cond_wait(&crossbar->state_available, &crossbar->mutex);
if (crossbar->failed) {
pthread_mutex_unlock(&crossbar->mutex);
return 1;
}
*voltage =
crossbar->config.spike_amplitude *
crossbar->state_ring[(timestep % 2) * crossbar->config.num_neurons +
row];
if (crossbar->consumed_count < timestep + 1) {
crossbar->consumed_count = timestep + 1;
pthread_cond_broadcast(&crossbar->progress);
}
pthread_mutex_unlock(&crossbar->mutex);
return 0;
}
static int callback_data(pvecvaluesall values, int count, int ident,
void *user_data)
{
(void)count;
(void)ident;
Online_Crossbar *crossbar = user_data;
double time = -1.0;
double *columns =
calloc(crossbar->config.num_outputs * 2, sizeof(*columns));
unsigned char *found =
calloc(crossbar->config.num_outputs * 2, sizeof(*found));
if (!columns || !found) {
free(columns);
free(found);
pthread_mutex_lock(&crossbar->mutex);
fail_locked(crossbar);
pthread_mutex_unlock(&crossbar->mutex);
return 1;
}
for (int i = 0; i < values->veccount; i++) {
pvecvalues value = values->vecsa[i];
if (value->is_scale || strcmp(value->name, "time") == 0) {
time = value->creal;
continue;
}
const char *col = strstr(value->name, "col");
if (!col)
continue;
char *end = NULL;
unsigned long index = strtoul(col + 3, &end, 10);
if (end != col + 3 &&
index < crossbar->config.num_outputs * 2) {
columns[index] = value->creal;
found[index] = 1;
}
}
if (time >= 0.0) {
double scaled = time / crossbar->config.time_step;
double rounded = nearbyint(scaled);
if (fabs(scaled - rounded) <= 1.0e-7 && rounded >= 1.0 &&
(size_t)rounded <= crossbar->config.num_timesteps) {
size_t timestep = (size_t)rounded - 1;
int complete = 1;
for (size_t i = 0; i < crossbar->config.num_outputs * 2;
i++)
complete = complete && found[i];
if (complete) {
double *decoded =
crossbar->results +
timestep * crossbar->config.num_outputs;
if (convert_output_to_software(
crossbar->config.num_neurons,
crossbar->config.num_outputs, 1,
columns, crossbar->resistances,
crossbar->config.load_resistance,
&crossbar->mapping, NULL,
crossbar->config.spike_amplitude,
decoded) == 0) {
pthread_mutex_lock(&crossbar->mutex);
if (crossbar->result_count <
timestep + 1)
crossbar->result_count =
timestep + 1;
if (timestep + 1 <
crossbar->config.num_timesteps)
ngSpice_SetBkpt(
(timestep + 2) *
crossbar->config.time_step);
pthread_cond_broadcast(
&crossbar->progress);
pthread_mutex_unlock(&crossbar->mutex);
}
}
}
}
free(columns);
free(found);
return 0;
}
int online_crossbar_init(const Online_Crossbar_Config *config,
const spires_reservoir *reservoir,
Online_Crossbar **crossbar_out)
{
if (!config || !reservoir || !crossbar_out ||
config->num_neurons == 0 || config->num_outputs == 0 ||
config->num_timesteps == 0 || config->time_step <= 0.0 ||
config->spike_amplitude == 0.0 || config->load_resistance <= 0.0 ||
!config->model_path || !config->subcircuit_name ||
!config->netlist_path)
return -1;
Online_Crossbar *crossbar = calloc(1, sizeof(*crossbar));
if (!crossbar)
return -1;
crossbar->config = *config;
if (pthread_mutex_init(&crossbar->mutex, NULL) != 0)
goto fail;
crossbar->mutex_initialized = 1;
if (pthread_cond_init(&crossbar->state_available, NULL) != 0)
goto fail;
crossbar->state_condition_initialized = 1;
if (pthread_cond_init(&crossbar->progress, NULL) != 0)
goto fail;
crossbar->progress_condition_initialized = 1;
crossbar->state_ring = calloc(2 * config->num_neurons, sizeof(double));
crossbar->results =
calloc(config->num_timesteps * config->num_outputs, sizeof(double));
if (!crossbar->state_ring || !crossbar->results)
goto fail;
if (convert_weights_to_resistances(
reservoir, config->num_neurons, config->num_outputs,
config->r_on, config->r_off, &crossbar->resistances,
&crossbar->mapping) != 0)
goto fail;
Crossbar_Config netlist = {.rows = config->num_neurons,
.columns = config->num_outputs * 2,
.input_series = crossbar->state_ring,
.num_samples = config->num_timesteps,
.initial_resistance = crossbar->resistances,
.model_path = config->model_path,
.subcircuit_name = config->subcircuit_name,
.load_resistance = config->load_resistance,
.time_step = config->time_step,
.stop_time = config->num_timesteps *
config->time_step,
.print_state_nodes = 0};
if (generate_online_crossbar(config->netlist_path, &netlist) != 0)
goto fail;
*crossbar_out = crossbar;
return 0;
fail:
online_crossbar_destroy(crossbar);
return -1;
}
int online_crossbar_start(Online_Crossbar *crossbar)
{
if (!crossbar || crossbar->started)
return -1;
if (ngSpice_Init(callback_text, callback_text, callback_exit,
callback_data, callback_init_data, callback_background,
crossbar) != 0 ||
ngSpice_Init_Sync(callback_voltage, NULL, NULL, NULL, crossbar) !=
0)
return -1;
char command[4096];
if (snprintf(command, sizeof(command), "source %s",
crossbar->config.netlist_path) >= (int)sizeof(command) ||
ngSpice_Command(command) != 0)
return -1;
crossbar->started = 1;
ngSpice_SetBkpt(crossbar->config.time_step);
if (ngSpice_Command("bg_run") != 0) {
crossbar->started = 0;
return -1;
}
return 0;
}
int online_crossbar_submit(Online_Crossbar *crossbar, size_t timestep,
const double *state, double *previous_output,
int *output_ready)
{
if (!crossbar || !state || !output_ready || !crossbar->started ||
timestep >= crossbar->config.num_timesteps ||
timestep != crossbar->published_count)
return -1;
pthread_mutex_lock(&crossbar->mutex);
while (!crossbar->failed && timestep >= crossbar->consumed_count + 2)
pthread_cond_wait(&crossbar->progress, &crossbar->mutex);
if (crossbar->failed) {
pthread_mutex_unlock(&crossbar->mutex);
return -1;
}
memcpy(crossbar->state_ring +
(timestep % 2) * crossbar->config.num_neurons,
state, crossbar->config.num_neurons * sizeof(double));
crossbar->published_count++;
pthread_cond_broadcast(&crossbar->state_available);
*output_ready = timestep != 0;
if (timestep != 0) {
if (!previous_output) {
pthread_mutex_unlock(&crossbar->mutex);
return -1;
}
while (!crossbar->failed && crossbar->result_count < timestep)
pthread_cond_wait(&crossbar->progress,
&crossbar->mutex);
if (crossbar->failed) {
pthread_mutex_unlock(&crossbar->mutex);
return -1;
}
memcpy(previous_output,
crossbar->results +
(timestep - 1) * crossbar->config.num_outputs,
crossbar->config.num_outputs * sizeof(double));
}
pthread_mutex_unlock(&crossbar->mutex);
return 0;
}
int online_crossbar_finish(Online_Crossbar *crossbar, double *final_output)
{
if (!crossbar || !final_output ||
crossbar->published_count != crossbar->config.num_timesteps)
return -1;
pthread_mutex_lock(&crossbar->mutex);
while (!crossbar->failed &&
crossbar->result_count < crossbar->config.num_timesteps)
pthread_cond_wait(&crossbar->progress, &crossbar->mutex);
if (crossbar->failed) {
pthread_mutex_unlock(&crossbar->mutex);
return -1;
}
memcpy(final_output,
crossbar->results + (crossbar->config.num_timesteps - 1) *
crossbar->config.num_outputs,
crossbar->config.num_outputs * sizeof(double));
pthread_mutex_unlock(&crossbar->mutex);
return 0;
}
void online_crossbar_destroy(Online_Crossbar *crossbar)
{
if (!crossbar)
return;
if (crossbar->started && ngSpice_running()) {
ngSpice_Command("bg_halt");
if (crossbar->mutex_initialized) {
pthread_mutex_lock(&crossbar->mutex);
while (!crossbar->simulation_done && ngSpice_running())
pthread_cond_wait(&crossbar->progress,
&crossbar->mutex);
pthread_mutex_unlock(&crossbar->mutex);
}
}
if (crossbar->started)
ngSpice_Reset();
free(crossbar->resistances);
free(crossbar->state_ring);
free(crossbar->results);
if (crossbar->state_condition_initialized)
pthread_cond_destroy(&crossbar->state_available);
if (crossbar->progress_condition_initialized)
pthread_cond_destroy(&crossbar->progress);
if (crossbar->mutex_initialized)
pthread_mutex_destroy(&crossbar->mutex);
free(crossbar);
}
|