Reduce in parallel?
Marcin Przeradzki
kryzoo.m at gmail.com
Tue Mar 26 14:19:51 PDT 2013
2013/3/26 Henry Jen <henry.jen at oracle.com>:
> That's expected, minus is not even associative.
>
Hi
Yes, I noticed it in javadoc in the code:
@param reducer An associative function for combining two elements
Looking at parameter 'identity' I expected it works more as foldLeft
in Scala. Now it is clear.
Same effect appears for reduce in Scala on List.par.
However there are also functions reduceLeft/Right in Scala and they
are not paralleled.
In tis case it seems there is no associative requirement for function
op. It just need to be executed sequentially as order of operations is
deterministic. Same is about foldLeft/Right.
lazy val list = 1 to 1000000 //> list :
scala.collection.immutable.Range.Inclusive = <lazy>
list.reduce(_ - _) //> res0: Int = -1784293662
list.par.reduce(_ - _) //> res1: Int = 370640256
list.reduceLeft(_ - _) //> res2: Int = -1784293662
list.par.reduceLeft(_ - _) //> res3: Int = -1784293662
Thanks for clarification.
Greetings
Marcin
P.S
I hope it is not a big faux pas referencing Scala here :)
> Cheers,
> Henry
>
> On Mar 25, 2013, at 3:30 PM, Marcin Przeradzki <kryzoo.m at gmail.com> wrote:
>
>> Hello Everybody,
>> if I run the following small app:
>>
>> import java.util.Arrays;
>> import java.util.List;
>> import java.util.function.BinaryOperator;
>>
>> class Kolekcje {
>>
>> public static void main(String[] args) {
>> List<Integer> liczby = Arrays.asList(1, 2, 3, 4, 5, 6, 7);
>>
>> System.out.println("Reducer (a,b) -> (a+b)");
>> compareStreams(liczby, (a, b) -> (a + b));
>>
>> System.out.println("Reducer (a,b) -> (a-b)");
>> compareStreams(liczby, (a, b) -> (a - b));
>> }
>>
>> private static void compareStreams(List<Integer> liczby,
>> BinaryOperator<Integer> reducer) {
>> int sum1 = liczby.stream()
>> .reduce(0, reducer);
>>
>> int sum2 = liczby.parallelStream()
>> .reduce(0, reducer);
>>
>> System.out.printf("stream: %d\nparallelstream: %d\n", sum1, sum2);
>>
>> if(sum1 != sum2){
>> System.err.println("ERROR!!!!!");
>> }
>> }
>> }
>>
>> I get following result:
>>
>> Reducer (a,b) -> (a+b)
>> stream: 28
>> parallelstream: 28
>> Reducer (a,b) -> (a-b)
>> stream: -28
>> parallelstream: -2
>> ERROR!!!!!
>>
>>
>> tested on:
>>
>> openjdk version "1.8.0-ea"
>> OpenJDK Runtime Environment (build
>> 1.8.0-ea-lambda-nightly-h3728-20130318-b82-b00)
>> OpenJDK 64-Bit Server VM (build 25.0-b21, mixed mode)
>>
>>
>> Best regards
>> Marcin
>>
>
More information about the lambda-dev
mailing list