aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/erasure/lrc_isa.h
blob: 0255577ce76eda5ef0eee20fbe33612257f8536d (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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#pragma once

#include "lrc.h"
#include "helpers.h"

#include "isa_erasure.h"

extern "C" {
    #include <contrib/libs/isa-l/include/erasure_code.h>
}

#include <library/cpp/sse/sse.h>

#include <util/generic/array_ref.h>

#include <functional>
#include <optional>
#include <vector>

namespace NErasure {

template <int DataPartCount, int ParityPartCount, int WordSize, class TCodecTraits>
class TLrcIsa
    : public TLrcCodecBase<DataPartCount, ParityPartCount, WordSize, TCodecTraits>
{
    static_assert(WordSize == 8, "ISA-l erasure codes support computations only in GF(2^8)");
public:
    //! Main blob for storing data.
    using TBlobType = typename TCodecTraits::TBlobType;
    //! Main mutable blob for decoding data.
    using TMutableBlobType = typename TCodecTraits::TMutableBlobType;

    static constexpr ui64 RequiredDataAlignment = alignof(ui64);

    TLrcIsa()
        : TLrcCodecBase<DataPartCount, ParityPartCount, WordSize, TCodecTraits>()
    {
        EncodeGFTables_.resize(DataPartCount * ParityPartCount * 32, 0);
        GeneratorMatrix_.resize((DataPartCount + ParityPartCount) * DataPartCount, 0);

        for (int row = 0; row < DataPartCount; ++row) {
            GeneratorMatrix_[row * DataPartCount + row] = 1;
        }
        this->template InitializeGeneratorMatrix<typename decltype(GeneratorMatrix_)::value_type>(
            &GeneratorMatrix_[DataPartCount * DataPartCount],
            std::bind(&gf_mul_erasure, std::placeholders::_1, std::placeholders::_1));

        ec_init_tables(
            DataPartCount,
            ParityPartCount,
            &GeneratorMatrix_.data()[DataPartCount * DataPartCount],
            EncodeGFTables_.data());
    }

    std::vector<TBlobType> Encode(const std::vector<TBlobType>& blocks) const override {
        return ISAErasureEncode<DataPartCount, ParityPartCount, TCodecTraits, TBlobType, TMutableBlobType>(EncodeGFTables_, blocks);
    }

    virtual ~TLrcIsa() = default;

private:
    std::vector<TBlobType> FallbackToCodecDecode(
        const std::vector<TBlobType>& blocks,
        TPartIndexList erasedIndices) const override
    {
        return ISAErasureDecode<DataPartCount, ParityPartCount, TCodecTraits, TBlobType, TMutableBlobType>(
            blocks,
            std::move(erasedIndices),
            this->GetXorGroups(),
            GeneratorMatrix_);
    }

    std::vector<unsigned char> GeneratorMatrix_;
    std::vector<unsigned char> EncodeGFTables_;
};

} // NErasure