ExecutionEngineImpl.cpp 36.4 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

S
starlord 已提交
12
#include "db/engine/ExecutionEngineImpl.h"
T
Tinkerrr 已提交
13

14
#include <faiss/utils/ConcurrentBitset.h>
S
shengjh 已提交
15
#include <fiu-local.h>
16

T
Tinkerrr 已提交
17 18 19 20
#include <stdexcept>
#include <utility>
#include <vector>

S
starlord 已提交
21
#include "cache/CpuCacheMgr.h"
S
starlord 已提交
22
#include "cache/GpuCacheMgr.h"
23
#include "db/Utils.h"
X
xiaojun.lin 已提交
24
#include "knowhere/common/Config.h"
S
starlord 已提交
25
#include "metrics/Metrics.h"
X
xiaojun.lin 已提交
26 27
#include "scheduler/Utils.h"
#include "server/Config.h"
J
jinhai 已提交
28
#include "utils/CommonUtil.h"
S
starlord 已提交
29
#include "utils/Exception.h"
S
starlord 已提交
30
#include "utils/Log.h"
31
#include "utils/TimeRecorder.h"
G
groot 已提交
32 33
#include "utils/ValidationUtil.h"
#include "wrapper/BinVecImpl.h"
S
starlord 已提交
34 35
#include "wrapper/ConfAdapter.h"
#include "wrapper/ConfAdapterMgr.h"
S
starlord 已提交
36 37
#include "wrapper/VecImpl.h"
#include "wrapper/VecIndex.h"
X
xj.lin 已提交
38

J
JinHai-CN 已提交
39
//#define ON_SEARCH
S
starlord 已提交
40 41 42
namespace milvus {
namespace engine {

G
groot 已提交
43 44 45
namespace {

Status
46
MappingMetricType(MetricType metric_type, milvus::json& conf) {
G
groot 已提交
47 48
    switch (metric_type) {
        case MetricType::IP:
49
            conf[knowhere::Metric::TYPE] = knowhere::Metric::IP;
G
groot 已提交
50 51
            break;
        case MetricType::L2:
52
            conf[knowhere::Metric::TYPE] = knowhere::Metric::L2;
G
groot 已提交
53 54
            break;
        case MetricType::HAMMING:
55
            conf[knowhere::Metric::TYPE] = knowhere::Metric::HAMMING;
G
groot 已提交
56 57
            break;
        case MetricType::JACCARD:
58
            conf[knowhere::Metric::TYPE] = knowhere::Metric::JACCARD;
G
groot 已提交
59 60
            break;
        case MetricType::TANIMOTO:
61
            conf[knowhere::Metric::TYPE] = knowhere::Metric::TANIMOTO;
G
groot 已提交
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
            break;
        default:
            return Status(DB_ERROR, "Unsupported metric type");
    }

    return Status::OK();
}

bool
IsBinaryIndexType(IndexType type) {
    return type == IndexType::FAISS_BIN_IDMAP || type == IndexType::FAISS_BIN_IVFLAT_CPU;
}

}  // namespace

W
wxyu 已提交
77 78
class CachedQuantizer : public cache::DataObj {
 public:
W
wxyu 已提交
79 80
    explicit CachedQuantizer(knowhere::QuantizerPtr data) : data_(std::move(data)) {
    }
W
wxyu 已提交
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95

    knowhere::QuantizerPtr
    Data() {
        return data_;
    }

    int64_t
    Size() override {
        return data_->size;
    }

 private:
    knowhere::QuantizerPtr data_;
};

S
starlord 已提交
96
ExecutionEngineImpl::ExecutionEngineImpl(uint16_t dimension, const std::string& location, EngineType index_type,
97 98 99 100 101 102
                                         MetricType metric_type, const milvus::json& index_params)
    : location_(location),
      dim_(dimension),
      index_type_(index_type),
      metric_type_(metric_type),
      index_params_(index_params) {
103 104
    EngineType tmp_index_type =
        utils::IsBinaryMetricType((int32_t)metric_type) ? EngineType::FAISS_BIN_IDMAP : EngineType::FAISS_IDMAP;
G
groot 已提交
105
    index_ = CreatetVecIndex(tmp_index_type);
106
    if (!index_) {
107
        throw Exception(DB_ERROR, "Unsupported index type");
108
    }
X
xj.lin 已提交
109

110 111 112 113 114
    milvus::json conf = index_params;
    conf[knowhere::meta::DEVICEID] = gpu_num_;
    conf[knowhere::meta::DIM] = dimension;
    MappingMetricType(metric_type, conf);
    ENGINE_LOG_DEBUG << "Index params: " << conf.dump();
X
xiaojun.lin 已提交
115
    auto adapter = AdapterMgr::GetInstance().GetAdapter(index_->GetType());
116 117 118
    if (!adapter->CheckTrain(conf)) {
        throw Exception(DB_ERROR, "Illegal index params");
    }
X
xiaojun.lin 已提交
119

G
groot 已提交
120 121 122 123 124 125
    ErrorCode ec = KNOWHERE_UNEXPECTED_ERROR;
    if (auto bf_index = std::dynamic_pointer_cast<BFIndex>(index_)) {
        ec = bf_index->Build(conf);
    } else if (auto bf_bin_index = std::dynamic_pointer_cast<BinBFIndex>(index_)) {
        ec = bf_bin_index->Build(conf);
    }
126 127 128
    if (ec != KNOWHERE_SUCCESS) {
        throw Exception(DB_ERROR, "Build index error");
    }
S
starlord 已提交
129 130
}

S
starlord 已提交
131
ExecutionEngineImpl::ExecutionEngineImpl(VecIndexPtr index, const std::string& location, EngineType index_type,
132 133 134 135 136 137
                                         MetricType metric_type, const milvus::json& index_params)
    : index_(std::move(index)),
      location_(location),
      index_type_(index_type),
      metric_type_(metric_type),
      index_params_(index_params) {
X
xj.lin 已提交
138
}
S
starlord 已提交
139

S
starlord 已提交
140 141
VecIndexPtr
ExecutionEngineImpl::CreatetVecIndex(EngineType type) {
Y
yudong.cai 已提交
142
#ifdef MILVUS_GPU_VERSION
143 144 145
    server::Config& config = server::Config::GetInstance();
    bool gpu_resource_enable = true;
    config.GetGpuResourceConfigEnable(gpu_resource_enable);
S
shengjh 已提交
146
    fiu_do_on("ExecutionEngineImpl.CreatetVecIndex.gpu_res_disabled", gpu_resource_enable = false);
Y
yudong.cai 已提交
147
#endif
S
shengjh 已提交
148 149

    fiu_do_on("ExecutionEngineImpl.CreatetVecIndex.invalid_type", type = EngineType::INVALID);
X
xj.lin 已提交
150 151 152 153
    std::shared_ptr<VecIndex> index;
    switch (type) {
        case EngineType::FAISS_IDMAP: {
            index = GetVecIndexFactory(IndexType::FAISS_IDMAP);
S
starlord 已提交
154 155
            break;
        }
J
jinhai 已提交
156
        case EngineType::FAISS_IVFFLAT: {
Y
yudong.cai 已提交
157
#ifdef MILVUS_GPU_VERSION
158 159 160
            if (gpu_resource_enable)
                index = GetVecIndexFactory(IndexType::FAISS_IVFFLAT_MIX);
            else
Y
youny626 已提交
161
#endif
162
                index = GetVecIndexFactory(IndexType::FAISS_IVFFLAT_CPU);
S
starlord 已提交
163 164
            break;
        }
J
jinhai 已提交
165
        case EngineType::FAISS_IVFSQ8: {
Y
yudong.cai 已提交
166
#ifdef MILVUS_GPU_VERSION
167 168 169
            if (gpu_resource_enable)
                index = GetVecIndexFactory(IndexType::FAISS_IVFSQ8_MIX);
            else
Y
youny626 已提交
170
#endif
171
                index = GetVecIndexFactory(IndexType::FAISS_IVFSQ8_CPU);
S
starlord 已提交
172 173
            break;
        }
X
xj.lin 已提交
174 175 176 177
        case EngineType::NSG_MIX: {
            index = GetVecIndexFactory(IndexType::NSG_MIX);
            break;
        }
Y
Yukikaze-CZR 已提交
178
#ifdef CUSTOMIZATION
179
#ifdef MILVUS_GPU_VERSION
W
wxyu 已提交
180
        case EngineType::FAISS_IVFSQ8H: {
181 182 183 184 185
            if (gpu_resource_enable) {
                index = GetVecIndexFactory(IndexType::FAISS_IVFSQ8_HYBRID);
            } else {
                throw Exception(DB_ERROR, "No GPU resources for IVFSQ8H");
            }
W
wxyu 已提交
186 187
            break;
        }
188
#endif
Y
Yukikaze-CZR 已提交
189
#endif
Z
zirui.chen 已提交
190
        case EngineType::FAISS_PQ: {
Y
yudong.cai 已提交
191
#ifdef MILVUS_GPU_VERSION
192 193 194
            if (gpu_resource_enable)
                index = GetVecIndexFactory(IndexType::FAISS_IVFPQ_MIX);
            else
Z
zirui.chen 已提交
195
#endif
196
                index = GetVecIndexFactory(IndexType::FAISS_IVFPQ_CPU);
Z
zirui.chen 已提交
197 198
            break;
        }
199 200 201 202 203 204 205 206
        case EngineType::SPTAG_KDT: {
            index = GetVecIndexFactory(IndexType::SPTAG_KDT_RNT_CPU);
            break;
        }
        case EngineType::SPTAG_BKT: {
            index = GetVecIndexFactory(IndexType::SPTAG_BKT_RNT_CPU);
            break;
        }
T
Tinkerrr 已提交
207 208 209 210
        case EngineType::HNSW: {
            index = GetVecIndexFactory(IndexType::HNSW);
            break;
        }
G
groot 已提交
211 212 213 214 215 216 217 218
        case EngineType::FAISS_BIN_IDMAP: {
            index = GetVecIndexFactory(IndexType::FAISS_BIN_IDMAP);
            break;
        }
        case EngineType::FAISS_BIN_IVFFLAT: {
            index = GetVecIndexFactory(IndexType::FAISS_BIN_IVFLAT_CPU);
            break;
        }
X
xj.lin 已提交
219
        default: {
220
            ENGINE_LOG_ERROR << "Unsupported index type";
S
starlord 已提交
221 222 223
            return nullptr;
        }
    }
X
xj.lin 已提交
224
    return index;
S
starlord 已提交
225 226
}

227
void
W
wxyu 已提交
228 229 230 231 232
ExecutionEngineImpl::HybridLoad() const {
    if (index_type_ != EngineType::FAISS_IVFSQ8H) {
        return;
    }

W
update  
wxyu 已提交
233 234 235 236 237
    if (index_->GetType() == IndexType::FAISS_IDMAP) {
        ENGINE_LOG_WARNING << "HybridLoad with type FAISS_IDMAP, ignore";
        return;
    }

G
groot 已提交
238
#ifdef MILVUS_GPU_VERSION
W
wxyu 已提交
239
    const std::string key = location_ + ".quantizer";
240 241

    server::Config& config = server::Config::GetInstance();
Y
yudong.cai 已提交
242
    std::vector<int64_t> gpus;
243 244 245 246 247
    Status s = config.GetGpuResourceConfigSearchResources(gpus);
    if (!s.ok()) {
        ENGINE_LOG_ERROR << s.message();
        return;
    }
W
wxyu 已提交
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281

    // cache hit
    {
        const int64_t NOT_FOUND = -1;
        int64_t device_id = NOT_FOUND;
        knowhere::QuantizerPtr quantizer = nullptr;

        for (auto& gpu : gpus) {
            auto cache = cache::GpuCacheMgr::GetInstance(gpu);
            if (auto cached_quantizer = cache->GetIndex(key)) {
                device_id = gpu;
                quantizer = std::static_pointer_cast<CachedQuantizer>(cached_quantizer)->Data();
            }
        }

        if (device_id != NOT_FOUND) {
            index_->SetQuantizer(quantizer);
            return;
        }
    }

    // cache miss
    {
        std::vector<int64_t> all_free_mem;
        for (auto& gpu : gpus) {
            auto cache = cache::GpuCacheMgr::GetInstance(gpu);
            auto free_mem = cache->CacheCapacity() - cache->CacheUsage();
            all_free_mem.push_back(free_mem);
        }

        auto max_e = std::max_element(all_free_mem.begin(), all_free_mem.end());
        auto best_index = std::distance(all_free_mem.begin(), max_e);
        auto best_device_id = gpus[best_index];

282
        milvus::json quantizer_conf{{knowhere::meta::DEVICEID, best_device_id}, {"mode", 1}};
W
wxyu 已提交
283
        auto quantizer = index_->LoadQuantizer(quantizer_conf);
284
        ENGINE_LOG_DEBUG << "Quantizer params: " << quantizer_conf.dump();
W
add log  
wxyu 已提交
285 286 287
        if (quantizer == nullptr) {
            ENGINE_LOG_ERROR << "quantizer is nullptr";
        }
W
wxyu 已提交
288 289 290 291
        index_->SetQuantizer(quantizer);
        auto cache_quantizer = std::make_shared<CachedQuantizer>(quantizer);
        cache::GpuCacheMgr::GetInstance(best_device_id)->InsertItem(key, cache_quantizer);
    }
G
groot 已提交
292
#endif
W
wxyu 已提交
293 294 295 296 297 298 299
}

void
ExecutionEngineImpl::HybridUnset() const {
    if (index_type_ != EngineType::FAISS_IVFSQ8H) {
        return;
    }
W
update  
wxyu 已提交
300 301 302
    if (index_->GetType() == IndexType::FAISS_IDMAP) {
        return;
    }
W
wxyu 已提交
303
    index_->UnsetQuantizer();
304 305
}

S
starlord 已提交
306
Status
S
starlord 已提交
307
ExecutionEngineImpl::AddWithIds(int64_t n, const float* xdata, const int64_t* xids) {
308 309
    auto status = index_->Add(n, xdata, xids);
    return status;
S
starlord 已提交
310 311
}

G
groot 已提交
312 313 314 315 316 317
Status
ExecutionEngineImpl::AddWithIds(int64_t n, const uint8_t* xdata, const int64_t* xids) {
    auto status = index_->Add(n, xdata, xids);
    return status;
}

S
starlord 已提交
318 319 320
size_t
ExecutionEngineImpl::Count() const {
    if (index_ == nullptr) {
S
starlord 已提交
321
        ENGINE_LOG_ERROR << "ExecutionEngineImpl: index is null, return count 0";
S
starlord 已提交
322 323
        return 0;
    }
X
xj.lin 已提交
324
    return index_->Count();
S
starlord 已提交
325 326
}

S
starlord 已提交
327 328
size_t
ExecutionEngineImpl::Size() const {
G
groot 已提交
329 330 331 332 333
    if (IsBinaryIndexType(index_->GetType())) {
        return (size_t)(Count() * Dimension() / 8);
    } else {
        return (size_t)(Count() * Dimension()) * sizeof(float);
    }
S
starlord 已提交
334 335
}

S
starlord 已提交
336 337 338
size_t
ExecutionEngineImpl::Dimension() const {
    if (index_ == nullptr) {
S
starlord 已提交
339
        ENGINE_LOG_ERROR << "ExecutionEngineImpl: index is null, return dimension " << dim_;
S
starlord 已提交
340 341
        return dim_;
    }
X
xj.lin 已提交
342
    return index_->Dimension();
S
starlord 已提交
343 344
}

S
starlord 已提交
345 346
size_t
ExecutionEngineImpl::PhysicalSize() const {
J
jinhai 已提交
347
    return server::CommonUtil::GetFileSize(location_);
S
starlord 已提交
348 349
}

S
starlord 已提交
350 351
Status
ExecutionEngineImpl::Serialize() {
352
    auto status = write_index(index_, location_);
353 354 355 356

    // here we reset index size by file size,
    // since some index type(such as SQ8) data size become smaller after serialized
    index_->set_size(PhysicalSize());
G
add log  
groot 已提交
357
    ENGINE_LOG_DEBUG << "Finish serialize index file: " << location_ << " size: " << index_->Size();
358

G
groot 已提交
359 360 361 362 363
    if (index_->Size() == 0) {
        std::string msg = "Failed to serialize file: " + location_ + " reason: out of disk space or memory";
        status = Status(DB_ERROR, msg);
    }

364
    return status;
S
starlord 已提交
365 366
}

367
/*
S
starlord 已提交
368 369
Status
ExecutionEngineImpl::Load(bool to_cache) {
370
    index_ = std::static_pointer_cast<VecIndex>(cache::CpuCacheMgr::GetInstance()->GetIndex(location_));
J
jinhai 已提交
371
    bool already_in_cache = (index_ != nullptr);
S
starlord 已提交
372
    if (!already_in_cache) {
X
xj.lin 已提交
373
        try {
Y
Yu Kun 已提交
374 375
            double physical_size = PhysicalSize();
            server::CollectExecutionEngineMetrics metrics(physical_size);
X
xj.lin 已提交
376
            index_ = read_index(location_);
S
starlord 已提交
377
            if (index_ == nullptr) {
S
starlord 已提交
378 379
                std::string msg = "Failed to load index from " + location_;
                ENGINE_LOG_ERROR << msg;
S
starlord 已提交
380
                return Status(DB_ERROR, msg);
S
starlord 已提交
381 382 383
            } else {
                ENGINE_LOG_DEBUG << "Disk io from: " << location_;
            }
S
starlord 已提交
384
        } catch (std::exception& e) {
S
starlord 已提交
385
            ENGINE_LOG_ERROR << e.what();
S
starlord 已提交
386
            return Status(DB_ERROR, e.what());
X
xj.lin 已提交
387
        }
X
xj.lin 已提交
388 389
    }

J
jinhai 已提交
390
    if (!already_in_cache && to_cache) {
X
xj.lin 已提交
391 392 393
        Cache();
    }
    return Status::OK();
X
xj.lin 已提交
394
}
395 396 397 398 399 400 401 402 403 404 405 406 407
*/

Status
ExecutionEngineImpl::Load(bool to_cache) {
    // TODO(zhiru): refactor

    index_ = std::static_pointer_cast<VecIndex>(cache::CpuCacheMgr::GetInstance()->GetIndex(location_));
    bool already_in_cache = (index_ != nullptr);
    if (!already_in_cache) {
        std::string segment_dir;
        utils::GetParentPath(location_, segment_dir);
        auto segment_reader_ptr = std::make_shared<segment::SegmentReader>(segment_dir);

408
        if (utils::IsRawIndexType((int32_t)index_type_)) {
409 410
            index_ = index_type_ == EngineType::FAISS_IDMAP ? GetVecIndexFactory(IndexType::FAISS_IDMAP)
                                                            : GetVecIndexFactory(IndexType::FAISS_BIN_IDMAP);
411 412
            milvus::json conf{{knowhere::meta::DEVICEID, gpu_num_}, {knowhere::meta::DIM, dim_}};
            MappingMetricType(metric_type_, conf);
413
            auto adapter = AdapterMgr::GetInstance().GetAdapter(index_->GetType());
414 415 416 417
            ENGINE_LOG_DEBUG << "Index params: " << conf.dump();
            if (!adapter->CheckTrain(conf)) {
                throw Exception(DB_ERROR, "Illegal index params");
            }
418

419
            auto status = segment_reader_ptr->Load();
420 421 422 423 424 425 426 427 428 429 430 431 432
            if (!status.ok()) {
                std::string msg = "Failed to load segment from " + location_;
                ENGINE_LOG_ERROR << msg;
                return Status(DB_ERROR, msg);
            }

            segment::SegmentPtr segment_ptr;
            segment_reader_ptr->GetSegment(segment_ptr);
            auto& vectors = segment_ptr->vectors_ptr_;
            auto& deleted_docs = segment_ptr->deleted_docs_ptr_->GetDeletedDocs();

            auto vectors_uids = vectors->GetUids();
            index_->SetUids(vectors_uids);
433
            ENGINE_LOG_DEBUG << "set uids " << index_->GetUids().size() << " for index " << location_;
434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456

            auto vectors_data = vectors->GetData();

            faiss::ConcurrentBitsetPtr concurrent_bitset_ptr =
                std::make_shared<faiss::ConcurrentBitset>(vectors->GetCount());
            for (auto& offset : deleted_docs) {
                if (!concurrent_bitset_ptr->test(offset)) {
                    concurrent_bitset_ptr->set(offset);
                }
            }

            ErrorCode ec = KNOWHERE_UNEXPECTED_ERROR;
            if (index_type_ == EngineType::FAISS_IDMAP) {
                std::vector<float> float_vectors;
                float_vectors.resize(vectors_data.size() / sizeof(float));
                memcpy(float_vectors.data(), vectors_data.data(), vectors_data.size());
                ec = std::static_pointer_cast<BFIndex>(index_)->Build(conf);
                if (ec != KNOWHERE_SUCCESS) {
                    return status;
                }
                status = std::static_pointer_cast<BFIndex>(index_)->AddWithoutIds(vectors->GetCount(),
                                                                                  float_vectors.data(), Config());
                status = std::static_pointer_cast<BFIndex>(index_)->SetBlacklist(concurrent_bitset_ptr);
T
Tinkerrr 已提交
457

458
                int64_t index_size = vectors->GetCount() * dim_ * sizeof(float);
T
Tinkerrr 已提交
459 460
                int64_t bitset_size = vectors->GetCount() / 8;
                index_->set_size(index_size + bitset_size);
461 462 463 464 465 466 467 468
            } else if (index_type_ == EngineType::FAISS_BIN_IDMAP) {
                ec = std::static_pointer_cast<BinBFIndex>(index_)->Build(conf);
                if (ec != KNOWHERE_SUCCESS) {
                    return status;
                }
                status = std::static_pointer_cast<BinBFIndex>(index_)->AddWithoutIds(vectors->GetCount(),
                                                                                     vectors_data.data(), Config());
                status = std::static_pointer_cast<BinBFIndex>(index_)->SetBlacklist(concurrent_bitset_ptr);
T
Tinkerrr 已提交
469

470
                int64_t index_size = vectors->GetCount() * dim_ * sizeof(uint8_t);
T
Tinkerrr 已提交
471 472
                int64_t bitset_size = vectors->GetCount() / 8;
                index_->set_size(index_size + bitset_size);
473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512
            }
            if (!status.ok()) {
                return status;
            }

            ENGINE_LOG_DEBUG << "Finished loading raw data from segment " << segment_dir;

        } else {
            try {
                double physical_size = PhysicalSize();
                server::CollectExecutionEngineMetrics metrics(physical_size);
                index_ = read_index(location_);

                if (index_ == nullptr) {
                    std::string msg = "Failed to load index from " + location_;
                    ENGINE_LOG_ERROR << msg;
                    return Status(DB_ERROR, msg);
                } else {
                    segment::DeletedDocsPtr deleted_docs_ptr;
                    auto status = segment_reader_ptr->LoadDeletedDocs(deleted_docs_ptr);
                    if (!status.ok()) {
                        std::string msg = "Failed to load deleted docs from " + location_;
                        ENGINE_LOG_ERROR << msg;
                        return Status(DB_ERROR, msg);
                    }
                    auto& deleted_docs = deleted_docs_ptr->GetDeletedDocs();

                    faiss::ConcurrentBitsetPtr concurrent_bitset_ptr =
                        std::make_shared<faiss::ConcurrentBitset>(index_->Count());
                    for (auto& offset : deleted_docs) {
                        if (!concurrent_bitset_ptr->test(offset)) {
                            concurrent_bitset_ptr->set(offset);
                        }
                    }

                    index_->SetBlacklist(concurrent_bitset_ptr);

                    std::vector<segment::doc_id_t> uids;
                    segment_reader_ptr->LoadUids(uids);
                    index_->SetUids(uids);
513
                    ENGINE_LOG_DEBUG << "set uids " << index_->GetUids().size() << " for index " << location_;
514 515 516 517 518 519 520 521 522 523 524 525 526 527 528

                    ENGINE_LOG_DEBUG << "Finished loading index file from segment " << segment_dir;
                }
            } catch (std::exception& e) {
                ENGINE_LOG_ERROR << e.what();
                return Status(DB_ERROR, e.what());
            }
        }
    }

    if (!already_in_cache && to_cache) {
        Cache();
    }
    return Status::OK();
}  // namespace engine
X
xj.lin 已提交
529

S
starlord 已提交
530
Status
W
wxyu 已提交
531
ExecutionEngineImpl::CopyToGpu(uint64_t device_id, bool hybrid) {
X
xiaojun.lin 已提交
532
#if 0
W
wxyu 已提交
533
    if (hybrid) {
X
xiaojun.lin 已提交
534
        const std::string key = location_ + ".quantizer";
W
wxyu 已提交
535
        std::vector<uint64_t> gpus{device_id};
X
xiaojun.lin 已提交
536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553

        const int64_t NOT_FOUND = -1;
        int64_t device_id = NOT_FOUND;

        // cache hit
        {
            knowhere::QuantizerPtr quantizer = nullptr;

            for (auto& gpu : gpus) {
                auto cache = cache::GpuCacheMgr::GetInstance(gpu);
                if (auto cached_quantizer = cache->GetIndex(key)) {
                    device_id = gpu;
                    quantizer = std::static_pointer_cast<CachedQuantizer>(cached_quantizer)->Data();
                }
            }

            if (device_id != NOT_FOUND) {
                // cache hit
554
                milvus::json quantizer_conf{{knowhere::meta::DEVICEID : device_id}, {"mode" : 2}};
X
xiaojun.lin 已提交
555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579
                auto new_index = index_->LoadData(quantizer, config);
                index_ = new_index;
            }
        }

        if (device_id == NOT_FOUND) {
            // cache miss
            std::vector<int64_t> all_free_mem;
            for (auto& gpu : gpus) {
                auto cache = cache::GpuCacheMgr::GetInstance(gpu);
                auto free_mem = cache->CacheCapacity() - cache->CacheUsage();
                all_free_mem.push_back(free_mem);
            }

            auto max_e = std::max_element(all_free_mem.begin(), all_free_mem.end());
            auto best_index = std::distance(all_free_mem.begin(), max_e);
            device_id = gpus[best_index];

            auto pair = index_->CopyToGpuWithQuantizer(device_id);
            index_ = pair.first;

            // cache
            auto cached_quantizer = std::make_shared<CachedQuantizer>(pair.second);
            cache::GpuCacheMgr::GetInstance(device_id)->InsertItem(key, cached_quantizer);
        }
W
wxyu 已提交
580 581
        return Status::OK();
    }
X
xiaojun.lin 已提交
582
#endif
Y
youny626 已提交
583

G
groot 已提交
584
#ifdef MILVUS_GPU_VERSION
Y
youny626 已提交
585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601
    auto index = std::static_pointer_cast<VecIndex>(cache::GpuCacheMgr::GetInstance(device_id)->GetIndex(location_));
    bool already_in_cache = (index != nullptr);
    if (already_in_cache) {
        index_ = index;
    } else {
        if (index_ == nullptr) {
            ENGINE_LOG_ERROR << "ExecutionEngineImpl: index is null, failed to copy to gpu";
            return Status(DB_ERROR, "index is null");
        }

        try {
            index_ = index_->CopyToGpu(device_id);
            ENGINE_LOG_DEBUG << "CPU to GPU" << device_id;
        } catch (std::exception& e) {
            ENGINE_LOG_ERROR << e.what();
            return Status(DB_ERROR, e.what());
        }
602
    }
Y
youny626 已提交
603 604 605 606

    if (!already_in_cache) {
        GpuCache(device_id);
    }
G
groot 已提交
607
#endif
Y
youny626 已提交
608

609 610 611
    return Status::OK();
}

Y
Yu Kun 已提交
612 613
Status
ExecutionEngineImpl::CopyToIndexFileToGpu(uint64_t device_id) {
G
groot 已提交
614
#ifdef MILVUS_GPU_VERSION
G
groot 已提交
615
    // the ToIndexData is only a placeholder, cpu-copy-to-gpu action is performed in
F
fishpenguin 已提交
616
    gpu_num_ = device_id;
Y
Yu Kun 已提交
617 618
    auto to_index_data = std::make_shared<ToIndexData>(PhysicalSize());
    cache::DataObjPtr obj = std::static_pointer_cast<cache::DataObj>(to_index_data);
G
groot 已提交
619
    milvus::cache::GpuCacheMgr::GetInstance(device_id)->InsertItem(location_ + "_placeholder", obj);
G
groot 已提交
620
#endif
Y
Yu Kun 已提交
621 622 623
    return Status::OK();
}

S
starlord 已提交
624 625
Status
ExecutionEngineImpl::CopyToCpu() {
626
    auto index = std::static_pointer_cast<VecIndex>(cache::CpuCacheMgr::GetInstance()->GetIndex(location_));
W
wxyu 已提交
627 628 629 630
    bool already_in_cache = (index != nullptr);
    if (already_in_cache) {
        index_ = index;
    } else {
S
starlord 已提交
631
        if (index_ == nullptr) {
S
starlord 已提交
632
            ENGINE_LOG_ERROR << "ExecutionEngineImpl: index is null, failed to copy to cpu";
S
starlord 已提交
633
            return Status(DB_ERROR, "index is null");
S
starlord 已提交
634 635
        }

Y
Yu Kun 已提交
636 637 638
        try {
            index_ = index_->CopyToCpu();
            ENGINE_LOG_DEBUG << "GPU to CPU";
S
starlord 已提交
639
        } catch (std::exception& e) {
S
starlord 已提交
640
            ENGINE_LOG_ERROR << e.what();
S
starlord 已提交
641
            return Status(DB_ERROR, e.what());
Y
Yu Kun 已提交
642 643 644
        }
    }

W
wxyu 已提交
645
    if (!already_in_cache) {
Y
Yu Kun 已提交
646
        Cache();
647 648 649 650
    }
    return Status::OK();
}

651 652 653 654 655 656 657 658 659 660 661 662
// ExecutionEnginePtr
// ExecutionEngineImpl::Clone() {
//    if (index_ == nullptr) {
//        ENGINE_LOG_ERROR << "ExecutionEngineImpl: index is null, failed to clone";
//        return nullptr;
//    }
//
//    auto ret = std::make_shared<ExecutionEngineImpl>(dim_, location_, index_type_, metric_type_, nlist_);
//    ret->Init();
//    ret->index_ = index_->Clone();
//    return ret;
//}
W
wxyu 已提交
663

664
/*
S
starlord 已提交
665
Status
S
starlord 已提交
666
ExecutionEngineImpl::Merge(const std::string& location) {
X
xj.lin 已提交
667
    if (location == location_) {
S
starlord 已提交
668
        return Status(DB_ERROR, "Cannot Merge Self");
X
xj.lin 已提交
669 670
    }
    ENGINE_LOG_DEBUG << "Merge index file: " << location << " to: " << location_;
S
starlord 已提交
671

S
starlord 已提交
672
    auto to_merge = cache::CpuCacheMgr::GetInstance()->GetIndex(location);
X
xj.lin 已提交
673
    if (!to_merge) {
X
xj.lin 已提交
674
        try {
Y
Yu Kun 已提交
675 676
            double physical_size = server::CommonUtil::GetFileSize(location);
            server::CollectExecutionEngineMetrics metrics(physical_size);
X
xj.lin 已提交
677
            to_merge = read_index(location);
S
starlord 已提交
678
        } catch (std::exception& e) {
S
starlord 已提交
679
            ENGINE_LOG_ERROR << e.what();
S
starlord 已提交
680
            return Status(DB_ERROR, e.what());
X
xj.lin 已提交
681
        }
X
xj.lin 已提交
682 683
    }

S
starlord 已提交
684
    if (index_ == nullptr) {
S
starlord 已提交
685
        ENGINE_LOG_ERROR << "ExecutionEngineImpl: index is null, failed to merge";
S
starlord 已提交
686
        return Status(DB_ERROR, "index is null");
S
starlord 已提交
687 688
    }

X
xj.lin 已提交
689
    if (auto file_index = std::dynamic_pointer_cast<BFIndex>(to_merge)) {
690 691
        auto status = index_->Add(file_index->Count(), file_index->GetRawVectors(), file_index->GetRawIds());
        if (!status.ok()) {
G
groot 已提交
692
            ENGINE_LOG_ERROR << "Failed to merge: " << location << " to: " << location_;
G
groot 已提交
693 694
        } else {
            ENGINE_LOG_DEBUG << "Finish merge index file: " << location;
X
xj.lin 已提交
695
        }
696
        return status;
G
groot 已提交
697 698 699 700 701 702 703 704
    } else if (auto bin_index = std::dynamic_pointer_cast<BinBFIndex>(to_merge)) {
        auto status = index_->Add(bin_index->Count(), bin_index->GetRawVectors(), bin_index->GetRawIds());
        if (!status.ok()) {
            ENGINE_LOG_ERROR << "Failed to merge: " << location << " to: " << location_;
        } else {
            ENGINE_LOG_DEBUG << "Finish merge index file: " << location;
        }
        return status;
X
xj.lin 已提交
705
    } else {
S
starlord 已提交
706
        return Status(DB_ERROR, "file index type is not idmap");
X
xj.lin 已提交
707
    }
S
starlord 已提交
708
}
709
*/
S
starlord 已提交
710 711

ExecutionEnginePtr
S
starlord 已提交
712
ExecutionEngineImpl::BuildIndex(const std::string& location, EngineType engine_type) {
X
xj.lin 已提交
713 714 715
    ENGINE_LOG_DEBUG << "Build index file: " << location << " from: " << location_;

    auto from_index = std::dynamic_pointer_cast<BFIndex>(index_);
G
groot 已提交
716 717
    auto bin_from_index = std::dynamic_pointer_cast<BinBFIndex>(index_);
    if (from_index == nullptr && bin_from_index == nullptr) {
S
starlord 已提交
718 719 720 721
        ENGINE_LOG_ERROR << "ExecutionEngineImpl: from_index is null, failed to build index";
        return nullptr;
    }

S
starlord 已提交
722
    auto to_index = CreatetVecIndex(engine_type);
X
xj.lin 已提交
723
    if (!to_index) {
724
        throw Exception(DB_ERROR, "Unsupported index type");
X
xj.lin 已提交
725 726
    }

727 728 729 730 731 732
    milvus::json conf = index_params_;
    conf[knowhere::meta::DIM] = Dimension();
    conf[knowhere::meta::ROWS] = Count();
    conf[knowhere::meta::DEVICEID] = gpu_num_;
    MappingMetricType(metric_type_, conf);
    ENGINE_LOG_DEBUG << "Index params: " << conf.dump();
X
xiaojun.lin 已提交
733
    auto adapter = AdapterMgr::GetInstance().GetAdapter(to_index->GetType());
734 735 736 737
    if (!adapter->CheckTrain(conf)) {
        throw Exception(DB_ERROR, "Illegal index params");
    }
    ENGINE_LOG_DEBUG << "Index config: " << conf.dump();
738

739
    auto status = Status::OK();
740
    std::vector<segment::doc_id_t> uids;
G
groot 已提交
741 742
    if (from_index) {
        status = to_index->BuildAll(Count(), from_index->GetRawVectors(), from_index->GetRawIds(), conf);
743
        uids = from_index->GetUids();
G
groot 已提交
744 745
    } else if (bin_from_index) {
        status = to_index->BuildAll(Count(), bin_from_index->GetRawVectors(), bin_from_index->GetRawIds(), conf);
746
        uids = bin_from_index->GetUids();
G
groot 已提交
747
    }
748 749 750
    to_index->SetUids(uids);
    ENGINE_LOG_DEBUG << "set uids " << to_index->GetUids().size() << " for " << location;

S
starlord 已提交
751 752 753
    if (!status.ok()) {
        throw Exception(DB_ERROR, status.message());
    }
X
xj.lin 已提交
754

G
add log  
groot 已提交
755
    ENGINE_LOG_DEBUG << "Finish build index file: " << location << " size: " << to_index->Size();
756
    return std::make_shared<ExecutionEngineImpl>(to_index, location, engine_type, metric_type_, index_params_);
S
starlord 已提交
757 758
}

759 760 761 762 763 764 765 766 767 768 769
// map offsets to ids
void
MapUids(const std::vector<segment::doc_id_t>& uids, int64_t* labels, size_t num) {
    for (int64_t i = 0; i < num; ++i) {
        int64_t& offset = labels[i];
        if (offset != -1) {
            offset = uids[offset];
        }
    }
}

S
starlord 已提交
770
Status
771 772
ExecutionEngineImpl::Search(int64_t n, const float* data, int64_t k, const milvus::json& extra_params, float* distances,
                            int64_t* labels, bool hybrid) {
773
#if 0
J
JinHai-CN 已提交
774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795
    if (index_type_ == EngineType::FAISS_IVFSQ8H) {
        if (!hybrid) {
            const std::string key = location_ + ".quantizer";
            std::vector<uint64_t> gpus = scheduler::get_gpu_pool();

            const int64_t NOT_FOUND = -1;
            int64_t device_id = NOT_FOUND;

            // cache hit
            {
                knowhere::QuantizerPtr quantizer = nullptr;

                for (auto& gpu : gpus) {
                    auto cache = cache::GpuCacheMgr::GetInstance(gpu);
                    if (auto cached_quantizer = cache->GetIndex(key)) {
                        device_id = gpu;
                        quantizer = std::static_pointer_cast<CachedQuantizer>(cached_quantizer)->Data();
                    }
                }

                if (device_id != NOT_FOUND) {
                    // cache hit
796
                    milvus::json quantizer_conf{{knowhere::meta::DEVICEID : device_id}, {"mode" : 2}};
J
JinHai-CN 已提交
797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823
                    auto new_index = index_->LoadData(quantizer, config);
                    index_ = new_index;
                }
            }

            if (device_id == NOT_FOUND) {
                // cache miss
                std::vector<int64_t> all_free_mem;
                for (auto& gpu : gpus) {
                    auto cache = cache::GpuCacheMgr::GetInstance(gpu);
                    auto free_mem = cache->CacheCapacity() - cache->CacheUsage();
                    all_free_mem.push_back(free_mem);
                }

                auto max_e = std::max_element(all_free_mem.begin(), all_free_mem.end());
                auto best_index = std::distance(all_free_mem.begin(), max_e);
                device_id = gpus[best_index];

                auto pair = index_->CopyToGpuWithQuantizer(device_id);
                index_ = pair.first;

                // cache
                auto cached_quantizer = std::make_shared<CachedQuantizer>(pair.second);
                cache::GpuCacheMgr::GetInstance(device_id)->InsertItem(key, cached_quantizer);
            }
        }
    }
824
#endif
825
    TimeRecorder rc("ExecutionEngineImpl::Search float");
J
JinHai-CN 已提交
826

S
starlord 已提交
827
    if (index_ == nullptr) {
S
starlord 已提交
828
        ENGINE_LOG_ERROR << "ExecutionEngineImpl: index is null, failed to search";
S
starlord 已提交
829
        return Status(DB_ERROR, "index is null");
S
starlord 已提交
830 831
    }

832 833
    milvus::json conf = extra_params;
    conf[knowhere::meta::TOPK] = k;
X
xiaojun.lin 已提交
834
    auto adapter = AdapterMgr::GetInstance().GetAdapter(index_->GetType());
835 836 837 838
    ENGINE_LOG_DEBUG << "Search params: " << conf.dump();
    if (!adapter->CheckSearch(conf, index_->GetType())) {
        throw Exception(DB_ERROR, "Illegal search params");
    }
X
xiaojun.lin 已提交
839

W
wxyu 已提交
840 841 842
    if (hybrid) {
        HybridLoad();
    }
W
wxyu 已提交
843

844
    rc.RecordSection("search prepare");
X
xiaojun.lin 已提交
845
    auto status = index_->Search(n, data, distances, labels, conf);
846 847 848
    rc.RecordSection("search done");

    // map offsets to ids
849
    ENGINE_LOG_DEBUG << "get uids " << index_->GetUids().size() << " from index " << location_;
850
    MapUids(index_->GetUids(), labels, n * k);
851

852
    rc.RecordSection("map uids " + std::to_string(n * k));
W
wxyu 已提交
853

W
wxyu 已提交
854 855 856
    if (hybrid) {
        HybridUnset();
    }
W
wxyu 已提交
857

858
    if (!status.ok()) {
G
groot 已提交
859
        ENGINE_LOG_ERROR << "Search error:" << status.message();
X
xj.lin 已提交
860
    }
861
    return status;
S
starlord 已提交
862 863
}

G
groot 已提交
864
Status
865 866
ExecutionEngineImpl::Search(int64_t n, const uint8_t* data, int64_t k, const milvus::json& extra_params,
                            float* distances, int64_t* labels, bool hybrid) {
867
    TimeRecorder rc("ExecutionEngineImpl::Search uint8");
868

G
groot 已提交
869 870 871 872 873
    if (index_ == nullptr) {
        ENGINE_LOG_ERROR << "ExecutionEngineImpl: index is null, failed to search";
        return Status(DB_ERROR, "index is null");
    }

874 875
    milvus::json conf = extra_params;
    conf[knowhere::meta::TOPK] = k;
G
groot 已提交
876
    auto adapter = AdapterMgr::GetInstance().GetAdapter(index_->GetType());
877 878 879 880
    ENGINE_LOG_DEBUG << "Search params: " << conf.dump();
    if (!adapter->CheckSearch(conf, index_->GetType())) {
        throw Exception(DB_ERROR, "Illegal search params");
    }
G
groot 已提交
881 882 883 884 885

    if (hybrid) {
        HybridLoad();
    }

886
    rc.RecordSection("search prepare");
G
groot 已提交
887
    auto status = index_->Search(n, data, distances, labels, conf);
888 889 890
    rc.RecordSection("search done");

    // map offsets to ids
891
    ENGINE_LOG_DEBUG << "get uids " << index_->GetUids().size() << " from index " << location_;
892
    MapUids(index_->GetUids(), labels, n * k);
893

894
    rc.RecordSection("map uids " + std::to_string(n * k));
895 896 897 898 899 900 901 902 903 904 905 906

    if (hybrid) {
        HybridUnset();
    }

    if (!status.ok()) {
        ENGINE_LOG_ERROR << "Search error:" << status.message();
    }
    return status;
}

Status
907 908
ExecutionEngineImpl::Search(int64_t n, const std::vector<int64_t>& ids, int64_t k, const milvus::json& extra_params,
                            float* distances, int64_t* labels, bool hybrid) {
909
    TimeRecorder rc("ExecutionEngineImpl::Search vector of ids");
910 911 912 913 914 915

    if (index_ == nullptr) {
        ENGINE_LOG_ERROR << "ExecutionEngineImpl: index is null, failed to search";
        return Status(DB_ERROR, "index is null");
    }

916 917
    milvus::json conf = extra_params;
    conf[knowhere::meta::TOPK] = k;
918
    auto adapter = AdapterMgr::GetInstance().GetAdapter(index_->GetType());
919 920 921 922
    ENGINE_LOG_DEBUG << "Search params: " << conf.dump();
    if (!adapter->CheckSearch(conf, index_->GetType())) {
        throw Exception(DB_ERROR, "Illegal search params");
    }
923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971

    if (hybrid) {
        HybridLoad();
    }

    rc.RecordSection("search prepare");

    // std::string segment_dir;
    // utils::GetParentPath(location_, segment_dir);
    // segment::SegmentReader segment_reader(segment_dir);
    //    segment::IdBloomFilterPtr id_bloom_filter_ptr;
    //    segment_reader.LoadBloomFilter(id_bloom_filter_ptr);

    // Check if the id is present. If so, find its offset
    const std::vector<segment::doc_id_t>& uids = index_->GetUids();

    std::vector<int64_t> offsets;
    /*
    std::vector<segment::doc_id_t> uids;
    auto status = segment_reader.LoadUids(uids);
    if (!status.ok()) {
        return status;
    }
     */

    // There is only one id in ids
    for (auto& id : ids) {
        //        if (id_bloom_filter_ptr->Check(id)) {
        //            if (uids.empty()) {
        //                segment_reader.LoadUids(uids);
        //            }
        //            auto found = std::find(uids.begin(), uids.end(), id);
        //            if (found != uids.end()) {
        //                auto offset = std::distance(uids.begin(), found);
        //                offsets.emplace_back(offset);
        //            }
        //        }
        auto found = std::find(uids.begin(), uids.end(), id);
        if (found != uids.end()) {
            auto offset = std::distance(uids.begin(), found);
            offsets.emplace_back(offset);
        }
    }

    rc.RecordSection("get offset");

    auto status = Status::OK();
    if (!offsets.empty()) {
        status = index_->SearchById(offsets.size(), offsets.data(), distances, labels, conf);
972
        rc.RecordSection("search done");
973 974

        // map offsets to ids
975
        ENGINE_LOG_DEBUG << "get uids " << index_->GetUids().size() << " from index " << location_;
976 977 978
        MapUids(uids, labels, offsets.size() * k);

        rc.RecordSection("map uids " + std::to_string(offsets.size() * k));
979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003
    }

    if (hybrid) {
        HybridUnset();
    }

    if (!status.ok()) {
        ENGINE_LOG_ERROR << "Search error:" << status.message();
    }
    return status;
}

Status
ExecutionEngineImpl::GetVectorByID(const int64_t& id, float* vector, bool hybrid) {
    if (index_ == nullptr) {
        ENGINE_LOG_ERROR << "ExecutionEngineImpl: index is null, failed to search";
        return Status(DB_ERROR, "index is null");
    }

    if (hybrid) {
        HybridLoad();
    }

    // Only one id for now
    std::vector<int64_t> ids{id};
1004
    auto status = index_->GetVectorById(1, ids.data(), vector, milvus::json());
1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030

    if (hybrid) {
        HybridUnset();
    }

    if (!status.ok()) {
        ENGINE_LOG_ERROR << "Search error:" << status.message();
    }
    return status;
}

Status
ExecutionEngineImpl::GetVectorByID(const int64_t& id, uint8_t* vector, bool hybrid) {
    if (index_ == nullptr) {
        ENGINE_LOG_ERROR << "ExecutionEngineImpl: index is null, failed to search";
        return Status(DB_ERROR, "index is null");
    }

    ENGINE_LOG_DEBUG << "Get binary vector by id:  " << id;

    if (hybrid) {
        HybridLoad();
    }

    // Only one id for now
    std::vector<int64_t> ids{id};
1031
    auto status = index_->GetVectorById(1, ids.data(), vector, milvus::json());
G
groot 已提交
1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042

    if (hybrid) {
        HybridUnset();
    }

    if (!status.ok()) {
        ENGINE_LOG_ERROR << "Search error:" << status.message();
    }
    return status;
}

S
starlord 已提交
1043 1044
Status
ExecutionEngineImpl::Cache() {
1045
    cache::DataObjPtr obj = std::static_pointer_cast<cache::DataObj>(index_);
S
starlord 已提交
1046
    milvus::cache::CpuCacheMgr::GetInstance()->InsertItem(location_, obj);
S
starlord 已提交
1047 1048 1049 1050

    return Status::OK();
}

S
starlord 已提交
1051 1052
Status
ExecutionEngineImpl::GpuCache(uint64_t gpu_id) {
G
groot 已提交
1053
#ifdef MILVUS_GPU_VERSION
1054
    cache::DataObjPtr obj = std::static_pointer_cast<cache::DataObj>(index_);
S
starlord 已提交
1055
    milvus::cache::GpuCacheMgr::GetInstance(gpu_id)->InsertItem(location_, obj);
G
groot 已提交
1056
#endif
1057
    return Status::OK();
Y
Yu Kun 已提交
1058 1059
}

X
xj.lin 已提交
1060
// TODO(linxj): remove.
S
starlord 已提交
1061 1062
Status
ExecutionEngineImpl::Init() {
G
groot 已提交
1063
#ifdef MILVUS_GPU_VERSION
S
starlord 已提交
1064
    server::Config& config = server::Config::GetInstance();
Y
yudong.cai 已提交
1065
    std::vector<int64_t> gpu_ids;
1066
    Status s = config.GetGpuResourceConfigBuildIndexResources(gpu_ids);
F
fishpenguin 已提交
1067
    if (!s.ok()) {
1068
        gpu_num_ = -1;
1069
        return s;
F
fishpenguin 已提交
1070
    }
1071 1072 1073 1074
    for (auto id : gpu_ids) {
        if (gpu_num_ == id) {
            return Status::OK();
        }
S
starlord 已提交
1075
    }
S
starlord 已提交
1076

1077 1078
    std::string msg = "Invalid gpu_num";
    return Status(SERVER_INVALID_ARGUMENT, msg);
G
groot 已提交
1079 1080 1081
#else
    return Status::OK();
#endif
S
starlord 已提交
1082 1083
}

S
starlord 已提交
1084 1085
}  // namespace engine
}  // namespace milvus