|
1 | | -/// Corenrnmech is a wrapper lib providing a single solve_core function |
2 | | -/// which initializes the solver, loads the external mechanisms and launches the simulation |
| 1 | +/** |
| 2 | + * \file |
| 3 | + * \brief Provides interface function for CoreNEURON mechanism library and NEURON |
| 4 | + * |
| 5 | + * libcorenrnmech is a interface library provided to building standalone executable |
| 6 | + * special-core. Also, it is used by NEURON to run CoreNEURON via dlopen to execute |
| 7 | + * models via in-memory transfer. |
| 8 | + */ |
3 | 9 |
|
| 10 | +#include <cstdlib> |
4 | 11 | #include <coreneuron/engine.h> |
5 | 12 |
|
6 | | -#ifdef ADDITIONAL_MECHS |
7 | 13 | namespace coreneuron { |
| 14 | + |
| 15 | +/** Mechanism registration function |
| 16 | + * |
| 17 | + * If external mechanisms present then use modl_reg function generated |
| 18 | + * in mod_func.cpp otherwise use empty one. |
| 19 | + */ |
| 20 | +#ifdef ADDITIONAL_MECHS |
8 | 21 | extern void modl_reg(); |
| 22 | +#else |
| 23 | +void modl_reg() { |
9 | 24 | } |
10 | 25 | #endif |
11 | 26 |
|
| 27 | +/// variables defined in coreneuron library |
| 28 | +extern bool nrn_have_gaps; |
| 29 | +extern bool nrn_use_fast_imem; |
| 30 | + |
| 31 | +} // namespace coreneuron |
| 32 | + |
| 33 | +/** Initialize mechanisms and run simulation using CoreNEURON |
| 34 | + * |
| 35 | + * This is mainly used to build nrniv-core executable |
| 36 | + */ |
12 | 37 | int solve_core(int argc, char** argv) { |
13 | 38 | mk_mech_init(argc, argv); |
| 39 | + coreneuron::modl_reg(); |
| 40 | + return run_solve_core(argc, argv); |
| 41 | +} |
14 | 42 |
|
15 | | -#ifdef ADDITIONAL_MECHS |
16 | | - /// Initializing additional Neurodamus mechanisms (in mod_func.c, built by mech/mod_func.c.pl) |
| 43 | +extern "C" { |
| 44 | + |
| 45 | +/// global variables from coreneuron library |
| 46 | +extern bool corenrn_embedded; |
| 47 | +extern int corenrn_embedded_nthread; |
| 48 | + |
| 49 | +/// parse arguments from neuron and prepare new one for coreneuron |
| 50 | +char* prepare_args(int& argc, char**& argv, int use_mpi, const char* nrn_arg); |
| 51 | + |
| 52 | +/// initialize standard mechanisms from coreneuron |
| 53 | +void mk_mech_init(int argc, char** argv); |
| 54 | + |
| 55 | +/// set openmp threads equal to neuron's pthread |
| 56 | +void set_openmp_threads(int nthread); |
| 57 | + |
| 58 | +/** Run CoreNEURON in embedded mode with NEURON |
| 59 | + * |
| 60 | + * @param nthread Number of Pthreads on NEURON side |
| 61 | + * @param have_gaps True if gap junctions are used |
| 62 | + * @param use_mpi True if MPI is used on NEURON side |
| 63 | + * @param use_fast_imem True if fast imembrance calculation enabled |
| 64 | + * @param nrn_arg Command line arguments passed by NEURON |
| 65 | + * @return 1 if embedded mode is used otherwise 0 |
| 66 | + * \todo Change return type semantics |
| 67 | + */ |
| 68 | +int corenrn_embedded_run(int nthread, |
| 69 | + int have_gaps, |
| 70 | + int use_mpi, |
| 71 | + int use_fast_imem, |
| 72 | + const char* nrn_arg) { |
| 73 | + // set coreneuron's internal variable based on neuron arguments |
| 74 | + corenrn_embedded = true; |
| 75 | + corenrn_embedded_nthread = nthread; |
| 76 | + coreneuron::nrn_have_gaps = have_gaps != 0; |
| 77 | + |
| 78 | + if (use_fast_imem != 0) { |
| 79 | + coreneuron::nrn_use_fast_imem = true; |
| 80 | + } |
| 81 | + |
| 82 | + // set number of openmp threads |
| 83 | + set_openmp_threads(nthread); |
| 84 | + |
| 85 | + // pre-process argumnets from neuron and prepare new for coreneuron |
| 86 | + int argc; |
| 87 | + char** argv; |
| 88 | + char* new_arg = prepare_args(argc, argv, use_mpi, nrn_arg); |
| 89 | + |
| 90 | + // initialize internal arguments |
| 91 | + mk_mech_init(argc, argv); |
| 92 | + |
| 93 | + // initialize extra arguments built into special-core |
17 | 94 | coreneuron::modl_reg(); |
18 | | -#endif |
19 | 95 |
|
20 | | - return run_solve_core(argc, argv); |
| 96 | + // run simulation |
| 97 | + run_solve_core(argc, argv); |
| 98 | + |
| 99 | + // free temporary string created from prepare_args |
| 100 | + free(new_arg); |
| 101 | + |
| 102 | + // delete array for argv |
| 103 | + delete[] argv; |
| 104 | + |
| 105 | + return corenrn_embedded ? 1 : 0; |
| 106 | +} |
21 | 107 | } |
0 commit comments