Skip to content

Commit

Permalink
Merge branch 'master' into hines/contour-test
Browse files Browse the repository at this point in the history
  • Loading branch information
nrnhines authored Sep 19, 2023
2 parents 8bd8b92 + b38dd11 commit 38f8dad
Show file tree
Hide file tree
Showing 11 changed files with 193 additions and 56 deletions.
74 changes: 73 additions & 1 deletion share/lib/python/neuron/coreneuron.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,17 @@ class coreneuron(object):
Attributes
----------
cell_permute
model_path
enable
file_mode
gpu
prcellstate
sim_config
verbose
warp_balance
save_path
restore_path
skip_write_model_to_disk
Examples
--------
Expand All @@ -72,6 +77,11 @@ def __init__(self):
self._verbose = 2 # INFO
self._prcellstate = -1
self._model_stats = False
self._sim_config = None
self._model_path = None
self._save_path = None
self._restore_path = None
self._skip_write_model_to_disk = False

def __call__(self, **kwargs):
"""
Expand Down Expand Up @@ -211,6 +221,57 @@ def model_stats(self):
def model_stats(self, value):
self._model_stats = bool(value)

@property
def sim_config(self):
"""Simulation config file."""
return self._sim_config

@sim_config.setter
def sim_config(self, value):
self._sim_config = str(value)

@property
def model_path(self):
"""Data path of the model."""
return self._model_path

@sim_config.setter
def model_path(self, value):
self._model_path = str(value)

@property
def save_path(self):
"""Data path for save."""
return self._save_path

@sim_config.setter
def save_path(self, value):
self._save_path = str(value)

@property
def restore_path(self):
"""Data path for restore."""
return self._restore_path

@sim_config.setter
def restore_path(self, value):
self._restore_path = str(value)

@property
def skip_write_model_to_disk(self):
"""Set internal flag to only simulate the model with CoreNEURON.
Avoids writing the coreneuron input data to the data_path in
CoreNEURON embedded mode when launched thourgh the NEURON Python
API. The coreneuron input data should already be there by calling
prior to pc.psolve() pc.nrncore_write() and CoreNEURON uses them
for launching the simulation.
"""
return self._skip_write_model_to_disk

@sim_config.setter
def skip_write_model_to_disk(self, value):
self._skip_write_model_to_disk = value

def nrncore_arg(self, tstop):
"""
Return str that can be used for pc.nrncore_run(str)
Expand All @@ -234,7 +295,12 @@ def nrncore_arg(self, tstop):
if self._num_gpus:
arg += " --num-gpus %d" % self._num_gpus
if self._file_mode:
arg += " --datpath %s" % CORENRN_DATA_DIR
if self._model_path is not None:
arg += " --datpath %s" % self._model_path
else:
arg += " --datpath %s" % CORENRN_DATA_DIR
if self._skip_write_model_to_disk:
arg += " --skip-write-model-to-disk"
arg += " --tstop %g" % tstop
arg += " --cell-permute %d" % self.cell_permute
if self._warp_balance > 0:
Expand All @@ -244,6 +310,12 @@ def nrncore_arg(self, tstop):
arg += " --verbose %d" % self.verbose
if self._model_stats:
arg += " --model-stats"
if self._save_path:
arg += " --checkpoint %s" % self._save_path
if self._restore_path:
arg += " --restore %s" % self._restore_path
if self._sim_config:
arg += " --read-config %s" % self._sim_config

# args derived from current NEURON settings.
pc = h.ParallelContext()
Expand Down
6 changes: 4 additions & 2 deletions src/coreneuron/apps/main1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -621,7 +621,7 @@ extern "C" int run_solve_core(int argc, char** argv) {
}

// copy weights back to NEURON NetCon
if (nrn2core_all_weights_return_) {
if (nrn2core_all_weights_return_ && corenrn_embedded) {
// first update weights from gpu
update_weights_from_gpu(nrn_threads, nrn_nthread);

Expand All @@ -635,7 +635,9 @@ extern "C" int run_solve_core(int argc, char** argv) {
(*nrn2core_all_weights_return_)(weights);
}

core2nrn_data_return();
if (corenrn_embedded) {
core2nrn_data_return();
}

{
Instrumentor::phase p("checkpoint");
Expand Down
29 changes: 20 additions & 9 deletions src/coreneuron/mechanism/mech/enginemech.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <cstdlib>
#include <coreneuron/engine.h>
#include <string>
#include <iostream>

namespace coreneuron {

Expand Down Expand Up @@ -107,32 +108,42 @@ int corenrn_embedded_run(int nthread,
int use_fast_imem,
const char* mpi_lib,
const char* nrn_arg) {
bool corenrn_skip_write_model_to_disk = false;
const std::string corenrn_skip_write_model_to_disk_arg{"--skip-write-model-to-disk"};
// If "only_simulate_str" exists in "nrn_arg" then avoid transferring any data between NEURON
// and CoreNEURON Instead run the CoreNEURON simulation only with the coredat files provided
// "only_simulate_str" is an internal string and shouldn't be made public to the CoreNEURON CLI
// options so it is removed from "nrn_arg" first construct all arguments as string
std::string filtered_nrn_arg{nrn_arg};
const auto ind =
static_cast<std::string>(filtered_nrn_arg).find(corenrn_skip_write_model_to_disk_arg);
if (ind != std::string::npos) {
corenrn_skip_write_model_to_disk = true;
filtered_nrn_arg.erase(ind, corenrn_skip_write_model_to_disk_arg.size());
}
// set coreneuron's internal variable based on neuron arguments
corenrn_embedded = true;
corenrn_embedded = !corenrn_skip_write_model_to_disk;
corenrn_embedded_nthread = nthread;
coreneuron::nrn_have_gaps = have_gaps != 0;
coreneuron::nrn_use_fast_imem = use_fast_imem != 0;

// set number of openmp threads
set_openmp_threads(nthread);

// first construct all arguments as string
std::string args(nrn_arg);

args.insert(0, " coreneuron ");
args.append(" --skip-mpi-finalize ");
filtered_nrn_arg.insert(0, " coreneuron ");
filtered_nrn_arg.append(" --skip-mpi-finalize ");

if (use_mpi) {
args.append(" --mpi ");
filtered_nrn_arg.append(" --mpi ");
}

add_mpi_library_arg(mpi_lib, args);
add_mpi_library_arg(mpi_lib, filtered_nrn_arg);

// pre-process argumnets from neuron and prepare new for coreneuron
int argc;
char** argv;

char* new_arg = prepare_args(argc, argv, args);
char* new_arg = prepare_args(argc, argv, filtered_nrn_arg);

// initialize internal arguments
mk_mech_init(argc, argv);
Expand Down
Empty file modified src/ivoc/ivocconf.h
100755 → 100644
Empty file.
39 changes: 19 additions & 20 deletions src/ivoc/matrix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,13 @@ Matrix* matrix_arg(int i) {
return (Matrix*) (ob->u.this_pointer);
}

Object** Matrix::temp_objvar() {
Matrix* m = (Matrix*) this;
static Object** temp_objvar(Matrix* m) {
Object** po;
if (m->obj_) {
po = hoc_temp_objptr(m->obj_);
} else {
po = hoc_temp_objvar(nrn_matrix_sym, (void*) m);
obj_ = *po;
m->obj_ = *po;
}
return po;
}
Expand Down Expand Up @@ -189,7 +188,7 @@ static double m_scanf(void* v) {
static Object** m_resize(void* v) {
Matrix* m = (Matrix*) v;
m->resize((int) (chkarg(1, 1., 1e9) + EPS), (int) (chkarg(2, 1., 1e9) + EPS));
return m->temp_objvar();
return temp_objvar(m);
}

static Object** m_mulv(void* v) {
Expand Down Expand Up @@ -265,7 +264,7 @@ static Object** m_add(void* v) {
out = matrix_arg(2);
}
m->add(matrix_arg(1), out);
return out->temp_objvar();
return temp_objvar(out);
}

static Object** m_bcopy(void* v) {
Expand All @@ -287,7 +286,7 @@ static Object** m_bcopy(void* v) {
}
out = get_out_mat(m, m0, n0, i);
m->bcopy(out, i0, j0, m0, n0, i1, j1);
return out->temp_objvar();
return temp_objvar(out);
}

static Object** m_mulm(void* v) {
Expand All @@ -305,22 +304,22 @@ static Object** m_mulm(void* v) {
out->resize(m->nrow(), in->ncol());
check_domain(m->ncol(), in->nrow());
m->mulm(in, out);
return out->temp_objvar();
return temp_objvar(out);
}

static Object** m_c(void* v) {
Matrix* m = (Matrix*) v;
Matrix* out = get_out_mat(m, 1);
m->copy(out);
return out->temp_objvar();
return temp_objvar(out);
}

static Object** m_transpose(void* v) {
Matrix* m = (Matrix*) v;
Matrix* out = get_out_mat(m, 1);
out->resize(m->ncol(), m->nrow());
m->transpose(out);
return out->temp_objvar();
return temp_objvar(out);
}

static Object** m_symmeig(void* v) {
Expand Down Expand Up @@ -375,7 +374,7 @@ static Object** m_muls(void* v) {
// out->resize(...
// }
m->muls(*getarg(1), out);
return out->temp_objvar();
return temp_objvar(out);
}

static Object** m_getrow(void* v) {
Expand Down Expand Up @@ -444,7 +443,7 @@ static Object** m_setrow(void* v) {
#endif
m->setrow(k, in);
}
return m->temp_objvar();
return temp_objvar(m);
}

static Object** m_setcol(void* v) {
Expand All @@ -461,7 +460,7 @@ static Object** m_setcol(void* v) {
#endif
m->setcol(k, in);
}
return m->temp_objvar();
return temp_objvar(m);
}

static Object** m_setdiag(void* v) {
Expand All @@ -478,7 +477,7 @@ static Object** m_setdiag(void* v) {
#endif
m->setdiag(k, in);
}
return m->temp_objvar();
return temp_objvar(m);
}

static Object** m_getdiag(void* v) {
Expand Down Expand Up @@ -510,35 +509,35 @@ static Object** m_getdiag(void* v) {
static Object** m_zero(void* v) {
Matrix* m = (Matrix*) v;
m->zero();
return m->temp_objvar();
return temp_objvar(m);
}

static Object** m_ident(void* v) {
Matrix* m = (Matrix*) v;
m->ident();
return m->temp_objvar();
return temp_objvar(m);
}

static Object** m_exp(void* v) {
Matrix* m = (Matrix*) v;
Matrix* out = get_out_mat(m, 1, "exponentiation");
m->exp(out);
return out->temp_objvar();
return temp_objvar(out);
}

static Object** m_pow(void* v) {
Matrix* m = (Matrix*) v;
int k = (int) chkarg(1, 0., 100.);
Matrix* out = get_out_mat(m, 2, "raising to a power");
m->pow(k, out);
return out->temp_objvar();
return temp_objvar(out);
}

static Object** m_inverse(void* v) {
Matrix* m = (Matrix*) v;
Matrix* out = get_out_mat(m, 1);
m->inverse(out);
return out->temp_objvar();
return temp_objvar(out);
}

static double m_det(void* v) {
Expand Down Expand Up @@ -619,7 +618,7 @@ static Object** m_set(void* v) {
*(m->mep(i, j)) = *getarg(++k);
}
}
return m->temp_objvar();
return temp_objvar(m);
}

static Object** m_to_vector(void* v) {
Expand Down Expand Up @@ -657,7 +656,7 @@ static Object** m_from_vector(void* v) {
for (i = 0; i < nrow; ++i) {
*(m->mep(i, j)) = ve[k++];
}
return m->temp_objvar();
return temp_objvar(m);
}

static Member_func m_members[] = {
Expand Down
1 change: 0 additions & 1 deletion src/ivoc/ocmatrix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ static void Vect2VEC(Vect* v1, VEC& v2) {
}

OcMatrix::OcMatrix(int type) {
obj_ = nullptr;
type_ = type;
}
OcMatrix::~OcMatrix() {}
Expand Down
3 changes: 1 addition & 2 deletions src/ivoc/ocmatrix.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,13 +139,12 @@ class OcMatrix {
}

void unimp();
Object** temp_objvar();

protected:
OcMatrix(int type);

public:
Object* obj_;
Object* obj_{};

private:
int type_;
Expand Down
2 changes: 1 addition & 1 deletion src/neuron/container/node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ struct handle_interface: handle_base<Identifier> {
<< " v=" << handle.v() << " area=" << handle.area() << " a=" << handle.a()
<< " b=" << handle.b() << " d=" << handle.d() << '}';
} else {
os << "Node{null}";
return os << "Node{null}";
}
}
};
Expand Down
Loading

0 comments on commit 38f8dad

Please sign in to comment.