Commit 84e2ce9b authored by Matthieu Dorier's avatar Matthieu Dorier

moved from boost.python to pybind11

parent e42fea37
# (C) 2018 The University of Chicago
# See COPYRIGHT in top-level directory.
import _pybaketarget
import _pybakeserver
import pymargo
from pybake.target import BakeTargetID
......
This diff is collapsed.
......@@ -3,14 +3,7 @@
*
* See COPYRIGHT in top-level directory.
*/
#define BOOST_NO_AUTO_PTR
#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 <pybind11/pybind11.h>
#include <string>
#include <vector>
#include <cstring>
......@@ -19,59 +12,66 @@
#include <bake.h>
#include <bake-server.h>
BOOST_PYTHON_OPAQUE_SPECIALIZED_TYPE_ID(margo_instance)
BOOST_PYTHON_OPAQUE_SPECIALIZED_TYPE_ID(bake_server_context_t)
namespace py11 = pybind11;
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;
int ret = bake_provider_register(mid, provider_id, BAKE_ABT_POOL_DEFAULT, &provider);
if(ret != 0) return NULL;
else return provider;
if(ret != 0) return py11::none();
else return BAKEPR2CAPSULE(provider);
}
static bpl::object pybake_provider_add_storage_target(
bake_provider_t provider,
static py11::object pybake_provider_add_storage_target(
pybake_provider_t provider,
const std::string& target_name) {
bake_target_id_t target_id;
std::memset(&target_id, 0, sizeof(target_id));
int ret = bake_provider_add_storage_target(
provider, target_name.c_str(), &target_id);
if(ret != 0) return bpl::object();
return bpl::object(target_id);
if(ret != 0) {
return py11::none();
}
return py11::cast(target_id);
}
static bool pybake_provider_remove_storage_target(
bake_provider_t provider,
pybake_provider_t provider,
bake_target_id_t target_id)
{
return 0 == bake_provider_remove_storage_target(provider, target_id);
}
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);
}
static uint64_t pybake_provider_count_storage_targets(
bake_provider_t provider)
pybake_provider_t provider)
{
uint64_t n = 0;
bake_provider_count_storage_targets(provider, &n);
return n;
}
static bpl::list pybake_provider_list_storage_targets(
bake_provider_t provider)
static py11::list pybake_provider_list_storage_targets(
pybake_provider_t provider)
{
std::vector<bake_target_id_t> result;
uint64_t n = pybake_provider_count_storage_targets(provider);
if(n == 0) return bpl::list();
if(n == 0) return py11::list();
result.resize(n);
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);
return list_result;
}
......@@ -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);
}
BOOST_PYTHON_MODULE(_pybakeserver)
PYBIND11_MODULE(_pybakeserver, m)
{
#define ret_policy_opaque bpl::return_value_policy<bpl::return_opaque_pointer>()
bpl::import("_pybaketarget");
bpl::opaque<bake_server_context_t>();
bpl::def("register", &pybake_provider_register, ret_policy_opaque);
bpl::def("add_storage_target", &pybake_provider_add_storage_target);
bpl::def("remove_storage_target", &pybake_provider_remove_storage_target);
bpl::def("remove_all_storage_targets", &pybake_provider_remove_all_storage_targets);
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
py11::module::import("_pybaketarget");
m.def("register", &pybake_provider_register);
m.def("add_storage_target", &pybake_provider_add_storage_target);
m.def("remove_storage_target", &pybake_provider_remove_storage_target);
m.def("remove_all_storage_targets", &pybake_provider_remove_all_storage_targets);
m.def("count_storage_targets", &pybake_provider_count_storage_targets);
m.def("list_storage_targets", &pybake_provider_list_storage_targets);
m.def("make_pool", &pybake_make_pool);
}
......@@ -3,14 +3,7 @@
*
* See COPYRIGHT in top-level directory.
*/
#define BOOST_NO_AUTO_PTR
#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 <pybind11/pybind11.h>
#include <string>
#include <vector>
#include <cstring>
......@@ -19,43 +12,45 @@
#include <bake.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];
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;
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);
if(ret == 0) return bpl::object(tid);
else return bpl::object();
if(ret == 0) return py11::cast(tid);
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));
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;
std::string region_str = (std::string)bregion_str;
memset(&result, 0, sizeof(result));
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));
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);
bpl::def("target_id_from_string", pybake_target_id_from_string);
bpl::class_<bake_region_id_t>("bake_region_id", bpl::no_init)
m.def("target_id_from_string", pybake_target_id_from_string);
py11::class_<bake_region_id_t>(m,"bake_region_id")
.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
os.environ['OPT'] = " ".join(
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
try:
import numpy
......@@ -23,23 +21,18 @@ except ImportError:
# For client...
pk = pkgconfig.parse('bake-client')
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_include_dirs = pk['include_dirs']
client_include_dirs.append(".")
# For server...
pk = pkgconfig.parse('bake-server')
server_libraries = pk['libraries']
server_libraries.append('boost_python'+python_version)
server_library_dirs = pk['library_dirs']
server_include_dirs = pk['include_dirs']
server_include_dirs.append(".")
# For target...
pk = pkgconfig.parse('uuid')
target_libraries = pk['libraries']
target_libraries.append('boost_python'+python_version)
target_library_dirs = pk['library_dirs']
target_include_dirs = pk['include_dirs']
target_include_dirs.append('.')
......
......@@ -8,7 +8,7 @@ from pybake.server import BakeProvider
mid = MargoInstance('tcp')
mid.enable_remote_shutdown()
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)
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