better not to be clever here

This commit is contained in:
Ahmet Inan 2023-06-07 12:27:01 +02:00
commit 272440d749
2 changed files with 28 additions and 28 deletions

View file

@ -30,7 +30,7 @@ struct CauchyReedSolomonErasureCoding
__attribute__((flatten))
IndexType cauchy_matrix(int i, int j)
{
ValueType row(i), col(ValueType::N - j);
ValueType row(i), col(j);
return rcp(index(row + col));
}
// $b_{ij} = \frac{\prod_{k=1}^{n}{(x_j + y_k)(x_k + y_i)}}{(x_j + y_i)\prod_{k \ne j}^{n}{(x_j - x_k)}\prod_{k \ne i}^{n}{(y_i - y_k)}}$
@ -38,10 +38,10 @@ struct CauchyReedSolomonErasureCoding
IndexType inverse_cauchy_matrix(const ValueType *rows, int i, int j, int n)
{
#if 0
ValueType col_i(ValueType::N - i);
ValueType col_i(i);
IndexType prod_xy(0), prod_x(0), prod_y(0);
for (int k = 0; k < n; k++) {
ValueType col_k(ValueType::N - k);
ValueType col_k(k);
prod_xy *= index(rows[j] + col_k) * index(rows[k] + col_i);
if (k != j)
prod_x *= index(rows[j] - rows[k]);
@ -50,11 +50,11 @@ struct CauchyReedSolomonErasureCoding
}
return prod_xy / (index(rows[j] + col_i) * prod_x * prod_y);
#else
ValueType col_i(ValueType::N - i);
ValueType col_i(i);
if (j == 0) {
IndexType num(0), den(0);
for (int k = 0; k < n; k++) {
ValueType col_k(ValueType::N - k);
ValueType col_k(k);
num *= index(rows[k] + col_i);
if (k != i)
den *= index(col_i - col_k);
@ -64,7 +64,7 @@ struct CauchyReedSolomonErasureCoding
}
IndexType num(row_num), den(row_den);
for (int k = 0; k < n; k++) {
ValueType col_k(ValueType::N - k);
ValueType col_k(k);
num *= index(rows[j] + col_k);
if (k != j)
den *= index(rows[j] - rows[k]);
@ -297,38 +297,38 @@ struct CauchyReedSolomonErasureCoding
c[i] = fma(b, a[i], c[i]);
}
}
void encode(const ValueType *data, ValueType *block, int block_num, int block_len, int block_cnt)
void encode(const ValueType *data, ValueType *block, int block_id, int block_len, int block_cnt)
{
assert(block_num <= ValueType::N - block_cnt);
assert(block_id >= block_cnt && block_id <= ValueType::N);
for (int k = 0; k < block_cnt; k++) {
IndexType a_ik = cauchy_matrix(block_num, k);
IndexType a_ik = cauchy_matrix(block_id, k);
multiply_accumulate(block, data + block_len * k, a_ik, block_len, !k);
}
}
void decode(ValueType *data, const ValueType *blocks, const ValueType *block_nums, int block_num, int block_len, int block_cnt)
void decode(ValueType *data, const ValueType *blocks, const ValueType *block_ids, int block_id, int block_len, int block_cnt)
{
for (int k = 0; k < block_cnt; k++) {
IndexType b_ik = inverse_cauchy_matrix(block_nums, block_num, k, block_cnt);
IndexType b_ik = inverse_cauchy_matrix(block_ids, block_id, k, block_cnt);
multiply_accumulate(data, blocks + block_len * k, b_ik, block_len, !k);
}
}
void encode(const value_type *data, value_type *block, int block_num, int block_len, int block_cnt)
void encode(const value_type *data, value_type *block, int block_id, int block_len, int block_cnt)
{
encode(reinterpret_cast<const ValueType *>(data), reinterpret_cast<ValueType *>(block), block_num, block_len, block_cnt);
encode(reinterpret_cast<const ValueType *>(data), reinterpret_cast<ValueType *>(block), block_id, block_len, block_cnt);
}
void decode(value_type *data, const value_type *blocks, const value_type *block_nums, int block_num, int block_len, int block_cnt)
void decode(value_type *data, const value_type *blocks, const value_type *block_ids, int block_id, int block_len, int block_cnt)
{
decode(reinterpret_cast<ValueType *>(data), reinterpret_cast<const ValueType *>(blocks), reinterpret_cast<const ValueType *>(block_nums), block_num, block_len, block_cnt);
decode(reinterpret_cast<ValueType *>(data), reinterpret_cast<const ValueType *>(blocks), reinterpret_cast<const ValueType *>(block_ids), block_id, block_len, block_cnt);
}
void encode(const void *data, void *block, int block_number, int block_bytes, int block_count)
void encode(const void *data, void *block, int block_identifier, int block_bytes, int block_count)
{
assert(block_bytes % sizeof(value_type) == 0);
encode(reinterpret_cast<const value_type *>(data), reinterpret_cast<value_type *>(block), block_number, block_bytes / sizeof(value_type), block_count);
encode(reinterpret_cast<const value_type *>(data), reinterpret_cast<value_type *>(block), block_identifier, block_bytes / sizeof(value_type), block_count);
}
void decode(void *data, const void *blocks, const value_type *block_numbers, int block_number, int block_bytes, int block_count)
void decode(void *data, const void *blocks, const value_type *block_identifiers, int block_identifier, int block_bytes, int block_count)
{
assert(block_bytes % sizeof(value_type) == 0);
decode(reinterpret_cast<value_type *>(data), reinterpret_cast<const value_type *>(blocks), block_numbers, block_number, block_bytes / sizeof(value_type), block_count);
decode(reinterpret_cast<value_type *>(data), reinterpret_cast<const value_type *>(blocks), block_identifiers, block_identifier, block_bytes / sizeof(value_type), block_count);
}
};

View file

@ -30,7 +30,7 @@ void crs_test(int trials)
auto rnd_dat = std::bind(distribution(0, 255), generator);
while (--trials) {
int block_count = rnd_cnt();
int numbers_total = GF::Q - block_count;
int identifiers_total = GF::Q - block_count;
int block_bytes = rnd_len() * sizeof(typename GF::value_type) * SIMD;
int data_bytes = block_count * block_bytes;
uint8_t *orig = reinterpret_cast<uint8_t *>(std::aligned_alloc(SIMD, data_bytes));
@ -38,29 +38,29 @@ void crs_test(int trials)
uint8_t *blocks = reinterpret_cast<uint8_t *>(std::aligned_alloc(SIMD, data_bytes));
for (int i = 0; i < data_bytes; ++i)
orig[i] = rnd_dat();
auto numbers = new typename GF::value_type[numbers_total];
for (int i = 0; i < numbers_total; ++i)
numbers[i] = i;
auto identifiers = new typename GF::value_type[identifiers_total];
for (int i = 0; i < identifiers_total; ++i)
identifiers[i] = block_count + i;
for (int i = 0; i < block_count; i++) {
std::uniform_int_distribution<int> hat(i, numbers_total - 1);
std::swap(numbers[i], numbers[hat(generator)]);
std::uniform_int_distribution<int> hat(i, identifiers_total - 1);
std::swap(identifiers[i], identifiers[hat(generator)]);
}
auto enc_start = std::chrono::system_clock::now();
for (int i = 0; i < block_count; ++i)
crs.encode(orig, blocks + block_bytes * i, numbers[i], block_bytes, block_count);
crs.encode(orig, blocks + block_bytes * i, identifiers[i], block_bytes, block_count);
auto enc_end = std::chrono::system_clock::now();
auto enc_usec = std::chrono::duration_cast<std::chrono::microseconds>(enc_end - enc_start);
double enc_mbs = double(data_bytes) / enc_usec.count();
auto dec_start = std::chrono::system_clock::now();
for (int i = 0; i < block_count; ++i)
crs.decode(data + block_bytes * i, blocks, numbers, i, block_bytes, block_count);
crs.decode(data + block_bytes * i, blocks, identifiers, i, block_bytes, block_count);
auto dec_end = std::chrono::system_clock::now();
auto dec_usec = std::chrono::duration_cast<std::chrono::microseconds>(dec_end - dec_start);
double dec_mbs = double(data_bytes) / dec_usec.count();
std::cout << "block count = " << block_count << ", block size = " << block_bytes << " bytes, encoding speed = " << enc_mbs << " megabyte per second, decoding speed = " << dec_mbs << " megabyte per second" << std::endl;
for (int i = 0; i < data_bytes; ++i)
assert(data[i] == orig[i]);
delete[] numbers;
delete[] identifiers;
std::free(blocks);
std::free(orig);
std::free(data);