/*
 * $Id$
 */

package edu.jas.ufd;


import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;

import org.apache.log4j.Logger;

import edu.jas.kern.ComputerThreads;
import edu.jas.kern.PreemptingException;
import edu.jas.poly.GenPolynomial;
import edu.jas.structure.GcdRingElem;


/**
 * Greatest common divisor parallel proxy.  
 * Executes methods from two implementations in parallel and 
 * returns the result from the fastest run.
 * @author Heinz Kredel
 */

public class GCDProxy> extends GreatestCommonDivisorAbstract {


    //       implements GreatestCommonDivisor {

    private static final Logger logger = Logger.getLogger(GCDProxy.class);


    private final boolean debug = logger.isDebugEnabled(); //logger.isInfoEnabled();


    /**
     * GCD and resultant engines.
     */
    public final GreatestCommonDivisorAbstract e1;


    public final GreatestCommonDivisorAbstract e2;


    /**
     * Thread pool.
     */
    protected transient ExecutorService pool;


    /**
     * Proxy constructor.
     */
    public GCDProxy(GreatestCommonDivisorAbstract e1, GreatestCommonDivisorAbstract e2) {
        this.e1 = e1;
        this.e2 = e2;
        pool = ComputerThreads.getPool();
        //System.out.println("pool 2 = "+pool);
    }


    /**
     * Get the String representation with gcd engines.
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return "GCDProxy[ " + e1.getClass().getName() + ", " + e2.getClass().getName() + " ]";
    }


    /**
     * Univariate GenPolynomial greatest common divisor. 
     * @param P univariate GenPolynomial.
     * @param S univariate GenPolynomial.
     * @return gcd(P,S).
     */
    @Override
    public GenPolynomial baseGcd(final GenPolynomial P, final GenPolynomial S) {
        if ( debug ) {
            if ( ComputerThreads.NO_THREADS ) {
                throw new RuntimeException("this should not happen");
            }
        }
        if (S == null || S.isZERO()) {
            return P;
        }
        if (P == null || P.isZERO()) {
            return S;
        }
        // parallel case
        GenPolynomial g = null;
        //Callable> c0;
        //Callable> c1;
        List>> cs = new ArrayList>>(2);
        cs.add(new Callable>() {


            public GenPolynomial call() {
                try {
                    //System.out.println("starting e1 " + e1.getClass().getName());
                    GenPolynomial g = e1.baseGcd(P, S);
                    if (debug) {
                        logger.info("GCDProxy done e1 " + e1.getClass().getName());
                    }
                    return g;
                } catch (PreemptingException e) {
                    throw new RuntimeException("GCDProxy e1 pre " + e);
                    //return P.ring.getONE();
                } catch (Exception e) {
                    //e.printStackTrace();
                    logger.info("GCDProxy e1 " + e);
                    logger.info("GCDProxy P = " + P);
                    logger.info("GCDProxy S = " + S);
                    throw new RuntimeException("GCDProxy e1 " + e);
                    //return P.ring.getONE();
                }
            }
        });
        cs.add(new Callable>() {


            public GenPolynomial call() {
                try {
                    //System.out.println("starting e2 " + e2.getClass().getName());
                    GenPolynomial g = e2.baseGcd(P, S);
                    if (debug) {
                        logger.info("GCDProxy done e2 " + e2.getClass().getName());
                    }
                    return g;
                } catch (PreemptingException e) {
                    throw new RuntimeException("GCDProxy e2 pre " + e);
                    //return P.ring.getONE();
                } catch (Exception e) {
                    //e.printStackTrace();
                    logger.info("GCDProxy e2 " + e);
                    logger.info("GCDProxy P = " + P);
                    logger.info("GCDProxy S = " + S);
                    throw new RuntimeException("GCDProxy e2 " + e);
                    //return P.ring.getONE();
                }
            }
        });
        try {
            g = pool.invokeAny(cs);
        } catch (InterruptedException ignored) {
            logger.info("InterruptedException " + ignored);
            Thread.currentThread().interrupt();
        } catch (ExecutionException e) {
            logger.info("ExecutionException " + e);
            Thread.currentThread().interrupt();
        }
        return g;
    }


    /**
     * Univariate GenPolynomial recursive greatest common divisor.
     * @param P univariate recursive GenPolynomial.
     * @param S univariate recursive GenPolynomial.
     * @return gcd(P,S).
     */
    @Override
    public GenPolynomial> recursiveUnivariateGcd(final GenPolynomial> P,
            final GenPolynomial> S) {
        if ( debug ) {
            if ( ComputerThreads.NO_THREADS ) {
                throw new RuntimeException("this should not happen");
            }
        }
        if (S == null || S.isZERO()) {
            return P;
        }
        if (P == null || P.isZERO()) {
            return S;
        }
        // parallel case
        GenPolynomial> g = null;
        //Callable>> c0;
        //Callable>> c1;
        List>>> cs = new ArrayList>>>(
                2);
        cs.add(new Callable>>() {


            public GenPolynomial> call() {
                try {
                    GenPolynomial> g = e1.recursiveUnivariateGcd(P, S);
                    if (debug) {
                        logger.info("GCDProxy done e1 " + e1.getClass().getName());
                    }
                    return g;
                } catch (PreemptingException e) {
                    throw new RuntimeException("GCDProxy e1 pre " + e);
                    //return P.ring.getONE();
                } catch (Exception e) {
                    //e.printStackTrace();
                    logger.info("GCDProxy e1 " + e);
                    logger.info("GCDProxy P = " + P);
                    logger.info("GCDProxy S = " + S);
                    throw new RuntimeException("GCDProxy e1 " + e);
                    //return P.ring.getONE();
                }
            }
        });
        cs.add(new Callable>>() {


            public GenPolynomial> call() {
                try {
                    GenPolynomial> g = e2.recursiveUnivariateGcd(P, S);
                    if (debug) {
                        logger.info("GCDProxy done e2 " + e2.getClass().getName());
                    }
                    return g;
                } catch (PreemptingException e) {
                    throw new RuntimeException("GCDProxy e2 pre " + e);
                    //return P.ring.getONE();
                } catch (Exception e) {
                    //e.printStackTrace();
                    logger.info("GCDProxy e2 " + e);
                    logger.info("GCDProxy P = " + P);
                    logger.info("GCDProxy S = " + S);
                    throw new RuntimeException("GCDProxy e2 " + e);
                    //return P.ring.getONE();
                }
            }
        });
        try {
            g = pool.invokeAny(cs);
        } catch (InterruptedException ignored) {
            logger.info("InterruptedException " + ignored);
            Thread.currentThread().interrupt();
        } catch (ExecutionException e) {
            logger.info("ExecutionException " + e);
            Thread.currentThread().interrupt();
        }
        return g;
    }


    /**
     * GenPolynomial greatest common divisor. 
     * @param P GenPolynomial.
     * @param S GenPolynomial.
     * @return gcd(P,S).
     */
    @Override
    public GenPolynomial gcd(final GenPolynomial P, final GenPolynomial S) {
        if ( debug ) {
            if ( ComputerThreads.NO_THREADS ) {
                throw new RuntimeException("this should not happen");
            }
        }
        if (S == null || S.isZERO()) {
            return P;
        }
        if (P == null || P.isZERO()) {
            return S;
        }
        // parallel case
        GenPolynomial g = null;
        //Callable> c0;
        //Callable> c1;
        List>> cs = new ArrayList>>(2);
        cs.add(new Callable>() {


            public GenPolynomial call() {
                try {
                    //System.out.println("starting e1 " + e1.getClass().getName());
                    GenPolynomial g = e1.gcd(P, S);
                    if (debug) {
                        logger.info("GCDProxy done e1 " + e1.getClass().getName());
                    }
                    return g;
                } catch (PreemptingException e) {
                    throw new RuntimeException("GCDProxy e1 pre " + e);
                    //return P.ring.getONE();
                } catch (Exception e) {
                    //e.printStackTrace();
                    logger.info("GCDProxy e1 " + e);
                    logger.info("GCDProxy P = " + P);
                    logger.info("GCDProxy S = " + S);
                    throw new RuntimeException("GCDProxy e1 " + e);
                    //return P.ring.getONE();
                }
            }
        });
        cs.add(new Callable>() {


            public GenPolynomial call() {
                try {
                    //System.out.println("starting e2 " + e2.getClass().getName());
                    GenPolynomial g = e2.gcd(P, S);
                    if (debug) {
                        logger.info("GCDProxy done e2 " + e2.getClass().getName());
                    }
                    return g;
                } catch (PreemptingException e) {
                    throw new RuntimeException("GCDProxy e2 pre " + e);
                    //return P.ring.getONE();
                } catch (Exception e) {
                    //e.printStackTrace();
                    logger.info("GCDProxy e2 " + e);
                    logger.info("GCDProxy P = " + P);
                    logger.info("GCDProxy S = " + S);
                    throw new RuntimeException("GCDProxy e2 " + e);
                    //return P.ring.getONE();
                }
            }
        });
        try {
            g = pool.invokeAny(cs);
        } catch (InterruptedException ignored) {
            logger.info("InterruptedException " + ignored);
            Thread.currentThread().interrupt();
        } catch (ExecutionException e) {
            logger.info("ExecutionException " + e);
            Thread.currentThread().interrupt();
        }
        return g;
    }


    /**
     * Univariate GenPolynomial resultant. 
     * @param P univariate GenPolynomial.
     * @param S univariate GenPolynomial.
     * @return res(P,S).
     */
    @Override
    public GenPolynomial baseResultant(final GenPolynomial P, final GenPolynomial S) {
        if ( debug ) {
            if ( ComputerThreads.NO_THREADS ) {
                throw new RuntimeException("this should not happen");
            }
        }
        if (S == null || S.isZERO()) {
            return S;
        }
        if (P == null || P.isZERO()) {
            return P;
        }
        // parallel case
        GenPolynomial g = null;
        //Callable> c0;
        //Callable> c1;
        List>> cs = new ArrayList>>(2);
        cs.add(new Callable>() {


            public GenPolynomial call() {
                try {
                    //System.out.println("starting e1 " + e1.getClass().getName());
                    GenPolynomial g = e1.baseResultant(P, S);
                    if (debug) {
                        logger.info("GCDProxy done e1 " + e1.getClass().getName());
                    }
                    return g;
                } catch (PreemptingException e) {
                    throw new RuntimeException("GCDProxy e1 pre " + e);
                    //return P.ring.getONE();
                } catch (Exception e) {
                    //e.printStackTrace();
                    logger.info("GCDProxy e1 " + e);
                    logger.info("GCDProxy P = " + P);
                    logger.info("GCDProxy S = " + S);
                    throw new RuntimeException("GCDProxy e1 " + e);
                    //return P.ring.getONE();
                }
            }
        });
        cs.add(new Callable>() {


            public GenPolynomial call() {
                try {
                    //System.out.println("starting e2 " + e2.getClass().getName());
                    GenPolynomial g = e2.baseResultant(P, S);
                    if (debug) {
                        logger.info("GCDProxy done e2 " + e2.getClass().getName());
                    }
                    return g;
                } catch (PreemptingException e) {
                    throw new RuntimeException("GCDProxy e2 pre " + e);
                    //return P.ring.getONE();
                } catch (Exception e) {
                    //e.printStackTrace();
                    logger.info("GCDProxy e2 " + e);
                    logger.info("GCDProxy P = " + P);
                    logger.info("GCDProxy S = " + S);
                    throw new RuntimeException("GCDProxy e2 " + e);
                    //return P.ring.getONE();
                }
            }
        });
        try {
            g = pool.invokeAny(cs);
        } catch (InterruptedException ignored) {
            logger.info("InterruptedException " + ignored);
            Thread.currentThread().interrupt();
        } catch (ExecutionException e) {
            logger.info("ExecutionException " + e);
            Thread.currentThread().interrupt();
        }
        return g;
    }


    /**
     * Univariate GenPolynomial resultant. 
     * @param P univariate recursive GenPolynomial.
     * @param S univariate recursive GenPolynomial.
     * @return res(P,S).
     */
    @Override
    public GenPolynomial> recursiveUnivariateResultant(final GenPolynomial> P,
            final GenPolynomial> S) {
        if ( debug ) {
            if ( ComputerThreads.NO_THREADS ) {
                throw new RuntimeException("this should not happen");
            }
        }
        if (S == null || S.isZERO()) {
            return S;
        }
        if (P == null || P.isZERO()) {
            return P;
        }
        // parallel case
        GenPolynomial> g = null;
        //Callable>> c0;
        //Callable>> c1;
        List>>> cs = new ArrayList>>>(
                2);
        cs.add(new Callable>>() {


            public GenPolynomial> call() {
                try {
                    GenPolynomial> g = e1.recursiveUnivariateResultant(P, S);
                    if (debug) {
                        logger.info("GCDProxy done e1 " + e1.getClass().getName());
                    }
                    return g;
                } catch (PreemptingException e) {
                    throw new RuntimeException("GCDProxy e1 pre " + e);
                    //return P.ring.getONE();
                } catch (Exception e) {
                    //e.printStackTrace();
                    logger.info("GCDProxy e1 " + e);
                    logger.info("GCDProxy P = " + P);
                    logger.info("GCDProxy S = " + S);
                    throw new RuntimeException("GCDProxy e1 " + e);
                    //return P.ring.getONE();
                }
            }
        });
        cs.add(new Callable>>() {


            public GenPolynomial> call() {
                try {
                    GenPolynomial> g = e2.recursiveUnivariateResultant(P, S);
                    if (debug) {
                        logger.info("GCDProxy done e2 " + e2.getClass().getName());
                    }
                    return g;
                } catch (PreemptingException e) {
                    throw new RuntimeException("GCDProxy e2 pre " + e);
                    //return P.ring.getONE();
                } catch (Exception e) {
                    //e.printStackTrace();
                    logger.info("GCDProxy e2 " + e);
                    logger.info("GCDProxy P = " + P);
                    logger.info("GCDProxy S = " + S);
                    throw new RuntimeException("GCDProxy e2 " + e);
                    //return P.ring.getONE();
                }
            }
        });
        try {
            g = pool.invokeAny(cs);
        } catch (InterruptedException ignored) {
            logger.info("InterruptedException " + ignored);
            Thread.currentThread().interrupt();
        } catch (ExecutionException e) {
            logger.info("ExecutionException " + e);
            Thread.currentThread().interrupt();
        }
        return g;
    }


    /**
     * GenPolynomial resultant. Main entry driver method.
     * @param P GenPolynomial.
     * @param S GenPolynomial.
     * @return res(P,S).
     */
    @Override
    public GenPolynomial resultant(final GenPolynomial P, final GenPolynomial S) {
        if ( debug ) {
            if ( ComputerThreads.NO_THREADS ) {
                throw new RuntimeException("this should not happen");
            }
        }
        if (S == null || S.isZERO()) {
            return S;
        }
        if (P == null || P.isZERO()) {
            return P;
        }
        // parallel case
        GenPolynomial g = null;
        //Callable> c0;
        //Callable> c1;
        List>> cs = new ArrayList>>(2);
        cs.add(new Callable>() {


            public GenPolynomial call() {
                try {
                    //System.out.println("starting e1 " + e1.getClass().getName());
                    GenPolynomial g = e1.resultant(P, S);
                    if (debug) {
                        logger.info("GCDProxy done e1 " + e1.getClass().getName());
                    }
                    return g;
                } catch (PreemptingException e) {
                    throw new RuntimeException("GCDProxy e1 pre " + e);
                    //return P.ring.getONE();
                } catch (Exception e) {
                    //e.printStackTrace();
                    logger.info("GCDProxy e1 " + e);
                    logger.info("GCDProxy P = " + P);
                    logger.info("GCDProxy S = " + S);
                    throw new RuntimeException("GCDProxy e1 " + e);
                    //return P.ring.getONE();
                }
            }
        });
        cs.add(new Callable>() {


            public GenPolynomial call() {
                try {
                    //System.out.println("starting e2 " + e2.getClass().getName());
                    GenPolynomial g = e2.resultant(P, S);
                    if (debug) {
                        logger.info("GCDProxy done e2 " + e2.getClass().getName());
                    }
                    return g;
                } catch (PreemptingException e) {
                    throw new RuntimeException("GCDProxy e2 pre " + e);
                    //return P.ring.getONE();
                } catch (Exception e) {
                    //e.printStackTrace();
                    logger.info("GCDProxy e2 " + e);
                    logger.info("GCDProxy P = " + P);
                    logger.info("GCDProxy S = " + S);
                    throw new RuntimeException("GCDProxy e2 " + e);
                    //return P.ring.getONE();
                }
            }
        });
        try {
            g = pool.invokeAny(cs);
        } catch (InterruptedException ignored) {
            logger.info("InterruptedException " + ignored);
            Thread.currentThread().interrupt();
        } catch (ExecutionException e) {
            logger.info("ExecutionException " + e);
            Thread.currentThread().interrupt();
        }
        return g;
    }

}

 

Ads help maintain this website.