fn row_major_buffer(m: MatRef<'_, Scalar>) -> Mat<Scalar> {
m.transpose().to_owned()
}
#[test]
fn gemm_writes_a_row_major_destination() {
for (m, n, k) in [
(0, 0, 0),
(0, 2, 3),
(2, 0, 3),
(2, 3, 0),
(1, 1, 1),
(2, 3, 2),
(3, 2, 5),
(4, 4, 4),
(7, 3, 3),
] {
let a = Mat::<Scalar>::from_fn(m, k, |i, j| integer(i, j + 1));
let b = Mat::<Scalar>::from_fn(k, n, |i, j| integer(i + 2, j));
let c = Mat::<Scalar>::from_fn(m, n, |i, j| integer(i + 3, j));
let a_rm = row_major_buffer(a.as_ref());
let b_rm = row_major_buffer(b.as_ref());
let a_rm_before = a_rm.clone();
let b_rm_before = b_rm.clone();
let pad = n + 1;
let mut c_parent = Mat::<Scalar>::full(pad, m, rogue());
for i in 0..m {
for j in 0..n {
c_parent[(j, i)] = c[(i, j)].copy();
}
}
let c_parent_before = c_parent.clone();
for beta in [Accum::Replace, Accum::Add] {
for alpha in [z(0.0), z(1.0), z(-2.0), z(0.5)] {
let mut parent = c_parent_before.clone();
matmul(
parent.as_mut().transpose_mut().submatrix_mut(0, 0, m, n),
beta,
a_rm.as_ref().transpose(),
b_rm.as_ref().transpose(),
alpha,
Par::Seq,
);
let got = parent.transpose().submatrix(0, 0, m, n).to_owned();
check_blend(
"gemm row major",
&c,
&got,
BlockStructure::Rectangular,
a_rm_before.as_ref().transpose(),
BlockStructure::Rectangular,
Conj::No,
b_rm_before.as_ref().transpose(),
BlockStructure::Rectangular,
Conj::No,
beta,
alpha,
);
crate::assert!(
untouched_outside(
c_parent_before.transpose(),
parent.transpose(),
0,
0,
m,
n
),
"gemm row major wrote into the padded column: m={m} \
n={n} k={k} beta={beta:?} alpha={:?}",
real(&alpha)
);
crate::assert!(
exact(a_rm.as_ref(), a_rm_before.as_ref())
&& exact(b_rm.as_ref(), b_rm_before.as_ref()),
"gemm row major modified an operand: m={m} n={n} k={k} \
beta={beta:?} alpha={:?}",
real(&alpha)
);
}
}
}
}
#[test]
fn trmm_writes_a_row_major_destination() {
for structure in STRUCTURES {
for trans in 0..3 {
for (m, n) in [(0, 0), (1, 1), (2, 3), (3, 2), (4, 4), (5, 3)] {
for left in [true, false] {
let order = if left { m } else { n };
let a = operand(order, structure);
let a_before = a.clone();
let b =
Mat::<Scalar>::from_fn(m, n, |i, j| integer(i + 1, j));
let b_before = b.clone();
let (a_view, a_structure, a_conj) = match trans {
0 => (a.as_ref(), structure, Conj::No),
1 => (
a.as_ref().transpose(),
structure.transpose(),
Conj::No,
),
_ => (
a.as_ref().transpose(),
structure.transpose(),
Conj::Yes,
),
};
let (lhs, lhs_structure, conj_lhs, rhs, rhs_structure) =
if left {
(
a_view,
a_structure,
a_conj,
b_before.as_ref(),
BlockStructure::Rectangular,
)
} else {
(
b_before.as_ref(),
BlockStructure::Rectangular,
Conj::No,
a_view,
a_structure,
)
};
let conj_rhs = if left { Conj::No } else { a_conj };
for alpha in [z(0.0), z(1.0), z(2.0), z(0.5)] {
let empty = Mat::<Scalar>::zeros(m, n);
let mut dst = row_major_buffer(empty.as_ref());
matmul_with_conj(
dst.as_mut().transpose_mut(),
BlockStructure::Rectangular,
Accum::Replace,
lhs,
lhs_structure,
conj_lhs,
rhs,
rhs_structure,
conj_rhs,
alpha,
Par::Seq,
);
let want = blended(
Accum::Replace,
empty.as_ref(),
BlockStructure::Rectangular,
lhs,
lhs_structure,
conj_lhs,
rhs,
rhs_structure,
conj_rhs,
alpha,
);
crate::assert!(
exact(dst.as_ref().transpose(), want.as_ref()),
"trmm row major structure={structure:?} \
trans={trans} m={m} n={n} left={left} \
alpha={:?}\ngot={:?}\nwant={:?}",
real(&alpha),
dst.as_ref().transpose(),
want.as_ref()
);
}
crate::assert!(
exact(a.as_ref(), a_before.as_ref())
&& exact(b.as_ref(), b_before.as_ref()),
"trmm row major modified an operand: \
structure={structure:?} trans={trans} left={left}"
);
}
}
}
}
}
#[test]
fn rank_k_update_writes_a_row_major_triangle() {
for structure in [
BlockStructure::TriangularLower,
BlockStructure::TriangularUpper,
] {
for trans in 0..4 {
for (n, k) in [(0, 0), (1, 1), (2, 3), (3, 2), (4, 4), (5, 3)] {
let (ma, na) =
if trans == 0 || trans == 3 { (n, k) } else { (k, n) };
let a = Mat::<Scalar>::from_fn(ma, na, |i, j| integer(i, j + 1));
let a_before = a.clone();
let (a_view, a_conj, rhs, conj_rhs) = match trans {
0 => (a.as_ref(), Conj::No, a.as_ref().transpose(), Conj::No),
1 => (a.as_ref().transpose(), Conj::No, a.as_ref(), Conj::No),
2 => {
(a.as_ref().transpose(), Conj::Yes, a.as_ref(), Conj::No)
},
_ => (a.as_ref(), Conj::No, a.as_ref().transpose(), Conj::Yes),
};
for beta in [Accum::Replace, Accum::Add] {
for alpha in [z(0.0), z(1.0), z(-1.0), z(0.5)] {
let c = Mat::<Scalar>::from_fn(n, n, |i, j| {
if accessed(structure, i, j) {
integer(i + 2, j + 5)
} else {
nan()
}
});
let mut dst = row_major_buffer(c.as_ref());
matmul_with_conj(
dst.as_mut().transpose_mut(),
structure,
beta,
a_view,
BlockStructure::Rectangular,
a_conj,
rhs,
BlockStructure::Rectangular,
conj_rhs,
alpha,
Par::Seq,
);
let want = blended(
beta,
c.as_ref(),
structure,
a_view,
BlockStructure::Rectangular,
a_conj,
rhs,
BlockStructure::Rectangular,
conj_rhs,
alpha,
);
crate::assert!(
exact(dst.as_ref().transpose(), want.as_ref()),
"rank-k row major structure={structure:?} \
trans={trans} n={n} k={k} beta={beta:?} \
alpha={:?}\ngot={:?}\nwant={:?}",
real(&alpha),
dst.as_ref().transpose(),
want.as_ref()
);
crate::assert!(
exact(a.as_ref(), a_before.as_ref()),
"rank-k row major modified A: \
structure={structure:?} trans={trans}"
);
}
}
}
}
}
}