All Downloads are FREE. Search and download functionalities are using the official Maven repository.

edu.jas.gb.GBProxy Maven / Gradle / Ivy

The newest version!
/*
 * $Id: GBProxy.java 4289 2012-11-04 14:29:36Z kredel $
 */

package edu.jas.gb;


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;


/**
 * Groebner bases parallel proxy.
 * @author Heinz Kredel
 */

public class GBProxy> extends GroebnerBaseAbstract {


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


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


    /**
     * GB engines.
     */
    public final GroebnerBaseAbstract e1;


    public final GroebnerBaseAbstract e2;


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


    /**
     * Proxy constructor.
     * @param e1 Groebner base engine.
     * @param e2 Groebner base engine.
     */
    public GBProxy(GroebnerBaseAbstract e1, GroebnerBaseAbstract e2) {
        this.e1 = e1;
        this.e2 = e2;
        pool = ComputerThreads.getPool();
        //System.out.println("pool 2 = "+pool);
    }


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


    /**
     * Cleanup and terminate ThreadPool.
     */
    @Override
    public void terminate() {
        e1.terminate();
        e2.terminate();
    }


    /**
     * Cancel ThreadPool.
     */
    @Override
    public int cancel() {
        int s = e1.cancel();
        s += e2.cancel();
        return s;
    }


    /**
     * Groebner base.
     * @param modv module variable number.
     * @param F polynomial list.
     * @return GB(F) a Groebner base of F.
     */
    //JAVA6only: @Override
    public List> GB( final int modv, final List> F ) {
        if (F == null || F.isEmpty()) {
            return F;
        }
        // parallel case
        List> G = null;
        List>>> cs = new ArrayList>>>(2);
        cs.add(new Callable>>() {
            public List> call() {
                try {
                    //System.out.println("starting e1 " + e1.getClass().getName());
                    List> G = e1.GB(modv,F);
                    if (debug) {
                        logger.info("GBProxy done e1 " + e1.getClass().getName());
                    }
                    return G;
                } catch (PreemptingException e) {
                    throw new RuntimeException("GBProxy e1 preempted " + e);
                    //return P.ring.getONE();
                } catch (Exception e) {
                    //e.printStackTrace();
                    logger.info("GBProxy e1 " + e);
                    logger.info("Exception GBProxy F = " + F);
                    throw new RuntimeException("GBProxy e1 " + e);
                    //return P.ring.getONE();
                }
            }
        });
        cs.add(new Callable>>() {
            public List> call() {
                try {
                    //System.out.println("starting e2 " + e2.getClass().getName());
                    List> G = e2.GB(modv,F);
                    if (debug) {
                        logger.info("GBProxy done e2 " + e2.getClass().getName());
                    }
                    return G;
                } catch (PreemptingException e) {
                    throw new RuntimeException("GBProxy e2 preempted " + e);
                    //return P.ring.getONE();
                } catch (Exception e) {
                    //e.printStackTrace();
                    logger.info("GBProxy e2 " + e);
                    logger.info("Exception GBProxy F = " + F);
                    throw new RuntimeException("GBProxy 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;
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy