(Not logged on) | Log On
View  History

  

Combining arrays

7/28/2008 8:18 PM
You can subscribe to this wiki article using an RSS feed reader.

[Up to jsr166y.forkjoin Examples]

Moving right along; assuming you have mastered Creating arrays, Initializing arrays, and Modifying arrays, this article introduces operations on two (or more) Parallel Arrays. Two arrays a and b can be combined using binary operations, as in: resulti = op(ai, bi).

Given two existing ParallelDoubleArray instances a and b, we can add them together or perform a dot product. From these examples, you can easily construct your own combinations. See also Using jsr166y.forkjoin Mappings and Predicates for tips on using mappers and predicates effectively.

Dot product of two arrays

The following method computes the dot product of two arrays, resulting in a new third array.

public ParallelDoubleArray dotProduct(final ParallelDoubleArray a, final ParallelDoubleArray b) {
        return a.withMapping(new Ops.BinaryDoubleOp() {
            @Override
            public double op(final double x, final double y) {
                return x * y;
            }
        }, b).all();
    }

This applies a Ops.BinaryDoubleOp, (known as a combiner or a reducer) which has a

public double op(double x, double y)

method, to perform element wise product. The withMapping method returns a  ParallelDoubleArrayWithDoubleMapping  which is like a future - it holds the potential to create a ParallelDoubleArray, but it is not itself a ParallelDoubleArray. That potential is realized by the all() method which performs the mapping concurrently.

If you wish to perform the dot product in place rather than constructing a new ParallelDoubleArray, you can use replaceWithMapping:

a.replaceWithMapping(new Ops.BinaryDoubleOp() {
            @Override
            public double op(final double x, final double y) {
                return x * y;
            }
        }, b);

Here, you will note that the anonymus Ops.BinaryDoubleOp is the same, so you may want to create that once and simply reuse it

private static final Ops.BinaryDoubleOp multiplier = new Ops.BinaryDoubleOp() {
            @Override
            public double op(final double x, final double y) {
                return x * y;
            }
        };

// dot product returning a new ParallelDoubleArray:
public ParallelDoubleArray dotProduct(final ParallelDoubleArray a, final ParallelDoubleArray b) {
        return a.withMapping(multiplier, b).all();
    }

// in place dot product
a.replaceWithMapping(multiplier, b);

Side note: Ops.DoubleReducer is an additional interface which extends the more verbosely named Ops.BinaryDoubleOp and is commonly used in reduction operations, similar to functional programming fold operation.

Adding two arrays

Adding two arrays is like the dot product, but simpler because we do not have to create a Ops.BinaryDoubleOp object to perform addition — one is already provided by the CommonOps class.

public ParallelDoubleArray plus(final ParallelDoubleArray a, final ParallelDoubleArray b) {
        return a.withMapping(CommonOps.doubleAdder(), b).all();
    }

or, to add b to a in place,

a.replaceWithMapping(CommonOps.doubleAdder(), b);

--  David Biesack