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
/***************************************************************************
 * 
 * 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) {
23
        LOG(ERROR) << "Failed register WeightedRandomRender";
W
sdk-cpp  
wangguibao 已提交
24 25 26 27 28 29 30 31 32 33
        return -1;
    }

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

    return 0;
}

int PredictorApi::create(const char* path, const char* file) {
    if (register_all() != 0) {
34
        LOG(ERROR)  << "Failed do register all!";
W
sdk-cpp  
wangguibao 已提交
35 36 37 38
        return -1;
    }

    if (_config_manager.create(path, file) != 0) {
39
        LOG(ERROR) << "Failed create config manager from conf:" 
W
sdk-cpp  
wangguibao 已提交
40 41 42 43 44 45 46 47 48 49 50
            << 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) {
51
            LOG(ERROR) << "Failed intialize endpoint:"
W
sdk-cpp  
wangguibao 已提交
52 53 54 55 56 57
                << ep_info.endpoint_name;
            return -1;
        }

        if (_endpoints.find(
                    ep_info.endpoint_name) != _endpoints.end()) {
58
            LOG(ERROR) << "Cannot insert duplicated endpoint:"
W
sdk-cpp  
wangguibao 已提交
59 60 61 62 63 64 65 66
                << 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) {
67
            LOG(ERROR) << "Failed insert endpoint:" 
W
sdk-cpp  
wangguibao 已提交
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
                << 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) {
84
            LOG(ERROR) << "Failed thrd initialize endpoint:"
W
sdk-cpp  
wangguibao 已提交
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
                << 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) {
100
            LOG(ERROR) << "Failed thrd clear endpoint:"
W
sdk-cpp  
wangguibao 已提交
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
                << 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) {
116
            LOG(ERROR) << "Failed thrd finalize endpoint:"
W
sdk-cpp  
wangguibao 已提交
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
                << 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: */