Compare commits

...

4 Commits

Author SHA1 Message Date
Miguel M c7fe98e620 Party swapping was broken 2023-05-08 12:50:10 +01:00
Miguel M 30f9cdc120 Merge the permutations back into the Cg-P conversion
As promised.
2023-05-04 19:49:43 +01:00
Miguel M 18940c8fb8 Split the permutation into its own step
(Rather than having, as before, the permutation merged into the Cg-P
step.) I'll revert this commit in a moment, to make sure the issue
really was just with the matrix parsing.
2023-05-04 19:45:42 +01:00
Miguel M 3ee003e55e Off by one in writing the last entry when parsing input 2023-05-04 19:41:32 +01:00
3 changed files with 53 additions and 41 deletions

View File

@ -39,8 +39,8 @@
// permutation.
//
// `p` isn't required to be initialized, only allocated.
void FromCgToP(size_t a_out, size_t b_out, size_t a_in, size_t b_in,
const data_t* restrict cg, data_t* restrict p,
void FromCgToP(const size_t a_out, const size_t b_out, const size_t a_in,
const size_t b_in, const data_t* restrict cg, data_t* restrict p,
const size_t* a_in_perm, const size_t* b_in_perm,
const permutation_generator_t* restrict a_out_perms,
const permutation_generator_t* restrict b_out_perms);
@ -48,8 +48,9 @@ void FromCgToP(size_t a_out, size_t b_out, size_t a_in, size_t b_in,
// Convert from P representation to CG representation.
//
// `cg` isn't required to be initialized, only allocated.
void FromPToCg(size_t a_out, size_t b_out, size_t a_in, size_t b_in,
data_t* restrict cg, const data_t* restrict p);
void FromPToCg(const size_t a_out, const size_t b_out, const size_t a_in,
const size_t b_in, data_t* restrict cg,
const data_t* restrict p);
// Swaps the input labels of the two parties, and the output labels of the two
// parties.
@ -57,7 +58,7 @@ void FromPToCg(size_t a_out, size_t b_out, size_t a_in, size_t b_in,
// This function only makes sense if `a_out == b_out` and `a_in ==
// b_in`. Calling it otherwise may be undefined behaviour. `p` is modified in
// place.
void PSwapParties(size_t out, size_t in, data_t* p);
void PSwapParties(const size_t out, const size_t in, data_t* p);
// Debug print a CG row.
void PrintCg(size_t a_out, size_t b_out, size_t a_in, size_t b_in,
@ -69,6 +70,6 @@ void PrintP(size_t a_out, size_t b_out, size_t a_in, size_t b_in,
// Print a P row as if it was in the input format.
void PrintPRaw(size_t a_out, size_t b_out, size_t a_in, size_t b_in,
const data_t* p);
const data_t* p);
#endif

View File

@ -49,18 +49,21 @@ static inline size_t BOutPermuted(size_t b_out_i, size_t b_in_i,
return b_out_perms[b_in_i].permutation[b_out_i];
}
void FromCgToP(size_t a_out, size_t b_out, size_t a_in, size_t b_in,
const data_t *restrict cg, data_t *restrict p,
void FromCgToP(const size_t a_out, const size_t b_out, const size_t a_in,
const size_t b_in, const data_t *restrict cg, data_t *restrict p,
const size_t *a_in_perm, const size_t *b_in_perm,
const permutation_generator_t *restrict a_out_perms,
const permutation_generator_t *restrict b_out_perms) {
// size_t p_row_size = a_out * a_in * b_out * b_in;
size_t p_row_size = a_out * a_in * b_out * b_in;
size_t cg_row_size = ((a_out - 1) * (b_out - 1) * a_in * b_in +
(a_out - 1) * a_in + (b_out - 1) * b_in) +
1; // +1 accounts for L.
for (size_t i = 0; i < p_row_size; i++) {
p[i] = 0;
}
// Copy the given joint probabilities
// (and initialize to 0 those that aren't given).
for (size_t a_in_i = 0; a_in_i < a_in; a_in_i++) {
for (size_t b_in_i = 0; b_in_i < b_in; b_in_i++) {
for (size_t a_out_i = 0; a_out_i < a_out - 1; a_out_i++) {
@ -74,29 +77,8 @@ void FromCgToP(size_t a_out, size_t b_out, size_t a_in, size_t b_in,
BOutPermuted(b_out_i, BInPermuted(b_in_i, b_in_perm),
b_out_perms),
AInPermuted(a_in_i, a_in_perm), BInPermuted(b_in_i, b_in_perm));
p[p_index] = cg[cg_index];
p[p_index] += cg[cg_index];
}
{
size_t p_index = PIndex(
a_out, b_out, a_in, b_in,
AOutPermuted(a_out_i, AInPermuted(a_in_i, a_in_perm),
a_out_perms),
BOutPermuted(b_out - 1, BInPermuted(b_in_i, b_in_perm),
b_out_perms),
AInPermuted(a_in_i, a_in_perm), BInPermuted(b_in_i, b_in_perm));
p[p_index] = 0;
}
}
for (size_t b_out_i = 0; b_out_i < b_out; b_out_i++) {
size_t p_index = PIndex(
a_out, b_out, a_in, b_in,
AOutPermuted(a_out - 1, AInPermuted(a_in_i, a_in_perm),
a_out_perms),
BOutPermuted(b_out_i, BInPermuted(b_in_i, b_in_perm), b_out_perms),
AInPermuted(a_in_i, a_in_perm), BInPermuted(b_in_i, b_in_perm));
p[p_index] = 0;
}
}
}
@ -169,8 +151,9 @@ void FromCgToP(size_t a_out, size_t b_out, size_t a_in, size_t b_in,
}
}
void FromPToCg(size_t a_out, size_t b_out, size_t a_in, size_t b_in,
data_t *restrict cg, const data_t *restrict p) {
void FromPToCg(const size_t a_out, const size_t b_out, const size_t a_in,
const size_t b_in, data_t *restrict cg,
const data_t *restrict p) {
size_t cg_len = ((a_out - 1) * (b_out - 1) * a_in * b_in +
(a_out - 1) * a_in + (b_out - 1) * b_in) +
1;
@ -289,11 +272,29 @@ void FromPToCg(size_t a_out, size_t b_out, size_t a_in, size_t b_in,
}
}
void PSwapParties(size_t out, size_t in, data_t *p) {
for (size_t a_out_i = 0; a_out_i < out; a_out_i++) {
for (size_t b_out_i = a_out_i + 1; b_out_i < out; b_out_i++) {
for (size_t a_in_i = 0; a_in_i < in; a_in_i++) {
for (size_t b_in_i = a_in_i + 1; b_in_i < in; b_in_i++) {
// To perform a swap for every combination, we need only to consider tuples
//
// (oA, oB, iA, iB)
//
// such that, when lexicographically ordered (and we take this order to be
// right-to-left, in the sense that a greater iB places an entry later, then
// a greater iA, and so on), we have that
//
// (oA, oB, iA, iB) < (oB, oA, iB, iA)
//
// meaning the left-hand side comes before, in the ordering, to the right-hand
// side.
// (The case where the two sides are equal is not covered, but that case does
// not require a swap.)
//
// This can easily be seen to mean that we require
//
// iB < iA OR (iB = iA AND oB < oA)
void PSwapParties(const size_t out, const size_t in, data_t *p) {
for (size_t a_in_i = 0; a_in_i < in; a_in_i++) {
for (size_t b_in_i = 0; b_in_i < a_in_i; b_in_i++) {
for (size_t a_out_i = 0; a_out_i < out; a_out_i++) {
for (size_t b_out_i = 0; b_out_i < out; b_out_i++) {
size_t u = PIndex(out, out, in, in, a_out_i, b_out_i, a_in_i, b_in_i);
size_t v = PIndex(out, out, in, in, b_out_i, a_out_i, b_in_i, a_in_i);
data_t tmp = p[u];
@ -302,6 +303,16 @@ void PSwapParties(size_t out, size_t in, data_t *p) {
}
}
}
for (size_t a_out_i = 0; a_out_i < out; a_out_i++) {
for (size_t b_out_i = 0; b_out_i < a_out_i; b_out_i++) {
size_t u = PIndex(out, out, in, in, a_out_i, b_out_i, a_in_i, a_in_i);
size_t v = PIndex(out, out, in, in, b_out_i, a_out_i, a_in_i, a_in_i);
data_t tmp = p[u];
p[u] = p[v];
p[v] = tmp;
}
}
}
}
@ -369,7 +380,7 @@ void PrintP(size_t a_out, size_t b_out, size_t a_in, size_t b_in,
}
void PrintPRaw(size_t a_out, size_t b_out, size_t a_in, size_t b_in,
const data_t* p) {
const data_t *p) {
const size_t len = a_out * b_out * a_in * b_in;
for (size_t a_out_i = 0; a_out_i < a_out; a_out_i++) {
for (size_t b_out_i = 0; b_out_i < b_out; b_out_i++) {

View File

@ -143,7 +143,7 @@ matrix_t ParseMatrix(size_t row_len) {
// Write the last entry if necessary
if (!spaces) {
data_t value = as_matrix_entry(scan_str, scan_len);
head[len] = value;
head[len - 1] = value;
}
// Input sanitization: confirm that the number of entries read is a multiple