simpler is better

This commit is contained in:
Ahmet Inan 2024-03-26 19:15:45 +01:00
commit 270519aa79
2 changed files with 19 additions and 17 deletions

View file

@ -21,7 +21,7 @@ struct CauchyPrimeFieldErasureCoding
return rcp(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)}}$
PF inverse_cauchy_matrix(const int *rows, int i, int j, int n)
PF inverse_cauchy_matrix(const IO *rows, int i, int j, int n)
{
#if 0
PF row_j(rows[j]), col_i(i);
@ -83,9 +83,9 @@ struct CauchyPrimeFieldErasureCoding
temp[i] = add(temp[i], b * PF(a[i]));
}
}
void mac_sub(IO *c, const IO *a, PF b, int len, bool first, bool last)
void mac_sub(IO *c, const IO *a, PF b, IO s, int len, bool first, bool last)
{
int s = a[len], v = PF::P-1;
int v = PF::P-1;
if (first && last) {
for (int i = 0; i < len; i++)
c[i] = (b * PF(a[i] == s ? v : a[i]))();
@ -111,7 +111,7 @@ struct CauchyPrimeFieldErasureCoding
++s;
return s;
}
void encode(const IO *data, IO *block, int block_id, int block_len, int block_cnt)
int encode(const IO *data, IO *block, int block_id, int block_len, int block_cnt)
{
assert(block_id >= block_cnt && block_id < int(PF::P) / 2);
assert(block_len < int(PF::P-1) && block_len <= MAX_LEN);
@ -122,13 +122,13 @@ struct CauchyPrimeFieldErasureCoding
int sub = find_unused(block_len);
for (int i = 0; i < block_len; ++i)
block[i] = temp[i]() == PF::P-1 ? sub : temp[i]();
block[block_len] = sub;
return sub;
}
void decode(IO *data, const IO *blocks, const int *block_ids, int block_idx, int block_len, int block_cnt)
void decode(IO *data, const IO *blocks, const IO *block_subs, const IO *block_ids, int block_idx, int block_len, int block_cnt)
{
for (int k = 0; k < block_cnt; k++) {
PF b_ik = inverse_cauchy_matrix(block_ids, block_idx, k, block_cnt);
mac_sub(data, blocks + (block_len+1) * k, b_ik, block_len, !k, k == block_cnt - 1);
mac_sub(data, blocks + block_len * k, b_ik, block_subs[k], block_len, !k, k == block_cnt - 1);
}
}
};

View file

@ -28,42 +28,44 @@ void cpf_test(int trials)
auto rnd_dat = std::bind(distribution(0, (1 << value_bits) - 1), generator);
while (--trials) {
int block_count = rnd_cnt();
int identifiers_total = PF::P / 2 - block_count;
int idents_total = PF::P / 2 - block_count;
int block_values = rnd_len();
int block_bytes = block_values * value_bytes;
int data_values = block_count * block_values;
int data_bytes = data_values * value_bytes;
IO *subs = new IO[block_count];
IO *orig = new IO[data_values];
IO *data = new IO[data_values];
IO *blocks = new IO[data_values+block_count];
IO *blocks = new IO[data_values];
IO *idents = new IO[idents_total];
for (int i = 0; i < data_values; ++i)
orig[i] = rnd_dat();
auto identifiers = new int[identifiers_total];
for (int i = 0; i < identifiers_total; ++i)
identifiers[i] = block_count + i;
for (int i = 0; i < idents_total; ++i)
idents[i] = block_count + i;
for (int i = 0; i < block_count; i++) {
std::uniform_int_distribution<int> hat(i, identifiers_total - 1);
std::swap(identifiers[i], identifiers[hat(generator)]);
std::uniform_int_distribution<int> hat(i, idents_total - 1);
std::swap(idents[i], idents[hat(generator)]);
}
auto enc_start = std::chrono::system_clock::now();
for (int i = 0; i < block_count; ++i)
crs.encode(orig, blocks + (block_values+1) * i, identifiers[i], block_values, block_count);
subs[i] = crs.encode(orig, blocks + block_values * i, idents[i], block_values, 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_values * i, blocks, identifiers, i, block_values, block_count);
crs.decode(data + block_values * i, blocks, subs, idents, i, block_values, 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_values; ++i)
assert(data[i] == orig[i]);
delete[] identifiers;
delete[] idents;
delete[] blocks;
delete[] orig;
delete[] data;
delete[] subs;
}
}