[concurrency-interest] FJ usage question: Mutable procedures

David J. Biesack David.Biesack at sas.com
Mon Aug 25 15:59:36 EDT 2008


ParallelDoubleArray has a summary() method; but it's not implemented in
terms of the public apply/Ops.DoubleProcedure API of
ParallelDoubleArray. I'd like to make that more general and define a
Ops.DoubleProcedure to compute extended summary statistics, or perform
other operations which require updating state within the ops/procedures.
As noted earlier, there is no IntAndDoubleProcedure, so I'm using a
mutable Ops.IntAndDoubleToDouble instead

Naturally, I must add a lock around the mutating portions of the class

    @Override
    public double op(final int index, final double a) {
        // System.out.println(a);
        if (!Double.isNaN(a)) {
            synchronized (this) {
                sum += a;
                sumOfSquares += (a * a);
                n++;
                if (a > max) {
                    max = a;
                    maxIndex = index;
                }
                if (a < min) {
                    min = a;
                    minIndex = index;
                }
            }

        }
        return a;
    }

However, the PAS.FJDStats does not use synchronization but rather
creates new instances during the divide/conquer algorithms, and thus no
race conditions.

However, I don't see where this divide/conquer approach is part of the
public ParallelArray API; PAS and its kin are not public.

I would like to be able to do the divide/conquer for general purpose
mutable procedures/mappings. Is this possible? Another more simplistic
option would be to use ex.getParallelismLevel() and be able to create an
array of procedures/mappers, each of which the framework guarantees
safe, non-blocking execution

  Ops.DoubleProcedure procedures[] = new MyProcedure[ex.getParallelismLevel()];
  // initialize
  parallelArray.apply(procedures);

after which I can run a reducer over that array to combine their
intermediate results.

Or, are there better ways to achieve these goals?

-- 
David J. Biesack     SAS Institute Inc.
(919) 531-7771       SAS Campus Drive
http://www.sas.com   Cary, NC 27513


More information about the Concurrency-interest mailing list