Utils.cpp 2.3 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/Utils.h"
W
wxyu 已提交
19
#include "server/Config.h"
W
wxyu 已提交
20
#include "utils/Log.h"
Y
Yu Kun 已提交
21

W
wxyu 已提交
22
#include <cuda_runtime.h>
S
starlord 已提交
23
#include <chrono>
W
wxyu 已提交
24 25
#include <set>
#include <string>
W
wxyu 已提交
26

Y
Yu Kun 已提交
27
namespace milvus {
W
wxyu 已提交
28
namespace scheduler {
Y
Yu Kun 已提交
29 30

uint64_t
W
wxyu 已提交
31
get_current_timestamp() {
32 33 34 35
    std::chrono::time_point<std::chrono::system_clock> now = std::chrono::system_clock::now();
    auto duration = now.time_since_epoch();
    auto millis = std::chrono::duration_cast<std::chrono::milliseconds>(duration).count();
    return millis;
Y
Yu Kun 已提交
36 37
}

W
wxyu 已提交
38 39 40 41 42 43 44
uint64_t
get_num_gpu() {
    int n_devices = 0;
    cudaGetDeviceCount(&n_devices);
    return n_devices;
}

W
wxyu 已提交
45 46 47 48 49 50
std::vector<uint64_t>
get_gpu_pool() {
    std::vector<uint64_t> gpu_pool;

    server::Config& config = server::Config::GetInstance();
    std::vector<std::string> pool;
51
    Status s = config.GetResourceConfigSearchResources(pool);
W
wxyu 已提交
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
    if (!s.ok()) {
        SERVER_LOG_ERROR << s.message();
    }

    std::set<uint64_t> gpu_ids;

    for (auto& resource : pool) {
        if (resource == "cpu") {
            continue;
        } else {
            if (resource.length() < 4 || resource.substr(0, 3) != "gpu") {
                // error
                exit(-1);
            }
            auto gpu_id = std::stoi(resource.substr(3));
            if (gpu_id >= scheduler::get_num_gpu()) {
                // error
                exit(-1);
            }
            gpu_ids.insert(gpu_id);
        }
    }

    for (auto& gpu_id : gpu_ids) {
        gpu_pool.push_back(gpu_id);
    }

    return gpu_pool;
W
wxyu 已提交
80
}
W
wxyu 已提交
81

S
starlord 已提交
82 83
}  // namespace scheduler
}  // namespace milvus