Commit a28d22f6 authored by Matthieu Dorier's avatar Matthieu Dorier

Merge branch 'dev-pybind11' into 'master'

moved from boost.python to pybind11

See merge request !2
parents e42fea37 84e2ce9b
# (C) 2018 The University of Chicago # (C) 2018 The University of Chicago
# See COPYRIGHT in top-level directory. # See COPYRIGHT in top-level directory.
import _pybaketarget
import _pybakeserver import _pybakeserver
import pymargo import pymargo
from pybake.target import BakeTargetID from pybake.target import BakeTargetID
......
This diff is collapsed.
...@@ -3,14 +3,7 @@ ...@@ -3,14 +3,7 @@
* *
* See COPYRIGHT in top-level directory. * See COPYRIGHT in top-level directory.
*/ */
#define BOOST_NO_AUTO_PTR #include <pybind11/pybind11.h>
#include <boost/python.hpp>
#include <boost/python/return_opaque_pointer.hpp>
#include <boost/python/handle.hpp>
#include <boost/python/enum.hpp>
#include <boost/python/def.hpp>
#include <boost/python/module.hpp>
#include <boost/python/return_value_policy.hpp>
#include <string> #include <string>
#include <vector> #include <vector>
#include <cstring> #include <cstring>
...@@ -19,59 +12,66 @@ ...@@ -19,59 +12,66 @@
#include <bake.h> #include <bake.h>
#include <bake-server.h> #include <bake-server.h>
BOOST_PYTHON_OPAQUE_SPECIALIZED_TYPE_ID(margo_instance) namespace py11 = pybind11;
BOOST_PYTHON_OPAQUE_SPECIALIZED_TYPE_ID(bake_server_context_t)
namespace bpl = boost::python; typedef py11::capsule pymargo_instance_id;
typedef py11::capsule pymargo_addr;
typedef py11::capsule pybake_provider_t;
static bake_provider_t pybake_provider_register(margo_instance_id mid, uint8_t provider_id) { #define MID2CAPSULE(__mid) py11::capsule((void*)(__mid), "margo_instance_id", nullptr)
#define ADDR2CAPSULE(__addr) py11::capsule((void*)(__addr), "hg_addr_t", nullptr)
#define BAKEPR2CAPSULE(__bpr) py11::capsule((void*)(__bpr), "bake_provider_t", nullptr)
static pybake_provider_t pybake_provider_register(pymargo_instance_id mid, uint8_t provider_id) {
bake_provider_t provider; bake_provider_t provider;
int ret = bake_provider_register(mid, provider_id, BAKE_ABT_POOL_DEFAULT, &provider); int ret = bake_provider_register(mid, provider_id, BAKE_ABT_POOL_DEFAULT, &provider);
if(ret != 0) return NULL; if(ret != 0) return py11::none();
else return provider; else return BAKEPR2CAPSULE(provider);
} }
static bpl::object pybake_provider_add_storage_target( static py11::object pybake_provider_add_storage_target(
bake_provider_t provider, pybake_provider_t provider,
const std::string& target_name) { const std::string& target_name) {
bake_target_id_t target_id; bake_target_id_t target_id;
std::memset(&target_id, 0, sizeof(target_id)); std::memset(&target_id, 0, sizeof(target_id));
int ret = bake_provider_add_storage_target( int ret = bake_provider_add_storage_target(
provider, target_name.c_str(), &target_id); provider, target_name.c_str(), &target_id);
if(ret != 0) return bpl::object(); if(ret != 0) {
return bpl::object(target_id); return py11::none();
}
return py11::cast(target_id);
} }
static bool pybake_provider_remove_storage_target( static bool pybake_provider_remove_storage_target(
bake_provider_t provider, pybake_provider_t provider,
bake_target_id_t target_id) bake_target_id_t target_id)
{ {
return 0 == bake_provider_remove_storage_target(provider, target_id); return 0 == bake_provider_remove_storage_target(provider, target_id);
} }
static bool pybake_provider_remove_all_storage_targets( static bool pybake_provider_remove_all_storage_targets(
bake_provider_t provider) pybake_provider_t provider)
{ {
return 0 == bake_provider_remove_all_storage_targets(provider); return 0 == bake_provider_remove_all_storage_targets(provider);
} }
static uint64_t pybake_provider_count_storage_targets( static uint64_t pybake_provider_count_storage_targets(
bake_provider_t provider) pybake_provider_t provider)
{ {
uint64_t n = 0; uint64_t n = 0;
bake_provider_count_storage_targets(provider, &n); bake_provider_count_storage_targets(provider, &n);
return n; return n;
} }
static bpl::list pybake_provider_list_storage_targets( static py11::list pybake_provider_list_storage_targets(
bake_provider_t provider) pybake_provider_t provider)
{ {
std::vector<bake_target_id_t> result; std::vector<bake_target_id_t> result;
uint64_t n = pybake_provider_count_storage_targets(provider); uint64_t n = pybake_provider_count_storage_targets(provider);
if(n == 0) return bpl::list(); if(n == 0) return py11::list();
result.resize(n); result.resize(n);
bake_provider_list_storage_targets(provider, result.data()); bake_provider_list_storage_targets(provider, result.data());
bpl::list list_result; py11::list list_result;
for(const auto& t : result) list_result.append(t); for(const auto& t : result) list_result.append(t);
return list_result; return list_result;
} }
...@@ -81,19 +81,14 @@ static bool pybake_make_pool(const std::string& pool_name, ...@@ -81,19 +81,14 @@ static bool pybake_make_pool(const std::string& pool_name,
return 0 == bake_makepool(pool_name.c_str(), pool_size, mode); return 0 == bake_makepool(pool_name.c_str(), pool_size, mode);
} }
BOOST_PYTHON_MODULE(_pybakeserver) PYBIND11_MODULE(_pybakeserver, m)
{ {
#define ret_policy_opaque bpl::return_value_policy<bpl::return_opaque_pointer>() py11::module::import("_pybaketarget");
m.def("register", &pybake_provider_register);
bpl::import("_pybaketarget"); m.def("add_storage_target", &pybake_provider_add_storage_target);
bpl::opaque<bake_server_context_t>(); m.def("remove_storage_target", &pybake_provider_remove_storage_target);
bpl::def("register", &pybake_provider_register, ret_policy_opaque); m.def("remove_all_storage_targets", &pybake_provider_remove_all_storage_targets);
bpl::def("add_storage_target", &pybake_provider_add_storage_target); m.def("count_storage_targets", &pybake_provider_count_storage_targets);
bpl::def("remove_storage_target", &pybake_provider_remove_storage_target); m.def("list_storage_targets", &pybake_provider_list_storage_targets);
bpl::def("remove_all_storage_targets", &pybake_provider_remove_all_storage_targets); m.def("make_pool", &pybake_make_pool);
bpl::def("count_storage_targets", &pybake_provider_count_storage_targets);
bpl::def("list_storage_targets", &pybake_provider_list_storage_targets);
bpl::def("make_pool", &pybake_make_pool);
#undef ret_policy_opaque
} }
...@@ -3,14 +3,7 @@ ...@@ -3,14 +3,7 @@
* *
* See COPYRIGHT in top-level directory. * See COPYRIGHT in top-level directory.
*/ */
#define BOOST_NO_AUTO_PTR #include <pybind11/pybind11.h>
#include <boost/python.hpp>
#include <boost/python/return_opaque_pointer.hpp>
#include <boost/python/handle.hpp>
#include <boost/python/enum.hpp>
#include <boost/python/def.hpp>
#include <boost/python/module.hpp>
#include <boost/python/return_value_policy.hpp>
#include <string> #include <string>
#include <vector> #include <vector>
#include <cstring> #include <cstring>
...@@ -19,43 +12,45 @@ ...@@ -19,43 +12,45 @@
#include <bake.h> #include <bake.h>
#include <bake-server.h> #include <bake-server.h>
namespace bpl = boost::python; namespace py11 = pybind11;
static std::string pybake_target_id_to_string(bake_target_id_t tid) { static py11::bytes pybake_target_id_to_string(bake_target_id_t tid) {
char id[37]; char id[37];
uuid_unparse(tid.id, id); uuid_unparse(tid.id, id);
return std::string(id); return py11::bytes(std::string(id));
} }
static bpl::object pybake_target_id_from_string(const std::string& tidstr) { static py11::object pybake_target_id_from_string(const py11::bytes& btidstr) {
bake_target_id_t tid; bake_target_id_t tid;
memset(tid.id, 0, sizeof(uuid_t)); memset(tid.id, 0, sizeof(uuid_t));
if(tidstr.size() != 36) return bpl::object(); std::string tidstr = (std::string)btidstr;
if(tidstr.size() != 36) return py11::none();
int ret = uuid_parse((char*)tidstr.c_str(), tid.id); int ret = uuid_parse((char*)tidstr.c_str(), tid.id);
if(ret == 0) return bpl::object(tid); if(ret == 0) return py11::cast(tid);
else return bpl::object(); else return py11::none();
} }
static std::string pybake_region_id_to_string(const bake_region_id_t& region_id) { static py11::bytes pybake_region_id_to_string(const bake_region_id_t& region_id) {
std::string result((const char*)(&region_id), sizeof(region_id)); std::string result((const char*)(&region_id), sizeof(region_id));
return result; return py11::bytes(result);
} }
static bpl::object pybake_region_id_from_string(const std::string& region_str) { static py11::object pybake_region_id_from_string(const py11::bytes& bregion_str) {
bake_region_id_t result; bake_region_id_t result;
std::string region_str = (std::string)bregion_str;
memset(&result, 0, sizeof(result)); memset(&result, 0, sizeof(result));
if(region_str.size() != sizeof(bake_region_id_t)) if(region_str.size() != sizeof(bake_region_id_t))
return bpl::object(); return py11::none();
memcpy(&result, region_str.data(), sizeof(bake_region_id_t)); memcpy(&result, region_str.data(), sizeof(bake_region_id_t));
return bpl::object(result); return py11::cast(result);
} }
BOOST_PYTHON_MODULE(_pybaketarget) PYBIND11_MODULE(_pybaketarget, m)
{ {
bpl::class_<bake_target_id_t>("bake_target_id", bpl::no_init) py11::class_<bake_target_id_t>(m,"bake_target_id")
.def("__str__", pybake_target_id_to_string); .def("__str__", pybake_target_id_to_string);
bpl::def("target_id_from_string", pybake_target_id_from_string); m.def("target_id_from_string", pybake_target_id_from_string);
bpl::class_<bake_region_id_t>("bake_region_id", bpl::no_init) py11::class_<bake_region_id_t>(m,"bake_region_id")
.def("__str__", pybake_region_id_to_string); .def("__str__", pybake_region_id_to_string);
bpl::def("region_id_from_string", pybake_region_id_from_string); m.def("region_id_from_string", pybake_region_id_from_string);
} }
...@@ -10,8 +10,6 @@ import sys ...@@ -10,8 +10,6 @@ import sys
os.environ['OPT'] = " ".join( os.environ['OPT'] = " ".join(
flag for flag in opt.split() if flag != '-Wstrict-prototypes' flag for flag in opt.split() if flag != '-Wstrict-prototypes'
) )
python_version = str(sys.version_info[0])+str(sys.version_info[1])
# Find out if Numpy is present # Find out if Numpy is present
try: try:
import numpy import numpy
...@@ -23,23 +21,18 @@ except ImportError: ...@@ -23,23 +21,18 @@ except ImportError:
# For client... # For client...
pk = pkgconfig.parse('bake-client') pk = pkgconfig.parse('bake-client')
client_libraries = pk['libraries'] client_libraries = pk['libraries']
client_libraries.append('boost_python'+python_version)
if(has_numpy == 1):
client_libraries.append('boost_numpy'+python_version)
client_library_dirs = pk['library_dirs'] client_library_dirs = pk['library_dirs']
client_include_dirs = pk['include_dirs'] client_include_dirs = pk['include_dirs']
client_include_dirs.append(".") client_include_dirs.append(".")
# For server... # For server...
pk = pkgconfig.parse('bake-server') pk = pkgconfig.parse('bake-server')
server_libraries = pk['libraries'] server_libraries = pk['libraries']
server_libraries.append('boost_python'+python_version)
server_library_dirs = pk['library_dirs'] server_library_dirs = pk['library_dirs']
server_include_dirs = pk['include_dirs'] server_include_dirs = pk['include_dirs']
server_include_dirs.append(".") server_include_dirs.append(".")
# For target... # For target...
pk = pkgconfig.parse('uuid') pk = pkgconfig.parse('uuid')
target_libraries = pk['libraries'] target_libraries = pk['libraries']
target_libraries.append('boost_python'+python_version)
target_library_dirs = pk['library_dirs'] target_library_dirs = pk['library_dirs']
target_include_dirs = pk['include_dirs'] target_include_dirs = pk['include_dirs']
target_include_dirs.append('.') target_include_dirs.append('.')
......
...@@ -8,7 +8,7 @@ from pybake.server import BakeProvider ...@@ -8,7 +8,7 @@ from pybake.server import BakeProvider
mid = MargoInstance('tcp') mid = MargoInstance('tcp')
mid.enable_remote_shutdown() mid.enable_remote_shutdown()
mplex_id = 42 mplex_id = 42
print "Server running at address "+str(mid.addr())+"with mplex_id="+str(mplex_id) print "Server running at address "+str(mid.addr())+" with mplex_id="+str(mplex_id)
provider = BakeProvider(mid, mplex_id) provider = BakeProvider(mid, mplex_id)
target = provider.add_storage_target("/dev/shm/baketarget") target = provider.add_storage_target("/dev/shm/baketarget")
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment