scheduler_test.cpp 8.1 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.

18
#include "scheduler/Scheduler.h"
W
wxyu 已提交
19
#include <gtest/gtest.h>
20
#include <src/scheduler/tasklabel/DefaultLabel.h>
21
#include <src/server/ServerConfig.h>
22 23 24 25 26 27
#include "cache/DataObj.h"
#include "cache/GpuCacheMgr.h"
#include "scheduler/task/TestTask.h"
#include "scheduler/ResourceFactory.h"
#include "scheduler/resource/Resource.h"
#include "utils/Error.h"
X
xiaojun.lin 已提交
28
#include "src/wrapper/vec_index.h"
29
#include "scheduler/tasklabel/SpecResLabel.h"
W
wxyu 已提交
30

31

W
wxyu 已提交
32 33 34 35
namespace zilliz {
namespace milvus {
namespace engine {

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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
class MockVecIndex : public engine::VecIndex {
public:
    virtual ErrorCode BuildAll(const long &nb,
                               const float *xb,
                               const long *ids,
                               const engine::Config &cfg,
                               const long &nt = 0,
                               const float *xt = nullptr) {

    }

    engine::VecIndexPtr Clone() override {
        return zilliz::milvus::engine::VecIndexPtr();
    }

    int64_t GetDeviceId() override {
        return 0;
    }

    engine::IndexType GetType() override {
        return engine::IndexType::INVALID;
    }

    virtual ErrorCode Add(const long &nb,
                          const float *xb,
                          const long *ids,
                          const engine::Config &cfg = engine::Config()) {

    }

    virtual ErrorCode Search(const long &nq,
                             const float *xq,
                             float *dist,
                             long *ids,
                             const engine::Config &cfg = engine::Config()) {

    }

    engine::VecIndexPtr CopyToGpu(const int64_t &device_id, const engine::Config &cfg) override {

    }

    engine::VecIndexPtr CopyToCpu(const engine::Config &cfg) override {

    }

    virtual int64_t Dimension() {
        return dimension_;
    }

    virtual int64_t Count() {
        return ntotal_;
    }

    virtual zilliz::knowhere::BinarySet Serialize() {
        zilliz::knowhere::BinarySet binset;
        return binset;
    }

    virtual ErrorCode Load(const zilliz::knowhere::BinarySet &index_binary) {

    }

public:
    int64_t dimension_ = 512;
    int64_t ntotal_ = 0;
};


class SchedulerTest : public testing::Test {
protected:
    void
    SetUp() override {
S
starlord 已提交
109 110 111 112
        constexpr int64_t cache_cap = 1024*1024*1024;
        cache::GpuCacheMgr::GetInstance(0)->SetCapacity(cache_cap);
        cache::GpuCacheMgr::GetInstance(1)->SetCapacity(cache_cap);

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 149 150 151 152 153 154 155 156
        ResourcePtr cpu = ResourceFactory::Create("cpu", "CPU", 0, true, false);
        ResourcePtr gpu_0 = ResourceFactory::Create("gpu0", "GPU", 0);
        ResourcePtr gpu_1 = ResourceFactory::Create("gpu1", "GPU", 1);

        res_mgr_ = std::make_shared<ResourceMgr>();
        cpu_resource_ = res_mgr_->Add(std::move(cpu));
        gpu_resource_0_ = res_mgr_->Add(std::move(gpu_0));
        gpu_resource_1_ = res_mgr_->Add(std::move(gpu_1));

        auto PCIE = Connection("IO", 11000.0);
        res_mgr_->Connect("cpu", "gpu0", PCIE);
        res_mgr_->Connect("cpu", "gpu1", PCIE);

        scheduler_ = std::make_shared<Scheduler>(res_mgr_);

        res_mgr_->Start();
        scheduler_->Start();
    }

    void
    TearDown() override {
        scheduler_->Stop();
        res_mgr_->Stop();
    }

    ResourceWPtr cpu_resource_;
    ResourceWPtr gpu_resource_0_;
    ResourceWPtr gpu_resource_1_;

    ResourceMgrPtr res_mgr_;
    std::shared_ptr<Scheduler> scheduler_;
};

void
insert_dummy_index_into_gpu_cache(uint64_t device_id) {
    MockVecIndex *mock_index = new MockVecIndex();
    mock_index->ntotal_ = 1000;
    engine::VecIndexPtr index(mock_index);

    cache::DataObjPtr obj = std::make_shared<cache::DataObj>(index);

    cache::GpuCacheMgr::GetInstance(device_id)->InsertItem("location", obj);
}

S
starlord 已提交
157
TEST_F(SchedulerTest, ON_LOAD_COMPLETED) {
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
    const uint64_t NUM = 10;
    std::vector<std::shared_ptr<TestTask>> tasks;
    TableFileSchemaPtr dummy = std::make_shared<meta::TableFileSchema>();
    dummy->location_ = "location";

    insert_dummy_index_into_gpu_cache(1);

    for (uint64_t i = 0; i < NUM; ++i) {
        auto task = std::make_shared<TestTask>(dummy);
        task->label() = std::make_shared<DefaultLabel>();
        tasks.push_back(task);
        cpu_resource_.lock()->task_table().Put(task);
    }

    sleep(3);
    ASSERT_EQ(res_mgr_->GetResource(ResourceType::GPU, 1)->task_table().Size(), NUM);

}

S
starlord 已提交
177
TEST_F(SchedulerTest, PUSH_TASK_TO_NEIGHBOUR_RANDOMLY_TEST) {
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
    const uint64_t NUM = 10;
    std::vector<std::shared_ptr<TestTask>> tasks;
    TableFileSchemaPtr dummy1 = std::make_shared<meta::TableFileSchema>();
    dummy1->location_ = "location";

    tasks.clear();

    for (uint64_t i = 0; i < NUM; ++i) {
        auto task = std::make_shared<TestTask>(dummy1);
        task->label() = std::make_shared<DefaultLabel>();
        tasks.push_back(task);
        cpu_resource_.lock()->task_table().Put(task);
    }

    sleep(3);
Y
Yu Kun 已提交
193
//    ASSERT_EQ(res_mgr_->GetResource(ResourceType::GPU, 1)->task_table().Size(), NUM);
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
}

class SchedulerTest2 : public testing::Test {
protected:
    void
    SetUp() override {
        ResourcePtr disk = ResourceFactory::Create("disk", "DISK", 0, true, false);
        ResourcePtr cpu0 = ResourceFactory::Create("cpu0", "CPU", 0, true, false);
        ResourcePtr cpu1 = ResourceFactory::Create("cpu1", "CPU", 1, true, false);
        ResourcePtr cpu2 = ResourceFactory::Create("cpu2", "CPU", 2, true, false);
        ResourcePtr gpu0 = ResourceFactory::Create("gpu0", "GPU", 0, true, true);
        ResourcePtr gpu1 = ResourceFactory::Create("gpu1", "GPU", 1, true, true);

        res_mgr_ = std::make_shared<ResourceMgr>();
        disk_ = res_mgr_->Add(std::move(disk));
        cpu_0_ = res_mgr_->Add(std::move(cpu0));
        cpu_1_ = res_mgr_->Add(std::move(cpu1));
        cpu_2_ = res_mgr_->Add(std::move(cpu2));
        gpu_0_ = res_mgr_->Add(std::move(gpu0));
        gpu_1_ = res_mgr_->Add(std::move(gpu1));
        auto IO = Connection("IO", 5.0);
        auto PCIE1 = Connection("PCIE", 11.0);
        auto PCIE2 = Connection("PCIE", 20.0);
        res_mgr_->Connect("disk", "cpu0", IO);
        res_mgr_->Connect("cpu0", "cpu1", IO);
        res_mgr_->Connect("cpu1", "cpu2", IO);
        res_mgr_->Connect("cpu0", "cpu2", IO);
        res_mgr_->Connect("cpu1", "gpu0", PCIE1);
        res_mgr_->Connect("cpu2", "gpu1", PCIE2);

        scheduler_ = std::make_shared<Scheduler>(res_mgr_);

        res_mgr_->Start();
        scheduler_->Start();
    }

    void
    TearDown() override {
        scheduler_->Stop();
        res_mgr_->Stop();
    }

    ResourceWPtr disk_;
    ResourceWPtr cpu_0_;
    ResourceWPtr cpu_1_;
    ResourceWPtr cpu_2_;
    ResourceWPtr gpu_0_;
    ResourceWPtr gpu_1_;
    ResourceMgrPtr res_mgr_;

    std::shared_ptr<Scheduler> scheduler_;
};


S
starlord 已提交
248
TEST_F(SchedulerTest2, SPECIFIED_RESOURCE_TEST) {
249 250 251 252 253 254 255 256 257 258 259 260 261 262
    const uint64_t NUM = 10;
    std::vector<std::shared_ptr<TestTask>> tasks;
    TableFileSchemaPtr dummy = std::make_shared<meta::TableFileSchema>();
    dummy->location_ = "location";

    for (uint64_t i = 0; i < NUM; ++i) {
        std::shared_ptr<TestTask> task = std::make_shared<TestTask>(dummy);
        task->label() = std::make_shared<SpecResLabel>(disk_);
        tasks.push_back(task);
        disk_.lock()->task_table().Put(task);
    }

//    ASSERT_EQ(res_mgr_->GetResource(ResourceType::GPU, 1)->task_table().Size(), NUM);
}
263

W
wxyu 已提交
264 265 266
}
}
}