Cloner.cpp 3.1 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
// 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
C
Cai Yudong 已提交
10
// or implied. See the License for the specific language governing permissions and limitations under the License
J
jinhai 已提交
11

12
#ifdef MILVUS_GPU_VERSION
S
starlord 已提交
13
#include "knowhere/index/vector_index/helpers/Cloner.h"
X
xiaojun.lin 已提交
14 15
#include "knowhere/common/Exception.h"
#include "knowhere/index/vector_index/IndexIDMAP.h"
S
starlord 已提交
16 17 18
#include "knowhere/index/vector_index/IndexIVF.h"
#include "knowhere/index/vector_index/IndexIVFPQ.h"
#include "knowhere/index/vector_index/IndexIVFSQ.h"
C
Cai Yudong 已提交
19 20 21
#include "knowhere/index/vector_index/gpu/GPUIndex.h"
#include "knowhere/index/vector_index/gpu/IndexGPUIVF.h"
#include "knowhere/index/vector_index/gpu/IndexIVFSQHybrid.h"
22
#include "knowhere/index/vector_offset_index/IndexIVF_NM.h"
X
xj.lin 已提交
23

C
Cai Yudong 已提交
24
namespace milvus {
X
xj.lin 已提交
25
namespace knowhere {
X
xiaojun.lin 已提交
26
namespace cloner {
X
xj.lin 已提交
27

28 29 30 31 32 33 34 35 36
void
CopyIndexData(const VecIndexPtr& dst_index, const VecIndexPtr& src_index) {
    /* do real copy */
    auto uids = src_index->GetUids();
    dst_index->SetUids(uids);
    dst_index->SetBlacklist(src_index->GetBlacklist());
    dst_index->SetIndexSize(src_index->IndexSize());
}

C
Cai Yudong 已提交
37 38
VecIndexPtr
CopyGpuToCpu(const VecIndexPtr& index, const Config& config) {
X
xj.lin 已提交
39
    if (auto device_index = std::dynamic_pointer_cast<GPUIndex>(index)) {
C
Cai Yudong 已提交
40
        VecIndexPtr result = device_index->CopyGpuToCpu(config);
41
        CopyIndexData(result, index);
42
        return result;
X
xj.lin 已提交
43 44 45 46 47
    } else {
        KNOWHERE_THROW_MSG("index type is not gpuindex");
    }
}

C
Cai Yudong 已提交
48 49 50
VecIndexPtr
CopyCpuToGpu(const VecIndexPtr& index, const int64_t device_id, const Config& config) {
    VecIndexPtr result;
X
xiaojun.lin 已提交
51
    if (auto device_index = std::dynamic_pointer_cast<IVFSQHybrid>(index)) {
52
        result = device_index->CopyCpuToGpu(device_id, config);
53 54
    } else if (auto cpu_index = std::dynamic_pointer_cast<IVF_NM>(index)) {
        result = cpu_index->CopyCpuToGpu(device_id, config);
55
    } else if (auto device_index = std::dynamic_pointer_cast<GPUIndex>(index)) {
56
        result = device_index->CopyGpuToGpu(device_id, config);
57
    } else if (auto cpu_index = std::dynamic_pointer_cast<IVFSQ>(index)) {
58
        result = cpu_index->CopyCpuToGpu(device_id, config);
X
xj.lin 已提交
59
    } else if (auto cpu_index = std::dynamic_pointer_cast<IVFPQ>(index)) {
60
        result = cpu_index->CopyCpuToGpu(device_id, config);
X
xj.lin 已提交
61
    } else if (auto cpu_index = std::dynamic_pointer_cast<IVF>(index)) {
62
        result = cpu_index->CopyCpuToGpu(device_id, config);
X
xj.lin 已提交
63
    } else if (auto cpu_index = std::dynamic_pointer_cast<IDMAP>(index)) {
64
        result = cpu_index->CopyCpuToGpu(device_id, config);
X
xj.lin 已提交
65
    } else {
C
Cai Yudong 已提交
66
        KNOWHERE_THROW_MSG("this index type not support transfer to gpu");
X
xj.lin 已提交
67
    }
68 69 70
    if (result != nullptr) {
        CopyIndexData(result, index);
    }
71
    return result;
X
xj.lin 已提交
72 73
}

S
starlord 已提交
74 75
}  // namespace cloner
}  // namespace knowhere
C
Cai Yudong 已提交
76
}  // namespace milvus
77
#endif