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

Add code to drop the GIL. Demostrate usefulness on reads/writes. #209

Draft
wants to merge 8 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ On ubuntu you can install these with::
this::

CFLAGS="-std=c++11 \
-lpthread \
-I/usr/local/Cellar/boost/1.68.0/include/ \
-I/usr/local/include/ \
-L/usr/local/Cellar/boost/1.68.0/lib \
Expand Down
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,8 @@ def get_extensions():
library_dirs=library_dirs,
include_dirs=include_dirs,
# Since casacore 3.0.0 we have to be C++11
extra_compile_args=['-std=c++11']))
extra_compile_args=['-std=c++11',
'-lpthread']))
return extensions


Expand Down
115 changes: 115 additions & 0 deletions src/guards.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
#include <iostream>

#include <boost/function.hpp>
#include <boost/function_types/components.hpp>
#include <boost/function_types/function_type.hpp>
#include <boost/function_types/result_type.hpp>
#include <boost/python.hpp>
#include <boost/tuple/tuple.hpp>

namespace details {

/// @brief Functor that will invoke a function while holding a guard.
/// Upon returning from the function, the guard is released.
template <typename Signature,
typename Guard>
class guarded_function
{
public:

typedef typename boost::function_types::result_type<Signature>::type
result_type;

template <typename Fn>
guarded_function(Fn fn)
: fn_(fn)
{}

template <typename... Args>
result_type operator()(Args... args)
{
Guard g;
return fn_(args...);
}

private:
boost::function<Signature> fn_;
};

/// @brief Provides signature type.
template <typename Signature>
struct mpl_signature
{
typedef typename boost::function_types::components<Signature>::type type;
};

// Support boost::function.
template <typename Signature>
struct mpl_signature<boost::function<Signature> >:
public mpl_signature<Signature>
{};

/// @brief Create a callable object with guards.
template <typename Guard,
typename Fn,
typename Policy>
boost::python::object with_aux(Fn fn, const Policy& policy)
{
// Obtain the components of the Fn. This will decompose non-member
// and member functions into an mpl sequence.
// R (*)(A1) => R, A1
// R (C::*)(A1) => R, C*, A1
typedef typename mpl_signature<Fn>::type mpl_signature_type;

// Synthesize the components into a function type. This process
// causes member functions to require the instance argument.
// This is necessary because member functions will be explicitly
// provided the 'self' argument.
// R, A1 => R (*)(A1)
// R, C*, A1 => R (*)(C*, A1)
typedef typename boost::function_types::function_type<
mpl_signature_type>::type signature_type;

// Create a callable boost::python::object that delegates to the
// guarded_function.
return boost::python::make_function(
guarded_function<signature_type, Guard>(fn),
policy, mpl_signature_type());
}

} // namespace details

/// @brief Create a callable object with guards.
template <typename Guard,
typename Fn,
typename Policy>
boost::python::object with(const Fn& fn, const Policy& policy)
{
return details::with_aux<Guard>(fn, policy);
}

/// @brief Create a callable object with guards.
template <typename Guard,
typename Fn>
boost::python::object with(const Fn& fn)
{
return with<Guard>(fn, boost::python::default_call_policies());
}

/// @brief Guard that will unlock the GIL upon construction, and
/// reacquire it upon destruction.
struct no_gil
{
public:
no_gil() { state_ = PyEval_SaveThread(); }
~no_gil() { PyEval_RestoreThread(state_); }
private:
PyThreadState* state_;
};

/// @brief Guard that prints to std::cout.
struct echo_guard
{
echo_guard() { std::cout << "echo_guard()" << std::endl; }
~echo_guard() { std::cout << "~echo_guard()" << std::endl; }
};
100 changes: 15 additions & 85 deletions src/pytable.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

#include <boost/python.hpp>
#include <boost/python/args.hpp>
#include "guards.h"

using namespace boost::python;

Expand Down Expand Up @@ -139,91 +140,20 @@ namespace casacore { namespace python {
.def ("_iscelldefined", &TableProxy::cellContentsDefined,
(boost::python::arg("columnname"),
boost::python::arg("rownr")))
.def ("_getcell", &TableProxy::getCell,
(boost::python::arg("columnname"),
boost::python::arg("rownr")))
.def ("_getcellvh", &TableProxy::getCellVH,
(boost::python::arg("columnname"),
boost::python::arg("rownr"),
boost::python::arg("value")))
.def ("_getcellslice", &TableProxy::getCellSliceIP,
(boost::python::arg("columnname"),
boost::python::arg("rownr"),
boost::python::arg("blc"),
boost::python::arg("trc"),
boost::python::arg("inc")))
.def ("_getcellslicevh", &TableProxy::getCellSliceVHIP,
(boost::python::arg("columnname"),
boost::python::arg("rownr"),
boost::python::arg("blc"),
boost::python::arg("trc"),
boost::python::arg("inc"),
boost::python::arg("value")))
.def ("_getcol", &TableProxy::getColumn,
(boost::python::arg("columnname"),
boost::python::arg("startrow"),
boost::python::arg("nrow"),
boost::python::arg("rowincr")))
.def ("_getcolvh", &TableProxy::getColumnVH,
(boost::python::arg("columnname"),
boost::python::arg("startrow"),
boost::python::arg("nrow"),
boost::python::arg("rowincr"),
boost::python::arg("value")))
.def ("_getvarcol", &TableProxy::getVarColumn,
(boost::python::arg("columnname"),
boost::python::arg("startrow"),
boost::python::arg("nrow"),
boost::python::arg("rowincr")))
.def ("_getcolslice", &TableProxy::getColumnSliceIP,
(boost::python::arg("columnname"),
boost::python::arg("blc"),
boost::python::arg("trc"),
boost::python::arg("inc"),
boost::python::arg("startrow"),
boost::python::arg("nrow"),
boost::python::arg("rowincr")))
.def ("_getcolslicevh", &TableProxy::getColumnSliceVHIP,
(boost::python::arg("columnname"),
boost::python::arg("blc"),
boost::python::arg("trc"),
boost::python::arg("inc"),
boost::python::arg("startrow"),
boost::python::arg("nrow"),
boost::python::arg("rowincr"),
boost::python::arg("value")))
.def ("_putcell", &TableProxy::putCell,
(boost::python::arg("columnname"),
boost::python::arg("rownr"),
boost::python::arg("value")))
.def ("_putcellslice", &TableProxy::putCellSliceIP,
(boost::python::arg("columnname"),
boost::python::arg("rownr"),
boost::python::arg("value"),
boost::python::arg("blc"),
boost::python::arg("trc"),
boost::python::arg("inc")))
.def ("_putcol", &TableProxy::putColumn,
(boost::python::arg("columnname"),
boost::python::arg("startrow"),
boost::python::arg("nrow"),
boost::python::arg("rowincr"),
boost::python::arg("value")))
.def ("_putvarcol", &TableProxy::putVarColumn,
(boost::python::arg("columnname"),
boost::python::arg("startrow"),
boost::python::arg("nrow"),
boost::python::arg("rowincr"),
boost::python::arg("value")))
.def ("_putcolslice", &TableProxy::putColumnSliceIP,
(boost::python::arg("columnname"),
boost::python::arg("value"),
boost::python::arg("blc"),
boost::python::arg("trc"),
boost::python::arg("inc"),
boost::python::arg("startrow"),
boost::python::arg("nrow"),
boost::python::arg("rowincr")))
.def ("_getcell", with<no_gil>(&TableProxy::getCell))
.def ("_getcellvh", with<no_gil>(&TableProxy::getCellVH))
.def ("_getcellslice", with<no_gil>(&TableProxy::getCellSliceIP))
.def ("_getcellslicevh", with<no_gil>(&TableProxy::getCellSliceVHIP))
.def ("_getcol", with<no_gil>(&TableProxy::getColumn))
.def ("_getcolvh", with<no_gil>(&TableProxy::getColumnVH))
.def ("_getvarcol", with<no_gil>(&TableProxy::getVarColumn))
.def ("_getcolslice", with<no_gil>(&TableProxy::getColumnSliceIP))
.def ("_getcolslicevh", with<no_gil>(&TableProxy::getColumnSliceVHIP))
.def ("_putcell", with<no_gil>(&TableProxy::putCell))
.def ("_putcellslice", with<no_gil>(&TableProxy::putCellSliceIP))
.def ("_putcol", with<no_gil>(&TableProxy::putColumn))
.def ("_putvarcol", with<no_gil>(&TableProxy::putVarColumn))
.def ("_putcolslice", with<no_gil>(&TableProxy::putColumnSliceIP))
.def ("_getcolshapestring", &TableProxy::getColumnShapeString,
(boost::python::arg("columnname"),
boost::python::arg("startrow"),
Expand Down
3 changes: 3 additions & 0 deletions src/tables.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,18 @@
#include <casacore/python/Converters/PycValueHolder.h>
#include <casacore/python/Converters/PycRecord.h>
#include <casacore/python/Converters/PycArray.h>
#include <casacore/python/Converters/PyNotThreadSafe.h>
#include <casacore/tables/Tables/TableProxy.h>

#include <casacore/meas/MeasUDF/Register.h>
#include <casacore/derivedmscal/DerivedMC/Register.h>

#include <boost/python.hpp>
#include <string>

BOOST_PYTHON_MODULE(_tables)
{
casacore::python::registerNotThreadSafeException();
casacore::python::register_convert_excp();
casacore::python::register_convert_basicdata();
casacore::python::register_convert_casa_valueholder();
Expand Down