test_algorithm.cpp 6.0 KB
Newer Older
1
// Copyright (C) 2019-2020 Zilliz. All rights reserved.
J
jinhai 已提交
2
//
3 4
// 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
J
jinhai 已提交
5
//
6 7 8 9 10
// 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.
J
jinhai 已提交
11

12
#include <gtest/gtest.h>
13 14
#include <iostream>
#include <ostream>
15

Y
youny626 已提交
16 17
#include "scheduler/Algorithm.h"
#include "scheduler/ResourceFactory.h"
18 19
#include "scheduler/ResourceMgr.h"
#include "scheduler/resource/CpuResource.h"
Y
youny626 已提交
20
#include "scheduler/resource/Resource.h"
S
starlord 已提交
21

22
namespace milvus {
W
wxyu 已提交
23
namespace scheduler {
24 25 26 27 28

class AlgorithmTest : public testing::Test {
 protected:
    void
    SetUp() override {
W
Wang XiangYu 已提交
29 30
        ResourcePtr disk = ResourceFactory::Create("disk", "DISK", 0, false);
        ResourcePtr cpu0 = ResourceFactory::Create("cpu0", "CPU", 0);
31 32 33 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
        ResourcePtr cpu1 = ResourceFactory::Create("cpu1", "CPU", 1);
        ResourcePtr cpu2 = ResourceFactory::Create("cpu2", "CPU", 2);
        ResourcePtr gpu0 = ResourceFactory::Create("gpu0", "GPU", 0);
        ResourcePtr gpu1 = ResourceFactory::Create("gpu1", "GPU", 1);

        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 PCIE = Connection("PCIE", 11.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", PCIE);
        res_mgr_->Connect("cpu2", "gpu1", PCIE);
    }

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

62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
TEST_F(AlgorithmTest, SHORTESTPATH_INVALID_PATH_TEST) {
    std::vector<std::string> sp;
    uint64_t cost;
    // disk to disk is invalid
    cost = ShortestPath(disk_.lock(), disk_.lock(), res_mgr_, sp);
    ASSERT_TRUE(sp.empty());

    // cpu_0 to disk is invalid
    cost = ShortestPath(cpu_0_.lock(), disk_.lock(), res_mgr_, sp);
    ASSERT_TRUE(sp.empty());

    // cpu2 to gpu0 is invalid
    cost = ShortestPath(cpu_2_.lock(), gpu_0_.lock(), res_mgr_, sp);
    ASSERT_TRUE(sp.empty());


    // gpu0 to gpu1 is invalid
    cost = ShortestPath(gpu_0_.lock(), gpu_1_.lock(), res_mgr_, sp);
    ASSERT_TRUE(sp.empty());
}

S
starlord 已提交
83
TEST_F(AlgorithmTest, SHORTESTPATH_TEST) {
84 85
    std::vector<std::string> sp;
    uint64_t cost;
86 87 88 89

    //disk to gpu0
    //disk -> cpu0 -> cpu1 -> gpu0
    std::cout << "************************************\n";
90
    cost = ShortestPath(disk_.lock(), gpu_0_.lock(), res_mgr_, sp);
91 92 93 94 95 96 97 98 99 100 101 102
    ASSERT_EQ(sp.size(), 4);
    while (!sp.empty()) {
        std::cout << sp[sp.size() - 1] << " ";
        sp.pop_back();
    }
    std::cout << std::endl;

    //disk to gpu1
    //disk -> cpu0 -> cpu2 -> gpu1
    std::cout << "************************************\n";
    cost = ShortestPath(disk_.lock(), gpu_1_.lock(), res_mgr_, sp);
    ASSERT_EQ(sp.size(), 4);
103
    while (!sp.empty()) {
104
        std::cout << sp[sp.size() - 1] << " ";
105 106
        sp.pop_back();
    }
107
    std::cout << std::endl;
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
    // disk to cpu0
    // disk -> cpu0
    std::cout << "************************************\n";
    cost = ShortestPath(disk_.lock(), cpu_0_.lock(), res_mgr_, sp);
    ASSERT_EQ(sp.size(), 2);
    while (!sp.empty()) {
        std::cout << sp[sp.size() - 1] << " ";
        sp.pop_back();
    }
    std::cout << std::endl;

    // disk to cpu1
    // disk -> cpu0 -> cpu1
    std::cout << "************************************\n";
    cost = ShortestPath(disk_.lock(), cpu_1_.lock(), res_mgr_, sp);
    ASSERT_EQ(sp.size(), 3);
    while (!sp.empty()) {
        std::cout << sp[sp.size() - 1] << " ";
        sp.pop_back();
    }
    std::cout << std::endl;

    // disk to cpu2
    // disk -> cpu0 -> cpu2
    std::cout << "************************************\n";
    cost = ShortestPath(disk_.lock(), cpu_2_.lock(), res_mgr_, sp);
    ASSERT_EQ(sp.size(), 3);
    while (!sp.empty()) {
        std::cout << sp[sp.size() - 1] << " ";
        sp.pop_back();
    }
    std::cout << std::endl;

    // cpu0 to gpu0
    // cpu0 -> cpu1 -> gpu0
144 145
    std::cout << "************************************\n";
    cost = ShortestPath(cpu_0_.lock(), gpu_0_.lock(), res_mgr_, sp);
146
    ASSERT_EQ(sp.size(), 3);
147
    while (!sp.empty()) {
148
        std::cout << sp[sp.size() - 1] << " ";
149 150
        sp.pop_back();
    }
151
    std::cout << std::endl;
152

153 154
    // cpu0 to cpu1
    // cpu0 -> cpu1
155
    std::cout << "************************************\n";
156 157
    cost = ShortestPath(cpu_0_.lock(), cpu_1_.lock(), res_mgr_, sp);
    ASSERT_EQ(sp.size(), 2);
158
    while (!sp.empty()) {
159
        std::cout << sp[sp.size() - 1] << " ";
160 161
        sp.pop_back();
    }
162
    std::cout << std::endl;
163

164 165
    // cpu0 to cpu2
    // cpu0 -> cpu2
166
    std::cout << "************************************\n";
167 168
    cost = ShortestPath(cpu_0_.lock(), cpu_2_.lock(), res_mgr_, sp);
    // ASSERT_EQ(sp.size(), 2);
169
    while (!sp.empty()) {
170
        std::cout << sp[sp.size() - 1] << " ";
171 172
        sp.pop_back();
    }
173
    std::cout << std::endl;
174

175 176
    // cpu0 to gpu1
    // cpu0 -> cpu2 -> gpu1
177
    std::cout << "************************************\n";
178 179
    cost = ShortestPath(cpu_0_.lock(), gpu_1_.lock(), res_mgr_, sp);
    // ASSERT_EQ(sp.size(), 3);
180
    while (!sp.empty()) {
181
        std::cout << sp[sp.size() - 1] << " ";
182 183
        sp.pop_back();
    }
184
    std::cout << std::endl;
185 186
}

Y
youny626 已提交
187 188
}  // namespace scheduler
}  // namespace milvus