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

Switch and/or to use new cudf binops to improve performance #4501

Merged
merged 4 commits into from
Jan 21, 2022
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
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019-2021, NVIDIA CORPORATION.
* Copyright (c) 2019-2022, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -271,27 +271,11 @@ trait GpuBinaryExpression extends ShimBinaryExpression with GpuExpression {

trait GpuBinaryOperator extends BinaryOperator with GpuBinaryExpression

object CudfBinaryExpression {
lazy val opToAstMap: Map[BinaryOp, ast.BinaryOperator] = Map(
BinaryOp.ADD -> ast.BinaryOperator.ADD,
BinaryOp.BITWISE_AND -> ast.BinaryOperator.BITWISE_AND,
BinaryOp.BITWISE_OR -> ast.BinaryOperator.BITWISE_OR,
BinaryOp.BITWISE_XOR -> ast.BinaryOperator.BITWISE_XOR,
BinaryOp.GREATER -> ast.BinaryOperator.GREATER,
BinaryOp.GREATER_EQUAL -> ast.BinaryOperator.GREATER_EQUAL,
BinaryOp.LESS -> ast.BinaryOperator.LESS,
BinaryOp.LESS_EQUAL -> ast.BinaryOperator.LESS_EQUAL,
BinaryOp.LOGICAL_AND -> ast.BinaryOperator.NULL_LOGICAL_AND,
BinaryOp.LOGICAL_OR -> ast.BinaryOperator.NULL_LOGICAL_OR,
BinaryOp.MUL -> ast.BinaryOperator.MUL,
BinaryOp.POW -> ast.BinaryOperator.POW,
BinaryOp.SUB -> ast.BinaryOperator.SUB)
}

trait CudfBinaryExpression extends GpuBinaryExpression {
def binaryOp: BinaryOp
def outputTypeOverride: DType = null
def castOutputAtEnd: Boolean = false
def astOperator: Option[ast.BinaryOperator] = None

def outputType(l: BinaryOperable, r: BinaryOperable): DType = {
val over = outputTypeOverride
Expand Down Expand Up @@ -339,7 +323,7 @@ trait CudfBinaryExpression extends GpuBinaryExpression {
}

override def convertToAst(numFirstTableColumns: Int): ast.AstExpression = {
val astOp = CudfBinaryExpression.opToAstMap.getOrElse(binaryOp,
val astOp = astOperator.getOrElse(
throw new IllegalStateException(s"$this is not supported by AST"))
assert(left.dataType == right.dataType)
new ast.BinaryOperation(astOp,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@

package com.nvidia.spark.rapids

import ai.rapids.cudf.{ColumnVector, NullPolicy, Scalar, ScanAggregation, ScanType, Table, UnaryOp}
import ai.rapids.cudf.ast
import ai.rapids.cudf.{BinaryOp, ColumnVector, DType, NullPolicy, Scalar, ScanAggregation, ScanType, Table, UnaryOp}
import com.nvidia.spark.rapids.RapidsPluginImplicits._
import com.nvidia.spark.rapids.shims.v2.ShimExpression

Expand Down Expand Up @@ -441,15 +440,8 @@ case class GpuCaseWhen(
cumulativePred match {
case Some(prev) =>
withResource(prev) { _ =>
val result = withResource(new Table(prev.getBase, whenBool.getBase)) { t =>
val or = new ast.BinaryOperation(ast.BinaryOperator.NULL_LOGICAL_OR,
new ast.ColumnReference(0),
new ast.ColumnReference(1)
)
withResource(or.compile()) {
_.computeColumn(t)
}
}
val result = prev.getBase.binaryOp(BinaryOp.NULL_LOGICAL_OR,
whenBool.getBase, DType.BOOL8)
GpuColumnVector.from(result, DataTypes.BooleanType)
}
case _ =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package org.apache.spark.sql.rapids
import java.math.BigInteger

import ai.rapids.cudf._
import ai.rapids.cudf.ast.BinaryOperator
import com.nvidia.spark.rapids._
import com.nvidia.spark.rapids.RapidsPluginImplicits._
import com.nvidia.spark.rapids.shims.v2.ShimExpression
Expand Down Expand Up @@ -219,6 +220,7 @@ case class GpuAdd(
override def symbol: String = "+"

override def binaryOp: BinaryOp = BinaryOp.ADD
override def astOperator: Option[BinaryOperator] = Some(ast.BinaryOperator.ADD)

override def doColumnar(lhs: BinaryOperable, rhs: BinaryOperable): ColumnVector = {
val ret = super.doColumnar(lhs, rhs)
Expand Down Expand Up @@ -246,6 +248,7 @@ case class GpuSubtract(
override def symbol: String = "-"

override def binaryOp: BinaryOp = BinaryOp.SUB
override def astOperator: Option[BinaryOperator] = Some(ast.BinaryOperator.SUB)

private[this] def basicOpOverflowCheck(
lhs: BinaryOperable,
Expand Down Expand Up @@ -534,6 +537,7 @@ case class GpuMultiply(
override def symbol: String = "*"

override def binaryOp: BinaryOp = BinaryOp.MUL
override def astOperator: Option[BinaryOperator] = Some(ast.BinaryOperator.MUL)
}

object GpuDivModLike extends Arm {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020-2021, NVIDIA CORPORATION.
* Copyright (c) 2020-2022, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,7 +16,8 @@

package org.apache.spark.sql.rapids

import ai.rapids.cudf.{BinaryOp, ColumnVector, DType, Scalar, UnaryOp}
import ai.rapids.cudf.{ast, BinaryOp, ColumnVector, DType, Scalar, UnaryOp}
import ai.rapids.cudf.ast.BinaryOperator
import com.nvidia.spark.rapids._

import org.apache.spark.sql.catalyst.expressions.{ExpectsInputTypes, Expression, ImplicitCastInputTypes}
Expand Down Expand Up @@ -120,6 +121,7 @@ case class GpuBitwiseAnd(left: Expression, right: Expression) extends CudfBinary
override def symbol: String = "&"

override def binaryOp: BinaryOp = BinaryOp.BITWISE_AND
override def astOperator: Option[BinaryOperator] = Some(ast.BinaryOperator.BITWISE_AND)
}

case class GpuBitwiseOr(left: Expression, right: Expression) extends CudfBinaryArithmetic {
Expand All @@ -128,6 +130,7 @@ case class GpuBitwiseOr(left: Expression, right: Expression) extends CudfBinaryA
override def symbol: String = "|"

override def binaryOp: BinaryOp = BinaryOp.BITWISE_OR
override def astOperator: Option[BinaryOperator] = Some(ast.BinaryOperator.BITWISE_OR)
}

case class GpuBitwiseXor(left: Expression, right: Expression) extends CudfBinaryArithmetic {
Expand All @@ -136,6 +139,7 @@ case class GpuBitwiseXor(left: Expression, right: Expression) extends CudfBinary
override def symbol: String = "^"

override def binaryOp: BinaryOp = BinaryOp.BITWISE_XOR
override def astOperator: Option[BinaryOperator] = Some(ast.BinaryOperator.BITWISE_XOR)
}

case class GpuBitwiseNot(child: Expression) extends CudfUnaryExpression with ExpectsInputTypes {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019-2021, NVIDIA CORPORATION.
* Copyright (c) 2019-2022, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -19,6 +19,7 @@ package org.apache.spark.sql.rapids
import java.io.Serializable

import ai.rapids.cudf._
import ai.rapids.cudf.ast.BinaryOperator
import com.nvidia.spark.rapids._
import com.nvidia.spark.rapids.RapidsPluginImplicits.ReallyAGpuExpression

Expand Down Expand Up @@ -650,6 +651,7 @@ case class GpuRound(child: Expression, scale: Expression) extends
case class GpuPow(left: Expression, right: Expression)
extends CudfBinaryMathExpression("POWER") {
override def binaryOp: BinaryOp = BinaryOp.POW
override def astOperator: Option[BinaryOperator] = Some(ast.BinaryOperator.POW)
override def outputTypeOverride: DType = DType.FLOAT64
}

Expand Down
Loading