Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicolas Cornu committed Nov 20, 2024
2 parents 6a83aa2 + 9aeafb3 commit 6b675a0
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 206 deletions.
1 change: 0 additions & 1 deletion src/gnu/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ add_library(
Poisson.cpp
Rand.cpp
Random.cpp
RNG.cpp
Uniform.cpp
Weibull.cpp)
set_property(TARGET nrngnu PROPERTY POSITION_INDEPENDENT_CODE ON)
Expand Down
132 changes: 0 additions & 132 deletions src/gnu/RNG.cpp

This file was deleted.

68 changes: 20 additions & 48 deletions src/gnu/RNG.h
Original file line number Diff line number Diff line change
@@ -1,56 +1,28 @@
// This may look like C code, but it is really -*- C++ -*-
/*
Copyright (C) 1988 Free Software Foundation
written by Dirk Grunwald ([email protected])
This file is part of the GNU C++ Library. This library is free
software; you can redistribute it and/or modify it under the terms of
the GNU Library General Public License as published by the Free
Software Foundation; either version 2 of the License, or (at your
option) any later version. This library is distributed in the hope
that it will be useful, but WITHOUT ANY WARRANTY; without even the
implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the GNU Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free Software
Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#pragma once
/*
* int32_t and uint32_t have been defined by the configure procedure. Just
* use these in place of the ones that libg++ used to provide.
*/
#include <cstdint>
#include <cassert>
#include <cmath>

union PrivateRNGSingleType { // used to access floats as unsigneds
float s;
uint32_t u;
};

union PrivateRNGDoubleType { // used to access doubles as unsigneds
double d;
uint32_t u[2];
};
#include <limits>

//
// Base class for Random Number Generators.
//
class RNG {
static PrivateRNGSingleType singleMantissa; // mantissa bit vector
static PrivateRNGDoubleType doubleMantissa; // mantissa bit vector
public:
RNG();
virtual ~RNG();
//
using result_type = std::uint32_t;

RNG() = default;
virtual ~RNG() = default;

// Return a long-words word of random bits
//
virtual uint32_t asLong() = 0;
virtual result_type asLong() = 0;
virtual void reset() = 0;
//
// Return random bits converted to either a float or a double
//
virtual float asFloat();
virtual double asDouble();

// Return random bits converted to a double
virtual double asDouble() = 0;

static constexpr result_type min() {
return std::numeric_limits<result_type>::min();
}
static constexpr result_type max() {
return std::numeric_limits<result_type>::max();
}
result_type operator()() {
return asLong();
}
};
7 changes: 2 additions & 5 deletions src/nrnpython/nrnpy_hoc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3368,7 +3368,6 @@ extern PyObject* nrn_type_from_metaclass(PyTypeObject* meta,

extern "C" NRN_EXPORT PyObject* nrnpy_hoc() {
PyObject* m;
PyObject* bases;
PyTypeObject* pto;
PyType_Spec spec;
nrnpy_vec_from_python_p_ = nrnpy_vec_from_python;
Expand Down Expand Up @@ -3437,14 +3436,13 @@ extern "C" NRN_EXPORT PyObject* nrnpy_hoc() {
}


bases = PyTuple_Pack(1, hocobject_type);
Py_INCREF(bases);
auto bases = nb::steal(PyTuple_Pack(1, hocobject_type));
for (auto name: py_exposed_classes) {
// TODO: obj_spec_from_name needs a hoc. prepended
exposed_py_type_names.push_back(std::string("hoc.") + name);
spec = obj_spec_from_name(exposed_py_type_names.back().c_str());
pto = (PyTypeObject*)
nrn_type_from_metaclass((PyTypeObject*) custom_hocclass, m, &spec, bases);
nrn_type_from_metaclass((PyTypeObject*) custom_hocclass, m, &spec, bases.ptr());
hocclass* hclass = (hocclass*) pto;
hclass->sym = hoc_lookup(name);
// printf("%s hocclass pto->tp_basicsize = %zd sizeof(*pto)=%zd\n",
Expand All @@ -3458,7 +3456,6 @@ extern "C" NRN_EXPORT PyObject* nrnpy_hoc() {
return NULL;
}
}
Py_DECREF(bases);

topmethdict = PyDict_New();
for (PyMethodDef* meth = toplevel_methods; meth->ml_name != NULL; meth++) {
Expand Down
33 changes: 13 additions & 20 deletions src/nrnpython/nrnpy_p2h.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ static void py2n_component(Object* ob, Symbol* sym, int nindex, int isfunc) {
if (strcmp(sym->name, "_") == 0) {
tail = head;
} else {
tail = nb::steal(PyObject_GetAttrString(head.ptr(), sym->name));
tail = head.attr(sym->name);
}
}
if (!tail) {
Expand Down Expand Up @@ -224,7 +224,7 @@ static void py2n_component(Object* ob, Symbol* sym, int nindex, int isfunc) {
// TypeError: list indices must be integers or slices, not hoc.HocObject
arg = nb::steal(nrnpy_hoc_pop("nindex py2n_component"));
}
result = nb::steal(PyObject_GetItem(tail.ptr(), arg.ptr()));
result = tail[arg];
if (!result) {
PyErr_Print();
hoc_execerror("Python get item failed:", hoc_object_name(ob));
Expand All @@ -237,8 +237,8 @@ static void py2n_component(Object* ob, Symbol* sym, int nindex, int isfunc) {
// if numeric, string, or python HocObject return those
if (nrnpy_numbercheck(result.ptr())) {
hoc_pop_defer();
auto pn = nb::steal(PyNumber_Float(result.ptr()));
hoc_pushx(PyFloat_AsDouble(pn.ptr()));
double d = static_cast<double>(nb::float_(result));
hoc_pushx(d);
} else if (is_python_string(result.ptr())) {
char** ts = hoc_temp_charptr();
Py2NRNString str(result.ptr(), true);
Expand Down Expand Up @@ -599,32 +599,25 @@ static char* nrnpyerr_str() {
auto traceback = nb::steal(ptraceback);

// try for full backtrace
nb::object py_str;
char* cmes = NULL;
nb::str py_str;
char* cmes = nullptr;

// Since traceback.format_exception returns list of strings, wrap
// in neuron.format_exception that returns a string.
if (!traceback) {
traceback = nb::none();
}
auto pyth_module = nb::steal(PyImport_ImportModule("neuron"));
nb::module_ pyth_module = nb::module_::import_("neuron");
if (pyth_module) {
auto pyth_func = nb::steal(
PyObject_GetAttrString(pyth_module.ptr(), "format_exception"));
nb::callable pyth_func = pyth_module.attr("format_exception");
if (pyth_func) {
py_str = nb::steal(PyObject_CallFunctionObjArgs(
pyth_func.ptr(), type.ptr(), value.ptr(), traceback.ptr(), NULL));
py_str = nb::str(pyth_func(type, value, traceback));
}
}
if (py_str) {
Py2NRNString mes(py_str.ptr());
if (mes.err()) {
Fprintf(stderr, "nrnperr_str: Py2NRNString failed\n");
} else {
cmes = strdup(mes.c_str());
if (!cmes) {
Fprintf(stderr, "nrnpyerr_str: strdup failed\n");
}
cmes = strdup(py_str.c_str());
if (!cmes) {
Fprintf(stderr, "nrnpyerr_str: strdup failed\n");
}
}

Expand All @@ -635,7 +628,7 @@ static char* nrnpyerr_str() {

return cmes;
}
return NULL;
return nullptr;
}

std::vector<char> call_picklef(const std::vector<char>& fname, int narg) {
Expand Down

0 comments on commit 6b675a0

Please sign in to comment.