Skip to content

Commit

Permalink
mul_strassen: avoid computing some entries twice
Browse files Browse the repository at this point in the history
  • Loading branch information
fredrik-johansson committed Sep 26, 2024
1 parent 02d4e5d commit b4976c2
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
16 changes: 14 additions & 2 deletions src/fmpz_mat/mul_strassen.c
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,22 @@ void fmpz_mat_mul_strassen(fmpz_mat_t C, const fmpz_mat_t A, const fmpz_mat_t B)

if (a > 2*anr)
{
fmpz_mat_t Ar, Cr;
fmpz_mat_t Ar, Br, Cr;
fmpz_mat_window_init(Ar, A, 2*anr, 0, a, b);
fmpz_mat_window_init(Cr, C, 2*anr, 0, a, c);
fmpz_mat_mul(Cr, Ar, B);

/* don't compute the overlapping entries twice */
if (c > 2 * bnc)
{
fmpz_mat_window_init(Br, B, 0, 0, b, 2*bnc);
fmpz_mat_mul(Cr, Ar, Br);
fmpz_mat_window_clear(Br);
}
else
{
fmpz_mat_mul(Cr, Ar, B);
}

fmpz_mat_window_clear(Ar);
fmpz_mat_window_clear(Cr);
}
Expand Down
16 changes: 14 additions & 2 deletions src/gr_mat/mul_strassen.c
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,22 @@ int gr_mat_mul_strassen(gr_mat_t C, const gr_mat_t A, const gr_mat_t B, gr_ctx_t

if (ar > 2 * anr)
{
gr_mat_t Ar, Cr;
gr_mat_t Ar, Br, Cr;
gr_mat_window_init(Ar, A, 2 * anr, 0, ar, ac, ctx);
gr_mat_window_init(Cr, C, 2 * anr, 0, ar, bc, ctx);
status |= gr_mat_mul(Cr, Ar, B, ctx);

/* don't compute the overlapping entries twice */
if (bc > 2 * bnc)
{
gr_mat_window_init(Br, B, 0, 0, ac, 2 * bnc, ctx);
status |= gr_mat_mul(Cr, Ar, Br, ctx);
gr_mat_window_clear(Br, ctx);
}
else
{
status |= gr_mat_mul(Cr, Ar, B, ctx);
}

gr_mat_window_clear(Ar, ctx);
gr_mat_window_clear(Cr, ctx);
}
Expand Down

0 comments on commit b4976c2

Please sign in to comment.