Skip to content

Commit 72ec121

Browse files
authored
Removed AlignedArray (dotnet#1657)
* Removing Aligned Array usage from rff and timeseries. Removing aligned matrix and cpumathaligned entirely. moving Aligned array to FactorAware where it is only being used * adding some asserts * unrolling the loop, aligned removed from name, using new float comparision * enabling some more tests * case comment added, tanners feedback
1 parent 8a9b016 commit 72ec121

File tree

17 files changed

+809
-1699
lines changed

17 files changed

+809
-1699
lines changed

src/Microsoft.ML.CpuMath/AlignedMatrix.cs

Lines changed: 0 additions & 681 deletions
This file was deleted.

src/Microsoft.ML.CpuMath/AvxIntrinsics.cs

Lines changed: 171 additions & 135 deletions
Large diffs are not rendered by default.

src/Microsoft.ML.CpuMath/CpuAligenedMathUtils.cs

Lines changed: 0 additions & 148 deletions
This file was deleted.

src/Microsoft.ML.CpuMath/CpuMathUtils.netcoreapp.cs

Lines changed: 46 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -11,76 +11,62 @@ namespace Microsoft.ML.Runtime.Internal.CpuMath
1111
{
1212
internal static partial class CpuMathUtils
1313
{
14-
// The count of bytes in Vector128<T>, corresponding to _cbAlign in AlignedArray
15-
private const int Vector128Alignment = 16;
16-
17-
// The count of bytes in Vector256<T>, corresponding to _cbAlign in AlignedArray
18-
private const int Vector256Alignment = 32;
19-
20-
// The count of bytes in a 32-bit float, corresponding to _cbAlign in AlignedArray
21-
private const int FloatAlignment = 4;
22-
23-
// If neither AVX nor SSE is supported, return basic alignment for a 4-byte float.
24-
[MethodImplAttribute(MethodImplOptions.AggressiveInlining)]
25-
public static int GetVectorAlignment()
26-
=> Avx.IsSupported ? Vector256Alignment : (Sse.IsSupported ? Vector128Alignment : FloatAlignment);
27-
28-
public static void MatrixTimesSource(bool transpose, AlignedArray matrix, AlignedArray source, AlignedArray destination, int stride)
14+
public static void MatrixTimesSource(bool transpose, ReadOnlySpan<float> matrix, ReadOnlySpan<float> source, Span<float> destination, int stride)
2915
{
30-
Contracts.Assert(matrix.Size == destination.Size * source.Size);
16+
Contracts.AssertNonEmpty(matrix);
17+
Contracts.AssertNonEmpty(source);
18+
Contracts.AssertNonEmpty(destination);
19+
Contracts.Assert(matrix.Length == destination.Length * source.Length);
3120
Contracts.Assert(stride >= 0);
3221

33-
if (Avx.IsSupported)
22+
if (!transpose)
3423
{
35-
if (!transpose)
24+
if (Avx.IsSupported && source.Length >= 8)
3625
{
37-
Contracts.Assert(stride <= destination.Size);
38-
AvxIntrinsics.MatMul(matrix, source, destination, stride, source.Size);
26+
Contracts.Assert(stride <= destination.Length);
27+
AvxIntrinsics.MatMul(matrix, source, destination, stride, source.Length);
3928
}
40-
else
29+
else if (Sse.IsSupported && source.Length >= 4)
4130
{
42-
Contracts.Assert(stride <= source.Size);
43-
AvxIntrinsics.MatMulTran(matrix, source, destination, destination.Size, stride);
44-
}
45-
}
46-
else if (Sse.IsSupported)
47-
{
48-
if (!transpose)
49-
{
50-
Contracts.Assert(stride <= destination.Size);
51-
SseIntrinsics.MatMul(matrix, source, destination, stride, source.Size);
31+
Contracts.Assert(stride <= destination.Length);
32+
SseIntrinsics.MatMul(matrix, source, destination, stride, source.Length);
5233
}
5334
else
5435
{
55-
Contracts.Assert(stride <= source.Size);
56-
SseIntrinsics.MatMulTran(matrix, source, destination, destination.Size, stride);
57-
}
58-
}
59-
else
60-
{
61-
if (!transpose)
62-
{
63-
Contracts.Assert(stride <= destination.Size);
36+
Contracts.Assert(stride <= destination.Length);
6437
for (int i = 0; i < stride; i++)
6538
{
6639
float dotProduct = 0;
67-
for (int j = 0; j < source.Size; j++)
40+
for (int j = 0; j < source.Length; j++)
6841
{
69-
dotProduct += matrix[i * source.Size + j] * source[j];
42+
dotProduct += matrix[i * source.Length + j] * source[j];
7043
}
7144

7245
destination[i] = dotProduct;
7346
}
7447
}
48+
}
49+
else
50+
{
51+
if (Avx.IsSupported && destination.Length >= 8)
52+
{
53+
Contracts.Assert(stride <= source.Length);
54+
AvxIntrinsics.MatMulTran(matrix, source, destination, destination.Length, stride);
55+
}
56+
else if (Sse.IsSupported && destination.Length >=4)
57+
{
58+
Contracts.Assert(stride <= source.Length);
59+
SseIntrinsics.MatMulTran(matrix, source, destination, destination.Length, stride);
60+
}
7561
else
7662
{
77-
Contracts.Assert(stride <= source.Size);
78-
for (int i = 0; i < destination.Size; i++)
63+
Contracts.Assert(stride <= source.Length);
64+
for (int i = 0; i < destination.Length; i++)
7965
{
8066
float dotProduct = 0;
8167
for (int j = 0; j < stride; j++)
8268
{
83-
dotProduct += matrix[j * source.Size + i] * source[j];
69+
dotProduct += matrix[j * destination.Length + i] * source[j];
8470
}
8571

8672
destination[i] = dotProduct;
@@ -89,17 +75,22 @@ public static void MatrixTimesSource(bool transpose, AlignedArray matrix, Aligne
8975
}
9076
}
9177

92-
public static void MatrixTimesSource(AlignedArray matrix, ReadOnlySpan<int> rgposSrc, AlignedArray sourceValues,
93-
int posMin, int iposMin, int iposLimit, AlignedArray destination, int stride)
78+
public static void MatrixTimesSource(ReadOnlySpan<float> matrix, ReadOnlySpan<int> rgposSrc, ReadOnlySpan<float> sourceValues,
79+
int posMin, int iposMin, int iposLimit, Span<float> destination, int stride)
9480
{
9581
Contracts.Assert(iposMin >= 0);
9682
Contracts.Assert(iposMin <= iposLimit);
9783
Contracts.Assert(iposLimit <= rgposSrc.Length);
98-
Contracts.Assert(matrix.Size == destination.Size * sourceValues.Size);
84+
Contracts.AssertNonEmpty(matrix);
85+
Contracts.AssertNonEmpty(sourceValues);
86+
Contracts.AssertNonEmpty(destination);
87+
Contracts.AssertNonEmpty(rgposSrc);
88+
Contracts.Assert(stride > 0);
89+
Contracts.Assert(matrix.Length == destination.Length * sourceValues.Length);
9990

10091
if (iposMin >= iposLimit)
10192
{
102-
destination.ZeroItems();
93+
destination.Clear();
10394
return;
10495
}
10596

@@ -108,24 +99,24 @@ public static void MatrixTimesSource(AlignedArray matrix, ReadOnlySpan<int> rgpo
10899

109100
if (Avx.IsSupported)
110101
{
111-
Contracts.Assert(stride <= destination.Size);
112-
AvxIntrinsics.MatMulP(matrix, rgposSrc, sourceValues, posMin, iposMin, iposLimit, destination, stride, sourceValues.Size);
102+
Contracts.Assert(stride <= destination.Length);
103+
AvxIntrinsics.MatMulP(matrix, rgposSrc, sourceValues, posMin, iposMin, iposLimit, destination, stride, sourceValues.Length);
113104
}
114105
else if (Sse.IsSupported)
115106
{
116-
Contracts.Assert(stride <= destination.Size);
117-
SseIntrinsics.MatMulP(matrix, rgposSrc, sourceValues, posMin, iposMin, iposLimit, destination, stride, sourceValues.Size);
107+
Contracts.Assert(stride <= destination.Length);
108+
SseIntrinsics.MatMulP(matrix, rgposSrc, sourceValues, posMin, iposMin, iposLimit, destination, stride, sourceValues.Length);
118109
}
119110
else
120111
{
121-
Contracts.Assert(stride <= destination.Size);
112+
Contracts.Assert(stride <= destination.Length);
122113
for (int i = 0; i < stride; i++)
123114
{
124115
float dotProduct = 0;
125116
for (int j = iposMin; j < iposLimit; j++)
126117
{
127118
int col = rgposSrc[j] - posMin;
128-
dotProduct += matrix[i * sourceValues.Size + col] * sourceValues[col];
119+
dotProduct += matrix[i * sourceValues.Length + col] * sourceValues[col];
129120
}
130121
destination[i] = dotProduct;
131122
}
@@ -636,71 +627,6 @@ public static float L2DistSquared(ReadOnlySpan<float> left, ReadOnlySpan<float>
636627
}
637628
}
638629

639-
public static void ZeroMatrixItems(AlignedArray destination, int ccol, int cfltRow, int[] indices)
640-
{
641-
Contracts.Assert(ccol > 0);
642-
Contracts.Assert(ccol <= cfltRow);
643-
644-
if (ccol == cfltRow)
645-
{
646-
ZeroItemsU(destination, destination.Size, indices, indices.Length);
647-
}
648-
else
649-
{
650-
ZeroMatrixItemsCore(destination, destination.Size, ccol, cfltRow, indices, indices.Length);
651-
}
652-
}
653-
654-
private static unsafe void ZeroItemsU(AlignedArray destination, int c, int[] indices, int cindices)
655-
{
656-
fixed (float* pdst = &destination.Items[0])
657-
fixed (int* pidx = &indices[0])
658-
{
659-
for (int i = 0; i < cindices; ++i)
660-
{
661-
int index = pidx[i];
662-
Contracts.Assert(index >= 0);
663-
Contracts.Assert(index < c);
664-
pdst[index] = 0;
665-
}
666-
}
667-
}
668-
669-
private static unsafe void ZeroMatrixItemsCore(AlignedArray destination, int c, int ccol, int cfltRow, int[] indices, int cindices)
670-
{
671-
fixed (float* pdst = &destination.Items[0])
672-
fixed (int* pidx = &indices[0])
673-
{
674-
int ivLogMin = 0;
675-
int ivLogLim = ccol;
676-
int ivPhyMin = 0;
677-
678-
for (int i = 0; i < cindices; ++i)
679-
{
680-
int index = pidx[i];
681-
Contracts.Assert(index >= 0);
682-
Contracts.Assert(index < c);
683-
684-
int col = index - ivLogMin;
685-
if ((uint)col >= (uint)ccol)
686-
{
687-
Contracts.Assert(ivLogMin > index || index >= ivLogLim);
688-
689-
int row = index / ccol;
690-
ivLogMin = row * ccol;
691-
ivLogLim = ivLogMin + ccol;
692-
ivPhyMin = row * cfltRow;
693-
694-
Contracts.Assert(index >= ivLogMin);
695-
Contracts.Assert(index < ivLogLim);
696-
col = index - ivLogMin;
697-
}
698-
699-
pdst[ivPhyMin + col] = 0;
700-
}
701-
}
702-
}
703-
704630
public static void SdcaL1UpdateDense(float primalUpdate, int count, ReadOnlySpan<float> source, float threshold, Span<float> v, Span<float> w)
705631
{
706632
Contracts.AssertNonEmpty(source);

0 commit comments

Comments
 (0)