Skip to content

Common Implemenatation for MatMul and MatMulTran for both aligned and unaligned arrays #1218

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Oct 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 7 additions & 16 deletions src/Microsoft.ML.CpuMath/Avx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public static bool CheckAvx()
return Thunk.ChkAvx();
}

public static void MatTimesSrc(bool tran, bool add, AlignedArray mat, AlignedArray src, AlignedArray dst, int crun)
public static void MatTimesSrc(bool tran, AlignedArray mat, AlignedArray src, AlignedArray dst, int crun)
{
Contracts.Assert(Compat(mat));
Contracts.Assert(Compat(src));
Expand All @@ -50,18 +50,18 @@ public static void MatTimesSrc(bool tran, bool add, AlignedArray mat, AlignedArr
if (!tran)
{
Contracts.Assert(0 <= crun && crun <= dst.Size);
Thunk.MatMulX(add, Ptr(mat, pmat), Ptr(src, psrc), Ptr(dst, pdst), crun, src.Size);
Thunk.MatMulX(Ptr(mat, pmat), Ptr(src, psrc), Ptr(dst, pdst), crun, src.Size);
}
else
{
Contracts.Assert(0 <= crun && crun <= src.Size);
Thunk.MatMulTranX(add, Ptr(mat, pmat), Ptr(src, psrc), Ptr(dst, pdst), dst.Size, crun);
Thunk.MatMulTranX(Ptr(mat, pmat), Ptr(src, psrc), Ptr(dst, pdst), dst.Size, crun);
}
}
}
}

public static void MatTimesSrc(bool tran, bool add, AlignedArray mat, int[] rgposSrc, AlignedArray srcValues,
public static void MatTimesSrc(AlignedArray mat, int[] rgposSrc, AlignedArray srcValues,
int posMin, int iposMin, int iposLim, AlignedArray dst, int crun)
{
Contracts.Assert(Compat(mat));
Expand All @@ -73,8 +73,7 @@ public static void MatTimesSrc(bool tran, bool add, AlignedArray mat, int[] rgpo

if (iposMin >= iposLim)
{
if (!add)
dst.ZeroItems();
dst.ZeroItems();
return;
}
Contracts.AssertNonEmpty(rgposSrc);
Expand All @@ -85,16 +84,8 @@ public static void MatTimesSrc(bool tran, bool add, AlignedArray mat, int[] rgpo
fixed (float* psrc = &srcValues.Items[0])
fixed (int* ppossrc = &rgposSrc[0])
{
if (!tran)
{
Contracts.Assert(0 <= crun && crun <= dst.Size);
Thunk.MatMulPX(add, Ptr(mat, pmat), ppossrc, Ptr(srcValues, psrc), posMin, iposMin, iposLim, Ptr(dst, pdst), crun, srcValues.Size);
}
else
{
Contracts.Assert(0 <= crun && crun <= srcValues.Size);
Thunk.MatMulTranPX(add, Ptr(mat, pmat), ppossrc, Ptr(srcValues, psrc), posMin, iposMin, iposLim, Ptr(dst, pdst), dst.Size);
}
Contracts.Assert(0 <= crun && crun <= dst.Size);
Thunk.MatMulPX(Ptr(mat, pmat), ppossrc, Ptr(srcValues, psrc), posMin, iposMin, iposLim, Ptr(dst, pdst), crun, srcValues.Size);
}
}
}
Expand Down
550 changes: 380 additions & 170 deletions src/Microsoft.ML.CpuMath/AvxIntrinsics.cs

Large diffs are not rendered by default.

20 changes: 6 additions & 14 deletions src/Microsoft.ML.CpuMath/CpuAligenedMathUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,40 +75,32 @@ public static void AssertCompatible(ICpuFullMatrix mat, ICpuVector src, ICpuVect

/// <summary>
/// Matrix multiplication:
/// if (add)
/// dst = mat * src
/// else
/// dest += mat * src
/// dst = mat * src
/// </summary>
/// <param name="add">The addition flag</param>
/// <param name="mat">The multiplier matrix</param>
/// <param name="src">The source vector</param>
/// <param name="dst">The destination vector</param>
public static void MatTimesSrc(bool add, ICpuFullMatrix mat, ICpuVector src, ICpuVector dst)
public static void MatTimesSrc(ICpuFullMatrix mat, ICpuVector src, ICpuVector dst)
{
bool colMajor = typeof(TMatrix) == typeof(CpuAlignedMatrixCol);
AssertCompatible(mat, src, dst);
var m = A(mat);
CpuMathUtils.MatTimesSrc(colMajor, add, m.Items, A(src).Items, A(dst).Items, m.RunCnt);
CpuMathUtils.MatTimesSrc(colMajor, m.Items, A(src).Items, A(dst).Items, m.RunCnt);
}

/// <summary>
/// Matrix transpose multiplication:
/// if (add)
/// dst = mat' * src
/// else
/// dest += mat' * src
/// dst = mat' * src
/// </summary>
/// <param name="add">The addition flag</param>
/// <param name="mat">The multiplier matrix</param>
/// <param name="src">The source vector</param>
/// <param name="dst">The destination vector</param>
public static void MatTranTimesSrc(bool add, ICpuFullMatrix mat, ICpuVector src, ICpuVector dst)
public static void MatTranTimesSrc(ICpuFullMatrix mat, ICpuVector src, ICpuVector dst)
{
bool colMajor = typeof(TMatrix) == typeof(CpuAlignedMatrixCol);
AssertCompatible(mat, dst, src);
var m = A(mat);
CpuMathUtils.MatTimesSrc(!colMajor, add, m.Items, A(src).Items, A(dst).Items, m.RunCnt);
CpuMathUtils.MatTimesSrc(!colMajor, m.Items, A(src).Items, A(dst).Items, m.RunCnt);
}
}

Expand Down
105 changes: 20 additions & 85 deletions src/Microsoft.ML.CpuMath/CpuMathUtils.netcoreapp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public static partial class CpuMathUtils
public static int GetVectorAlignment()
=> Avx.IsSupported ? Vector256Alignment : (Sse.IsSupported ? Vector128Alignment : FloatAlignment);

public static void MatTimesSrc(bool tran, bool add, AlignedArray mat, AlignedArray src, AlignedArray dst, int crun)
public static void MatTimesSrc(bool tran, AlignedArray mat, AlignedArray src, AlignedArray dst, int crun)
{
Contracts.Assert(mat.Size == dst.Size * src.Size);
Contracts.Assert(crun >= 0);
Expand All @@ -34,25 +34,25 @@ public static void MatTimesSrc(bool tran, bool add, AlignedArray mat, AlignedArr
if (!tran)
{
Contracts.Assert(crun <= dst.Size);
AvxIntrinsics.MatMulX(add, mat, src, dst, crun, src.Size);
AvxIntrinsics.MatMulX(mat, src, dst, crun, src.Size);
}
else
{
Contracts.Assert(crun <= src.Size);
AvxIntrinsics.MatMulTranX(add, mat, src, dst, dst.Size, crun);
AvxIntrinsics.MatMulTranX(mat, src, dst, dst.Size, crun);
}
}
else if (Sse.IsSupported)
{
if (!tran)
{
Contracts.Assert(crun <= dst.Size);
SseIntrinsics.MatMulA(add, mat, src, dst, crun, src.Size);
SseIntrinsics.MatMul(mat, src, dst, crun, src.Size);
}
else
{
Contracts.Assert(crun <= src.Size);
SseIntrinsics.MatMulTranA(add, mat, src, dst, dst.Size, crun);
SseIntrinsics.MatMulTran(mat, src, dst, dst.Size, crun);
}
}
else
Expand All @@ -68,14 +68,7 @@ public static void MatTimesSrc(bool tran, bool add, AlignedArray mat, AlignedArr
dotProduct += mat[i * src.Size + j] * src[j];
}

if (add)
{
dst[i] += dotProduct;
}
else
{
dst[i] = dotProduct;
}
dst[i] = dotProduct;
}
}
else
Expand All @@ -89,20 +82,13 @@ public static void MatTimesSrc(bool tran, bool add, AlignedArray mat, AlignedArr
dotProduct += mat[j * src.Size + i] * src[j];
}

if (add)
{
dst[i] += dotProduct;
}
else
{
dst[i] = dotProduct;
}
dst[i] = dotProduct;
}
}
}
}

public static void MatTimesSrc(bool tran, bool add, AlignedArray mat, int[] rgposSrc, AlignedArray srcValues,
public static void MatTimesSrc(AlignedArray mat, int[] rgposSrc, AlignedArray srcValues,
int posMin, int iposMin, int iposLim, AlignedArray dst, int crun)
{
Contracts.AssertValue(rgposSrc);
Expand All @@ -113,8 +99,7 @@ public static void MatTimesSrc(bool tran, bool add, AlignedArray mat, int[] rgpo

if (iposMin >= iposLim)
{
if (!add)
dst.ZeroItems();
dst.ZeroItems();
return;
}

Expand All @@ -123,76 +108,26 @@ public static void MatTimesSrc(bool tran, bool add, AlignedArray mat, int[] rgpo

if (Avx.IsSupported)
{
if (!tran)
{
Contracts.Assert(crun <= dst.Size);
AvxIntrinsics.MatMulPX(add, mat, rgposSrc, srcValues, posMin, iposMin, iposLim, dst, crun, srcValues.Size);
}
else
{
Contracts.Assert(crun <= srcValues.Size);
AvxIntrinsics.MatMulTranPX(add, mat, rgposSrc, srcValues, posMin, iposMin, iposLim, dst, dst.Size);
}
Contracts.Assert(crun <= dst.Size);
AvxIntrinsics.MatMulPX(mat, rgposSrc, srcValues, posMin, iposMin, iposLim, dst, crun, srcValues.Size);
}
else if (Sse.IsSupported)
{
if (!tran)
{
Contracts.Assert(crun <= dst.Size);
SseIntrinsics.MatMulPA(add, mat, rgposSrc, srcValues, posMin, iposMin, iposLim, dst, crun, srcValues.Size);
}
else
{
Contracts.Assert(crun <= srcValues.Size);
SseIntrinsics.MatMulTranPA(add, mat, rgposSrc, srcValues, posMin, iposMin, iposLim, dst, dst.Size);
}
Contracts.Assert(crun <= dst.Size);
SseIntrinsics.MatMulPA(mat, rgposSrc, srcValues, posMin, iposMin, iposLim, dst, crun, srcValues.Size);
}
else
{
if (!tran)
{
Contracts.Assert(crun <= dst.Size);
for (int i = 0; i < crun; i++)
{
float dotProduct = 0;
for (int j = iposMin; j < iposLim; j++)
{
int col = rgposSrc[j] - posMin;
dotProduct += mat[i * srcValues.Size + col] * srcValues[col];
}

if (add)
{
dst[i] += dotProduct;
}
else
{
dst[i] = dotProduct;
}
}
}
else
Contracts.Assert(crun <= dst.Size);
for (int i = 0; i < crun; i++)
{
Contracts.Assert(crun <= srcValues.Size);
for (int i = 0; i < dst.Size; i++)
float dotProduct = 0;
for (int j = iposMin; j < iposLim; j++)
{
float dotProduct = 0;
for (int j = iposMin; j < iposLim; j++)
{
int col = rgposSrc[j] - posMin;
dotProduct += mat[col * dst.Size + i] * srcValues[col];
}

if (add)
{
dst[i] += dotProduct;
}
else
{
dst[i] = dotProduct;
}
int col = rgposSrc[j] - posMin;
dotProduct += mat[i * srcValues.Size + col] * srcValues[col];
}

dst[i] = dotProduct;
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/Microsoft.ML.CpuMath/CpuMathUtils.netstandard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ public static partial class CpuMathUtils
public static int GetVectorAlignment()
=> Vector128Alignment;

public static void MatTimesSrc(bool tran, bool add, AlignedArray mat, AlignedArray src, AlignedArray dst, int crun) => SseUtils.MatTimesSrc(tran, add, mat, src, dst, crun);
public static void MatTimesSrc(bool tran, AlignedArray mat, AlignedArray src, AlignedArray dst, int crun) => SseUtils.MatTimesSrc(tran, mat, src, dst, crun);

public static void MatTimesSrc(bool tran, bool add, AlignedArray mat, int[] rgposSrc, AlignedArray srcValues,
int posMin, int iposMin, int iposLim, AlignedArray dst, int crun) => SseUtils.MatTimesSrc(tran, add, mat, rgposSrc, srcValues, posMin, iposMin, iposLim, dst, crun);
public static void MatTimesSrc(AlignedArray mat, int[] rgposSrc, AlignedArray srcValues,
int posMin, int iposMin, int iposLim, AlignedArray dst, int crun) => SseUtils.MatTimesSrc(mat, rgposSrc, srcValues, posMin, iposMin, iposLim, dst, crun);

public static void Add(float a, Span<float> dst) => SseUtils.Add(a, dst);

Expand Down
23 changes: 7 additions & 16 deletions src/Microsoft.ML.CpuMath/Sse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ private static bool Compat(AlignedArray a)
return q;
}

public static void MatTimesSrc(bool tran, bool add, AlignedArray mat, AlignedArray src, AlignedArray dst, int crun)
public static void MatTimesSrc(bool tran, AlignedArray mat, AlignedArray src, AlignedArray dst, int crun)
{
Contracts.Assert(Compat(mat));
Contracts.Assert(Compat(src));
Expand All @@ -46,18 +46,18 @@ public static void MatTimesSrc(bool tran, bool add, AlignedArray mat, AlignedArr
if (!tran)
{
Contracts.Assert(0 <= crun && crun <= dst.Size);
Thunk.MatMulA(add, Ptr(mat, pmat), Ptr(src, psrc), Ptr(dst, pdst), crun, src.Size);
Thunk.MatMul(Ptr(mat, pmat), Ptr(src, psrc), Ptr(dst, pdst), crun, src.Size);
}
else
{
Contracts.Assert(0 <= crun && crun <= src.Size);
Thunk.MatMulTranA(add, Ptr(mat, pmat), Ptr(src, psrc), Ptr(dst, pdst), dst.Size, crun);
Thunk.MatMulTran(Ptr(mat, pmat), Ptr(src, psrc), Ptr(dst, pdst), dst.Size, crun);
}
}
}
}

public static void MatTimesSrc(bool tran, bool add, AlignedArray mat, int[] rgposSrc, AlignedArray srcValues,
public static void MatTimesSrc(AlignedArray mat, int[] rgposSrc, AlignedArray srcValues,
int posMin, int iposMin, int iposLim, AlignedArray dst, int crun)
{
Contracts.Assert(Compat(mat));
Expand All @@ -69,8 +69,7 @@ public static void MatTimesSrc(bool tran, bool add, AlignedArray mat, int[] rgpo

if (iposMin >= iposLim)
{
if (!add)
dst.ZeroItems();
dst.ZeroItems();
return;
}
Contracts.AssertNonEmpty(rgposSrc);
Expand All @@ -81,16 +80,8 @@ public static void MatTimesSrc(bool tran, bool add, AlignedArray mat, int[] rgpo
fixed (float* psrc = &srcValues.Items[0])
fixed (int* ppossrc = &rgposSrc[0])
{
if (!tran)
{
Contracts.Assert(0 <= crun && crun <= dst.Size);
Thunk.MatMulPA(add, Ptr(mat, pmat), ppossrc, Ptr(srcValues, psrc), posMin, iposMin, iposLim, Ptr(dst, pdst), crun, srcValues.Size);
}
else
{
Contracts.Assert(0 <= crun && crun <= srcValues.Size);
Thunk.MatMulTranPA(add, Ptr(mat, pmat), ppossrc, Ptr(srcValues, psrc), posMin, iposMin, iposLim, Ptr(dst, pdst), dst.Size);
}
Contracts.Assert(0 <= crun && crun <= dst.Size);
Thunk.MatMulPA(Ptr(mat, pmat), ppossrc, Ptr(srcValues, psrc), posMin, iposMin, iposLim, Ptr(dst, pdst), crun, srcValues.Size);
}
}
}
Expand Down
Loading