cg-p conversion accounts for permutations

This commit is contained in:
Miguel M 2023-05-01 14:49:36 +01:00
parent 4b1db5cec0
commit 678d1e1008
2 changed files with 61 additions and 15 deletions

View File

@ -35,14 +35,18 @@
#include "../includes/data.h"
#include "../includes/permutation.h"
// Convert from CG representation to P representation.
// Convert from CG representation to P representation, under a given
// 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);
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);
// 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);

View File

@ -31,8 +31,29 @@ static inline size_t PIndex(size_t a_out, size_t b_out,
return b_out_i + b_out * (b_in_i + b_in * (a_out_i + a_out * a_in_i));
}
static inline size_t AInPermuted(size_t a_in_i, size_t *a_in_perm) {
return a_in_perm[a_in_i];
}
static inline size_t BInPermuted(size_t b_in_i, size_t *b_in_perm) {
return b_in_perm[b_in_i];
}
static inline size_t AOutPermuted(size_t a_out_i, size_t a_in_i,
const permutation_generator_t *a_out_perms) {
return a_out_perms[a_in_i].permutation[a_out_i];
}
static inline size_t BOutPermuted(size_t b_out_i, size_t b_in_i,
const permutation_generator_t *b_out_perms) {
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) {
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 cg_row_size = ((a_out - 1) * (b_out - 1) * a_in * b_in +
(a_out - 1) * a_in + (b_out - 1) * b_in) +
@ -46,21 +67,30 @@ void FromCgToP(size_t a_out, size_t b_out, size_t a_in, size_t b_in,
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);
size_t p_index = PIndex(a_out, b_out, a_in, b_in,
AOutPermuted(a_out_i, a_in_i, a_out_perms),
BOutPermuted(b_out_i, b_in_i, b_out_perms),
AInPermuted(a_in_i, a_in_perm),
BInPermuted(b_in_i, b_in_perm));
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);
size_t p_index = PIndex(a_out, b_out, a_in, b_in,
AOutPermuted(a_out_i, a_in_i, a_out_perms),
BOutPermuted(b_out - 1, b_in_i, 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, a_in - 1, b_out_i, a_in_i, b_in_i);
size_t p_index = PIndex(a_out, b_out, a_in, b_in,
AOutPermuted(a_out - 1, a_in_i, a_out_perms),
BOutPermuted(b_out_i, b_in_i, b_out_perms),
AInPermuted(a_in_i, a_in_perm),
BInPermuted(b_in_i, b_in_perm));
p[p_index] = 0;
}
}
@ -72,7 +102,11 @@ void FromCgToP(size_t a_out, size_t b_out, size_t a_in, size_t 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++) {
size_t p_index = PIndex(a_out, b_out, a_in, b_in, a_out_i, b_out_i, 0, 0);
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, AOutPermuted(a_out_i, a_in_i, a_out_perms),
BOutPermuted(b_out_i, b_in_i, b_out_perms), a_in_i, b_in_i);
// Negative sign comes from moving L to the LHS of the inequality
p[p_index] -= l;
}
@ -93,8 +127,12 @@ void FromCgToP(size_t a_out, size_t b_out, size_t a_in, size_t b_in,
}
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_i, b_out_i, a_in_i, 0);
size_t b_in_i = 0;
size_t p_index = PIndex(a_out, b_out, a_in, b_in,
AOutPermuted(a_out_i, a_in_i, a_out_perms),
BOutPermuted(b_out_i, b_in_i, b_out_perms),
AInPermuted(a_in_i, a_in_perm),
BInPermuted(b_in_i, b_in_perm));
p[p_index] += a_marginal;
// PrintP(a_out, b_out, a_in, b_in, p);
}
@ -112,8 +150,12 @@ void FromCgToP(size_t a_out, size_t b_out, size_t a_in, size_t b_in,
}
for (size_t a_out_i = 0; a_out_i < a_out; a_out_i++) {
size_t p_index =
PIndex(a_out, b_out, a_in, b_in, a_out_i, b_out_i, 0, b_in_i);
size_t a_in_i = 0;
size_t p_index = PIndex(a_out, b_out, a_in, b_in,
AOutPermuted(a_out_i, a_in_i, a_out_perms),
BOutPermuted(b_out_i, b_in_i, b_out_perms),
AInPermuted(a_in_i, a_in_perm),
BInPermuted(b_in_i, b_in_perm));
p[p_index] += b_marginal;
// PrintP(a_out, b_out, a_in, b_in, p);
}