Saturday, 18 July 2015

Aggregate() - How it works


When I started reading Apache Spark, I come across the aggregate() function of Scala. I spend almost one day to find proper example which can help me to understand aggregate, Here I'm summarizing my understanding in a simple possible way.

Below is the scala code which uses aggregate() on input list values :

    val result = List(1,2,3,4,5,6,7,8,9,10).par.aggregate((0, 0))
             (
              (x, y) => (x._1 + y, x._2 + 1),
              (x,y) =>(x._1 + y._1, x._2 + y._2)
             )

result: (Int, Int) = (55,10)

aggregate function has 3 parts : 
  • initial value of accumulators : tuple(0,0) here
  • seqOp : It works like foldLeft with initial value of 0
  • combOp : It combines the result generated through parallelization (this part was difficult for me to understand)


Let's understand all 3 parts independently :

part-1 : Initial tuple (0,0)


Aggregate() starts with initial value of accumulators x which is (0,0) here. First tuple x._1 which is initially 0 is used to compute the sum, Second tuple x._2 which is also 0 is used to compute total number of elements in the list.

part-2 : (x, y) => (x._1 + y, x._2 + 1)


If you know how foldLeft works in scala then it should be easy to understand this part. Above function works just like foldLeft on our List(1,2,3,4...10).

Iteration#      (x._1 + y, x._2 + 1)
     1              (0+1, 0+1)
     2              (1+2, 1+1)
     3              (3+3, 2+1)
     4              (6+4, 3+1)
     .                ....
     .                ....
     10            (45+10, 9+1)

thus after all 10 iteration you'll get the result (55,10).
If you understand this part the rest is very easy but for me it was the most difficult part in understanding if all the required computation are finished then what is the use of second part i.e. compop - stay tuned :-)

part 3 :     (x,y) =>(x._1 + y._1, x._2 + y._2)


Well this 3rd part is combOp which combines the result generated by different threads during parallelization, remember we used 'par' in our code to enable parallel computation of list :

List(1,2,3,4,5,6,7,8,9,10).par.aggregate(....)

Apache spark is effectively using aggregate function to do parallel computation of RDD.

Let's assume that our List(1,2,3,4,5,6,7,8,9,10) is being computed by 3 threads in parallel. Here each thread is working on partial list and then aggregate() will combine the result of each thread's computation using the below code :

    (x,y) =>(x._1 + y._1, x._2 + y._2)

Let's understand it in our scenario,

We have a list : List(1,2,3,4,5,6,7,8,9,10)

Thread1 start computing on partial list say (1,2,3,4)
Thread2 start computing on partial list say (5,6,7,8)
Thread3 start computing on on partial list say (9,10)


At the end of computation, Thread-1 result will be (10,4)
At the end of computation, Thread-2 result will be (26,4)
At the end of computation, Thread-3 result will be (19,2)

At the end of parallel computation, we'll have a tuple containing the two Integer arrays with following values :

((10,4),(26,4),(19,2))

Where the 1st array consists the partial sum collected from each thread and the second array consist the partial result of total no. of elements calculated by each array.


Now combine operation will work as follow :


Iteration#      (x._1 + y._1, x._2 + y._2)

     1                (0+10, 0+4)
     2                (10+26, 4+4)
     3                (36+19, 8+2)


which is (55,10).

Here x._1 and x._2 both will be having value 0 when we start the 1st iteration, y._1 and y._2 will refer to the ([Int],[Int]) array which we received from different threads i.e. ((10,26,19),(4,4,2)).

I hope it be clear now about what is the purpose of combiner operation here.

Before I finish this post, Let's re-iterate that seqOp's job is to compute the sum of all the elements of list and total number of list whereas combine function's job is to combine different partial result generated during parallelization.

I hope above explanation help you to understand the aggregate(). Feel free to report any discrepancy or any issue you might encounter in understanding the concept.

Have a gr8 day ahead !

2 comments:

  1. Wonderful Rashmit. I almost spent one day, and I thought of giving up. And luckily I saw this post, and understood completely how aggregate is working. Thank you so much. Looking forward to more blogs from you like this.

    ReplyDelete
    Replies
    1. I can understand the frustration as I too gone through the same.. Good that you didn't gave up.. Thanks for your kind words.

      Delete