[concurrency-interest] ParallelArray Extension

Kasper Nielsen kasper at kav.dk
Wed Dec 19 15:02:38 EST 2007


Doug Lea wrote:
>  But my guess based on what the PLINQ (C#)
> folks are doing is that some people will want further extensions,
> lead to all sorts of syntax/tool/etc issues that I'd rather avoid.

I would prefer as many low level operations as possible in ParallelArray 
and friends, and then if it gets to crowded, move the high-level 
operations into utility classes. Here I'm thinking about mix, max, sum, 
etc...

For example,
public class ParallelOps {

public static <T> T max(ParallelArray<T> array) {
     return array.reduce(new RawMaxReducer<T>(), null);
}

public static <T> T max(ParallelArray.WithMapping<?, T> mapping) {
     return mapping.reduce(new RawMaxReducer<T>(), null);
}

public static int max(ParallelArray.WithIntMapping<?> mapping) {
     return mapping.reduce(NaturalIntMaxReducer.max, Integer.MIN_VALUE);
}

public static int max(ParallelIntArray array) {
     return array.reduce(NaturalIntMaxReducer.max, Integer.MIN_VALUE);
}
...min
...sum
...
}
Instead of call ParallelIntArray.max() you would use something along the 
lines of:
----
import static jsr166y.forkjoin.ParallelOps.max;

ParallelIntArray pia = new ParallelIntArray(new ForkJoinPool(), new 
int[] { 1, 2, 3 });
int max = max(pia);
----
distinct() and other operations could also be added to this class. The 
methods can always call some package-private methods if they can benefit 
from knowing the internal representation of ParallelArray.

finally, if this approach is taken. It might make sense to add these 
interfaces: Reducible, LongReducible, IntReducible, DoubleReducible
public interface Ops.LongReducible {
   long reduce(LongReducer reducer, long base);
}
In this way any datastructure could take advantage of the methods in 
ParallelOps.

- Kasper


More information about the Concurrency-interest mailing list