Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modernize DECREF (MPI edition). #3207

Draft
wants to merge 6 commits into
base: 1uc/modernize-decref-3
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 23 additions & 30 deletions src/nrnpython/nrnpy_p2h.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -523,24 +523,22 @@ static double guigetval(Object* ho) {
static void guisetval(Object* ho, double x) {
PyObject* po = ((Py2Nrn*) ho->u.this_pointer)->po_;
nanobind::gil_scoped_acquire lock{};
PyObject* pn = PyFloat_FromDouble(x);
auto pn = nb::steal(PyFloat_FromDouble(x));
PyObject* p = PyTuple_GetItem(po, 0);
if (PySequence_Check(p) || PyMapping_Check(p)) {
PyObject_SetItem(p, PyTuple_GetItem(po, 1), pn);
PyObject_SetItem(p, PyTuple_GetItem(po, 1), pn.ptr());
} else {
PyObject_SetAttr(p, PyTuple_GetItem(po, 1), pn);
PyObject_SetAttr(p, PyTuple_GetItem(po, 1), pn.ptr());
}
Py_XDECREF(pn);
}

static int guigetstr(Object* ho, char** cpp) {
PyObject* po = ((Py2Nrn*) ho->u.this_pointer)->po_;
nanobind::gil_scoped_acquire lock{};

PyObject* r = PyObject_GetAttr(PyTuple_GetItem(po, 0), PyTuple_GetItem(po, 1));
PyObject* pn = PyObject_Str(r);
Py2NRNString name(pn);
Py_DECREF(pn);
auto pn = nb::steal(PyObject_Str(r));
Py2NRNString name(pn.ptr());
char* cp = name.c_str();
if (*cpp && strcmp(*cpp, cp) == 0) {
return 0;
Expand Down Expand Up @@ -807,7 +805,6 @@ static PyObject* py_broadcast(PyObject* psrc, int root) {
static Object* py_alltoall_type(int size, int type) {
int np = nrnmpi_numprocs; // of subworld communicator
PyObject* psrc = NULL;
PyObject* pdest = NULL;

if (type == 1 || type == 5) { // alltoall, scatter
Object* o = *hoc_objgetarg(1);
Expand All @@ -828,13 +825,11 @@ static Object* py_alltoall_type(int size, int type) {
if (type == 1) {
return o;
} else { // return psrc[0]
pdest = PyList_GetItem(psrc, 0);
Py_INCREF(pdest);
Object* ho = nrnpy_po2ho(pdest);
auto pdest = nb::borrow(PyList_GetItem(psrc, 0));
Object* ho = nrnpy_po2ho(pdest.ptr());
if (ho) {
--ho->refcount;
}
Py_XDECREF(pdest);
return ho;
}
}
Expand All @@ -844,37 +839,38 @@ static Object* py_alltoall_type(int size, int type) {
Py_INCREF(psrc);

if (np == 1) {
nb::object pdest;
if (type == 4) { // broadcast is just the PyObject
pdest = psrc;
pdest = nb::steal(psrc);
} else { // allgather and gather must wrap psrc in list
pdest = PyList_New(1);
PyList_SetItem(pdest, 0, psrc);
pdest = nb::steal(PyList_New(1));
PyList_SetItem(pdest.ptr(), 0, psrc);
}
Object* ho = nrnpy_po2ho(pdest);
Object* ho = nrnpy_po2ho(pdest.ptr());
if (ho) {
--ho->refcount;
}
Py_XDECREF(pdest);
return ho;
}
}

#if NRNMPI
setpickle();
int root;
nb::object pdest;

if (type == 2) {
pdest = py_allgather(psrc);
pdest = nb::steal(py_allgather(psrc));
Py_DECREF(psrc);
} else if (type != 1 && type != 5) {
root = size;
if (root < 0 || root >= np) {
hoc_execerror("root rank must be >= 0 and < nhost", 0);
}
if (type == 3) {
pdest = py_gather(psrc, root);
pdest = nb::steal(py_gather(psrc, root));
} else if (type == 4) {
pdest = py_broadcast(psrc, root);
pdest = nb::steal(py_broadcast(psrc, root));
}
Py_DECREF(psrc);
} else {
Expand Down Expand Up @@ -929,9 +925,9 @@ static Object* py_alltoall_type(int size, int type) {
sdispl = mk_displ(scnt.data());
rdispl = mk_displ(rcnt);
if (size < 0) {
pdest = PyTuple_New(2);
PyTuple_SetItem(pdest, 0, Py_BuildValue("l", (long) sdispl[np]));
PyTuple_SetItem(pdest, 1, Py_BuildValue("l", (long) rdispl[np]));
pdest = nb::steal(PyTuple_New(2));
PyTuple_SetItem(pdest.ptr(), 0, Py_BuildValue("l", (long) sdispl[np]));
PyTuple_SetItem(pdest.ptr(), 1, Py_BuildValue("l", (long) rdispl[np]));
delete[] sdispl;
delete[] rcnt;
delete[] rdispl;
Expand All @@ -940,7 +936,7 @@ static Object* py_alltoall_type(int size, int type) {
nrnmpi_char_alltoallv(s.data(), scnt.data(), sdispl, r, rcnt, rdispl);
delete[] sdispl;

pdest = char2pylist(r, np, rcnt, rdispl);
pdest = nb::steal(char2pylist(r, np, rcnt, rdispl));

delete[] r;
delete[] rcnt;
Expand All @@ -963,19 +959,16 @@ static Object* py_alltoall_type(int size, int type) {
delete[] sdispl;

if (rcnt[0]) {
nb::object po = unpickle(r);
pdest = po.release().ptr();
pdest = unpickle(r);
} else {
pdest = Py_None;
Py_INCREF(pdest);
pdest = nb::none();
}

delete[] rcnt;
assert(rdispl == NULL);
}
}
Object* ho = nrnpy_po2ho(pdest);
Py_XDECREF(pdest);
Object* ho = nrnpy_po2ho(pdest.ptr());
if (ho) {
--ho->refcount;
}
Expand Down