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 an appendMonoid Aggregator factory which yields aggregators… #501

Merged
merged 5 commits into from
Dec 4, 2015
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
41 changes: 41 additions & 0 deletions algebird-core/src/main/scala/com/twitter/algebird/Aggregator.scala
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,47 @@ object Aggregator extends java.io.Serializable {
def present(reduction: T) = reduction
}

/**
* Obtain a [[MonoidAggregator]] that uses an efficient append operation for faster aggregation
* @tparam F Data input type
* @tparam T Aggregating [[Monoid]] type
* @param appnd Function that appends the [[Monoid]]. Defines the [[append]] method for this aggregator.
* Analogous to the 'seqop' function in Scala's sequence 'aggregate' method
* @param m The [[Monoid]] type class
*/
def appendMonoid[F, T](appnd: (T, F) => T)(implicit m: Monoid[T]): MonoidAggregator[F, T, T] =
appendMonoid(appnd, identity[T]_)(m)

/**
* Obtain a [[MonoidAggregator]] that uses an efficient append operation for faster aggregation
* @tparam F Data input type
* @tparam T Aggregating [[Monoid]] type
* @tparam P Presentation (output) type
* @param appnd Function that appends the [[Monoid]]. Defines the [[append]] method for this aggregator.
* Analogous to the 'seqop' function in Scala's sequence 'aggregate' method
* @param pres The presentation function
* @param m The [[Monoid]] type class
*/
def appendMonoid[F, T, P](appnd: (T, F) => T, pres: T => P)(implicit m: Monoid[T]): MonoidAggregator[F, T, P] =
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so, appnd must satisfy the law:

appdn(appnd(Monoid.zero, f1), f2) == Monoid.plus(appnd(Monoid.zero, f1), appnd(Monoid.zero, f2))

Otherwise prepare doesn't mean what we expect. If you agree, we should add this law to the comment.

This is addressing one performance issue, which might be a very important one, but also the case of dealing with a bulk set of items (Monoid.sum/Semigroup.sumOption) is also an important optimization.

Since we expose the monoid/semigroup of an Aggregator, scalding and spark use that to get the sumOption speedup. For those cases, they will not get the benefit of an optimized append and in fact will call prepare which may be even slower. So, I think the comment "faster aggregation" is more complex and we need to point out that this could be slower than the standard approach depending on the Monoid and appnd function.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I may be missing some angle here, but that seems like an opportunity for Scalding/Spark to take better advantage of traversableOnce.aggregate(...) over individual partitions.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@erikerlandson I think the question is whether the append() optimization is more effective than whatever optimization is in sumOption. They're both about eliminating unnecessary intermediate values of the type we have a Monoid on, but in different ways - it's sorta a question of, for each reduce(left: T, right: T), whether you need the left side to actually be a T (which sumOption eliminates) or the right side to actually be a T (which append eliminates). You could imagine an appendAll which requires neither to be, but that feels even more invasive for the Aggregator to eliminate vs. the AppendMonoid like you proposed.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To put it another way, if you ultimately think of this like a def foldLeft[B](z: B)(f: (B, A) => B): B on a Seq[A], and you have some Monoid[T], the question is do we have B == T (a constraint that sumOption gets rid of) or do we have A == T (a constraint that append gets rid of), or both, or neither, and what's the most efficient space to do this aggregation in.

new MonoidAggregator[F, T, P] {
def monoid: Monoid[T] = m
def prepare(input: F): T = appnd(m.zero, input)
def present(reduction: T): P = pres(reduction)

override def apply(inputs: TraversableOnce[F]): P = present(agg(inputs))

override def applyOption(inputs: TraversableOnce[F]): Option[P] =
if (inputs.isEmpty) None else Some(apply(inputs))

override def append(l: T, r: F): T = appnd(l, r)

override def appendAll(old: T, items: TraversableOnce[F]): T = reduce(old, agg(items))

override def appendAll(items: TraversableOnce[F]): T = agg(items)

private def agg(inputs: TraversableOnce[F]): T = inputs.aggregate(m.zero)(appnd, m.plus)
}

/**
* How many items satisfy a predicate
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.twitter.algebird

import org.scalatest._

class AppendAggregatorTest extends WordSpec with Matchers {
val data = Vector.fill(100) { scala.util.Random.nextInt(100) }
val mpty = Vector.empty[Int]

// test the methods that appendMonoid method defines or overrides
def testMethods[E, M, P](
agg1: MonoidAggregator[E, M, P],
agg2: MonoidAggregator[E, M, P],
data: Seq[E],
empty: Seq[E]) {

val n = data.length
val (half1, half2) = data.splitAt(n / 2)
val lhs = agg1.appendAll(half1)

data.foreach { e =>
agg1.prepare(e) should be(agg2.prepare(e))
}

agg1.present(lhs) should be(agg2.present(lhs))

agg1(data) should be (agg2(data))
agg1(empty) should be (agg2(empty))

agg1.applyOption(data) should be(agg2.applyOption(data))
agg1.applyOption(empty) should be(agg2.applyOption(empty))

half2.foreach { e =>
agg1.append(lhs, e) should be(agg2.append(lhs, e))
}

agg1.appendAll(lhs, half2) should be(agg2.appendAll(lhs, half2))

agg2.appendAll(data) should be(agg2.appendAll(data))
}

"appendMonoid" should {
"be equivalent to integer monoid aggregator" in {
val agg1 = Aggregator.fromMonoid[Int]
val agg2 = Aggregator.appendMonoid((m: Int, e: Int) => m + e)
testMethods(agg1, agg2, data, mpty)
}

"be equivalent to set monoid aggregator" in {
object setMonoid extends Monoid[Set[Int]] {
val zero = Set.empty[Int]
def plus(m1: Set[Int], m2: Set[Int]) = m1 ++ m2
}

val agg1 = Aggregator.prepareMonoid((e: Int) => Set(e))(setMonoid)
val agg2 = Aggregator.appendMonoid((m: Set[Int], e: Int) => m + e)(setMonoid)

testMethods(agg1, agg2, data, mpty)
}
}
}