utils.h 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.

X
xj.lin 已提交
18 19 20
#pragma once

#include <cstdio>
S
starlord 已提交
21
#include <cstdlib>
X
xj.lin 已提交
22
#include <fstream>
S
starlord 已提交
23 24
#include <string>
#include <vector>
X
xj.lin 已提交
25

X
xiaojun.lin 已提交
26
#include "knowhere/adapter/Structure.h"
H
Heisenberg 已提交
27
#include "knowhere/common/Log.h"
X
xj.lin 已提交
28

S
starlord 已提交
29
class DataGen {
X
xj.lin 已提交
30
 protected:
S
starlord 已提交
31 32
    void
    Init_with_default();
X
xj.lin 已提交
33

S
starlord 已提交
34 35
    void
    Generate(const int& dim, const int& nb, const int& nq);
X
xj.lin 已提交
36

S
starlord 已提交
37
    knowhere::DatasetPtr
S
starlord 已提交
38
    GenQuery(const int& nq);
X
xj.lin 已提交
39 40 41 42 43 44 45 46 47

 protected:
    int nb = 10000;
    int nq = 10;
    int dim = 64;
    int k = 10;
    std::vector<float> xb;
    std::vector<float> xq;
    std::vector<int64_t> ids;
S
starlord 已提交
48 49
    knowhere::DatasetPtr base_dataset = nullptr;
    knowhere::DatasetPtr query_dataset = nullptr;
X
xj.lin 已提交
50 51
};

S
starlord 已提交
52 53 54
extern void
GenAll(const int64_t dim, const int64_t& nb, std::vector<float>& xb, std::vector<int64_t>& ids, const int64_t& nq,
       std::vector<float>& xq);
X
xj.lin 已提交
55

S
starlord 已提交
56 57
extern void
GenAll(const int64_t& dim, const int64_t& nb, float* xb, int64_t* ids, const int64_t& nq, float* xq);
X
xj.lin 已提交
58

S
starlord 已提交
59 60
extern void
GenBase(const int64_t& dim, const int64_t& nb, float* xb, int64_t* ids);
X
xj.lin 已提交
61

S
starlord 已提交
62 63
extern void
InitLog();
H
Heisenberg 已提交
64

S
starlord 已提交
65
knowhere::DatasetPtr
S
starlord 已提交
66
generate_dataset(int64_t nb, int64_t dim, float* xb, int64_t* ids);
X
xj.lin 已提交
67

S
starlord 已提交
68
knowhere::DatasetPtr
S
starlord 已提交
69
generate_query_dataset(int64_t nb, int64_t dim, float* xb);
X
xj.lin 已提交
70 71 72 73 74

struct FileIOWriter {
    std::fstream fs;
    std::string name;

S
starlord 已提交
75
    explicit FileIOWriter(const std::string& fname);
X
xj.lin 已提交
76
    ~FileIOWriter();
S
starlord 已提交
77 78
    size_t
    operator()(void* ptr, size_t size);
X
xj.lin 已提交
79 80 81 82 83 84
};

struct FileIOReader {
    std::fstream fs;
    std::string name;

S
starlord 已提交
85
    explicit FileIOReader(const std::string& fname);
X
xj.lin 已提交
86
    ~FileIOReader();
S
starlord 已提交
87 88
    size_t
    operator()(void* ptr, size_t size);
X
xj.lin 已提交
89
};