predictor_sdk.cpp 3.5 KB
Newer Older
W
wangguibao 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// 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.

#include "sdk-cpp/include/predictor_sdk.h"
#include "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

W
wangguibao 已提交
28
  LOG(WARNING) << "Succ register all components!";
W
sdk-cpp  
wangguibao 已提交
29

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

int PredictorApi::create(const char* path, const char* file) {
W
wangguibao 已提交
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
  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 已提交
53 54
    }

W
wangguibao 已提交
55 56 57 58
    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 已提交
59 60
    }

W
wangguibao 已提交
61 62 63 64 65
    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 已提交
66 67
    }

W
wangguibao 已提交
68 69 70 71 72
    LOG(INFO) << "Succ create endpoint instance with name: "
              << ep_info.endpoint_name;
  }

  return 0;
W
sdk-cpp  
wangguibao 已提交
73 74 75
}

int PredictorApi::thrd_initialize() {
W
wangguibao 已提交
76 77 78 79 80 81
  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 已提交
82
    }
W
wangguibao 已提交
83 84 85 86

    LOG(WARNING) << "Succ thrd initialize endpoint:" << it->first;
  }
  return 0;
W
sdk-cpp  
wangguibao 已提交
87 88 89
}

int PredictorApi::thrd_clear() {
W
wangguibao 已提交
90 91 92 93 94 95
  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 已提交
96
    }
W
wangguibao 已提交
97 98
  }
  return 0;
W
sdk-cpp  
wangguibao 已提交
99 100 101
}

int PredictorApi::thrd_finalize() {
W
wangguibao 已提交
102 103 104 105 106 107
  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 已提交
108 109
    }

W
wangguibao 已提交
110 111 112
    LOG(INFO) << "Succ thrd finalize endpoint:" << it->first;
  }
  return 0;
W
sdk-cpp  
wangguibao 已提交
113 114
}

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

W
wangguibao 已提交
117 118 119
}  // namespace sdk_cpp
}  // namespace paddle_serving
}  // namespace baidu