-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.c
368 lines (342 loc) · 8.45 KB
/
main.c
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
// Copyright (c) 2023 - The University of Texas at Austin
// This work was produced under contract #2317831 to National Technology and
// Engineering Solutions of Sandia, LLC which is under contract
// No. DE-NA0003525 with the U.S. Department of Energy.
// main.c - Command line interface
// Performance simulation of neuromorphic architectures
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "print.h"
#include "sim.h"
#include "network.h"
#include "arch.h"
#include "description.h"
#include "command.h"
void run(struct simulation *sim, struct network *net, struct architecture *arch);
struct timespec calculate_elapsed_time(struct timespec ts_start, struct timespec ts_end);
enum program_args
{
ARCH_FILENAME,
NETWORK_FILENAME,
TIMESTEPS,
PROGRAM_NARGS,
};
int main(int argc, char *argv[])
{
struct simulation *sim;
struct network net;
struct architecture *arch;
char *filename;
double average_power;
int timesteps, ret;
FILE *network_fp, *arch_fp;
// Assume that if we don't get to the point where we write this with
// a valid value, something went wrong and we errored out
ret = RET_FAIL;
arch = arch_init();
network_init(&net);
INFO("Initializing simulation.\n");
sim = sim_init_sim();
if (argc < 1)
{
INFO("Error: No program arguments.\n");
goto clean_up;
}
// First arg is always program name, skip
argc--;
argv++;
// Parse optional args
while (argc > 2)
{
if (argv[0][0] == '-')
{
switch (argv[0][1])
{
case 'p':
sim->log_perf = 1;
break;
case 's':
sim->log_spikes = 1;
break;
case 'v':
sim->log_potential = 1;
break;
case 'm':
sim->log_messages = 1;
break;
case 'o':
if (argc <= 0)
{
INFO("Error: No output dir given.\n");
exit(1);
}
argc--;
argv++;
strncpy(sim->out_dir, argv[0], MAX_FIELD_LEN);
INFO("Writing output to %s\n", sim->out_dir);
break;
default:
INFO("Error: Flag %c not recognized.\n",
argv[0][1]);
break;
}
argc--;
argv++;
}
else
{
break;
}
}
if (argc < PROGRAM_NARGS)
{
INFO("Usage: ./sim [-p<log perf> -s<spike trace> "
"-v<potential trace> -m<message trace> "
"-o <output directory>] "
"<arch description> <network description> "
"<timesteps>\n");
goto clean_up;
}
if (sim->log_potential)
{
char file_path[2*MAX_FIELD_LEN];
strncpy(file_path, sim->out_dir, MAX_FIELD_LEN);
strncat(file_path, "/potential.csv", MAX_FIELD_LEN);
sim->potential_trace_fp = fopen(file_path, "w");
if (sim->potential_trace_fp == NULL)
{
INFO("Error: Couldn't open trace file for writing.\n");
goto clean_up;
}
}
if (sim->log_spikes)
{
char file_path[2*MAX_FIELD_LEN];
strncpy(file_path, sim->out_dir, MAX_FIELD_LEN);
strncat(file_path, "/spikes.csv", MAX_FIELD_LEN);
sim->spike_trace_fp = fopen(file_path, "w");
if (sim->spike_trace_fp == NULL)
{
INFO("Error: Couldn't open trace file for writing.\n");
goto clean_up;
}
}
if (sim->log_messages)
{
char file_path[2*MAX_FIELD_LEN];
strncpy(file_path, sim->out_dir, MAX_FIELD_LEN);
strncat(file_path, "/messages.csv", MAX_FIELD_LEN);
sim->message_trace_fp = fopen(file_path, "w");
if (sim->message_trace_fp == NULL)
{
INFO("Error: Couldn't open trace file for writing.\n");
goto clean_up;
}
}
if (sim->log_perf)
{
char file_path[2*MAX_FIELD_LEN];
strncpy(file_path, sim->out_dir, MAX_FIELD_LEN);
strncat(file_path, "/perf.csv", MAX_FIELD_LEN);
sim->perf_fp = fopen(file_path, "w");
if (sim->perf_fp == NULL)
{
INFO("Error: Couldn't open perf file for writing.\n");
goto clean_up;
}
}
// Read in program args, sanity check and parse inputs
filename = argv[ARCH_FILENAME];
arch_fp = fopen(filename, "r");
if (arch_fp == NULL)
{
INFO("Error: Architecture file %s failed to open.\n", filename);
goto clean_up;
}
ret = description_parse_file(arch_fp, NULL, arch);
//arch_print_description(&description, 0);
fclose(arch_fp);
if (ret == RET_FAIL)
{
goto clean_up;
}
//arch_init_message_scheduler(&scheduler, arch);
timesteps = 0;
ret = sscanf(argv[TIMESTEPS], "%d", ×teps);
if (ret < 1)
{
INFO("Error: Time-steps must be integer > 0 (%s).\n",
argv[TIMESTEPS]);
goto clean_up;
}
else if (timesteps <= 0)
{
INFO("Error: Time-steps must be > 0 (%d)\n", timesteps);
goto clean_up;
}
filename = argv[NETWORK_FILENAME];
// Create the network
network_fp = fopen(filename, "r");
if (network_fp == NULL)
{
INFO("Network data (%s) failed to open.\n", filename);
goto clean_up;
}
INFO("Reading network from file.\n");
ret = description_parse_file(network_fp, &net, arch);
fclose(network_fp);
if (ret == RET_FAIL)
{
goto clean_up;
}
network_check_mapped(&net);
arch_create_connection_maps(arch);
INFO("Creating probe and perf data files.\n");
if (sim->spike_trace_fp != NULL)
{
sim_spike_trace_write_header(sim);
}
if (sim->potential_trace_fp != NULL)
{
sim_potential_trace_write_header(sim, &net);
}
if (sim->message_trace_fp != NULL)
{
sim_message_trace_write_header(sim);
}
if (sim->perf_fp != NULL)
{
sim_perf_write_header(sim->perf_fp);
}
// Step simulation
INFO("Running simulation.\n");
for (sim->ts.timestep = 1; sim->ts.timestep <= timesteps;
(sim->ts.timestep)++)
{
if ((sim->ts.timestep % 100) == 0)
{
// Print heart-beat every hundred timesteps
INFO("*** Time-step %ld ***\n", sim->ts.timestep);
}
run(sim, &net, arch);
}
INFO("***** Run Summary *****\n");
sim_write_summary(stdout, sim);
if (sim->total_sim_time > 0.0)
{
average_power = sim->total_energy / sim->total_sim_time;
}
else
{
average_power = 0.0;
}
INFO("Average power consumption: %f W.\n", average_power);
char file_path[2*MAX_FIELD_LEN];
strncpy(file_path, sim->out_dir, MAX_FIELD_LEN);
strncat(file_path, "/run_summary.yaml", MAX_FIELD_LEN);
sim->stats_fp = fopen(file_path, "w");
if (sim->stats_fp != NULL)
{
sim_write_summary(sim->stats_fp, sim);
}
INFO("Run finished.\n");
clean_up:
// Free any larger structures here
network_free(&net);
arch_free(arch);
// Close any open files here
if (sim->potential_trace_fp != NULL)
{
fclose(sim->potential_trace_fp);
}
if (sim->spike_trace_fp != NULL)
{
fclose(sim->spike_trace_fp);
}
if (sim->message_trace_fp != NULL)
{
fclose(sim->message_trace_fp);
}
if (sim->perf_fp != NULL)
{
fclose(sim->perf_fp);
}
if (sim->stats_fp != NULL)
{
fclose(sim->stats_fp);
}
// Free the simulation structure only after we close all files
free(sim);
if (ret == RET_FAIL)
{
return 1;
}
else
{
return 0;
}
}
void run(struct simulation *sim, struct network *net, struct architecture *arch)
{
// TODO: remove the need to pass the network struct, only the arch
// should be needed (since it links back to the net anyway)
// Run neuromorphic hardware simulation for one timestep
// Measure the CPU time it takes and accumulate the stats
struct timespec ts_start, ts_end, ts_elapsed;
struct timestep *ts = &(sim->ts);
// Measure the wall-clock time taken to run the simulation
// on the host machine
clock_gettime(CLOCK_MONOTONIC, &ts_start);
sim_timestep(ts, net, arch);
// Calculate elapsed time
clock_gettime(CLOCK_MONOTONIC, &ts_end);
ts_elapsed = calculate_elapsed_time(ts_start, ts_end);
sim->total_energy += ts->energy;
sim->total_sim_time += ts->sim_time;
sim->total_spikes += ts->spike_count;
sim->total_neurons_fired += ts->total_neurons_fired;
sim->total_messages_sent += ts->packets_sent;
if (sim->log_spikes)
{
sim_trace_record_spikes(sim, net);
}
if (sim->log_potential)
{
sim_trace_record_potentials(sim, net);
}
if (sim->log_perf)
{
sim_perf_log_timestep(ts, sim->perf_fp);
}
if (sim->log_messages)
{
for (int i = 0; i < ARCH_MAX_CORES; i++)
{
for (int j = 0; j < ts->message_counts[i]; j++)
{
struct message *m = &(ts->messages[i][j]);
sim_trace_record_message(sim, m);
}
}
}
sim->timesteps = ts->timestep;
sim->wall_time += (double) ts_elapsed.tv_sec+(ts_elapsed.tv_nsec/1.0e9);
TRACE1("Time-step took: %fs.\n",
(double) ts_elapsed.tv_sec+(ts_elapsed.tv_nsec/1.0e9));
}
struct timespec calculate_elapsed_time(struct timespec ts_start,
struct timespec ts_end)
{
// Calculate elapsed wall-clock time between ts_start and ts_end
struct timespec ts_elapsed;
ts_elapsed.tv_nsec = ts_end.tv_nsec - ts_start.tv_nsec;
ts_elapsed.tv_sec = ts_end.tv_sec - ts_start.tv_sec;
if (ts_end.tv_nsec < ts_start.tv_nsec)
{
ts_elapsed.tv_sec--;
ts_elapsed.tv_nsec += 1000000000UL;
}
return ts_elapsed;
}