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

G
groot 已提交
18
#include "db/engine/ExecutionEngineImpl.h"
G
groot 已提交
19
#include "cache/CpuCacheMgr.h"
G
groot 已提交
20
#include "cache/GpuCacheMgr.h"
X
xiaojun.lin 已提交
21
#include "knowhere/common/Config.h"
G
groot 已提交
22
#include "metrics/Metrics.h"
X
xiaojun.lin 已提交
23 24
#include "scheduler/Utils.h"
#include "server/Config.h"
J
jinhai 已提交
25
#include "utils/CommonUtil.h"
G
groot 已提交
26
#include "utils/Exception.h"
G
groot 已提交
27 28 29
#include "utils/Log.h"
#include "wrapper/ConfAdapter.h"
#include "wrapper/ConfAdapterMgr.h"
G
groot 已提交
30 31
#include "wrapper/VecImpl.h"
#include "wrapper/VecIndex.h"
X
xj.lin 已提交
32

G
groot 已提交
33
#include <stdexcept>
G
groot 已提交
34
#include <utility>
W
wxyu 已提交
35
#include <vector>
G
groot 已提交
36

J
JinHai-CN 已提交
37
//#define ON_SEARCH
G
groot 已提交
38 39 40
namespace milvus {
namespace engine {

W
wxyu 已提交
41 42
class CachedQuantizer : public cache::DataObj {
 public:
W
wxyu 已提交
43 44
    explicit CachedQuantizer(knowhere::QuantizerPtr data) : data_(std::move(data)) {
    }
W
wxyu 已提交
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59

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

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

 private:
    knowhere::QuantizerPtr data_;
};

G
groot 已提交
60 61 62
ExecutionEngineImpl::ExecutionEngineImpl(uint16_t dimension, const std::string& location, EngineType index_type,
                                         MetricType metric_type, int32_t nlist)
    : location_(location), dim_(dimension), index_type_(index_type), metric_type_(metric_type), nlist_(nlist) {
X
xj.lin 已提交
63
    index_ = CreatetVecIndex(EngineType::FAISS_IDMAP);
64
    if (!index_) {
65
        throw Exception(DB_ERROR, "Unsupported index type");
66
    }
X
xj.lin 已提交
67

X
xiaojun.lin 已提交
68 69 70
    TempMetaConf temp_conf;
    temp_conf.gpu_id = gpu_num_;
    temp_conf.dim = dimension;
G
groot 已提交
71
    temp_conf.metric_type = (metric_type_ == MetricType::IP) ? knowhere::METRICTYPE::IP : knowhere::METRICTYPE::L2;
X
xiaojun.lin 已提交
72 73 74 75
    auto adapter = AdapterMgr::GetInstance().GetAdapter(index_->GetType());
    auto conf = adapter->Match(temp_conf);

    auto ec = std::static_pointer_cast<BFIndex>(index_)->Build(conf);
76 77 78
    if (ec != KNOWHERE_SUCCESS) {
        throw Exception(DB_ERROR, "Build index error");
    }
G
groot 已提交
79 80
}

G
groot 已提交
81 82 83
ExecutionEngineImpl::ExecutionEngineImpl(VecIndexPtr index, const std::string& location, EngineType index_type,
                                         MetricType metric_type, int32_t nlist)
    : index_(std::move(index)), location_(location), index_type_(index_type), metric_type_(metric_type), nlist_(nlist) {
X
xj.lin 已提交
84
}
G
groot 已提交
85

G
groot 已提交
86 87
VecIndexPtr
ExecutionEngineImpl::CreatetVecIndex(EngineType type) {
X
xj.lin 已提交
88 89 90 91
    std::shared_ptr<VecIndex> index;
    switch (type) {
        case EngineType::FAISS_IDMAP: {
            index = GetVecIndexFactory(IndexType::FAISS_IDMAP);
G
groot 已提交
92 93
            break;
        }
J
jinhai 已提交
94
        case EngineType::FAISS_IVFFLAT: {
X
xj.lin 已提交
95
            index = GetVecIndexFactory(IndexType::FAISS_IVFFLAT_MIX);
G
groot 已提交
96 97
            break;
        }
J
jinhai 已提交
98 99
        case EngineType::FAISS_IVFSQ8: {
            index = GetVecIndexFactory(IndexType::FAISS_IVFSQ8_MIX);
G
groot 已提交
100 101
            break;
        }
X
xj.lin 已提交
102 103 104 105
        case EngineType::NSG_MIX: {
            index = GetVecIndexFactory(IndexType::NSG_MIX);
            break;
        }
W
wxyu 已提交
106 107 108 109
        case EngineType::FAISS_IVFSQ8H: {
            index = GetVecIndexFactory(IndexType::FAISS_IVFSQ8_HYBRID);
            break;
        }
X
xj.lin 已提交
110
        default: {
111
            ENGINE_LOG_ERROR << "Unsupported index type";
G
groot 已提交
112 113 114
            return nullptr;
        }
    }
X
xj.lin 已提交
115
    return index;
G
groot 已提交
116 117
}

118
void
W
wxyu 已提交
119 120 121 122 123
ExecutionEngineImpl::HybridLoad() const {
    if (index_type_ != EngineType::FAISS_IVFSQ8H) {
        return;
    }

W
update  
wxyu 已提交
124 125 126 127 128
    if (index_->GetType() == IndexType::FAISS_IDMAP) {
        ENGINE_LOG_WARNING << "HybridLoad with type FAISS_IDMAP, ignore";
        return;
    }

W
wxyu 已提交
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 157 158 159 160 161 162 163 164 165 166 167 168
    const std::string key = location_ + ".quantizer";
    std::vector<uint64_t> gpus = scheduler::get_gpu_pool();

    // 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];

        auto quantizer_conf = std::make_shared<knowhere::QuantizerCfg>();
        quantizer_conf->mode = 1;
        quantizer_conf->gpu_id = best_device_id;
        auto quantizer = index_->LoadQuantizer(quantizer_conf);
W
add log  
wxyu 已提交
169 170 171
        if (quantizer == nullptr) {
            ENGINE_LOG_ERROR << "quantizer is nullptr";
        }
W
wxyu 已提交
172 173 174 175 176 177 178 179 180 181 182
        index_->SetQuantizer(quantizer);
        auto cache_quantizer = std::make_shared<CachedQuantizer>(quantizer);
        cache::GpuCacheMgr::GetInstance(best_device_id)->InsertItem(key, cache_quantizer);
    }
}

void
ExecutionEngineImpl::HybridUnset() const {
    if (index_type_ != EngineType::FAISS_IVFSQ8H) {
        return;
    }
W
update  
wxyu 已提交
183 184 185
    if (index_->GetType() == IndexType::FAISS_IDMAP) {
        return;
    }
W
wxyu 已提交
186
    index_->UnsetQuantizer();
187 188
}

G
groot 已提交
189
Status
G
groot 已提交
190
ExecutionEngineImpl::AddWithIds(int64_t n, const float* xdata, const int64_t* xids) {
191 192
    auto status = index_->Add(n, xdata, xids);
    return status;
G
groot 已提交
193 194
}

G
groot 已提交
195 196 197
size_t
ExecutionEngineImpl::Count() const {
    if (index_ == nullptr) {
G
groot 已提交
198
        ENGINE_LOG_ERROR << "ExecutionEngineImpl: index is null, return count 0";
G
groot 已提交
199 200
        return 0;
    }
X
xj.lin 已提交
201
    return index_->Count();
G
groot 已提交
202 203
}

G
groot 已提交
204 205
size_t
ExecutionEngineImpl::Size() const {
G
groot 已提交
206
    return (size_t)(Count() * Dimension()) * sizeof(float);
G
groot 已提交
207 208
}

G
groot 已提交
209 210 211
size_t
ExecutionEngineImpl::Dimension() const {
    if (index_ == nullptr) {
G
groot 已提交
212
        ENGINE_LOG_ERROR << "ExecutionEngineImpl: index is null, return dimension " << dim_;
G
groot 已提交
213 214
        return dim_;
    }
X
xj.lin 已提交
215
    return index_->Dimension();
G
groot 已提交
216 217
}

G
groot 已提交
218 219
size_t
ExecutionEngineImpl::PhysicalSize() const {
J
jinhai 已提交
220
    return server::CommonUtil::GetFileSize(location_);
G
groot 已提交
221 222
}

G
groot 已提交
223 224
Status
ExecutionEngineImpl::Serialize() {
225 226
    auto status = write_index(index_, location_);
    return status;
G
groot 已提交
227 228
}

G
groot 已提交
229 230
Status
ExecutionEngineImpl::Load(bool to_cache) {
231
    index_ = std::static_pointer_cast<VecIndex>(cache::CpuCacheMgr::GetInstance()->GetIndex(location_));
J
jinhai 已提交
232
    bool already_in_cache = (index_ != nullptr);
G
groot 已提交
233
    if (!already_in_cache) {
X
xj.lin 已提交
234
        try {
Y
Yu Kun 已提交
235 236
            double physical_size = PhysicalSize();
            server::CollectExecutionEngineMetrics metrics(physical_size);
X
xj.lin 已提交
237
            index_ = read_index(location_);
G
groot 已提交
238
            if (index_ == nullptr) {
G
groot 已提交
239 240
                std::string msg = "Failed to load index from " + location_;
                ENGINE_LOG_ERROR << msg;
G
groot 已提交
241
                return Status(DB_ERROR, msg);
G
groot 已提交
242 243 244
            } else {
                ENGINE_LOG_DEBUG << "Disk io from: " << location_;
            }
G
groot 已提交
245
        } catch (std::exception& e) {
G
groot 已提交
246
            ENGINE_LOG_ERROR << e.what();
G
groot 已提交
247
            return Status(DB_ERROR, e.what());
X
xj.lin 已提交
248
        }
X
xj.lin 已提交
249 250
    }

J
jinhai 已提交
251
    if (!already_in_cache && to_cache) {
X
xj.lin 已提交
252 253 254
        Cache();
    }
    return Status::OK();
X
xj.lin 已提交
255 256
}

G
groot 已提交
257
Status
W
wxyu 已提交
258 259
ExecutionEngineImpl::CopyToGpu(uint64_t device_id, bool hybrid) {
    if (hybrid) {
X
xiaojun.lin 已提交
260
        const std::string key = location_ + ".quantizer";
W
wxyu 已提交
261
        std::vector<uint64_t> gpus{device_id};
X
xiaojun.lin 已提交
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307

        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
                auto config = std::make_shared<knowhere::QuantizerCfg>();
                config->gpu_id = device_id;
                config->mode = 2;
                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 已提交
308 309 310
        return Status::OK();
    }

311
    auto index = std::static_pointer_cast<VecIndex>(cache::GpuCacheMgr::GetInstance(device_id)->GetIndex(location_));
W
wxyu 已提交
312 313 314 315
    bool already_in_cache = (index != nullptr);
    if (already_in_cache) {
        index_ = index;
    } else {
G
groot 已提交
316
        if (index_ == nullptr) {
G
groot 已提交
317
            ENGINE_LOG_ERROR << "ExecutionEngineImpl: index is null, failed to copy to gpu";
G
groot 已提交
318
            return Status(DB_ERROR, "index is null");
G
groot 已提交
319 320
        }

Y
Yu Kun 已提交
321 322 323
        try {
            index_ = index_->CopyToGpu(device_id);
            ENGINE_LOG_DEBUG << "CPU to GPU" << device_id;
G
groot 已提交
324
        } catch (std::exception& e) {
G
groot 已提交
325
            ENGINE_LOG_ERROR << e.what();
G
groot 已提交
326
            return Status(DB_ERROR, e.what());
Y
Yu Kun 已提交
327
        }
328
    }
Y
Yu Kun 已提交
329 330 331 332 333

    if (!already_in_cache) {
        GpuCache(device_id);
    }

334 335 336
    return Status::OK();
}

Y
Yu Kun 已提交
337 338
Status
ExecutionEngineImpl::CopyToIndexFileToGpu(uint64_t device_id) {
Y
Yu Kun 已提交
339 340 341
    auto to_index_data = std::make_shared<ToIndexData>(PhysicalSize());
    cache::DataObjPtr obj = std::static_pointer_cast<cache::DataObj>(to_index_data);
    milvus::cache::GpuCacheMgr::GetInstance(device_id)->InsertItem(location_, obj);
Y
Yu Kun 已提交
342 343 344
    return Status::OK();
}

G
groot 已提交
345 346
Status
ExecutionEngineImpl::CopyToCpu() {
347
    auto index = std::static_pointer_cast<VecIndex>(cache::CpuCacheMgr::GetInstance()->GetIndex(location_));
W
wxyu 已提交
348 349 350 351
    bool already_in_cache = (index != nullptr);
    if (already_in_cache) {
        index_ = index;
    } else {
G
groot 已提交
352
        if (index_ == nullptr) {
G
groot 已提交
353
            ENGINE_LOG_ERROR << "ExecutionEngineImpl: index is null, failed to copy to cpu";
G
groot 已提交
354
            return Status(DB_ERROR, "index is null");
G
groot 已提交
355 356
        }

Y
Yu Kun 已提交
357 358 359
        try {
            index_ = index_->CopyToCpu();
            ENGINE_LOG_DEBUG << "GPU to CPU";
G
groot 已提交
360
        } catch (std::exception& e) {
G
groot 已提交
361
            ENGINE_LOG_ERROR << e.what();
G
groot 已提交
362
            return Status(DB_ERROR, e.what());
Y
Yu Kun 已提交
363 364 365
        }
    }

W
wxyu 已提交
366
    if (!already_in_cache) {
Y
Yu Kun 已提交
367
        Cache();
368 369 370 371
    }
    return Status::OK();
}

G
groot 已提交
372 373 374
ExecutionEnginePtr
ExecutionEngineImpl::Clone() {
    if (index_ == nullptr) {
G
groot 已提交
375 376 377 378
        ENGINE_LOG_ERROR << "ExecutionEngineImpl: index is null, failed to clone";
        return nullptr;
    }

W
wxyu 已提交
379 380 381 382 383 384
    auto ret = std::make_shared<ExecutionEngineImpl>(dim_, location_, index_type_, metric_type_, nlist_);
    ret->Init();
    ret->index_ = index_->Clone();
    return ret;
}

G
groot 已提交
385
Status
G
groot 已提交
386
ExecutionEngineImpl::Merge(const std::string& location) {
X
xj.lin 已提交
387
    if (location == location_) {
G
groot 已提交
388
        return Status(DB_ERROR, "Cannot Merge Self");
X
xj.lin 已提交
389 390
    }
    ENGINE_LOG_DEBUG << "Merge index file: " << location << " to: " << location_;
G
groot 已提交
391

G
groot 已提交
392
    auto to_merge = cache::CpuCacheMgr::GetInstance()->GetIndex(location);
X
xj.lin 已提交
393
    if (!to_merge) {
X
xj.lin 已提交
394
        try {
Y
Yu Kun 已提交
395 396
            double physical_size = server::CommonUtil::GetFileSize(location);
            server::CollectExecutionEngineMetrics metrics(physical_size);
X
xj.lin 已提交
397
            to_merge = read_index(location);
G
groot 已提交
398
        } catch (std::exception& e) {
G
groot 已提交
399
            ENGINE_LOG_ERROR << e.what();
G
groot 已提交
400
            return Status(DB_ERROR, e.what());
X
xj.lin 已提交
401
        }
X
xj.lin 已提交
402 403
    }

G
groot 已提交
404
    if (index_ == nullptr) {
G
groot 已提交
405
        ENGINE_LOG_ERROR << "ExecutionEngineImpl: index is null, failed to merge";
G
groot 已提交
406
        return Status(DB_ERROR, "index is null");
G
groot 已提交
407 408
    }

X
xj.lin 已提交
409
    if (auto file_index = std::dynamic_pointer_cast<BFIndex>(to_merge)) {
410 411
        auto status = index_->Add(file_index->Count(), file_index->GetRawVectors(), file_index->GetRawIds());
        if (!status.ok()) {
X
xj.lin 已提交
412 413
            ENGINE_LOG_ERROR << "Merge: Add Error";
        }
414
        return status;
X
xj.lin 已提交
415
    } else {
G
groot 已提交
416
        return Status(DB_ERROR, "file index type is not idmap");
X
xj.lin 已提交
417
    }
G
groot 已提交
418 419 420
}

ExecutionEnginePtr
G
groot 已提交
421
ExecutionEngineImpl::BuildIndex(const std::string& location, EngineType engine_type) {
X
xj.lin 已提交
422 423 424
    ENGINE_LOG_DEBUG << "Build index file: " << location << " from: " << location_;

    auto from_index = std::dynamic_pointer_cast<BFIndex>(index_);
G
groot 已提交
425
    if (from_index == nullptr) {
G
groot 已提交
426 427 428 429
        ENGINE_LOG_ERROR << "ExecutionEngineImpl: from_index is null, failed to build index";
        return nullptr;
    }

G
groot 已提交
430
    auto to_index = CreatetVecIndex(engine_type);
X
xj.lin 已提交
431
    if (!to_index) {
432
        throw Exception(DB_ERROR, "Unsupported index type");
X
xj.lin 已提交
433 434
    }

X
xiaojun.lin 已提交
435 436 437 438
    TempMetaConf temp_conf;
    temp_conf.gpu_id = gpu_num_;
    temp_conf.dim = Dimension();
    temp_conf.nlist = nlist_;
G
groot 已提交
439
    temp_conf.metric_type = (metric_type_ == MetricType::IP) ? knowhere::METRICTYPE::IP : knowhere::METRICTYPE::L2;
X
xiaojun.lin 已提交
440 441 442 443
    temp_conf.size = Count();

    auto adapter = AdapterMgr::GetInstance().GetAdapter(to_index->GetType());
    auto conf = adapter->Match(temp_conf);
X
xj.lin 已提交
444

G
groot 已提交
445 446 447 448
    auto status = to_index->BuildAll(Count(), from_index->GetRawVectors(), from_index->GetRawIds(), conf);
    if (!status.ok()) {
        throw Exception(DB_ERROR, status.message());
    }
X
xj.lin 已提交
449

G
groot 已提交
450
    return std::make_shared<ExecutionEngineImpl>(to_index, location, engine_type, metric_type_, nlist_);
G
groot 已提交
451 452
}

G
groot 已提交
453
Status
W
wxyu 已提交
454
ExecutionEngineImpl::Search(int64_t n, const float* data, int64_t k, int64_t nprobe, float* distances, int64_t* labels,
J
JinHai-CN 已提交
455
                            bool hybrid) {
456
#if 0
J
JinHai-CN 已提交
457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 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
    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
                    auto config = std::make_shared<knowhere::QuantizerCfg>();
                    config->gpu_id = device_id;
                    config->mode = 2;
                    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);
            }
        }
    }
509
#endif
J
JinHai-CN 已提交
510

G
groot 已提交
511
    if (index_ == nullptr) {
G
groot 已提交
512
        ENGINE_LOG_ERROR << "ExecutionEngineImpl: index is null, failed to search";
G
groot 已提交
513
        return Status(DB_ERROR, "index is null");
G
groot 已提交
514 515
    }

Y
Yu Kun 已提交
516
    ENGINE_LOG_DEBUG << "Search Params: [k]  " << k << " [nprobe] " << nprobe;
X
xiaojun.lin 已提交
517 518 519 520 521 522 523 524 525

    // TODO(linxj): remove here. Get conf from function
    TempMetaConf temp_conf;
    temp_conf.k = k;
    temp_conf.nprobe = nprobe;

    auto adapter = AdapterMgr::GetInstance().GetAdapter(index_->GetType());
    auto conf = adapter->MatchSearch(temp_conf, index_->GetType());

W
wxyu 已提交
526 527 528
    if (hybrid) {
        HybridLoad();
    }
W
wxyu 已提交
529

X
xiaojun.lin 已提交
530
    auto status = index_->Search(n, data, distances, labels, conf);
W
wxyu 已提交
531

W
wxyu 已提交
532 533 534
    if (hybrid) {
        HybridUnset();
    }
W
wxyu 已提交
535

536
    if (!status.ok()) {
X
xj.lin 已提交
537 538
        ENGINE_LOG_ERROR << "Search error";
    }
539
    return status;
G
groot 已提交
540 541
}

G
groot 已提交
542 543
Status
ExecutionEngineImpl::Cache() {
544
    cache::DataObjPtr obj = std::static_pointer_cast<cache::DataObj>(index_);
G
groot 已提交
545
    milvus::cache::CpuCacheMgr::GetInstance()->InsertItem(location_, obj);
G
groot 已提交
546 547 548 549

    return Status::OK();
}

G
groot 已提交
550 551
Status
ExecutionEngineImpl::GpuCache(uint64_t gpu_id) {
552
    cache::DataObjPtr obj = std::static_pointer_cast<cache::DataObj>(index_);
G
groot 已提交
553
    milvus::cache::GpuCacheMgr::GetInstance(gpu_id)->InsertItem(location_, obj);
554 555

    return Status::OK();
Y
Yu Kun 已提交
556 557
}

X
xj.lin 已提交
558
// TODO(linxj): remove.
G
groot 已提交
559 560
Status
ExecutionEngineImpl::Init() {
G
groot 已提交
561
    server::Config& config = server::Config::GetInstance();
562
    Status s = config.GetResourceConfigIndexBuildDevice(gpu_num_);
G
groot 已提交
563
    if (!s.ok()) {
G
groot 已提交
564
        return s;
G
groot 已提交
565
    }
G
groot 已提交
566 567 568 569

    return Status::OK();
}

G
groot 已提交
570 571
}  // namespace engine
}  // namespace milvus