predictor_sdk.cpp 3.7 KB
Newer Older
W
sdk-cpp  
wangguibao 已提交
1 2 3 4 5 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 38 39 40 41 42 43 44 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
/***************************************************************************
 * 
 * Copyright (c) 2018 Baidu.com, Inc. All Rights Reserved
 * 
 **************************************************************************/
 
/**
 * @file src/predictor_api.cpp
 * @author wanlijin01(wanlijin01@baidu.com)
 * @date 2018/07/09 17:36:13
 * @brief 
 *  
 **/
#include "abtest.h"
#include "predictor_sdk.h"

namespace baidu {
namespace paddle_serving {
namespace sdk_cpp {

int PredictorApi::register_all() {
    if (WeightedRandomRender::register_self() != 0) {
        LOG(FATAL) << "Failed register WeightedRandomRender";
        return -1;
    }

    LOG(WARNING) << "Succ register all components!";

    return 0;
}

int PredictorApi::create(const char* path, const char* file) {
    if (register_all() != 0) {
        LOG(FATAL)  << "Failed do register all!";
        return -1;
    }

    if (_config_manager.create(path, file) != 0) {
        LOG(FATAL) << "Failed create config manager from conf:" 
            << path << "/" << file;
        return -1;
    }

    const std::map<std::string, EndpointInfo>& map 
        = _config_manager.config();
    std::map<std::string, EndpointInfo>::const_iterator it;
    for (it = map.begin(); it != map.end(); ++it) {
        const EndpointInfo& ep_info = it->second;
        Endpoint* ep = new (std::nothrow) Endpoint();
        if (ep->initialize(ep_info) != 0) {
            LOG(FATAL) << "Failed intialize endpoint:"
                << ep_info.endpoint_name;
            return -1;
        }

        if (_endpoints.find(
                    ep_info.endpoint_name) != _endpoints.end()) {
            LOG(FATAL) << "Cannot insert duplicated endpoint:"
                << ep_info.endpoint_name;
            return -1;
        }

        std::pair<std::map<std::string, Endpoint*>::iterator, bool> r
            = _endpoints.insert(std::make_pair(
                        ep_info.endpoint_name, ep));
        if (!r.second) {
            LOG(FATAL) << "Failed insert endpoint:" 
                << ep_info.endpoint_name;
            return -1;
        }

        LOG(INFO) << "Succ create endpoint instance with name: "
            << ep_info.endpoint_name;
    }

    return 0;
}

int PredictorApi::thrd_initialize() {
    std::map<std::string, Endpoint*>::const_iterator it;
    for (it = _endpoints.begin(); it != _endpoints.end(); ++it) {
        Endpoint* ep = it->second;
        if (ep->thrd_initialize() != 0) {
            LOG(FATAL) << "Failed thrd initialize endpoint:"
                << it->first;
            return -1;
        }

        LOG(WARNING) << "Succ thrd initialize endpoint:"
            << it->first;
    }
    return 0;
}

int PredictorApi::thrd_clear() {
    std::map<std::string, Endpoint*>::const_iterator it;
    for (it = _endpoints.begin(); it != _endpoints.end(); ++it) {
        Endpoint* ep = it->second;
        if (ep->thrd_clear() != 0) {
            LOG(FATAL) << "Failed thrd clear endpoint:"
                << it->first;
            return -1;
        }

        LOG(INFO) << "Succ thrd clear endpoint:"
            << it->first;
    }
    return 0;
}

int PredictorApi::thrd_finalize() {
    std::map<std::string, Endpoint*>::const_iterator it;
    for (it = _endpoints.begin(); it != _endpoints.end(); ++it) {
        Endpoint* ep = it->second;
        if (ep->thrd_finalize() != 0) {
            LOG(FATAL) << "Failed thrd finalize endpoint:"
                << it->first;
            return -1;
        }

        LOG(INFO) << "Succ thrd finalize endpoint:"
            << it->first;
    }
    return 0;
}

void PredictorApi::destroy() {
    // TODO
    return ;
}

} // sdk_cpp
} // paddle_serving
} // baidu

/* vim: set expandtab ts=4 sw=4 sts=4 tw=100: */