Coverage Report

Created: 2025-11-04 21:30

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/root/doris/contrib/faiss/faiss/utils/AlignedTable.h
Line
Count
Source
1
/*
2
 * Copyright (c) Meta Platforms, Inc. and affiliates.
3
 *
4
 * This source code is licensed under the MIT license found in the
5
 * LICENSE file in the root directory of this source tree.
6
 */
7
8
#pragma once
9
10
#include <cassert>
11
#include <cstdint>
12
#include <cstdlib>
13
#include <cstring>
14
15
#include <algorithm>
16
17
#include <faiss/impl/platform_macros.h>
18
19
namespace faiss {
20
21
template <int A = 32>
22
0
inline bool is_aligned_pointer(const void* x) {
23
0
    size_t xi = (size_t)x;
24
0
    return xi % A == 0;
25
0
}
26
27
// class that manages suitably aligned arrays for SIMD
28
// T should be a POV type. The default alignment is 32 for AVX
29
template <class T, int A = 32>
30
struct AlignedTableTightAlloc {
31
    T* ptr;
32
    size_t numel;
33
34
0
    AlignedTableTightAlloc() : ptr(nullptr), numel(0) {}
Unexecuted instantiation: _ZN5faiss22AlignedTableTightAllocIfLi32EEC2Ev
Unexecuted instantiation: _ZN5faiss22AlignedTableTightAllocItLi32EEC2Ev
Unexecuted instantiation: _ZN5faiss22AlignedTableTightAllocIhLi32EEC2Ev
35
36
0
    explicit AlignedTableTightAlloc(size_t n) : ptr(nullptr), numel(0) {
37
0
        resize(n);
38
0
    }
Unexecuted instantiation: _ZN5faiss22AlignedTableTightAllocIhLi32EEC2Em
Unexecuted instantiation: _ZN5faiss22AlignedTableTightAllocIfLi32EEC2Em
39
40
    size_t itemsize() const {
41
        return sizeof(T);
42
    }
43
44
0
    void resize(size_t n) {
45
0
        if (numel == n) {
46
0
            return;
47
0
        }
48
0
        T* new_ptr;
49
0
        if (n > 0) {
50
0
            int ret = posix_memalign((void**)&new_ptr, A, n * sizeof(T));
51
0
            if (ret != 0) {
52
0
                throw std::bad_alloc();
53
0
            }
54
0
            if (numel > 0) {
55
0
                memcpy(new_ptr, ptr, sizeof(T) * std::min(numel, n));
56
0
            }
57
0
        } else {
58
0
            new_ptr = nullptr;
59
0
        }
60
0
        numel = n;
61
0
        posix_memalign_free(ptr);
62
0
        ptr = new_ptr;
63
0
    }
Unexecuted instantiation: _ZN5faiss22AlignedTableTightAllocIfLi32EE6resizeEm
Unexecuted instantiation: _ZN5faiss22AlignedTableTightAllocIhLi32EE6resizeEm
Unexecuted instantiation: _ZN5faiss22AlignedTableTightAllocItLi32EE6resizeEm
64
65
0
    void clear() {
66
0
        if (numel > 0) {
67
0
            memset(ptr, 0, nbytes());
68
0
        }
69
0
    }
70
    size_t size() const {
71
        return numel;
72
    }
73
0
    size_t nbytes() const {
74
0
        return numel * sizeof(T);
75
0
    }
76
77
0
    T* get() {
78
0
        return ptr;
79
0
    }
Unexecuted instantiation: _ZN5faiss22AlignedTableTightAllocIfLi32EE3getEv
Unexecuted instantiation: _ZN5faiss22AlignedTableTightAllocIhLi32EE3getEv
Unexecuted instantiation: _ZN5faiss22AlignedTableTightAllocItLi32EE3getEv
80
0
    const T* get() const {
81
0
        return ptr;
82
0
    }
Unexecuted instantiation: _ZNK5faiss22AlignedTableTightAllocIfLi32EE3getEv
Unexecuted instantiation: _ZNK5faiss22AlignedTableTightAllocIhLi32EE3getEv
83
    T* data() {
84
        return ptr;
85
    }
86
    const T* data() const {
87
        return ptr;
88
    }
89
    T& operator[](size_t i) {
90
        return ptr[i];
91
    }
92
    T operator[](size_t i) const {
93
        return ptr[i];
94
    }
95
96
0
    ~AlignedTableTightAlloc() {
97
0
        posix_memalign_free(ptr);
98
0
    }
Unexecuted instantiation: _ZN5faiss22AlignedTableTightAllocIfLi32EED2Ev
Unexecuted instantiation: _ZN5faiss22AlignedTableTightAllocIhLi32EED2Ev
Unexecuted instantiation: _ZN5faiss22AlignedTableTightAllocItLi32EED2Ev
99
100
    AlignedTableTightAlloc<T, A>& operator=(
101
0
            const AlignedTableTightAlloc<T, A>& other) {
102
0
        resize(other.numel);
103
0
        if (numel > 0) {
104
0
            memcpy(ptr, other.ptr, sizeof(T) * numel);
105
0
        }
106
0
        return *this;
107
0
    }
Unexecuted instantiation: _ZN5faiss22AlignedTableTightAllocIfLi32EEaSERKS1_
Unexecuted instantiation: _ZN5faiss22AlignedTableTightAllocIhLi32EEaSERKS1_
108
109
    AlignedTableTightAlloc(const AlignedTableTightAlloc<T, A>& other)
110
0
            : ptr(nullptr), numel(0) {
111
0
        *this = other;
112
0
    }
Unexecuted instantiation: _ZN5faiss22AlignedTableTightAllocIfLi32EEC2ERKS1_
Unexecuted instantiation: _ZN5faiss22AlignedTableTightAllocIhLi32EEC2ERKS1_
113
};
114
115
// same as AlignedTableTightAlloc, but with geometric re-allocation
116
template <class T, int A = 32>
117
struct AlignedTable {
118
    AlignedTableTightAlloc<T, A> tab;
119
    size_t numel = 0;
120
121
0
    static size_t round_capacity(size_t n) {
122
0
        if (n == 0) {
123
0
            return 0;
124
0
        }
125
0
        if (n < 8 * A) {
126
0
            return 8 * A;
127
0
        }
128
0
        size_t capacity = 8 * A;
129
0
        while (capacity < n) {
130
0
            capacity *= 2;
131
0
        }
132
0
        return capacity;
133
0
    }
Unexecuted instantiation: _ZN5faiss12AlignedTableIfLi32EE14round_capacityEm
Unexecuted instantiation: _ZN5faiss12AlignedTableIhLi32EE14round_capacityEm
Unexecuted instantiation: _ZN5faiss12AlignedTableItLi32EE14round_capacityEm
134
135
0
    AlignedTable() {}
Unexecuted instantiation: _ZN5faiss12AlignedTableIfLi32EEC2Ev
Unexecuted instantiation: _ZN5faiss12AlignedTableItLi32EEC2Ev
Unexecuted instantiation: _ZN5faiss12AlignedTableIhLi32EEC2Ev
136
137
0
    explicit AlignedTable(size_t n) : tab(round_capacity(n)), numel(n) {}
Unexecuted instantiation: _ZN5faiss12AlignedTableIhLi32EEC2Em
Unexecuted instantiation: _ZN5faiss12AlignedTableIfLi32EEC2Em
138
139
    size_t itemsize() const {
140
        return sizeof(T);
141
    }
142
143
0
    void resize(size_t n) {
144
0
        tab.resize(round_capacity(n));
145
0
        numel = n;
146
0
    }
Unexecuted instantiation: _ZN5faiss12AlignedTableIfLi32EE6resizeEm
Unexecuted instantiation: _ZN5faiss12AlignedTableIhLi32EE6resizeEm
Unexecuted instantiation: _ZN5faiss12AlignedTableItLi32EE6resizeEm
147
148
0
    void clear() {
149
0
        tab.clear();
150
0
    }
151
0
    size_t size() const {
152
0
        return numel;
153
0
    }
Unexecuted instantiation: _ZNK5faiss12AlignedTableIhLi32EE4sizeEv
Unexecuted instantiation: _ZNK5faiss12AlignedTableIfLi32EE4sizeEv
154
0
    size_t nbytes() const {
155
0
        return numel * sizeof(T);
156
0
    }
157
158
0
    T* get() {
159
0
        return tab.get();
160
0
    }
Unexecuted instantiation: _ZN5faiss12AlignedTableIhLi32EE3getEv
Unexecuted instantiation: _ZN5faiss12AlignedTableItLi32EE3getEv
Unexecuted instantiation: _ZN5faiss12AlignedTableIfLi32EE3getEv
161
0
    const T* get() const {
162
0
        return tab.get();
163
0
    }
Unexecuted instantiation: _ZNK5faiss12AlignedTableIhLi32EE3getEv
Unexecuted instantiation: _ZNK5faiss12AlignedTableIfLi32EE3getEv
164
0
    T* data() {
165
0
        return tab.get();
166
0
    }
Unexecuted instantiation: _ZN5faiss12AlignedTableIfLi32EE4dataEv
Unexecuted instantiation: _ZN5faiss12AlignedTableIhLi32EE4dataEv
167
0
    const T* data() const {
168
0
        return tab.get();
169
0
    }
Unexecuted instantiation: _ZNK5faiss12AlignedTableIfLi32EE4dataEv
Unexecuted instantiation: _ZNK5faiss12AlignedTableIhLi32EE4dataEv
170
0
    T& operator[](size_t i) {
171
0
        return tab.ptr[i];
172
0
    }
Unexecuted instantiation: _ZN5faiss12AlignedTableIfLi32EEixEm
Unexecuted instantiation: _ZN5faiss12AlignedTableItLi32EEixEm
Unexecuted instantiation: _ZN5faiss12AlignedTableIhLi32EEixEm
173
    T operator[](size_t i) const {
174
        return tab.ptr[i];
175
    }
176
177
    // assign and copy constructor should work as expected
178
};
179
180
} // namespace faiss