lrc_isa.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #pragma once
  2. #include "lrc.h"
  3. #include "helpers.h"
  4. #include "isa_erasure.h"
  5. extern "C" {
  6. #include <contrib/libs/isa-l/include/erasure_code.h>
  7. }
  8. #include <library/cpp/sse/sse.h>
  9. #include <util/generic/array_ref.h>
  10. #include <functional>
  11. #include <optional>
  12. #include <vector>
  13. namespace NErasure {
  14. template <int DataPartCount, int ParityPartCount, int WordSize, class TCodecTraits>
  15. class TLrcIsa
  16. : public TLrcCodecBase<DataPartCount, ParityPartCount, WordSize, TCodecTraits>
  17. {
  18. static_assert(WordSize == 8, "ISA-l erasure codes support computations only in GF(2^8)");
  19. public:
  20. //! Main blob for storing data.
  21. using TBlobType = typename TCodecTraits::TBlobType;
  22. //! Main mutable blob for decoding data.
  23. using TMutableBlobType = typename TCodecTraits::TMutableBlobType;
  24. static constexpr ui64 RequiredDataAlignment = alignof(ui64);
  25. TLrcIsa()
  26. : TLrcCodecBase<DataPartCount, ParityPartCount, WordSize, TCodecTraits>()
  27. {
  28. EncodeGFTables_.resize(DataPartCount * ParityPartCount * 32, 0);
  29. GeneratorMatrix_.resize((DataPartCount + ParityPartCount) * DataPartCount, 0);
  30. for (int row = 0; row < DataPartCount; ++row) {
  31. GeneratorMatrix_[row * DataPartCount + row] = 1;
  32. }
  33. this->template InitializeGeneratorMatrix<typename decltype(GeneratorMatrix_)::value_type>(
  34. &GeneratorMatrix_[DataPartCount * DataPartCount],
  35. std::bind(&gf_mul_erasure, std::placeholders::_1, std::placeholders::_1));
  36. ec_init_tables(
  37. DataPartCount,
  38. ParityPartCount,
  39. &GeneratorMatrix_.data()[DataPartCount * DataPartCount],
  40. EncodeGFTables_.data());
  41. }
  42. std::vector<TBlobType> Encode(const std::vector<TBlobType>& blocks) const override {
  43. return ISAErasureEncode<DataPartCount, ParityPartCount, TCodecTraits, TBlobType, TMutableBlobType>(EncodeGFTables_, blocks);
  44. }
  45. virtual ~TLrcIsa() = default;
  46. private:
  47. std::vector<TBlobType> FallbackToCodecDecode(
  48. const std::vector<TBlobType>& blocks,
  49. TPartIndexList erasedIndices) const override
  50. {
  51. return ISAErasureDecode<DataPartCount, ParityPartCount, TCodecTraits, TBlobType, TMutableBlobType>(
  52. blocks,
  53. std::move(erasedIndices),
  54. this->GetXorGroups(),
  55. GeneratorMatrix_);
  56. }
  57. std::vector<unsigned char> GeneratorMatrix_;
  58. std::vector<unsigned char> EncodeGFTables_;
  59. };
  60. } // NErasure