Skip to content

Commit

Permalink
Mild performance improvement for functionLocal/1D calls to Python.
Browse files Browse the repository at this point in the history
These now avoid the heavy wrapper CallMethod and use the boost call_method
directly to avoid double locking the GIL and checking that the python
method exists each time.

Refs #970
  • Loading branch information
martyngigg committed Apr 17, 2013
1 parent b235d13 commit d7d443b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ namespace Mantid
}

/**
* Translates between the C++ signature & the Python signature
* Translates between the C++ signature & the Python signature called by Fit
* @param out The 1D data array of size nData that stores the output values
* @param xValues The input X values
* @param nData The size of the two arrays
Expand All @@ -40,11 +40,14 @@ namespace Mantid
Py_intptr_t dims[1] = { static_cast<Py_intptr_t>(nData) } ;
object xvals = object(handle<>(Converters::WrapReadOnly::apply<double>::createFromArray(xValues, 1,dims)));
object outnp = object(handle<>(Converters::WrapReadWrite::apply<double>::createFromArray(out, 1,dims)));
function1D(xvals, outnp);

// Deliberately avoids using the CallMethod wrappers. They lock the GIL again and
// will check for each function call whether the wrapped method exists.
boost::python::call_method<void,object,object>(getSelf(), "function1D", xvals, outnp);
}

/**
* Python-type signature version of above
* Python-type signature version of above to be called directly from Python
* @param xvals The input X values in read-only numpy array
* @param out A read/write numpy array of doubles to store the results
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ namespace Mantid


/**
* Translates between the C++ signature & the Python signature
* Translates between the C++ signature & the Python signature and will be called by Fit
* @param out The 1D data array of size nData that stores the output values
* @param xValues The input X values
* @param nData The size of the two arrays
Expand All @@ -92,11 +92,14 @@ namespace Mantid
object xvals = object(handle<>(WrapReadOnly::apply<double>::createFromArray(xValues, 1,dims)));
object outnp = object(handle<>(WrapReadWrite::apply<double>::createFromArray(out, 1,dims)));

functionLocal(xvals, outnp);
// Deliberately avoids using the CallMethod wrappers. They lock the GIL again and
// will check for each function call whether the wrapped method exists.
boost::python::call_method<void,object,object>(getSelf(), "functionLocal", xvals,outnp);
}

/**
* Python-type signature version of above
* Python-type signature version of above so that users can call functionLocal directly from Python on a factory
* created object
* @param xvals The input X values in read-only numpy array
* @param out A read/write numpy array of doubles to store the results
*/
Expand All @@ -106,7 +109,7 @@ namespace Mantid
}

/**
* Translates between the C++ signature & the Python signature
* Translates between the C++ signature & the Python signature and will be called by Fit
* @param out The Jacobian matrix storing the partial derivatives of the function w.r.t to the parameters
* @param xValues The input X values
* @param nData The size of the two arrays
Expand All @@ -126,11 +129,13 @@ namespace Mantid
// So we'll do the work of the wrapper for it
object jacobian = object(handle<>(boost::python::to_python_value<API::Jacobian*>()(out)));

functionDerivLocal(xvals, jacobian);
// Deliberately avoids using the CallMethod wrappers. They lock the GIL again and
// will check for each function call whether the wrapped method exists.
boost::python::call_method<void,object,object>(getSelf(), "functionDerivLocal", xvals,jacobian);
}

/**
* Python-type signature version of above
* Python-type signature version of above that can be called directly from Python
* @param xvals The input X values in read-only numpy array
* @param jacobian The Jacobian matrix storing the partial derivatives of the function w.r.t to the parameters
*/
Expand Down

0 comments on commit d7d443b

Please sign in to comment.