Skip to content

Commit e77f24e

Browse files
briancyluisafern
authored andcommitted
Change SseUtils call sites to call CpuMathUtils instead (#672)
* Changed SseUtils call sites to call CpuMathUtils instead * Minor changes from SseUtils to CpuMathUtils * Fixed CbAlign naming to SseCbAlign, and removed the CpuUtils switch in RffTransform * Changed naming of SseCbAlign to Vector128Alignment
1 parent b51d9f9 commit e77f24e

File tree

13 files changed

+80
-75
lines changed

13 files changed

+80
-75
lines changed

src/Microsoft.ML.CpuMath/AlignedMatrix.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -550,7 +550,7 @@ public void ZeroItems(int[] indices)
550550

551551
// REVIEW: Ideally, we'd adjust the indices once so we wouldn't need to
552552
// repeatedly deal with padding adjustments.
553-
SseUtils.ZeroMatrixItems(Items, ColCount, ColCountPhy, indices);
553+
CpuMathUtils.ZeroMatrixItems(Items, ColCount, ColCountPhy, indices);
554554
}
555555
}
556556

src/Microsoft.ML.CpuMath/CpuAligenedMathUtils.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public static void AssertCompatible(ICpuFullMatrix values)
1616
#if DEBUG
1717
var mat = values as TMatrix;
1818
Contracts.AssertValue(mat);
19-
Contracts.Assert(mat.Items.CbAlign == SseUtils.CbAlign);
19+
Contracts.Assert(mat.Items.CbAlign == CpuMathUtils.Vector128Alignment);
2020
#endif
2121
}
2222

@@ -29,7 +29,7 @@ public static void AssertCompatible(ICpuVector values)
2929
#if DEBUG
3030
CpuAlignedVector vec = values as CpuAlignedVector;
3131
Contracts.AssertValue(vec);
32-
Contracts.Assert(vec.Items.CbAlign == SseUtils.CbAlign);
32+
Contracts.Assert(vec.Items.CbAlign == CpuMathUtils.Vector128Alignment);
3333
#endif
3434
}
3535

@@ -89,7 +89,7 @@ public static void MatTimesSrc(bool add, ICpuFullMatrix mat, ICpuVector src, ICp
8989
bool colMajor = typeof(TMatrix) == typeof(CpuAlignedMatrixCol);
9090
AssertCompatible(mat, src, dst);
9191
var m = A(mat);
92-
SseUtils.MatTimesSrc(colMajor, add, m.Items, A(src).Items, A(dst).Items, m.RunCnt);
92+
CpuMathUtils.MatTimesSrc(colMajor, add, m.Items, A(src).Items, A(dst).Items, m.RunCnt);
9393
}
9494

9595
/// <summary>
@@ -108,7 +108,7 @@ public static void MatTranTimesSrc(bool add, ICpuFullMatrix mat, ICpuVector src,
108108
bool colMajor = typeof(TMatrix) == typeof(CpuAlignedMatrixCol);
109109
AssertCompatible(mat, dst, src);
110110
var m = A(mat);
111-
SseUtils.MatTimesSrc(!colMajor, add, m.Items, A(src).Items, A(dst).Items, m.RunCnt);
111+
CpuMathUtils.MatTimesSrc(!colMajor, add, m.Items, A(src).Items, A(dst).Items, m.RunCnt);
112112
}
113113
}
114114

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ namespace Microsoft.ML.Runtime.Internal.CpuMath
99
{
1010
public static partial class CpuMathUtils
1111
{
12+
// The count of bytes in Vector128<T>, corresponding to _cbAlign in AlignedArray
13+
public const int Vector128Alignment = 16;
14+
1215
public static void MatTimesSrc(bool tran, bool add, AlignedArray mat, AlignedArray src, AlignedArray dst, int crun)
1316
{
1417
Contracts.Assert(mat.Size == dst.Size * src.Size);

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ namespace Microsoft.ML.Runtime.Internal.CpuMath
66
{
77
public static partial class CpuMathUtils
88
{
9+
// The count of bytes in Vector128<T>, corresponding to _cbAlign in AlignedArray
10+
public const int Vector128Alignment = 16;
11+
912
public static void MatTimesSrc(bool tran, bool add, AlignedArray mat, AlignedArray src, AlignedArray dst, int crun) => SseUtils.MatTimesSrc(tran, add, mat, src, dst, crun);
1013

1114
public static void MatTimesSrc(bool tran, bool add, AlignedArray mat, int[] rgposSrc, AlignedArray srcValues,

src/Microsoft.ML.CpuMath/SseIntrinsics.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,21 @@ namespace Microsoft.ML.Runtime.Internal.CpuMath
2222
{
2323
internal static class SseIntrinsics
2424
{
25-
private const int CbAlign = 16;
25+
// The count of bytes in Vector128<T>, corresponding to _cbAlign in AlignedArray
26+
private const int Vector128Alignment = 16;
2627

2728
private static bool Compat(AlignedArray a)
2829
{
2930
Contracts.AssertValue(a);
3031
Contracts.Assert(a.Size > 0);
31-
return a.CbAlign == CbAlign;
32+
return a.CbAlign == Vector128Alignment;
3233
}
3334

3435
private static unsafe float* Ptr(AlignedArray a, float* p)
3536
{
3637
Contracts.AssertValue(a);
3738
float* q = p + a.GetBase((long)p);
38-
Contracts.Assert(((long)q & (CbAlign - 1)) == 0);
39+
Contracts.Assert(((long)q & (Vector128Alignment - 1)) == 0);
3940
return q;
4041
}
4142

src/Microsoft.ML.Data/Depricated/Vector/VBufferMathUtils.cs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@ public static Float NormSquared(in VBuffer<Float> a)
2222
{
2323
if (a.Count == 0)
2424
return 0;
25-
return SseUtils.SumSq(a.Values, 0, a.Count);
25+
return CpuMathUtils.SumSq(a.Values, 0, a.Count);
2626
}
2727

2828
/// <summary>
2929
/// Returns the L2 norm squared of the vector (sum of squares of the components).
3030
/// </summary>
3131
public static Float NormSquared(Float[] a, int offset, int count)
3232
{
33-
return SseUtils.SumSq(a, offset, count);
33+
return CpuMathUtils.SumSq(a, offset, count);
3434
}
3535

3636
/// <summary>
@@ -50,7 +50,7 @@ public static Float L1Norm(ref VBuffer<Float> a)
5050
{
5151
if (a.Count == 0)
5252
return 0;
53-
return SseUtils.SumAbs(a.Values, 0, a.Count);
53+
return CpuMathUtils.SumAbs(a.Values, 0, a.Count);
5454
}
5555

5656
/// <summary>
@@ -61,7 +61,7 @@ public static Float MaxNorm(ref VBuffer<Float> a)
6161
{
6262
if (a.Count == 0)
6363
return 0;
64-
return SseUtils.MaxAbs(a.Values, 0, a.Count);
64+
return CpuMathUtils.MaxAbs(a.Values, 0, a.Count);
6565
}
6666

6767
/// <summary>
@@ -71,7 +71,7 @@ public static Float Sum(ref VBuffer<Float> a)
7171
{
7272
if (a.Count == 0)
7373
return 0;
74-
return SseUtils.Sum(a.Values, 0, a.Count);
74+
return CpuMathUtils.Sum(a.Values, 0, a.Count);
7575
}
7676

7777
/// <summary>
@@ -84,7 +84,7 @@ public static void ScaleBy(ref VBuffer<Float> dst, Float c)
8484
if (c == 1 || dst.Count == 0)
8585
return;
8686
if (c != 0)
87-
SseUtils.Scale(c, dst.Values, dst.Count);
87+
CpuMathUtils.Scale(c, dst.Values, dst.Count);
8888
else // Maintain density of dst.
8989
Array.Clear(dst.Values, 0, dst.Count);
9090
// REVIEW: Any benefit in sparsifying?
@@ -114,7 +114,7 @@ public static void ScaleBy(in VBuffer<Float> src, ref VBuffer<Float> dst, Float
114114
if (c == 0)
115115
Array.Clear(dstValues, 0, length);
116116
else
117-
SseUtils.Scale(c, src.Values, dstValues, length);
117+
CpuMathUtils.Scale(c, src.Values, dstValues, length);
118118
dst = new VBuffer<Float>(length, dstValues, dst.Indices);
119119
}
120120
else
@@ -124,7 +124,7 @@ public static void ScaleBy(in VBuffer<Float> src, ref VBuffer<Float> dst, Float
124124
if (c == 0)
125125
Array.Clear(dstValues, 0, count);
126126
else
127-
SseUtils.Scale(c, src.Values, dstValues, count);
127+
CpuMathUtils.Scale(c, src.Values, dstValues, count);
128128
dst = new VBuffer<Float>(length, count, dstValues, dstIndices);
129129
}
130130
}
@@ -142,9 +142,9 @@ public static void Add(ref VBuffer<Float> src, ref VBuffer<Float> dst)
142142
if (dst.IsDense)
143143
{
144144
if (src.IsDense)
145-
SseUtils.Add(src.Values, dst.Values, src.Length);
145+
CpuMathUtils.Add(src.Values, dst.Values, src.Length);
146146
else
147-
SseUtils.Add(src.Values, src.Indices, dst.Values, src.Count);
147+
CpuMathUtils.Add(src.Values, src.Indices, dst.Values, src.Count);
148148
return;
149149
}
150150
// REVIEW: Should we use SSE for any of these possibilities?
@@ -168,9 +168,9 @@ public static void AddMult(ref VBuffer<Float> src, Float c, ref VBuffer<Float> d
168168
if (dst.IsDense)
169169
{
170170
if (src.IsDense)
171-
SseUtils.AddScale(c, src.Values, dst.Values, src.Length);
171+
CpuMathUtils.AddScale(c, src.Values, dst.Values, src.Length);
172172
else
173-
SseUtils.AddScale(c, src.Values, src.Indices, dst.Values, src.Count);
173+
CpuMathUtils.AddScale(c, src.Values, src.Indices, dst.Values, src.Count);
174174
return;
175175
}
176176
// REVIEW: Should we use SSE for any of these possibilities?
@@ -197,7 +197,7 @@ public static void AddMult(ref VBuffer<Float> src, Float c, ref VBuffer<Float> d
197197
if (dst.IsDense && src.IsDense)
198198
{
199199
Float[] resValues = Utils.Size(res.Values) >= length ? res.Values : new Float[length];
200-
SseUtils.AddScaleCopy(c, src.Values, dst.Values, resValues, length);
200+
CpuMathUtils.AddScaleCopy(c, src.Values, dst.Values, resValues, length);
201201
res = new VBuffer<Float>(length, resValues, res.Indices);
202202
return;
203203
}
@@ -239,9 +239,9 @@ public static void AddMultWithOffset(ref VBuffer<Float> src, Float c, ref VBuffe
239239
{
240240
// This is by far the most common case.
241241
if (src.IsDense)
242-
SseUtils.AddScale(c, src.Values, dst.Values, offset, src.Count);
242+
CpuMathUtils.AddScale(c, src.Values, dst.Values, offset, src.Count);
243243
else
244-
SseUtils.AddScale(c, src.Values, src.Indices, dst.Values, offset, src.Count);
244+
CpuMathUtils.AddScale(c, src.Values, src.Indices, dst.Values, offset, src.Count);
245245
return;
246246
}
247247
// REVIEW: Perhaps implementing an ApplyInto with an offset would be more

src/Microsoft.ML.Data/Depricated/Vector/VectorUtils.cs

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public static Float DotProduct(Float[] a, Float[] b)
2424
{
2525
Contracts.Check(Utils.Size(a) == Utils.Size(b), "Arrays must have the same length");
2626
Contracts.Check(Utils.Size(a) > 0);
27-
return SseUtils.DotProductDense(a, b, a.Length);
27+
return CpuMathUtils.DotProductDense(a, b, a.Length);
2828
}
2929

3030
public static Float DotProduct(Float[] a, ref VBuffer<Float> b)
@@ -33,8 +33,8 @@ public static Float DotProduct(Float[] a, ref VBuffer<Float> b)
3333
if (b.Count == 0)
3434
return 0;
3535
if (b.IsDense)
36-
return SseUtils.DotProductDense(a, b.Values, b.Length);
37-
return SseUtils.DotProductSparse(a, b.Values, b.Indices, b.Count);
36+
return CpuMathUtils.DotProductDense(a, b.Values, b.Length);
37+
return CpuMathUtils.DotProductSparse(a, b.Values, b.Indices, b.Count);
3838
}
3939

4040
public static Float DotProduct(ref VBuffer<Float> a, ref VBuffer<Float> b)
@@ -47,12 +47,12 @@ public static Float DotProduct(ref VBuffer<Float> a, ref VBuffer<Float> b)
4747
if (a.IsDense)
4848
{
4949
if (b.IsDense)
50-
return SseUtils.DotProductDense(a.Values, b.Values, a.Length);
51-
return SseUtils.DotProductSparse(a.Values, b.Values, b.Indices, b.Count);
50+
return CpuMathUtils.DotProductDense(a.Values, b.Values, a.Length);
51+
return CpuMathUtils.DotProductSparse(a.Values, b.Values, b.Indices, b.Count);
5252
}
5353

5454
if (b.IsDense)
55-
return SseUtils.DotProductSparse(b.Values, a.Values, a.Indices, a.Count);
55+
return CpuMathUtils.DotProductSparse(b.Values, a.Values, a.Indices, a.Count);
5656
return DotProductSparse(a.Values, a.Indices, 0, a.Count, b.Values, b.Indices, 0, b.Count, 0);
5757
}
5858

@@ -159,7 +159,7 @@ public static void MulElementWise(ref VBuffer<Float> a, ref VBuffer<Float> dst)
159159
Contracts.Check(a.Length == dst.Length, "Vectors must have the same dimensionality.");
160160

161161
if (a.IsDense && dst.IsDense)
162-
SseUtils.MulElementWise(a.Values, dst.Values, dst.Values, a.Length);
162+
CpuMathUtils.MulElementWise(a.Values, dst.Values, dst.Values, a.Length);
163163
else
164164
VBufferUtils.ApplyWithEitherDefined(ref a, ref dst, (int ind, Float v1, ref Float v2) => { v2 *= v1; });
165165
}
@@ -228,11 +228,11 @@ private static Float L2DistSquaredHalfSparse(Float[] valuesA, int lengthA, Float
228228
Contracts.Assert(0 <= countB && countB <= Utils.Size(indicesB));
229229
Contracts.Assert(countB <= Utils.Size(valuesB));
230230

231-
var normA = SseUtils.SumSq(valuesA, 0, lengthA);
231+
var normA = CpuMathUtils.SumSq(valuesA, 0, lengthA);
232232
if (countB == 0)
233233
return normA;
234-
var normB = SseUtils.SumSq(valuesB, 0, countB);
235-
var dotP = SseUtils.DotProductSparse(valuesA, valuesB, indicesB, countB);
234+
var normB = CpuMathUtils.SumSq(valuesB, 0, countB);
235+
var dotP = CpuMathUtils.DotProductSparse(valuesA, valuesB, indicesB, countB);
236236
var res = normA + normB - 2 * dotP;
237237
return res < 0 ? 0 : res;
238238
}
@@ -246,7 +246,7 @@ private static Float L2DiffSquaredDense(Float[] valuesA, Float[] valuesB, int le
246246

247247
if (length == 0)
248248
return 0;
249-
return SseUtils.L2DistSquared(valuesA, valuesB, length);
249+
return CpuMathUtils.L2DistSquared(valuesA, valuesB, length);
250250
}
251251

252252
/// <summary>
@@ -267,8 +267,8 @@ public static Float DotProductWithOffset(ref VBuffer<Float> a, int offset, ref V
267267
if (a.IsDense)
268268
{
269269
if (b.IsDense)
270-
return SseUtils.DotProductDense(a.Values, offset, b.Values, b.Length);
271-
return SseUtils.DotProductSparse(a.Values, offset, b.Values, b.Indices, b.Count);
270+
return CpuMathUtils.DotProductDense(a.Values, offset, b.Values, b.Length);
271+
return CpuMathUtils.DotProductSparse(a.Values, offset, b.Values, b.Indices, b.Count);
272272
}
273273
else
274274
{
@@ -314,8 +314,8 @@ public static Float DotProductWithOffset(Float[] a, int offset, ref VBuffer<Floa
314314
return 0;
315315

316316
if (b.IsDense)
317-
return SseUtils.DotProductDense(a, offset, b.Values, b.Length);
318-
return SseUtils.DotProductSparse(a, offset, b.Values, b.Indices, b.Count);
317+
return CpuMathUtils.DotProductDense(a, offset, b.Values, b.Length);
318+
return CpuMathUtils.DotProductSparse(a, offset, b.Values, b.Indices, b.Count);
319319
}
320320

321321
private static Float DotProductSparse(Float[] aValues, int[] aIndices, int ia, int iaLim, Float[] bValues, int[] bIndices, int ib, int ibLim, int offset)
@@ -434,7 +434,7 @@ public static void Add(Float[] src, Float[] dst)
434434
Contracts.CheckParam(src.Length == dst.Length, nameof(dst), "Arrays must have the same dimensionality.");
435435
if (src.Length == 0)
436436
return;
437-
SseUtils.Add(src, dst, src.Length);
437+
CpuMathUtils.Add(src, dst, src.Length);
438438
}
439439

440440
/// <summary>
@@ -452,7 +452,7 @@ public static void AddMult(ref VBuffer<Float> src, Float[] dst, Float c)
452452
return;
453453

454454
if (src.IsDense)
455-
SseUtils.AddScale(c, src.Values, dst, src.Count);
455+
CpuMathUtils.AddScale(c, src.Values, dst, src.Count);
456456
else
457457
{
458458
for (int i = 0; i < src.Count; i++)
@@ -502,15 +502,15 @@ public static void AddMult(Float[] src, Float[] dst, Float c)
502502
if (c == 0)
503503
return;
504504

505-
SseUtils.AddScale(c, src, dst, src.Length);
505+
CpuMathUtils.AddScale(c, src, dst, src.Length);
506506
}
507507

508508
/// <summary>
509509
/// Returns the L2 norm of the vector (sum of squares of the components).
510510
/// </summary>
511511
public static Float Norm(Float[] a)
512512
{
513-
return MathUtils.Sqrt(SseUtils.SumSq(a, a.Length));
513+
return MathUtils.Sqrt(CpuMathUtils.SumSq(a, a.Length));
514514
}
515515

516516
/// <summary>
@@ -520,7 +520,7 @@ public static Float Sum(Float[] a)
520520
{
521521
if (a == null || a.Length == 0)
522522
return 0;
523-
return SseUtils.Sum(a, a.Length);
523+
return CpuMathUtils.Sum(a, a.Length);
524524
}
525525

526526
/// <summary>
@@ -534,7 +534,7 @@ public static void ScaleBy(Float[] dst, Float c)
534534
return;
535535

536536
if (c != 0)
537-
SseUtils.Scale(c, dst, dst.Length);
537+
CpuMathUtils.Scale(c, dst, dst.Length);
538538
else
539539
Array.Clear(dst, 0, dst.Length);
540540
}

src/Microsoft.ML.KMeansClustering/KMeansPlusPlusTrainer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -830,7 +830,7 @@ public static void Initialize(IHost host, int numThreads, IChannel ch, FeatureFl
830830
{
831831
weights = new Float[totalSamples];
832832
for (int i = 0; i < workStateWeights.Length; i++)
833-
SseUtils.Add(workStateWeights[i], weights, totalSamples);
833+
CpuMathUtils.Add(workStateWeights[i], weights, totalSamples);
834834
},
835835
ref weightBuffer, ref totalWeights);
836836
#if DEBUG

src/Microsoft.ML.StandardLearners/Standard/LinearClassificationTrainer.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -797,9 +797,9 @@ protected virtual void TrainWithoutLock(IProgressChannelProvider progress, Float
797797
}
798798

799799
if (features.IsDense)
800-
SseUtils.SdcaL1UpdateDense(primalUpdate, features.Length, features.Values, l1Threshold, l1IntermediateWeights[0].Values, weights[0].Values);
800+
CpuMathUtils.SdcaL1UpdateDense(primalUpdate, features.Length, features.Values, l1Threshold, l1IntermediateWeights[0].Values, weights[0].Values);
801801
else if (features.Count > 0)
802-
SseUtils.SdcaL1UpdateSparse(primalUpdate, features.Length, features.Values, features.Indices, features.Count, l1Threshold, l1IntermediateWeights[0].Values, weights[0].Values);
802+
CpuMathUtils.SdcaL1UpdateSparse(primalUpdate, features.Length, features.Values, features.Indices, features.Count, l1Threshold, l1IntermediateWeights[0].Values, weights[0].Values);
803803
}
804804

805805
break;

src/Microsoft.ML.StandardLearners/Standard/SdcaMultiClass.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -175,9 +175,9 @@ protected override void TrainWithoutLock(IProgressChannelProvider progress, Floa
175175
}
176176

177177
if (features.IsDense)
178-
SseUtils.SdcaL1UpdateDense(-primalUpdate, features.Length, features.Values, l1Threshold, l1IntermediateWeights[iClass].Values, weights[iClass].Values);
178+
CpuMathUtils.SdcaL1UpdateDense(-primalUpdate, features.Length, features.Values, l1Threshold, l1IntermediateWeights[iClass].Values, weights[iClass].Values);
179179
else if (features.Count > 0)
180-
SseUtils.SdcaL1UpdateSparse(-primalUpdate, features.Length, features.Values, features.Indices, features.Count, l1Threshold, l1IntermediateWeights[iClass].Values, weights[iClass].Values);
180+
CpuMathUtils.SdcaL1UpdateSparse(-primalUpdate, features.Length, features.Values, features.Indices, features.Count, l1Threshold, l1IntermediateWeights[iClass].Values, weights[iClass].Values);
181181
}
182182

183183
break;
@@ -202,9 +202,9 @@ protected override void TrainWithoutLock(IProgressChannelProvider progress, Floa
202202
: 0;
203203

204204
if (features.IsDense)
205-
SseUtils.SdcaL1UpdateDense(labelPrimalUpdate, features.Length, features.Values, l1Threshold, l1IntermediateWeights[label].Values, weights[label].Values);
205+
CpuMathUtils.SdcaL1UpdateDense(labelPrimalUpdate, features.Length, features.Values, l1Threshold, l1IntermediateWeights[label].Values, weights[label].Values);
206206
else if (features.Count > 0)
207-
SseUtils.SdcaL1UpdateSparse(labelPrimalUpdate, features.Length, features.Values, features.Indices, features.Count, l1Threshold, l1IntermediateWeights[label].Values, weights[label].Values);
207+
CpuMathUtils.SdcaL1UpdateSparse(labelPrimalUpdate, features.Length, features.Values, features.Indices, features.Count, l1Threshold, l1IntermediateWeights[label].Values, weights[label].Values);
208208
}
209209

210210
rowCount++;

0 commit comments

Comments
 (0)