JDK8 : parallel programming made (too ?) easy

Post on 10-May-2015

3,799 views 3 download

Tags:

transcript

What did happen in 2005 ?

What did happen in 2005 ?

What did happen in 2005 ?

From 2005 till…

From 2005 till…

Java parallel toolbox

What is the Fork / Join about ?

Fork / Join caveats

Fork / Join caveats

Then comes the Parallel Arrays API

http://g.oswego.edu/dl/concurrency-interest/

Then comes the Parallel Arrays API

ForkJoinPool pool = new ForkJoinPool() ; // package !

ParralelLongArray a = ParralelLongArray.create(pool) ;

Then comes the Parallel Arrays API

Ops.LongOp add2 = new Ops.LongOp() {

@Override

public long op(long l1) {

return l1 + 2 ;

}

} ;

a2 = a.withMapping(add2) ;

Then comes the Parallel Arrays API

Ops.LongPredicate filter = new Ops.LongPredicate() {

@Override

public boolean op(long l1) {

return l1 > 50 ;

}

}

a2 = a.withFilter(filter) ;

a2.all() ;

Then comes the Parallel Arrays API

Ops.LongReducer reducer = new Ops.LongReducer() {

@Override

public long op(long l1, long l2) {

return l1 + l2 ;

}

}

long reducedValue = a.reduce(reducer, 0L) ;

JDK 6 & 7

2 tools available :

- Fork / Join

- Parallel Arrays

And then comes the JDK 8

Collection<Person> persons = ... ;

int maxAge = persons.stream().map(p -> p.getAge()).reduce(0, Math::max) ;

Collection<Person> oldies = new ArrayList<>() ;

persons.stream().filter(p -> p.age > 40).into(oldies) ;

And then comes the JDK 8

Collection<Person> persons = ... ;

int maxAge = persons.stream().parallel()

.map(p -> p.getAge()).reduce(0, Math::max) ;

Collection<Person> oldies = new ArrayList<>() ;

persons.stream().parallel().filter(p -> p.age > 40).into(oldies) ;

And then comes the JDK 8

public interface Spliterator<E> {

public Spliterator<E>[] splits() ;

public int getNaturalSplits() ;

}

http://www.parleys.com/#st=5&id=3125&sl=0

How to use all that ?

1st caveat : associativity

Associativity

Associativity

Associativity

2nd caveat : performance

long begin = System.nanoTime() ; List<String> hugeList = new ArrayList<>(N) ; // N = 16_000_000 for (int i = 0 ; i < N ; i++) { long l = random.nextLong() ; l = l > 0 ? l : -l ; hugeList.add(Long.toString(l, 32)) ; }

Sorting

Sorting performance

Sorting performance

Sorting performance

Sorting performance

Sorting performance

Sorting performance

Sorting performance

Sorting performance

Sorting performance

An even worse case

An even worse case

An even worse case

Arrays ? Going parallel is easy !

Arrays ? Going parallel is easy…

Arrays ? Going parallel is easy…

3rd caveat

3rd caveat

Conclusion

Conclusion

Conclusion

Conclusion

Conclusion

Thank you !

Q&A

Downloads