Skip to content

Commit

Permalink
Removes template parameter colptr_t from RateAdaptiveCode
Browse files Browse the repository at this point in the history
After reworking RateAdaptiveCode recently to store the mother matrix in mother_pos_varn, the object no longer depends on the type of the column pointer. Constructors using column pointers are now templated for colptr_t instead of the whole type.
  • Loading branch information
adomasbaliuka committed May 28, 2024
1 parent 4c31fea commit f388adf
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 30 deletions.
17 changes: 8 additions & 9 deletions benchmarks_error_rate/code_simulation_helpers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,20 +60,19 @@ namespace LDPC4QKD::CodeSimulationHelpers {
* @return Rate adaptive code
*/
template<typename Bit=bool,
typename colptr_t=std::uint32_t,
typename idx_t=std::uint32_t>
LDPC4QKD::RateAdaptiveCode<colptr_t, idx_t> load_ldpc_from_cscmat(
LDPC4QKD::RateAdaptiveCode< idx_t> load_ldpc_from_cscmat(
const std::string &cscmat_file_path, const std::string &rate_adaption_file_path=""
) {
auto pair = LDPC4QKD::read_matrix_from_cscmat<colptr_t, idx_t>(cscmat_file_path);
auto pair = LDPC4QKD::read_matrix_from_cscmat<idx_t>(cscmat_file_path);
auto colptr = pair.first;
auto row_idx = pair.second;

if(rate_adaption_file_path.empty()) {
return LDPC4QKD::RateAdaptiveCode<colptr_t, idx_t>(colptr, row_idx);
return LDPC4QKD::RateAdaptiveCode<idx_t>(colptr, row_idx);
} else {
std::vector<idx_t> rows_to_combine = read_rate_adaption_from_csv<idx_t>(rate_adaption_file_path);
return LDPC4QKD::RateAdaptiveCode<colptr_t, idx_t>(colptr, row_idx, rows_to_combine);
return LDPC4QKD::RateAdaptiveCode<idx_t>(colptr, row_idx, rows_to_combine);
}
}

Expand All @@ -94,7 +93,7 @@ namespace LDPC4QKD::CodeSimulationHelpers {
template<typename Bit=bool,
typename colptr_t=std::uint32_t,
typename idx_t=std::uint32_t>
LDPC4QKD::RateAdaptiveCode<colptr_t, idx_t> load_ldpc_from_json(
LDPC4QKD::RateAdaptiveCode<idx_t> load_ldpc_from_json(
const std::string &json_file_path, const std::string &rate_adaption_file_path = ""
) {
try {
Expand All @@ -113,10 +112,10 @@ namespace LDPC4QKD::CodeSimulationHelpers {
std::vector<idx_t> rowval = data["rowval"];

if (rate_adaption_file_path.empty()) {
return LDPC4QKD::RateAdaptiveCode<colptr_t, idx_t>(colptr, rowval);
return LDPC4QKD::RateAdaptiveCode<idx_t>(colptr, rowval);
} else {
std::vector<idx_t> rows_to_combine = read_rate_adaption_from_csv<idx_t>(rate_adaption_file_path);
return LDPC4QKD::RateAdaptiveCode<colptr_t, idx_t>(colptr, rowval, rows_to_combine);
return LDPC4QKD::RateAdaptiveCode<idx_t>(colptr, rowval, rows_to_combine);
}
} else if (data["format"] == "COMPRESSED_SPARSE_COLUMN") { // quasi-cyclic exponents stored
// std::vector<std::uint64_t> colptr = data["colptr"];
Expand Down Expand Up @@ -155,7 +154,7 @@ namespace LDPC4QKD::CodeSimulationHelpers {
template<typename Bit=bool,
typename colptr_t=std::uint32_t,
typename idx_t=std::uint32_t>
LDPC4QKD::RateAdaptiveCode<colptr_t, idx_t> load_ldpc(
LDPC4QKD::RateAdaptiveCode<idx_t> load_ldpc(
const std::string &file_path, const std::string &rate_adaption_file_path=""
) {
std::filesystem::path filePath = file_path;
Expand Down
4 changes: 2 additions & 2 deletions src/encoder_advanced.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ namespace LDPC4QKD {
//! \tparam N internal implementation detail, need not use.
//! \param code_id integer index into tuple of codes. Make sure both sides agree on these!
template<std::size_t N = 0>
std::size_t get_input_size(std::size_t code_id) {
constexpr std::size_t get_input_size(std::size_t code_id) {
if (N == code_id) {
return std::get<N>(all_encoders_tuple).get_input_size();
}
Expand All @@ -350,7 +350,7 @@ namespace LDPC4QKD {
//! \tparam N internal implementation detail, need not use.
//! \param code_id integer index into tuple of codes. Make sure both sides agree on these!
template<std::size_t N = 0>
std::size_t get_output_size(std::size_t code_id) {
constexpr std::size_t get_output_size(std::size_t code_id) {
if (N == code_id) {
return std::get<N>(all_encoders_tuple).get_output_size();
}
Expand Down
36 changes: 21 additions & 15 deletions src/rate_adaptive_code.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,25 +55,24 @@ namespace LDPC4QKD {
*
* (TODO use concept `std::unsigned_integral` when using C++20)
*
* @tparam colptr_t unsigned integert type that fits ("number of non-zero matrix entries" + 1)
* @tparam idx_t unsigned integer type fitting number of columns N (thus also number of rows M)
*/
template<typename colptr_t=std::uint32_t,
typename idx_t=std::uint16_t>
template<typename idx_t=std::uint16_t>
class RateAdaptiveCode {
public:
// ------------------------------------------------------------------------------------------------ type aliases
using MatrixIndex = idx_t;
using ColumnPointer = colptr_t;

// ------------------------------------------------------------------------------------------------ constructors
/*!
* Constructor for using the code without rate adaption.
* The parity check matrix matrix is stored using Compressed Sparse Column (CSC) format.
*
* @tparam colptr_t unsigned integer type that fits ("number of non-zero matrix entries" + 1)
* @param colptr column pointer array for specifying mother parity check matrix.
* @param rowIdx row index array for specifying mother parity check matrix.
*/
template <typename colptr_t>
RateAdaptiveCode(const std::vector<colptr_t> &colptr, const std::vector<idx_t> &rowIdx)
: n_mother_rows(*std::max_element(rowIdx.begin(), rowIdx.end()) + 1u),
n_cols(colptr.size() - 1),
Expand All @@ -96,13 +95,15 @@ namespace LDPC4QKD {
*
* note: there used to be a parameter `do_elimination_check` to check for repeated node indices after rate adaption.
* Such indices are now removed during `recompute_pos_vn_cn`. Consequentially, node eliminations are allowed.
* TODO reconsider this and remove commented-out function `has_var_node_eliminations` below
* TODO make sure this is correct
*
* @tparam colptr_t unsigned integer type that fits ("number of non-zero matrix entries" + 1)
* @param colptr column pointer array for specifying mother parity check matrix.
* @param rowIdx row index array for specifying mother parity check matrix.
* @param rows_to_combine_rate_adapt array of mother-matrix line indices to be combined for rate adaption
* @param initial_row_combs number of line indices to combine initially
*/
template <typename colptr_t>
RateAdaptiveCode(std::vector<colptr_t> colptr,
std::vector<idx_t> rowIdx,
std::vector<idx_t> rows_to_combine_rate_adapt,
Expand All @@ -123,11 +124,6 @@ namespace LDPC4QKD {

// compute current `pos_varn` and `pos_checkn` from `mother_pos_varn`
recompute_pos_vn_cn(initial_row_combs);

// if (do_elimination_check && has_var_node_eliminations()) {
// throw std::domain_error("Given rate adaption implies variable node eliminations. "
// "Rate adaption with eliminations degrades performance. Do not use!");
// }
}

/*!
Expand Down Expand Up @@ -414,17 +410,27 @@ namespace LDPC4QKD {
}
}

/// compute `mother_pos_varn` from `colptr` and `rowIdx`
/*!
* compute `mother_pos_varn` from `colptr` and `rowIdx` for a given LDPC matrix stored in compressed sparse column
* format. "Values" array is omitted because all values are assumed to be 1 (binary LDPC matrix).
*
* @tparam idx_t unsigned integer type fitting number of columns N (thus also number of rows M)
* @tparam colptr_t unsigned integer type that fits ("number of non-zero matrix entries" + 1)
* @param colptr column pointer array for specifying mother parity check matrix.
* @param rowIdx row index array for specifying mother parity check matrix.
* @return Input variable nodes to each check node (of the Tanner graph)
*/
template <typename colptr_t>
static std::vector<std::vector<idx_t>> compute_mother_pos_varn(
const std::vector<colptr_t> &colptr,
const std::vector<idx_t> &rowIdx) {
// number of columns in full matrix represented by given compressed sparse column (CSC) storage
const auto n_cols = colptr.size() - 1;
const auto nCols = colptr.size() - 1;
// number of rows in full matrix represented by given compressed sparse column (CSC) storage
const auto n_mother_rows = *std::max_element(rowIdx.begin(), rowIdx.end()) + 1u;
const auto nMotherRows = *std::max_element(rowIdx.begin(), rowIdx.end()) + 1u;

std::vector<std::vector<idx_t>> pos_varn_tmp{n_mother_rows, std::vector<idx_t>{}};
for (idx_t col = 0; col < n_cols; col++) {
std::vector<std::vector<idx_t>> pos_varn_tmp{nMotherRows, std::vector<idx_t>{}};
for (idx_t col = 0; col < nCols; col++) {
for (auto j = colptr[col]; j < colptr[col + 1u]; j++) {
pos_varn_tmp[rowIdx[j]].push_back(col);
}
Expand Down
5 changes: 2 additions & 3 deletions tests/test_rate_adaptive_code.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ TEST(rate_adaptive_code_from_colptr_rowIdx, equals_not_equals_operators) {

TEST(rate_adaptive_code_from_decoder, obtain_from_advanced_encoder_behaviour) {
std::vector<std::uint16_t> rows_to_combine{}; // not used here!
RateAdaptiveCode<std::uint32_t, std::uint16_t> H1(encoder2.get_pos_varn(), rows_to_combine);
RateAdaptiveCode<std::uint16_t> H1(encoder2.get_pos_varn(), rows_to_combine);

auto H2 = get_code_big_wra();

Expand All @@ -336,7 +336,6 @@ TEST(rate_adaptive_code_from_decoder, obtain_from_advanced_encoder_behaviour) {
std::vector<std::uint8_t> in = get_bitstring<std::uint8_t>(H2.getNCols());
std::vector<std::uint8_t> out(H2.get_n_rows_mother_matrix());

write_vector_to_csv("tmp_randkey_6144.csv", in, true);
std::cout << "input hash: " << hash_vector(in) << std::endl;
H2.encode_no_ra(in, out);
std::cout << "output hash: " << hash_vector(out) << std::endl;
Expand All @@ -347,7 +346,7 @@ TEST(rate_adaptive_code_from_decoder, obtain_from_advanced_encoder_behaviour) {

TEST(rate_adaptive_code_from_decoder, obtain_from_advanced_encoder_equals) {
std::vector<std::uint16_t> rows_to_combine(AutogenRateAdapt::rows.begin(), AutogenRateAdapt::rows.end());
RateAdaptiveCode<std::uint32_t, std::uint16_t> H1(encoder2.get_pos_varn(), rows_to_combine);
RateAdaptiveCode<std::uint16_t> H1(encoder2.get_pos_varn(), rows_to_combine);

// TODO add random rate adaption for comparison
auto H2 = get_code_big_wra();
Expand Down
2 changes: 1 addition & 1 deletion tests/test_read_ldpc_from_files.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace {
auto get_code_big_nora() {
std::vector<std::uint32_t> colptr(AutogenLDPC::colptr.begin(), AutogenLDPC::colptr.end());
std::vector<std::uint32_t> row_idx(AutogenLDPC::row_idx.begin(), AutogenLDPC::row_idx.end());
return RateAdaptiveCode<uint32_t, uint32_t>(colptr, row_idx);
return RateAdaptiveCode<uint32_t>(colptr, row_idx);
}

}
Expand Down

0 comments on commit f388adf

Please sign in to comment.