client.cpp 4.89 KB
Newer Older
Matthieu Dorier's avatar
Matthieu Dorier committed
1 2 3 4 5
/*
 * (C) 2018 The University of Chicago
 * 
 * See COPYRIGHT in top-level directory.
 */
Matthieu Dorier's avatar
Matthieu Dorier committed
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
#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 <string>
#include <vector>
#include <cstring>
#include <iostream>
#include <margo.h>
#include <bake.h>
#include <bake-client.h>

BOOST_PYTHON_OPAQUE_SPECIALIZED_TYPE_ID(margo_instance)
BOOST_PYTHON_OPAQUE_SPECIALIZED_TYPE_ID(bake_provider_handle)
BOOST_PYTHON_OPAQUE_SPECIALIZED_TYPE_ID(bake_client)
BOOST_PYTHON_OPAQUE_SPECIALIZED_TYPE_ID(hg_addr)

namespace bpl = boost::python;

static bake_client_t pybake_client_init(margo_instance_id mid) {
    bake_client_t result = BAKE_CLIENT_NULL;
    bake_client_init(mid, &result);
    return result;
}

static bake_provider_handle_t pybake_provider_handle_create(
        bake_client_t client,
        hg_addr_t addr,
38
        uint8_t provider_id) {
Matthieu Dorier's avatar
Matthieu Dorier committed
39 40

    bake_provider_handle_t providerHandle = BAKE_PROVIDER_HANDLE_NULL;
41
    bake_provider_handle_create(client, addr, provider_id, &providerHandle);
Matthieu Dorier's avatar
Matthieu Dorier committed
42 43 44
    return providerHandle;
}

45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
static uint64_t pybake_get_eager_limit(
        bake_provider_handle_t ph)
{
    uint64_t limit;
    int ret = bake_provider_handle_get_eager_limit(ph, &limit);
    if(ret != 0) return 0;
    return limit;
}

static bpl::object pybake_probe(
        bake_provider_handle_t ph,
        uint64_t max_targets)
{
    bpl::list result;
    std::vector<bake_target_id_t> targets(max_targets);
    uint64_t num_targets;
    int ret = bake_probe(ph, max_targets, targets.data(), &num_targets);
    if(ret != 0) return bpl::object();
    for(uint64_t i=0; i < num_targets; i++) {
        result.append(bpl::object(targets[i]));
    }
    return result;
}

static bpl::object pybake_create(
        bake_provider_handle_t ph,
        bake_target_id_t bti,
        size_t region_size)
{
    bake_region_id_t rid;
    std::memset(&rid, 0, sizeof(rid));
    int ret = bake_create(ph, bti, region_size, &rid);
    if(ret != 0) return bpl::object();
    else return bpl::object(rid);
}

static bpl::object pybake_write(
        bake_provider_handle_t ph,
        const bake_region_id_t& rid,
        uint64_t offset,
        const std::string& data)
{
    int ret = bake_write(ph, rid, offset, (const void*)data.data(), data.size());
    if(ret == 0) return bpl::object(true);
    else return bpl::object(false);
}

static bpl::object pybake_persist(
        bake_provider_handle_t ph,
        const bake_region_id_t& rid)
{
    int ret = bake_persist(ph, rid);
    if(ret == 0) return bpl::object(true);
    else return bpl::object(false);
}

static bpl::object pybake_create_write_persist(
        bake_provider_handle_t ph,
        bake_target_id_t tid,
        const std::string& data)
{
    bake_region_id_t rid;
    int ret = bake_create_write_persist(ph, tid, 
            data.data(), data.size(), &rid);
    if(ret == 0) return bpl::object(rid);
    else return bpl::object();
}

static bpl::object pybake_get_size(
        bake_provider_handle_t ph,
        const bake_region_id_t& rid)
{
    uint64_t size;
    int ret = bake_get_size(ph, rid, &size);
    if(ret == 0) return bpl::object(size);
    else return bpl::object();
}

static bpl::object pybake_read(
        bake_provider_handle_t ph,
        const bake_region_id_t& rid,
        uint64_t offset,
        size_t size) 
{
    std::string result(size, '\0');
    uint64_t bytes_read;
    int ret = bake_read(ph, rid, offset, (void*)result.data(), size, &bytes_read);
    if(ret != 0) return bpl::object();
    result.resize(bytes_read);
    return bpl::object(result);
}

Matthieu Dorier's avatar
Matthieu Dorier committed
137 138 139 140
BOOST_PYTHON_MODULE(_pybakeclient)
{
#define ret_policy_opaque bpl::return_value_policy<bpl::return_opaque_pointer>()

141
    bpl::import("_pybaketarget");
Matthieu Dorier's avatar
Matthieu Dorier committed
142 143
    bpl::opaque<bake_client>();
    bpl::opaque<bake_provider_handle>();
144
//    bpl::class_<bake_region_id_t>("bake_region_id", bpl::no_init);
Matthieu Dorier's avatar
Matthieu Dorier committed
145 146 147 148 149
    bpl::def("client_init", &pybake_client_init, ret_policy_opaque);
    bpl::def("client_finalize", &bake_client_finalize);
    bpl::def("provider_handle_create", &pybake_provider_handle_create, ret_policy_opaque);
    bpl::def("provider_handle_ref_incr", &bake_provider_handle_ref_incr);
    bpl::def("provider_handle_release", &bake_provider_handle_release);
150 151 152 153 154 155 156 157 158 159
    bpl::def("get_eager_limit", &pybake_get_eager_limit);
    bpl::def("set_eager_limit", &bake_provider_handle_set_eager_limit);
    bpl::def("probe", &pybake_probe);
    bpl::def("create", &pybake_create);
    bpl::def("write", &pybake_write);
    bpl::def("persist", &pybake_persist);
    bpl::def("create_write_persist", &pybake_create_write_persist);
    bpl::def("get_size", &pybake_get_size);
    bpl::def("read", &pybake_read);
    bpl::def("remove", &bake_remove);
Matthieu Dorier's avatar
Matthieu Dorier committed
160 161 162 163
    bpl::def("shutdown_service", &bake_shutdown_service);

#undef ret_policy_opaque
}