This repository was archived by the owner on Mar 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathmain1.cpp
More file actions
663 lines (546 loc) · 20.1 KB
/
main1.cpp
File metadata and controls
663 lines (546 loc) · 20.1 KB
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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
/*
# =============================================================================
# Copyright (c) 2016 - 2021 Blue Brain Project/EPFL
#
# See top-level LICENSE file for details.
# =============================================================================.
*/
/**
* @file main1.cpp
* @date 26 Oct 2014
* @brief File containing main driver routine for CoreNeuron
*/
#include <climits>
#include <cstring>
#include <csignal>
#include <memory>
#include <vector>
#include "coreneuron/config/config.h"
#include "coreneuron/engine.h"
#include "coreneuron/utils/randoms/nrnran123.h"
#include "coreneuron/nrnconf.h"
#include "coreneuron/sim/fast_imem.hpp"
#include "coreneuron/sim/multicore.hpp"
#include "coreneuron/mpi/nrnmpi.h"
#include "coreneuron/nrniv/nrniv_decl.h"
#include "coreneuron/mechanism/register_mech.hpp"
#include "coreneuron/io/output_spikes.hpp"
#include "coreneuron/io/nrn_checkpoint.hpp"
#include "coreneuron/utils/memory_utils.h"
#include "coreneuron/apps/corenrn_parameters.hpp"
#include "coreneuron/io/prcellstate.hpp"
#include "coreneuron/utils/nrnmutdec.h"
#include "coreneuron/utils/nrn_stats.h"
#include "coreneuron/io/reports/nrnreport.hpp"
#include "coreneuron/io/reports/binary_report_handler.hpp"
#include "coreneuron/io/reports/report_handler.hpp"
#include "coreneuron/io/reports/sonata_report_handler.hpp"
#include "coreneuron/gpu/nrn_acc_manager.hpp"
#include "coreneuron/utils/profile/profiler_interface.h"
#include "coreneuron/network/partrans.hpp"
#include "coreneuron/network/multisend.hpp"
#include "coreneuron/io/file_utils.hpp"
#include "coreneuron/io/nrn2core_direct.h"
#include "coreneuron/io/core2nrn_data_return.hpp"
extern "C" {
const char* corenrn_version() {
return coreneuron::bbcore_write_version;
}
// the CORENRN_USE_LEGACY_UNITS determined by CORENRN_ENABLE_LEGACY_UNITS
bool corenrn_units_use_legacy() {
return CORENRN_USE_LEGACY_UNITS;
}
void (*nrn2core_part2_clean_)();
// cf. utils/ispc_globals.c
extern double ispc_celsius;
/**
* If "export OMP_NUM_THREADS=n" is not set then omp by default sets
* the number of threads equal to the number of cores on this node.
* If there are a number of mpi processes on this node as well, things
* can go very slowly as there are so many more threads than cores.
* Assume the NEURON users pc.nthread() is well chosen if
* OMP_NUM_THREADS is not set.
*/
void set_openmp_threads(int nthread) {
#if defined(_OPENMP)
if (!getenv("OMP_NUM_THREADS")) {
omp_set_num_threads(nthread);
}
#endif
}
/**
* Convert char* containing arguments from neuron to char* argv[] for
* coreneuron command line argument parser.
*/
char* prepare_args(int& argc, char**& argv, int use_mpi, const char* arg) {
// first construct all arguments as string
std::string args(arg);
args.insert(0, " coreneuron ");
args.append(" --skip-mpi-finalize ");
if (use_mpi) {
args.append(" --mpi ");
}
// we can't modify string with strtok, make copy
char* first = strdup(args.c_str());
const char* sep = " ";
// first count the no of argument
char* token = strtok(first, sep);
argc = 0;
while (token) {
token = strtok(nullptr, sep);
argc++;
}
free(first);
// now build char*argv
argv = new char*[argc];
first = strdup(args.c_str());
token = strtok(first, sep);
for (int i = 0; token; i++) {
argv[i] = token;
token = strtok(nullptr, sep);
}
// return actual data to be freed
return first;
}
} // extern "C"
namespace coreneuron {
void call_prcellstate_for_prcellgid(int prcellgid, int compute_gpu, int is_init);
static std::string check_restore() {
auto restore_path = corenrn_param.restorepath;
const auto auto_chkpt_path = corenrn_param.outpath + "/_corenrn_ckpt";
if (restore_path.empty() && fs_isdir(auto_chkpt_path.c_str())) {
restore_path = auto_chkpt_path;
}
return restore_path;
}
void nrn_init_and_load_data(int argc,
char* argv[],
bool is_mapping_needed = false,
bool run_setup_cleanup = true) {
#if defined(NRN_FEEXCEPT)
nrn_feenableexcept();
#endif
/// profiler like tau/vtune : do not measure from begining
Instrumentor::stop_profile();
// memory footprint after mpi initialisation
if (!corenrn_param.is_quiet()) {
report_mem_usage("After MPI_Init");
}
// initialise default coreneuron parameters
initnrn();
// create mutex for nrn123, protect instance_count_
nrnran123_mutconstruct();
// set global variables
// precedence is: set by user, globals.dat, 34.0
celsius = corenrn_param.celsius;
#if _OPENACC
if (!corenrn_param.gpu && corenrn_param.cell_interleave_permute == 2) {
fprintf(stderr,
"compiled with _OPENACC does not allow the combination of --cell-permute=2 and "
"missing --gpu\n");
exit(1);
}
#endif
// if multi-threading enabled, make sure mpi library supports it
#if NRNMPI
if (corenrn_param.threading) {
nrnmpi_check_threading_support();
}
#endif
// full path of files.dat file
std::string filesdat(corenrn_param.datpath + "/" + corenrn_param.filesdat);
// read the global variable names and set their values from globals.dat
set_globals(corenrn_param.datpath.c_str(), (corenrn_param.seed >= 0), corenrn_param.seed);
// set global variables for start time, timestep and temperature
std::string restore_path = check_restore();
t = restore_time(restore_path.c_str());
if (corenrn_param.dt != -1000.) { // command line arg highest precedence
dt = corenrn_param.dt;
} else if (dt == -1000.) { // not on command line and no dt in globals.dat
dt = 0.025; // lowest precedence
}
corenrn_param.dt = dt;
rev_dt = (int) (1. / dt);
if (corenrn_param.celsius != -1000.) { // command line arg highest precedence
celsius = corenrn_param.celsius;
} else if (celsius == -1000.) { // not on command line and no celsius in globals.dat
celsius = 34.0; // lowest precedence
}
corenrn_param.celsius = celsius;
// for ispc backend
ispc_celsius = celsius;
// create net_cvode instance
mk_netcvode();
// One part done before call to nrn_setup. Other part after.
if (!corenrn_param.patternstim.empty()) {
nrn_set_extra_thread0_vdata();
}
if (!corenrn_param.is_quiet()) {
report_mem_usage("Before nrn_setup");
}
// set if need to interleave cells
interleave_permute_type = corenrn_param.cell_interleave_permute;
cellorder_nwarp = corenrn_param.nwarp;
use_solve_interleave = corenrn_param.cell_interleave_permute;
#if LAYOUT == 1
// permuting not allowed for AoS
interleave_permute_type = 0;
use_solve_interleave = false;
#endif
if (corenrn_param.gpu && interleave_permute_type == 0) {
if (nrnmpi_myid == 0) {
printf(
" WARNING : GPU execution requires --cell-permute type 1 or 2. Setting it to 1.\n");
}
interleave_permute_type = 1;
use_solve_interleave = true;
}
// multisend options
use_multisend_ = corenrn_param.multisend ? 1 : 0;
n_multisend_interval = corenrn_param.ms_subint;
use_phase2_ = (corenrn_param.ms_phases == 2) ? 1 : 0;
// reading *.dat files and setting up the data structures, setting mindelay
nrn_setup(filesdat.c_str(),
is_mapping_needed,
run_setup_cleanup,
corenrn_param.datpath.c_str(),
restore_path.c_str(),
&corenrn_param.mindelay);
// Allgather spike compression and bin queuing.
nrn_use_bin_queue_ = corenrn_param.binqueue;
int spkcompress = corenrn_param.spkcompress;
nrnmpi_spike_compress(spkcompress, (spkcompress ? true : false), use_multisend_);
if (!corenrn_param.is_quiet()) {
report_mem_usage("After nrn_setup ");
}
// Invoke PatternStim
if (!corenrn_param.patternstim.empty()) {
nrn_mkPatternStim(corenrn_param.patternstim.c_str(), corenrn_param.tstop);
}
/// Setting the timeout
nrn_set_timeout(200.);
// show all configuration parameters for current run
if (nrnmpi_myid == 0 && !corenrn_param.is_quiet()) {
std::cout << corenrn_param << std::endl;
std::cout << " Start time (t) = " << t << std::endl << std::endl;
}
// allocate buffer for mpi communication
mk_spikevec_buffer(corenrn_param.spikebuf);
if (!corenrn_param.is_quiet()) {
report_mem_usage("After mk_spikevec_buffer");
}
if (corenrn_param.gpu) {
setup_nrnthreads_on_device(nrn_threads, nrn_nthread);
}
if (nrn_have_gaps) {
nrn_partrans::gap_update_indices();
}
// call prcellstate for prcellgid
call_prcellstate_for_prcellgid(corenrn_param.prcellgid, corenrn_param.gpu, 1);
}
void call_prcellstate_for_prcellgid(int prcellgid, int compute_gpu, int is_init) {
char prcellname[1024];
#ifdef ENABLE_CUDA
const char* prprefix = "cu";
#else
const char* prprefix = "acc";
#endif
if (prcellgid >= 0) {
if (compute_gpu) {
if (is_init)
sprintf(prcellname, "%s_gpu_init", prprefix);
else
sprintf(prcellname, "%s_gpu_t%f", prprefix, t);
} else {
if (is_init)
strcpy(prcellname, "cpu_init");
else
sprintf(prcellname, "cpu_t%f", t);
}
update_nrnthreads_on_host(nrn_threads, nrn_nthread);
prcellstate(prcellgid, prcellname);
}
}
/* perform forwardskip and call prcellstate for prcellgid */
void handle_forward_skip(double forwardskip, int prcellgid) {
double savedt = dt;
double savet = t;
dt = forwardskip * 0.1;
t = -1e9;
for (int step = 0; step < 10; ++step) {
nrn_fixed_step_minimal();
}
if (prcellgid >= 0) {
prcellstate(prcellgid, "fs");
}
dt = savedt;
t = savet;
dt2thread(-1.);
// clear spikes generated during forward skip (with negative time)
clear_spike_vectors();
}
std::string cnrn_version() {
return coreneuron::version::to_string();
}
// bsize = 0 then per step transfer
// bsize > 1 then full trajectory save into arrays.
void get_nrn_trajectory_requests(int bsize) {
if (nrn2core_get_trajectory_requests_) {
for (int tid = 0; tid < nrn_nthread; ++tid) {
NrnThread& nt = nrn_threads[tid];
int n_pr;
int n_trajec;
int* types;
int* indices;
void** vpr;
double** varrays;
double** pvars;
// bsize is passed by reference, the return value will determine if
// per step return or entire trajectory return.
(*nrn2core_get_trajectory_requests_)(
tid, bsize, n_pr, vpr, n_trajec, types, indices, pvars, varrays);
delete_trajectory_requests(nt);
if (n_trajec) {
TrajectoryRequests* tr = new TrajectoryRequests;
nt.trajec_requests = tr;
tr->bsize = bsize;
tr->n_pr = n_pr;
tr->n_trajec = n_trajec;
tr->vsize = 0;
tr->vpr = vpr;
tr->gather = new double*[n_trajec];
tr->varrays = varrays;
tr->scatter = pvars;
for (int i = 0; i < n_trajec; ++i) {
tr->gather[i] = stdindex2ptr(types[i], indices[i], nt);
}
delete[] types;
delete[] indices;
}
}
}
}
static void trajectory_return() {
if (nrn2core_trajectory_return_) {
for (int tid = 0; tid < nrn_nthread; ++tid) {
NrnThread& nt = nrn_threads[tid];
TrajectoryRequests* tr = nt.trajec_requests;
if (tr && tr->varrays) {
(*nrn2core_trajectory_return_)(tid, tr->n_pr, tr->vsize, tr->vpr, nt._t);
}
}
}
}
std::unique_ptr<ReportHandler> create_report_handler(ReportConfiguration& config) {
std::unique_ptr<ReportHandler> report_handler;
if (config.format == "Bin") {
report_handler = std::make_unique<BinaryReportHandler>(config);
} else if (config.format == "SONATA") {
report_handler = std::make_unique<SonataReportHandler>(config);
} else {
if (nrnmpi_myid == 0) {
printf(" WARNING : Report name '%s' has unknown format: '%s'.\n",
config.name.data(),
config.format.data());
}
return nullptr;
}
return report_handler;
}
/**
* \brief Installs a SIGTERM handler so that we finish the current simulation without losing data
* \return True if a checkpoint was performed. False otherwise (not enough elapsed time)
*/
static void install_sigterm_handler() {
auto sigh = [](int) {
std::cerr << "SIGTERM caught! Halting sim and dumping checkpoint" << std::endl;
coreneuron::stoprun = true;
};
if (std::signal(SIGTERM, sigh) == SIG_ERR) {
std::cerr << "Could not install SIGTERM handler" << std::endl;
}
}
} // namespace coreneuron
/// The following high-level functions are marked as "extern C"
/// for compat with C, namely Neuron mod files.
/// They split the previous solve_core so that intermediate init of external mechanisms can occur.
/// See mech/corenrnmech.cpp for the new all-in-one solve_core (not compiled into the coreneuron
/// lib since with nrnivmodl-core we have 'future' external mechanisms)
using namespace coreneuron;
extern "C" void mk_mech_init(int argc, char** argv) {
// read command line parameters and parameter config files
corenrn_param.parse(argc, argv);
#if NRNMPI
if (corenrn_param.mpi_enable) {
nrnmpi_init(&argc, &argv);
}
#endif
#ifdef _OPENACC
if (corenrn_param.gpu) {
init_gpu();
}
#endif
if (!corenrn_param.writeParametersFilepath.empty()) {
std::ofstream out(corenrn_param.writeParametersFilepath, std::ios::trunc);
out << corenrn_param.app.config_to_str(false, false);
out.close();
}
// reads mechanism information from bbcore_mech.dat
mk_mech((corenrn_param.datpath).c_str());
}
extern "C" int run_solve_core(int argc, char** argv) {
Instrumentor::phase_begin("main");
std::vector<ReportConfiguration> configs;
std::vector<std::unique_ptr<ReportHandler>> report_handlers;
std::string spikes_population_name;
bool reports_needs_finalize = false;
if (!corenrn_param.is_quiet()) {
report_mem_usage("After mk_mech");
}
// Create outpath if it does not exist
if (nrnmpi_myid == 0) {
mkdir_p(corenrn_param.outpath.c_str());
}
if (!corenrn_param.reportfilepath.empty()) {
configs = create_report_configurations(corenrn_param.reportfilepath,
corenrn_param.outpath,
spikes_population_name);
reports_needs_finalize = configs.size();
}
// initializationa and loading functions moved to separate
{
Instrumentor::phase p("load-model");
nrn_init_and_load_data(argc, argv, !configs.empty());
}
nrn_checkpoint_arg_exists = !corenrn_param.checkpointpath.empty();
if (nrn_checkpoint_arg_exists) {
if (nrnmpi_myid == 0) {
mkdir_p(corenrn_param.checkpointpath.c_str());
}
}
std::string output_dir = corenrn_param.outpath;
if (nrnmpi_myid == 0) {
mkdir_p(output_dir.c_str());
}
install_sigterm_handler();
#if NRNMPI
nrnmpi_barrier();
#endif
bool compute_gpu = corenrn_param.gpu;
bool skip_mpi_finalize = corenrn_param.skip_mpi_finalize;
// clang-format off
#pragma acc update device(celsius, secondorder, pi) if (compute_gpu)
// clang-format on
{
double v = corenrn_param.voltage;
double dt = corenrn_param.dt;
double delay = corenrn_param.mindelay;
double tstop = corenrn_param.tstop;
if (tstop < t && nrnmpi_myid == 0) {
printf("Error: Stop time (%lf) < Start time (%lf), restoring from checkpoint? \n",
tstop,
t);
abort();
}
// In direct mode there are likely trajectory record requests
// to allow processing in NEURON after simulation by CoreNEURON
if (corenrn_embedded) {
// arg is vector size required but NEURON can instead
// specify that returns will be on a per time step basis.
get_nrn_trajectory_requests(int(tstop / dt) + 2);
(*nrn2core_part2_clean_)();
}
// TODO : if some ranks are empty then restore will go in deadlock
// phase (as some ranks won't have restored anything and hence return
// false in checkpoint_initialize
if (!checkpoint_initialize()) {
nrn_finitialize(v != 1000., v);
}
if (!corenrn_param.is_quiet()) {
report_mem_usage("After nrn_finitialize");
}
// register all reports into reportinglib
double min_report_dt = INT_MAX;
for (size_t i = 0; i < configs.size(); i++) {
std::unique_ptr<ReportHandler> report_handler = create_report_handler(configs[i]);
if (report_handler) {
report_handler->create_report(dt, tstop, delay);
report_handlers.push_back(std::move(report_handler));
}
if (configs[i].report_dt < min_report_dt) {
min_report_dt = configs[i].report_dt;
}
}
// Set the buffer size if is not the default value. Otherwise use report.conf on
// register_report
if (corenrn_param.report_buff_size != corenrn_param.report_buff_size_default) {
set_report_buffer_size(corenrn_param.report_buff_size);
}
setup_report_engine(min_report_dt, delay);
configs.clear();
// call prcellstate for prcellgid
call_prcellstate_for_prcellgid(corenrn_param.prcellgid, compute_gpu, 0);
// handle forwardskip
if (corenrn_param.forwardskip > 0.0) {
handle_forward_skip(corenrn_param.forwardskip, corenrn_param.prcellgid);
}
/// Solver execution
Instrumentor::start_profile();
Instrumentor::phase_begin("simulation");
BBS_netpar_solve(corenrn_param.tstop);
Instrumentor::phase_end("simulation");
Instrumentor::stop_profile();
// update cpu copy of NrnThread from GPU
update_nrnthreads_on_host(nrn_threads, nrn_nthread);
// direct mode and full trajectory gathering on CoreNEURON, send back.
if (corenrn_embedded) {
trajectory_return();
}
// Report global cell statistics
report_cell_stats();
// prcellstate after end of solver
call_prcellstate_for_prcellgid(corenrn_param.prcellgid, compute_gpu, 0);
}
// write spike information to outpath
{
Instrumentor::phase p("output-spike");
output_spikes(output_dir.c_str(), spikes_population_name);
}
// copy weights back to NEURON NetCon
if (nrn2core_all_weights_return_) {
// first update weights from gpu
update_weights_from_gpu(nrn_threads, nrn_nthread);
// store weight pointers
std::vector<double*> weights(nrn_nthread, NULL);
// could be one thread more (empty) than in NEURON but does not matter
for (int i = 0; i < nrn_nthread; ++i) {
weights[i] = nrn_threads[i].weights;
}
(*nrn2core_all_weights_return_)(weights);
}
core2nrn_data_return();
{
Instrumentor::phase p("checkpoint");
write_checkpoint(nrn_threads, nrn_nthread, corenrn_param.checkpointpath.c_str());
}
// must be done after checkpoint (to avoid deleting events)
if (reports_needs_finalize) {
finalize_report();
}
// cleanup threads on GPU
if (corenrn_param.gpu) {
delete_nrnthreads_on_device(nrn_threads, nrn_nthread);
}
// Cleaning the memory
nrn_cleanup();
// tau needs to resume profile
Instrumentor::start_profile();
// mpi finalize
#if NRNMPI
if (!skip_mpi_finalize) {
nrnmpi_finalize();
}
#endif
Instrumentor::phase_end("main");
return 0;
}