predictor_sdk.cpp 3.5 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

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
    }

61 62
    LOG(INFO) << "endpoint name: " << ep_info.endpoint_name;

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

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

  return 0;
W
sdk-cpp  
wangguibao 已提交
75 76 77
}

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

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

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

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

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

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

W
wangguibao 已提交
119 120 121
}  // namespace sdk_cpp
}  // namespace paddle_serving
}  // namespace baidu