Checkpoint. Very weird things are happening (memory overrunning?)

This commit is contained in:
Miguel M 2023-05-01 17:17:58 +01:00
parent 94c1aa1ae8
commit b10d646ea0
5 changed files with 166 additions and 10 deletions

View File

@ -45,6 +45,13 @@ void FromCgToP(size_t a_out, size_t b_out, size_t a_in, size_t b_in,
const permutation_generator_t* restrict a_out_perms,
const permutation_generator_t* restrict b_out_perms);
// Convert from CG representation to P representation, without applying any
// permutation.
//
// `p` isn't required to be initialized, only allocated.
void FromCgToPIdent(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);
// Convert from P representation to CG representation.
//
// `cg` isn't required to be initialized, only allocated.

View File

@ -44,4 +44,7 @@ void PermutationNext(permutation_generator_t *permutation_generator);
// This will invalidate the generator.
void PermutationFree(permutation_generator_t *permutation_generator);
// Print the current permutation of a generator.
void PrintPermutation(permutation_generator_t *permutation_generator);
#endif // ACED_INCLUDES_PERMUTATION_H_

View File

@ -163,6 +163,101 @@ void FromCgToP(size_t a_out, size_t b_out, size_t a_in, size_t b_in,
}
}
void FromCgToPIdent(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) {
// 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.
// 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++) {
for (size_t b_out_i = 0; b_out_i < b_out - 1; b_out_i++) {
size_t cg_index = CgJointIndex(a_out, b_out, a_in, b_in, a_out_i,
b_out_i, a_in_i, b_in_i);
size_t p_index = PIndex(a_out, b_out, a_in, b_in, a_out_i, b_out_i,
a_in_i, b_in_i);
p[p_index] = cg[cg_index];
}
{
size_t p_index = PIndex(a_out, b_out, a_in, b_in, a_out_i, b_out - 1,
a_in_i, b_in_i);
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, a_out - 1, b_out_i,
a_in_i, b_in_i);
p[p_index] = 0;
}
}
}
// L contributes to every joint probability, for a fixed choice of inputs,
// which we'll take to be 0,0.
data_t l = cg[cg_row_size - 1];
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++) {
size_t a_in_i = 0;
size_t b_in_i = 0;
size_t p_index =
PIndex(a_out, b_out, a_in, b_in, a_out_i, b_out_i, a_in_i, b_in_i);
// Negative sign comes from moving L to the LHS of the inequality
p[p_index] -= l;
}
}
// Account for the marginal probabilities given.
// The convention will be that, where ambiguous, i.e., in the input of the
// other party, we will take it to be 0.
// A's marginals:
for (size_t a_in_i = 0; a_in_i < a_in; a_in_i++) {
for (size_t a_out_i = 0; a_out_i < a_out - 1; a_out_i++) {
size_t cg_index =
CgAMarginalIndex(a_out, b_out, a_in, b_in, a_out_i, a_in_i);
data_t a_marginal = cg[cg_index];
if (a_marginal == 0) {
continue;
}
for (size_t b_out_i = 0; b_out_i < b_out; b_out_i++) {
size_t b_in_i = 0;
size_t p_index =
PIndex(a_out, b_out, a_in, b_in, a_out_i, b_out_i, a_in_i, b_in_i);
p[p_index] += a_marginal;
// PrintP(a_out, b_out, a_in, b_in, p);
}
}
}
// B's marginals:
for (size_t b_in_i = 0; b_in_i < b_in; b_in_i++) {
for (size_t b_out_i = 0; b_out_i < b_out - 1; b_out_i++) {
size_t cg_index = (a_out - 1) * (b_out - 1) * a_in * b_in +
(a_out - 1) * a_in + b_out_i + (b_out - 1) * b_in_i;
data_t b_marginal = cg[cg_index];
if (b_marginal == 0) {
continue;
}
for (size_t a_out_i = 0; a_out_i < a_out; a_out_i++) {
size_t a_in_i = 0;
size_t p_index =
PIndex(a_out, b_out, a_in, b_in, a_out_i, b_out_i, a_in_i, b_in_i);
p[p_index] += b_marginal;
// PrintP(a_out, b_out, a_in, b_in, p);
}
}
}
}
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) {
size_t cg_len = ((a_out - 1) * (b_out - 1) * a_in * b_in +

View File

@ -49,7 +49,6 @@ int main(int argc, char *argv[]) {
size_t row_len = (a_out - 1) * (b_out - 1) * a_in * b_in +
(a_out - 1) * a_in + (b_out - 1) * b_in + 1;
matrix_t matrix = ParseMatrix(row_len);
// DebugPrintMatrix(&matrix);
size_t row_count = matrix.len / matrix.row_len;
data_t *p_buf = malloc(a_out * b_out * a_in * b_in * sizeof(data_t));
@ -57,6 +56,19 @@ int main(int argc, char *argv[]) {
malloc((a_out - 1) * (b_out - 1) * a_in * b_in * sizeof(data_t));
_Bool *seen = calloc(row_count, sizeof(_Bool));
if (p_buf == NULL) {
fprintf(stderr, "Failed to allocate P notation buffer. Aborting.");
exit(EXIT_FAILURE);
}
if (cg_buf == NULL) {
fprintf(stderr, "Failed to allocate CG notation buffer. Aborting.");
exit(EXIT_FAILURE);
}
if (seen == NULL) {
fprintf(stderr, "Failed to allocate equivalence flag string. Aborting.");
exit(EXIT_FAILURE);
}
// This is a lot of separate allocations, but this section isn't expected to
// be hot.
permutation_generator_t a_in_perm = PermutationNewGenerator(a_in);
@ -65,6 +77,14 @@ int main(int argc, char *argv[]) {
malloc(a_in * sizeof(permutation_generator_t));
permutation_generator_t *b_out_perms =
malloc(b_in * sizeof(permutation_generator_t));
if (a_out_perms == NULL || b_out_perms == NULL) {
fprintf(stderr,
"Failed to allocate space for conditioned permutation generators. "
"Aborting.");
exit(EXIT_FAILURE);
}
for (size_t i = 0; i < a_in; i++) {
a_out_perms[i] = PermutationNewGenerator(a_out);
}
@ -75,34 +95,54 @@ int main(int argc, char *argv[]) {
// Start testing for equivalences.
for (size_t lhs_i = 0; lhs_i < row_count - 1; lhs_i++) {
data_t *lhs = matrix.head + lhs_i * matrix.row_len;
if (seen[lhs_i]) {
continue;
}
data_t *lhs = matrix.head + lhs_i * matrix.row_len;
FromCgToPIdent(a_out, b_out, a_in, b_in, lhs, p_buf);
FromPToCg(a_out, b_out, a_in, b_in, lhs, p_buf);
printf("LHS:");
PrintCg(a_out, b_out, a_in, b_in, lhs);
for (size_t rhs_i = lhs_i + 1; rhs_i < row_count; rhs_i++) {
if (seen[rhs_i]) {
continue;
}
data_t *rhs = matrix.head + rhs_i * matrix.row_len;
printf("RHS:");
PrintCg(a_out, b_out, a_in, b_in, rhs);
PermutationReset(&a_in_perm);
while (!a_in_perm.exhausted) {
PermutationReset(&b_in_perm);
while (!b_in_perm.exhausted) {
ResetConditionalPermutations(a_out_perms, a_out);
while (!a_out_perms[a_out - 1].exhausted) {
ResetConditionalPermutations(b_out_perms, b_out);
while (!b_out_perms[b_out - 1].exhausted) {
// Compare the two rows
_Bool equivalent = 1;
FromCgToP(a_out, b_out, a_in, b_in, rhs, p_buf,
a_in_perm.permutation, b_in_perm.permutation,
a_out_perms, b_out_perms);
FromPToCg(a_out, b_out, a_in, b_in, cg_buf, p_buf);
// printf("--------------\n");
// PrintPermutation(&a_in_perm);
// PrintPermutation(&b_in_perm);
// for (size_t i = 0; i < a_in; i++) {
// printf("A=%zu: ", i);
// PrintPermutation(a_out_perms + i);
//}
// for (size_t i = 0; i < b_in; i++) {
// printf("B=%zu: ", i);
// PrintPermutation(b_out_perms + i);
//}
// PrintCg(a_out, b_out, a_in, b_in, cg_buf);
_Bool equivalent = 1;
for (size_t i = 0; i < row_len; i++) {
if (lhs[i] != cg_buf[i]) {
equivalent = 0;
@ -111,7 +151,11 @@ int main(int argc, char *argv[]) {
}
if (equivalent) {
printf("%zu equivalent to %zu\n", rhs_i, lhs_i);
seen[rhs_i] = 1;
for (size_t i = 0; i < row_count; i++) {
printf("%s,", seen[i] ? "true" : "false");
}
goto skip_permutations;
}
@ -128,14 +172,15 @@ int main(int argc, char *argv[]) {
}
skip_permutations:;
} // For loop over rhs_i
} // For loop over lhs_i
} // For loop over rhs_i
} // For loop over lhs_i
// Print every unique row
for (size_t i = 0; i < row_count; i++) {
if (!seen[i]) {
PrintMatrixRow(&matrix, i);
}
printf("%s,", seen[i] ? "true" : "false");
// if (!seen[i]) {
// PrintMatrixRow(&matrix, i);
//}
}
return EXIT_SUCCESS;

View File

@ -7,6 +7,12 @@ permutation_generator_t PermutationNewGenerator(size_t len) {
size_t *permutation = malloc(2 * len * sizeof(size_t));
size_t *stack = permutation + len;
if (permutation == NULL) {
fprintf(stderr,
"Failed to allocate space for permutation array. Aborting.");
exit(EXIT_FAILURE);
}
permutation_generator_t generator = {
.len = len,
.permutation = permutation,