SchedInst.cpp 7.2 KB
Newer Older
J
jinhai 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The ASF licenses this file
// to you 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.

S
starlord 已提交
18
#include "scheduler/SchedInst.h"
W
wxyu 已提交
19
#include "ResourceFactory.h"
W
wxyu 已提交
20
#include "Utils.h"
S
starlord 已提交
21 22
#include "knowhere/index/vector_index/IndexGPUIVF.h"
#include "server/Config.h"
W
wxyu 已提交
23

S
starlord 已提交
24 25
#include <set>
#include <string>
S
starlord 已提交
26 27
#include <utility>
#include <vector>
28 29

namespace milvus {
W
wxyu 已提交
30
namespace scheduler {
31 32 33 34 35 36 37

ResourceMgrPtr ResMgrInst::instance = nullptr;
std::mutex ResMgrInst::mutex_;

SchedulerPtr SchedInst::instance = nullptr;
std::mutex SchedInst::mutex_;

W
wxyu 已提交
38 39 40
scheduler::JobMgrPtr JobMgrInst::instance = nullptr;
std::mutex JobMgrInst::mutex_;

W
wxyu 已提交
41
void
W
wxyu 已提交
42
load_simple_config() {
S
starlord 已提交
43
    server::Config& config = server::Config::GetInstance();
44 45 46 47 48
    std::string mode;
    config.GetResourceConfigMode(mode);
    std::vector<std::string> pool;
    config.GetResourceConfigPool(pool);

W
wxyu 已提交
49 50
    bool cpu = false;
    std::set<uint64_t> gpu_ids;
S
starlord 已提交
51
    for (auto& resource : pool) {
W
wxyu 已提交
52 53 54 55 56 57 58 59 60 61 62 63
        if (resource == "cpu") {
            cpu = true;
            break;
        } else {
            if (resource.length() < 4 || resource.substr(0, 3) != "gpu") {
                // error
                exit(-1);
            }
            auto gpu_id = std::stoi(resource.substr(3));
            if (gpu_id >= get_num_gpu()) {
                // error
                exit(-1);
Y
Yu Kun 已提交
64
            }
W
wxyu 已提交
65
            gpu_ids.insert(gpu_id);
X
xj.lin 已提交
66
        }
W
wxyu 已提交
67
    }
W
wxyu 已提交
68

W
wxyu 已提交
69 70 71 72 73 74 75 76 77 78
    ResMgrInst::GetInstance()->Add(ResourceFactory::Create("disk", "DISK", 0, true, false));
    auto io = Connection("io", 500);
    if (cpu) {
        ResMgrInst::GetInstance()->Add(ResourceFactory::Create("cpu", "CPU", 0, true, true));
        ResMgrInst::GetInstance()->Connect("disk", "cpu", io);
    } else {
        ResMgrInst::GetInstance()->Add(ResourceFactory::Create("cpu", "CPU", 0, true, false));
        ResMgrInst::GetInstance()->Connect("disk", "cpu", io);

        auto pcie = Connection("pcie", 12000);
S
starlord 已提交
79
        for (auto& gpu_id : gpu_ids) {
W
wxyu 已提交
80 81 82 83 84
            ResMgrInst::GetInstance()->Add(ResourceFactory::Create(std::to_string(gpu_id), "GPU", gpu_id, true, true));
            ResMgrInst::GetInstance()->Connect("cpu", std::to_string(gpu_id), io);
        }
    }
}
85

W
wxyu 已提交
86 87
void
load_advance_config() {
S
starlord 已提交
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 137 138 139 140 141 142 143 144 145 146 147 148
    //    try {
    //        server::ConfigNode &config = server::Config::GetInstance().GetConfig(server::CONFIG_RESOURCE);
    //
    //        if (config.GetChildren().empty()) throw "resource_config null exception";
    //
    //        auto resources = config.GetChild(server::CONFIG_RESOURCES).GetChildren();
    //
    //        if (resources.empty()) throw "Children of resource_config null exception";
    //
    //        for (auto &resource : resources) {
    //            auto &resname = resource.first;
    //            auto &resconf = resource.second;
    //            auto type = resconf.GetValue(server::CONFIG_RESOURCE_TYPE);
    ////        auto memory = resconf.GetInt64Value(server::CONFIG_RESOURCE_MEMORY);
    //            auto device_id = resconf.GetInt64Value(server::CONFIG_RESOURCE_DEVICE_ID);
    ////            auto enable_loader = resconf.GetBoolValue(server::CONFIG_RESOURCE_ENABLE_LOADER);
    //            auto enable_loader = true;
    //            auto enable_executor = resconf.GetBoolValue(server::CONFIG_RESOURCE_ENABLE_EXECUTOR);
    //            auto pinned_memory = resconf.GetInt64Value(server::CONFIG_RESOURCE_PIN_MEMORY);
    //            auto temp_memory = resconf.GetInt64Value(server::CONFIG_RESOURCE_TEMP_MEMORY);
    //            auto resource_num = resconf.GetInt64Value(server::CONFIG_RESOURCE_NUM);
    //
    //            auto res = ResMgrInst::GetInstance()->Add(ResourceFactory::Create(resname,
    //                                                                              type,
    //                                                                              device_id,
    //                                                                              enable_loader,
    //                                                                              enable_executor));
    //
    //            if (res.lock()->type() == ResourceType::GPU) {
    //                auto pinned_memory = resconf.GetInt64Value(server::CONFIG_RESOURCE_PIN_MEMORY, 300);
    //                auto temp_memory = resconf.GetInt64Value(server::CONFIG_RESOURCE_TEMP_MEMORY, 300);
    //                auto resource_num = resconf.GetInt64Value(server::CONFIG_RESOURCE_NUM, 2);
    //                pinned_memory = 1024 * 1024 * pinned_memory;
    //                temp_memory = 1024 * 1024 * temp_memory;
    //                knowhere::FaissGpuResourceMgr::GetInstance().InitDevice(device_id,
    //                                                                        pinned_memory,
    //                                                                        temp_memory,
    //                                                                        resource_num);
    //            }
    //        }
    //
    //        knowhere::FaissGpuResourceMgr::GetInstance().InitResource();
    //
    //        auto connections = config.GetChild(server::CONFIG_RESOURCE_CONNECTIONS).GetChildren();
    //        if (connections.empty()) throw "connections config null exception";
    //        for (auto &conn : connections) {
    //            auto &connect_name = conn.first;
    //            auto &connect_conf = conn.second;
    //            auto connect_speed = connect_conf.GetInt64Value(server::CONFIG_SPEED_CONNECTIONS);
    //            auto connect_endpoint = connect_conf.GetValue(server::CONFIG_ENDPOINT_CONNECTIONS);
    //
    //            std::string delimiter = "===";
    //            std::string left = connect_endpoint.substr(0, connect_endpoint.find(delimiter));
    //            std::string right = connect_endpoint.substr(connect_endpoint.find(delimiter) + 3,
    //                                                        connect_endpoint.length());
    //
    //            auto connection = Connection(connect_name, connect_speed);
    //            ResMgrInst::GetInstance()->Connect(left, right, connection);
    //        }
    //    } catch (const char *msg) {
    //        SERVER_LOG_ERROR << msg;
S
starlord 已提交
149
    //        // TODO(wxy): throw exception instead
S
starlord 已提交
150 151 152
    //        exit(-1);
    ////        throw std::exception();
    //    }
W
wxyu 已提交
153
}
W
wxyu 已提交
154

W
wxyu 已提交
155 156 157
void
StartSchedulerService() {
    load_simple_config();
S
starlord 已提交
158
    //    load_advance_config();
W
wxyu 已提交
159 160
    ResMgrInst::GetInstance()->Start();
    SchedInst::GetInstance()->Start();
W
wxyu 已提交
161
    JobMgrInst::GetInstance()->Start();
W
wxyu 已提交
162 163
}

S
starlord 已提交
164 165
void
StopSchedulerService() {
W
wxyu 已提交
166
    JobMgrInst::GetInstance()->Stop();
S
starlord 已提交
167
    SchedInst::GetInstance()->Stop();
W
wxyu 已提交
168
    ResMgrInst::GetInstance()->Stop();
S
starlord 已提交
169
}
S
starlord 已提交
170

S
starlord 已提交
171 172
}  // namespace scheduler
}  // namespace milvus