Example usage for java.util.concurrent ForkJoinPool invoke

List of usage examples for java.util.concurrent ForkJoinPool invoke

Introduction

In this page you can find the example usage for java.util.concurrent ForkJoinPool invoke.

Prototype

public <T> T invoke(ForkJoinTask<T> task) 

Source Link

Document

Performs the given task, returning its result upon completion.

Usage

From source file:org.rhwlab.BHCnotused.ThreadedAlgorithm.java

public void init(int seg) throws Exception {
    clusters = new ArrayList<>();
    // build the initial clusters with one data point in each cluster
    for (int n = 0; n < source.getK(); ++n) {
        RealVector v = source.getCenter(n);
        Dfp[] z = new Dfp[v.getDimension()];
        for (int i = 0; i < z.length; ++i) {
            z[i] = field.newDfp(v.getEntry(i));
        }//from   ww  w  .ja v a 2s  . c  o m
        LabeledFieldVector fv = new LabeledFieldVector(z, n);
        Cluster cluster = new Cluster(new GaussianGIWPrior(fv));
        clusters.add(cluster);
    }

    // make all possible pairings of initial clusters
    pairs = new HashMap<>();
    for (int i = 0; i < clusters.size() - 1; ++i) {
        HashMap<Cluster, Cluster> map = new HashMap<>();
        MergeAction merge = new MergeAction(clusters, i, i + 1, map);
        ForkJoinPool pool = new ForkJoinPool();
        pool.invoke(merge);
        Cluster clusterI = clusters.get(i);
        pairs.put(clusterI, map);
        System.out.printf("Cluster %d paired\n", i);
    }
}

From source file:org.rhwlab.BHCnotused.ThreadedAlgorithm.java

@Override
public void run() {
    while (clusters.size() > 1) {
        System.out.printf("\n%d Clusters\n", clusters.size());
        Cluster T = maximumRCluster();//from w w  w  .j av  a2s  .  co m

        if (T == null) {
            return;
        }

        T.printCluster(System.out);

        //            System.out.printf("size=%d,dpm=%e,like=%e,pi=%f,pi*like=%e,r=%.20f,d=%s,Gam=%s,f=%s,d2=%s\n", T.data.getN(),T.dpm,T.data.likelihood(),T.pi,T.pi*T.data.likelihood(),T.r,T.d,T.gammaN,T.f,T.d2);
        // remove the children of the max r pair
        pairs.remove(T.left);
        pairs.remove(T.right);
        for (HashMap<Cluster, Cluster> map : pairs.values()) {
            map.remove(T.left);
            map.remove(T.right);
        }
        clusters.remove(T.left);
        clusters.remove(T.right);

        // make new pairs with all the clusters
        HashMap<Cluster, Cluster> map = new HashMap<>();
        MergeAction merge = new MergeAction(clusters, T, 0, clusters.size() - 1, map);
        ForkJoinPool pool = new ForkJoinPool();
        pool.invoke(merge);
        pairs.put(T, map);
        clusters.add(T);
    }
}

From source file:org.ednovo.gooru.controller.ResourceCassandraRestController.java

public void executeEveryTenSecs() throws Exception {

    Set<String> domainKeysSet = resourceCassandraService
            .getAllRedisKeys(resourceCassandraService.getConfigSettings(ConfigSettings.DOMAIN_KEY) + "*");
    if (domainKeysSet != null && domainKeysSet.size() > 0) {
        String[] domainKeys = domainKeysSet.toArray(new String[0]);
        ForkJoinPool fjpool = new ForkJoinPool(64);
        logger.info("Start Time for RecurssiveAction : " + (System.currentTimeMillis()));
        JobsRecurssiveAction task = new JobsRecurssiveAction(domainKeys, 1, domainKeys.length,
                resourceCassandraService);
        long start = System.currentTimeMillis();
        fjpool.invoke(task);
        logger.info("End Time for RecurssiveAction : " + (System.currentTimeMillis()));
        logger.info("Parallel processing time: " + (System.currentTimeMillis() - start) + " ms");
    }/*from ww  w.  j  av a2  s .  co  m*/
}

From source file:edu.pitt.csb.stability.StabilityUtils.java

public static DoubleMatrix2D StabilitySearchPar(final DataSet data, final DataGraphSearch gs, int N, int b) {

    final int numVars = data.getNumColumns();
    final DoubleMatrix2D thetaMat = DoubleFactory2D.dense.make(numVars, numVars, 0.0);

    final int[][] samps = subSampleNoReplacement(data.getNumRows(), b, N);

    final ForkJoinPool pool = ForkJoinPoolInstance.getInstance().getPool();

    class StabilityAction extends RecursiveAction {
        private int chunk;
        private int from;
        private int to;

        public StabilityAction(int chunk, int from, int to) {
            this.chunk = chunk;
            this.from = from;
            this.to = to;
        }//from w w w . ja v a 2  s .c  om

        //could avoid using syncronized if we keep track of array of mats and add at end, but that needs lots of
        //memory
        private synchronized void addToMat(DoubleMatrix2D matSum, DoubleMatrix2D curMat) {
            matSum.assign(curMat, Functions.plus);
        }

        @Override
        protected void compute() {
            if (to - from <= chunk) {
                for (int s = from; s < to; s++) {
                    DataSet dataSubSamp = data.subsetRows(samps[s]).copy();
                    DataGraphSearch curGs = gs.copy();
                    Graph g = curGs.search(dataSubSamp);

                    //TODO update graphToMatrix method
                    DoubleMatrix2D curAdj = MixedUtils.skeletonToMatrix(g); //set weights so that undirected stability works
                    addToMat(thetaMat, curAdj);
                }

                return;
            } else {
                List<StabilityAction> tasks = new ArrayList<StabilityAction>();

                final int mid = (to - from) / 2;

                tasks.add(new StabilityAction(chunk, from, from + mid));
                tasks.add(new StabilityAction(chunk, from + mid, to));

                invokeAll(tasks);

                return;
            }
        }

    }

    final int chunk = 2;

    pool.invoke(new StabilityAction(chunk, 0, N));

    thetaMat.assign(Functions.mult(1.0 / N));

    //do this elsewhere
    //thetaMat.assign(thetaMat.copy().assign(Functions.minus(1.0)), Functions.mult).assign(Functions.mult(-2.0));
    return thetaMat;
}

From source file:cn.afterturn.easypoi.excel.imports.ExcelImportService.java

private <T> List<T> importExcel(Collection<T> result, Sheet sheet, Class<?> pojoClass, ImportParams params,
        Map<String, PictureData> pictures) throws Exception {
    List collection = new ArrayList();
    Map<String, ExcelImportEntity> excelParams = new HashMap<String, ExcelImportEntity>();
    List<ExcelCollectionParams> excelCollection = new ArrayList<ExcelCollectionParams>();
    String targetId = null;/*w  w  w . j a  v  a 2s .co  m*/
    i18nHandler = params.getI18nHandler();
    boolean isMap = Map.class.equals(pojoClass);
    if (!isMap) {
        Field[] fileds = PoiPublicUtil.getClassFields(pojoClass);
        ExcelTarget etarget = pojoClass.getAnnotation(ExcelTarget.class);
        if (etarget != null) {
            targetId = etarget.value();
        }
        getAllExcelField(targetId, fileds, excelParams, excelCollection, pojoClass, null, null);
    }
    Iterator<Row> rows = sheet.rowIterator();
    for (int j = 0; j < params.getTitleRows(); j++) {
        rows.next();
    }
    Map<Integer, String> titlemap = getTitleMap(rows, params, excelCollection, excelParams);
    checkIsValidTemplate(titlemap, excelParams, params, excelCollection);
    Row row = null;
    Object object = null;
    String picId;
    int readRow = 1;
    //
    for (int i = 0; i < params.getStartRows(); i++) {
        rows.next();
    }
    //index ?,?
    if (excelCollection.size() > 0 && params.getKeyIndex() == null) {
        params.setKeyIndex(0);
    }
    if (params.isConcurrentTask()) {
        ForkJoinPool forkJoinPool = new ForkJoinPool();
        int endRow = sheet.getLastRowNum() - params.getLastOfInvalidRow();
        if (params.getReadRows() > 0) {
            endRow = params.getReadRows();
        }
        ExcelImportForkJoinWork task = new ExcelImportForkJoinWork(
                params.getStartRows() + params.getHeadRows() + params.getTitleRows(), endRow, sheet, params,
                pojoClass, this, targetId, titlemap, excelParams);
        ExcelImportResult forkJoinResult = forkJoinPool.invoke(task);
        collection = forkJoinResult.getList();
        failCollection = forkJoinResult.getFailList();
    } else {
        StringBuilder errorMsg;
        while (rows.hasNext()
                && (row == null || sheet.getLastRowNum() - row.getRowNum() > params.getLastOfInvalidRow())) {
            if (params.getReadRows() > 0 && readRow > params.getReadRows()) {
                break;
            }
            row = rows.next();
            // Fix row
            if (sheet.getLastRowNum() - row.getRowNum() < params.getLastOfInvalidRow()) {
                break;
            }
            /* ?? */
            if (row.getLastCellNum() < 0) {
                continue;
            }
            if (isMap && object != null) {
                ((Map) object).put("excelRowNum", row.getRowNum());
            }
            errorMsg = new StringBuilder();
            // ???,?,?
            // keyIndex ??,??
            if (params.getKeyIndex() != null
                    && (row.getCell(params.getKeyIndex()) == null
                            || StringUtils.isEmpty(getKeyValue(row.getCell(params.getKeyIndex()))))
                    && object != null) {
                for (ExcelCollectionParams param : excelCollection) {
                    addListContinue(object, param, row, titlemap, targetId, pictures, params, errorMsg);
                }
            } else {
                object = PoiPublicUtil.createObject(pojoClass, targetId);
                try {
                    Set<Integer> keys = titlemap.keySet();
                    for (Integer cn : keys) {
                        Cell cell = row.getCell(cn);
                        String titleString = (String) titlemap.get(cn);
                        if (excelParams.containsKey(titleString) || isMap) {
                            if (excelParams.get(titleString) != null && excelParams.get(titleString)
                                    .getType() == BaseEntityTypeConstants.IMAGE_TYPE) {
                                picId = row.getRowNum() + "_" + cn;
                                saveImage(object, picId, excelParams, titleString, pictures, params);
                            } else {
                                try {
                                    saveFieldValue(params, object, cell, excelParams, titleString, row);
                                } catch (ExcelImportException e) {
                                    // ?,,
                                    if (params.isNeedVerify()
                                            && ExcelImportEnum.GET_VALUE_ERROR.equals(e.getType())) {
                                        errorMsg.append(" ").append(titleString)
                                                .append(ExcelImportEnum.GET_VALUE_ERROR.getMsg());
                                    }
                                }
                            }
                        }
                    }
                    //for (int i = row.getFirstCellNum(), le = titlemap.size(); i < le; i++) {

                    //}
                    if (object instanceof IExcelDataModel) {
                        ((IExcelDataModel) object).setRowNum(row.getRowNum());
                    }
                    for (ExcelCollectionParams param : excelCollection) {
                        addListContinue(object, param, row, titlemap, targetId, pictures, params, errorMsg);
                    }
                    if (verifyingDataValidity(object, row, params, isMap, errorMsg)) {
                        collection.add(object);
                    } else {
                        failCollection.add(object);
                    }
                } catch (ExcelImportException e) {
                    LOGGER.error("excel import error , row num:{},obj:{}", readRow,
                            ReflectionToStringBuilder.toString(object));
                    if (!e.getType().equals(ExcelImportEnum.VERIFY_ERROR)) {
                        throw new ExcelImportException(e.getType(), e);
                    }
                } catch (Exception e) {
                    LOGGER.error("excel import error , row num:{},obj:{}", readRow,
                            ReflectionToStringBuilder.toString(object));
                    throw new RuntimeException(e);
                }
            }
            readRow++;
        }
    }
    return collection;
}