predictor_sdk.cpp 4.8 KB
Newer Older
W
wangguibao 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

G
guru4elephant 已提交
15 16
#include "core/sdk-cpp/include/predictor_sdk.h"
#include "core/sdk-cpp/include/abtest.h"
W
sdk-cpp  
wangguibao 已提交
17 18 19 20 21 22

namespace baidu {
namespace paddle_serving {
namespace sdk_cpp {

int PredictorApi::register_all() {
W
wangguibao 已提交
23 24 25 26
  if (WeightedRandomRender::register_self() != 0) {
    LOG(ERROR) << "Failed register WeightedRandomRender";
    return -1;
  }
W
sdk-cpp  
wangguibao 已提交
27

G
guru4elephant 已提交
28
  VLOG(2) << "Succ register all components!";
W
sdk-cpp  
wangguibao 已提交
29

W
wangguibao 已提交
30
  return 0;
W
sdk-cpp  
wangguibao 已提交
31 32
}

B
barrierye 已提交
33
int PredictorApi::create(const std::string& api_desc_str) {
G
guru4elephant 已提交
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
  VLOG(2) << api_desc_str;
  if (register_all() != 0) {
    LOG(ERROR) << "Failed do register all!";
    return -1;
  }

  if (_config_manager.create(api_desc_str) != 0) {
    LOG(ERROR) << "Failed create config manager from desc string :"
               << api_desc_str;
    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(ERROR) << "Failed intialize endpoint:" << ep_info.endpoint_name;
      return -1;
    }

    if (_endpoints.find(ep_info.endpoint_name) != _endpoints.end()) {
      LOG(ERROR) << "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(ERROR) << "Failed insert endpoint:" << ep_info.endpoint_name;
      return -1;
    }

G
guru4elephant 已提交
69 70
    VLOG(2) << "Succ create endpoint instance with name: "
            << ep_info.endpoint_name;
G
guru4elephant 已提交
71 72 73 74 75
  }

  return 0;
}

W
sdk-cpp  
wangguibao 已提交
76
int PredictorApi::create(const char* path, const char* file) {
W
wangguibao 已提交
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
  if (register_all() != 0) {
    LOG(ERROR) << "Failed do register all!";
    return -1;
  }

  if (_config_manager.create(path, file) != 0) {
    LOG(ERROR) << "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(ERROR) << "Failed intialize endpoint:" << ep_info.endpoint_name;
      return -1;
W
sdk-cpp  
wangguibao 已提交
96 97
    }

W
wangguibao 已提交
98 99 100 101
    if (_endpoints.find(ep_info.endpoint_name) != _endpoints.end()) {
      LOG(ERROR) << "Cannot insert duplicated endpoint:"
                 << ep_info.endpoint_name;
      return -1;
W
sdk-cpp  
wangguibao 已提交
102 103
    }

G
guru4elephant 已提交
104
    VLOG(2) << "endpoint name: " << ep_info.endpoint_name;
105

W
wangguibao 已提交
106 107 108 109 110
    std::pair<std::map<std::string, Endpoint*>::iterator, bool> r =
        _endpoints.insert(std::make_pair(ep_info.endpoint_name, ep));
    if (!r.second) {
      LOG(ERROR) << "Failed insert endpoint:" << ep_info.endpoint_name;
      return -1;
W
sdk-cpp  
wangguibao 已提交
111 112
    }

G
guru4elephant 已提交
113 114
    VLOG(2) << "Succ create endpoint instance with name: "
            << ep_info.endpoint_name;
W
wangguibao 已提交
115 116 117
  }

  return 0;
W
sdk-cpp  
wangguibao 已提交
118 119 120
}

int PredictorApi::thrd_initialize() {
W
wangguibao 已提交
121 122 123 124 125 126
  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(ERROR) << "Failed thrd initialize endpoint:" << it->first;
      return -1;
W
sdk-cpp  
wangguibao 已提交
127
    }
W
wangguibao 已提交
128

G
guru4elephant 已提交
129
    VLOG(2) << "Succ thrd initialize endpoint:" << it->first;
W
wangguibao 已提交
130 131
  }
  return 0;
W
sdk-cpp  
wangguibao 已提交
132 133 134
}

int PredictorApi::thrd_clear() {
W
wangguibao 已提交
135 136 137 138 139 140
  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(ERROR) << "Failed thrd clear endpoint:" << it->first;
      return -1;
W
sdk-cpp  
wangguibao 已提交
141
    }
W
wangguibao 已提交
142 143
  }
  return 0;
W
sdk-cpp  
wangguibao 已提交
144 145 146
}

int PredictorApi::thrd_finalize() {
W
wangguibao 已提交
147 148 149 150 151 152
  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(ERROR) << "Failed thrd finalize endpoint:" << it->first;
      return -1;
W
sdk-cpp  
wangguibao 已提交
153 154
    }

G
guru4elephant 已提交
155
    VLOG(2) << "Succ thrd finalize endpoint:" << it->first;
W
wangguibao 已提交
156 157
  }
  return 0;
W
sdk-cpp  
wangguibao 已提交
158 159
}

W
wangguibao 已提交
160
void PredictorApi::destroy() { return; }
W
sdk-cpp  
wangguibao 已提交
161

W
wangguibao 已提交
162 163 164
}  // namespace sdk_cpp
}  // namespace paddle_serving
}  // namespace baidu