Skip to content
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

Implement vectorized binary arithmetic operations #6854

Merged
merged 5 commits into from
Oct 16, 2023
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
8 changes: 1 addition & 7 deletions src/Microsoft.Data.Analysis/BitUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,6 @@ public static void SetBits(Span<byte> data, long index, long length, bool value)
}
}

public static void ElementwiseAnd(ReadOnlySpan<byte> left, ReadOnlySpan<byte> right, Span<byte> result)
{
for (var i = 0; i < left.Length; i++)
result[i] = (byte)(left[i] & right[i]);
}

/// <summary>
/// Returns the population count (number of bits set) in a span of bytes starting
/// at 0 bit and limiting to length of bits.
Expand All @@ -167,7 +161,7 @@ public static long GetBitCount(ReadOnlySpan<byte> span, long length)
var partialByte = span[endByteIndex];
for (var j = 0; j < endBitOffset; j++)
{
count += GetBit(partialByte, j) ? 0 : 1;
count += GetBit(partialByte, j) ? 1 : 0;
}
}

Expand Down
22 changes: 0 additions & 22 deletions src/Microsoft.Data.Analysis/ColumnArithmetic.OperationEnums.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,6 @@ internal enum BinaryOperation
Xor,
}

internal enum BinaryScalarOperation
{
Add,
Subtract,
Multiply,
Divide,
Modulo,
And,
Or,
Xor,
}

internal enum BinaryIntOperation
{
LeftShift,
Expand All @@ -45,14 +33,4 @@ internal enum ComparisonOperation
ElementwiseGreaterThan,
ElementwiseLessThan,
}

internal enum ComparisonScalarOperation
{
ElementwiseEquals,
ElementwiseNotEquals,
ElementwiseGreaterThanOrEqual,
ElementwiseLessThanOrEqual,
ElementwiseGreaterThan,
ElementwiseLessThan,
}
}
18 changes: 0 additions & 18 deletions src/Microsoft.Data.Analysis/ColumnArithmetic.OperationEnums.tt
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,6 @@ namespace Microsoft.Data.Analysis
<# } #>
}

internal enum BinaryScalarOperation
{
<# foreach (MethodConfiguration method in methodConfiguration) { #>
<# if (method.MethodType == MethodType.BinaryScalar) { #>
<#=method.MethodName#>,
<# } #>
<# } #>
}

internal enum BinaryIntOperation
{
<# foreach (MethodConfiguration method in methodConfiguration) { #>
Expand All @@ -45,15 +36,6 @@ namespace Microsoft.Data.Analysis
<# if (method.MethodType == MethodType.Comparison) { #>
<#=method.MethodName#>,
<# } #>
<# } #>
}

internal enum ComparisonScalarOperation
{
<# foreach (MethodConfiguration method in methodConfiguration) { #>
<# if (method.MethodType == MethodType.ComparisonScalar) { #>
<#=method.MethodName#>,
<# } #>
<# } #>
}
}
49 changes: 24 additions & 25 deletions src/Microsoft.Data.Analysis/ColumnArithmeticTemplate.ttinclude
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@
<#+
public class TypeConfiguration
{
public TypeConfiguration(string typeName, string classPrefix = null, string oneLiteral = "1", string zeroLiteral = "0", bool supportsNumeric = true, bool supportsBitwise = true, IEnumerable<string> unsupportedMethods = null)
public TypeConfiguration(string typeName, string classPrefix = null, string oneLiteral = "1", string zeroLiteral = "0", bool supportsNumeric = true, bool supportsBitwise = true, IEnumerable<string> unsupportedMethods = null, bool supportsVectorization = false)
{
TypeName = typeName;
ClassPrefix = classPrefix ?? char.ToUpper(typeName[0]) + typeName.Substring(1);
OneLiteral = oneLiteral;
ZeroLiteral = zeroLiteral;
SupportsNumeric = supportsNumeric;
SupportsBitwise = supportsBitwise;
SupportsVectorization = supportsVectorization;
UnsupportedMethods = new HashSet<string>(unsupportedMethods ?? Enumerable.Empty<string>());
}

Expand All @@ -24,14 +25,11 @@

public bool SupportsNumeric { get; }
public bool SupportsBitwise { get; }
public ISet<string> UnsupportedMethods { get; }
}
public bool SupportsVectorization { get; }

public string GenerateInPlaceStatement(string trueCondition, string falseCondition)
{
return $"inPlace ? {trueCondition} : {falseCondition}";
public ISet<string> UnsupportedMethods { get; }
}

public string GenerateIfStatementHeader(TypeConfiguration type)
{
string keyword = (type == typeConfiguration[0]) ? "if" : "else if";
Expand Down Expand Up @@ -139,18 +137,18 @@
public TypeConfiguration[] typeConfiguration = new []
{
new TypeConfiguration("bool", oneLiteral:"true", zeroLiteral:"false", supportsNumeric: false, unsupportedMethods: new[] {"LeftShift", "RightShift"}),
new TypeConfiguration("byte", unsupportedMethods: new[] {"All", "Any"}),
new TypeConfiguration("char", oneLiteral:"(char)1", zeroLiteral:"(char)0", unsupportedMethods: new[] {"All", "Any"}),
new TypeConfiguration("byte", unsupportedMethods: new[] {"All", "Any"}, supportsVectorization: true),
new TypeConfiguration("char", oneLiteral:"(char)1", zeroLiteral:"(char)0", unsupportedMethods: new[] {"All", "Any"}, supportsVectorization: true),
new TypeConfiguration("decimal", supportsBitwise: false, unsupportedMethods: new[] {"All", "Any"}),
new TypeConfiguration("double", oneLiteral:"1.0", supportsBitwise: false, unsupportedMethods: new[] {"All", "Any"}),
new TypeConfiguration("float", oneLiteral:"1.0f", supportsBitwise: false, unsupportedMethods: new[] {"All", "Any"}),
new TypeConfiguration("int", unsupportedMethods: new[] {"All", "Any"}),
new TypeConfiguration("long", unsupportedMethods: new[] {"All", "Any"}),
new TypeConfiguration("sbyte", classPrefix:"SByte", unsupportedMethods: new[] {"All", "Any"}),
new TypeConfiguration("short", unsupportedMethods: new[] {"All", "Any"}),
new TypeConfiguration("uint", classPrefix:"UInt", unsupportedMethods: new[] {"UnaryMinus", "All", "Any"}),
new TypeConfiguration("ulong", classPrefix:"ULong", unsupportedMethods: new[] {"UnaryMinus", "All", "Any"}),
new TypeConfiguration("ushort", classPrefix:"UShort", unsupportedMethods: new[] {"UnaryMinus", "All", "Any"}),
new TypeConfiguration("double", oneLiteral:"1.0", supportsBitwise: false, unsupportedMethods: new[] {"All", "Any"}, supportsVectorization: true),
new TypeConfiguration("float", oneLiteral:"1.0f", supportsBitwise: false, unsupportedMethods: new[] {"All", "Any"}, supportsVectorization: true),
new TypeConfiguration("int", unsupportedMethods: new[] {"All", "Any"}, supportsVectorization: true),
new TypeConfiguration("long", unsupportedMethods: new[] {"All", "Any"}, supportsVectorization: true),
new TypeConfiguration("sbyte", classPrefix:"SByte", unsupportedMethods: new[] {"All", "Any"}, supportsVectorization: true),
new TypeConfiguration("short", unsupportedMethods: new[] {"All", "Any"}, supportsVectorization: true),
new TypeConfiguration("uint", classPrefix:"UInt", unsupportedMethods: new[] {"UnaryMinus", "All", "Any"}, supportsVectorization: true),
new TypeConfiguration("ulong", classPrefix:"ULong", unsupportedMethods: new[] {"UnaryMinus", "All", "Any"}, supportsVectorization: true),
new TypeConfiguration("ushort", classPrefix:"UShort", unsupportedMethods: new[] {"UnaryMinus", "All", "Any"}, supportsVectorization: true),
new TypeConfiguration("DateTime", supportsBitwise: false, supportsNumeric: false, unsupportedMethods: new[] {"And", "Or", "Xor"})
};

Expand Down Expand Up @@ -227,16 +225,16 @@
new MethodConfiguration("Sum", MethodType.Reduction, "+", isNumeric:true, hasReturnValue:true, supportsRowSubsets: true, methodComments: "Returns the sum of the values at the rowIndices"),
new MethodConfiguration("Round", MethodType.ElementwiseComputation, "Math.Round", isNumeric:true, methodComments: "Calls Math.Round on each value in a column"),
};

public MethodConfiguration[] methodConfiguration = new []
{
new MethodConfiguration("Add", MethodType.Binary, "+", isNumeric:true, methodComments: "Performs element-wise addition"),
new MethodConfiguration("Add", MethodType.Binary, "+", isNumeric:true, methodComments: "Performs element-wise addition", supportsVectorization: true),
new MethodConfiguration("Add", MethodType.BinaryScalar, "+", isNumeric:true, methodComments: "Performs an element-wise addition on each column"),
new MethodConfiguration("Subtract", MethodType.Binary, "-", isNumeric:true, methodComments: "Performs element-wise subtraction"),
new MethodConfiguration("Subtract", MethodType.Binary, "-", isNumeric:true, methodComments: "Performs element-wise subtraction", supportsVectorization: true),
new MethodConfiguration("Subtract", MethodType.BinaryScalar, "-", isNumeric:true, methodComments: "Performs an element-wise subtraction on each column"),
new MethodConfiguration("Multiply", MethodType.Binary, "*", isNumeric:true, methodComments: "Performs element-wise multiplication"), // element-wise product, not matrix product
new MethodConfiguration("Multiply", MethodType.Binary, "*", isNumeric:true, methodComments: "Performs element-wise multiplication", supportsVectorization: true),
new MethodConfiguration("Multiply", MethodType.BinaryScalar, "*", isNumeric:true, methodComments: "Performs an element-wise multiplication on each column"),
new MethodConfiguration("Divide", MethodType.Binary, "/", isNumeric:true, methodComments: "Performs element-wise division"),
new MethodConfiguration("Divide", MethodType.Binary, "/", isNumeric:true, methodComments: "Performs element-wise division", supportsVectorization: true),
new MethodConfiguration("Divide", MethodType.BinaryScalar, "/", isNumeric:true, methodComments: "Performs an element-wise division on each column"),
new MethodConfiguration("Modulo", MethodType.Binary, "%", isNumeric:true, methodComments: "Performs element-wise modulus"),
new MethodConfiguration("Modulo", MethodType.BinaryScalar, "%", isNumeric:true, methodComments: "Performs an element-wise modulus operation on each column"),
Expand Down Expand Up @@ -265,7 +263,7 @@

public class MethodConfiguration
{
public MethodConfiguration(string methodName, MethodType methodType, string op = null, bool isNumeric = false, bool isBitwise = false, bool hasReturnValue = false, bool supportsRowSubsets = false, string methodComments = null)
public MethodConfiguration(string methodName, MethodType methodType, string op = null, bool isNumeric = false, bool isBitwise = false, bool hasReturnValue = false, bool supportsRowSubsets = false, string methodComments = null, bool supportsVectorization = false)
{
MethodName = methodName;
MethodType = methodType;
Expand All @@ -275,6 +273,7 @@
HasReturnValue = hasReturnValue;
SupportsRowSubsets = supportsRowSubsets;
MethodComments = methodComments;
SupportsVectorization = supportsVectorization;
}

public string ResultName => "result";
Expand Down Expand Up @@ -330,7 +329,7 @@
public MethodType MethodType { get; }
public string Operator { get; }
public string MethodComments { get; }

public bool SupportsVectorization { get; }
public string GetColumnSpecificMethodComments()
{
var str = MethodComments;
Expand Down
Loading
Loading