aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/vespa/searchlib/tensor/tensor_buffer_type_mapper.cpp
blob: 16c2d65d82937af786d548bf07794f01654e1fe3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "tensor_buffer_type_mapper.h"
#include "tensor_buffer_operations.h"
#include <algorithm>
#include <cmath>
#include <limits>

namespace search::tensor {

TensorBufferTypeMapper::TensorBufferTypeMapper()
    : vespalib::datastore::ArrayStoreTypeMapper(),
      _ops(nullptr)
{
}

TensorBufferTypeMapper::TensorBufferTypeMapper(uint32_t max_small_subspaces_type_id, double grow_factor, size_t max_buffer_size, TensorBufferOperations* ops)
    : vespalib::datastore::ArrayStoreTypeMapper(),
      _ops(ops)
{
    _array_sizes.reserve(max_small_subspaces_type_id + 1);
    _array_sizes.emplace_back(0); // type id 0 uses LargeSubspacesBufferType
    uint32_t num_subspaces =  _ops->is_dense() ? 1 : 0;
    size_t prev_array_size = 0u;
    size_t array_size = 0u;
    for (uint32_t type_id = 1; type_id <= max_small_subspaces_type_id; ++type_id) {
        if (type_id > 1) {
            num_subspaces = std::max(num_subspaces +  1, static_cast<uint32_t>(std::floor(num_subspaces * grow_factor)));
        }
        array_size = _ops->get_buffer_size(num_subspaces);
        while (array_size <= prev_array_size) {
            ++num_subspaces;
            array_size = _ops->get_buffer_size(num_subspaces);
        }
        if (array_size > std::numeric_limits<uint32_t>::max() ||
            array_size >= 2 * max_buffer_size) {
            break;
        }
        _array_sizes.emplace_back(array_size);
        if (_ops->is_dense()) {
            break;
        }
        prev_array_size = array_size;
    }
}

TensorBufferTypeMapper::~TensorBufferTypeMapper() = default;

}