Search in sources :

Example 6 with RecursiveTask

use of java.util.concurrent.RecursiveTask in project tetrad by cmu-phil.

the class GraphUtils method edgeMisclassificationCounts.

public static int[][] edgeMisclassificationCounts(Graph leftGraph, Graph topGraph, boolean print) {
    class CountTask extends RecursiveTask<Counts> {

        private int chunk;

        private int from;

        private int to;

        private final List<Edge> edges;

        private final Graph leftGraph;

        private final Graph topGraph;

        private final Counts counts;

        private final int[] count;

        public CountTask(int chunk, int from, int to, List<Edge> edges, Graph leftGraph, Graph topGraph, int[] count) {
            this.chunk = chunk;
            this.from = from;
            this.to = to;
            this.edges = edges;
            this.leftGraph = leftGraph;
            this.topGraph = topGraph;
            this.counts = new Counts();
            this.count = count;
        }

        @Override
        protected Counts compute() {
            int range = to - from;
            if (range <= chunk) {
                for (int i = from; i < to; i++) {
                    int j = ++count[0];
                    if (j % 1000 == 0) {
                        System.out.println("Counted " + (count[0]));
                    }
                    Edge edge = edges.get(i);
                    Node x = edge.getNode1();
                    Node y = edge.getNode2();
                    Edge left = leftGraph.getEdge(x, y);
                    Edge top = topGraph.getEdge(x, y);
                    int m = getTypeLeft(left, top);
                    int n = getTypeTop(top);
                    counts.increment(m, n);
                }
                return counts;
            } else {
                int mid = (to + from) / 2;
                CountTask left = new CountTask(chunk, from, mid, edges, leftGraph, topGraph, count);
                CountTask right = new CountTask(chunk, mid, to, edges, leftGraph, topGraph, count);
                left.fork();
                Counts rightAnswer = right.compute();
                Counts leftAnswer = left.join();
                leftAnswer.addAll(rightAnswer);
                return leftAnswer;
            }
        }

        public Counts getCounts() {
            return counts;
        }
    }
    // System.out.println("Forming edge union");
    // topGraph = GraphUtils.replaceNodes(topGraph, leftGraph.getNodes());
    // int[][] counts = new int[8][6];
    Set<Edge> edgeSet = new HashSet<>();
    edgeSet.addAll(topGraph.getEdges());
    edgeSet.addAll(leftGraph.getEdges());
    // System.out.println("Union formed");
    if (print) {
        System.out.println("Top graph " + topGraph.getEdges().size());
        System.out.println("Left graph " + leftGraph.getEdges().size());
        System.out.println("All edges " + edgeSet.size());
    }
    List<Edge> edges = new ArrayList<>(edgeSet);
    // System.out.println("Finding pool");
    ForkJoinPoolInstance pool = ForkJoinPoolInstance.getInstance();
    // System.out.println("Starting count task");
    CountTask task = new CountTask(500, 0, edges.size(), edges, leftGraph, topGraph, new int[1]);
    Counts counts = pool.getPool().invoke(task);
    // System.out.println("Finishing count task");
    return counts.countArray();
}
Also used : ForkJoinPoolInstance(edu.cmu.tetrad.util.ForkJoinPoolInstance) RecursiveTask(java.util.concurrent.RecursiveTask) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) HashSet(java.util.HashSet)

Example 7 with RecursiveTask

use of java.util.concurrent.RecursiveTask in project mxisd by kamax-io.

the class DnsLookupProvider method populate.

@Override
public List<ThreePidMapping> populate(List<ThreePidMapping> mappings) {
    Map<String, List<ThreePidMapping>> domains = new HashMap<>();
    for (ThreePidMapping mapping : mappings) {
        if (!ThreePidMedium.Email.is(mapping.getMedium())) {
            log.info("Skipping unsupported type {} for {}", mapping.getMedium(), mapping.getValue());
            continue;
        }
        Optional<String> domainOpt = getDomain(mapping.getValue());
        if (!domainOpt.isPresent()) {
            log.warn("No domain for 3PID {}", mapping.getValue());
            continue;
        }
        String domain = domainOpt.get();
        List<ThreePidMapping> domainMappings = domains.computeIfAbsent(domain, s -> new ArrayList<>());
        domainMappings.add(mapping);
    }
    log.info("Looking mappings across {} domains", domains.keySet().size());
    ForkJoinPool pool = ForkJoinPool.commonPool();
    RecursiveTask<List<ThreePidMapping>> task = new RecursiveTask<List<ThreePidMapping>>() {

        @Override
        protected List<ThreePidMapping> compute() {
            List<ThreePidMapping> mappingsFound = new ArrayList<>();
            List<DomainBulkLookupTask> tasks = new ArrayList<>();
            for (String domain : domains.keySet()) {
                DomainBulkLookupTask domainTask = new DomainBulkLookupTask(domain, domains.get(domain));
                domainTask.fork();
                tasks.add(domainTask);
            }
            for (DomainBulkLookupTask task : tasks) {
                mappingsFound.addAll(task.join());
            }
            return mappingsFound;
        }
    };
    pool.submit(task);
    pool.shutdown();
    List<ThreePidMapping> mappingsFound = task.join();
    log.info("Found {} mappings overall", mappingsFound.size());
    return mappingsFound;
}
Also used : ThreePidMapping(io.kamax.mxisd.lookup.ThreePidMapping) RecursiveTask(java.util.concurrent.RecursiveTask) ForkJoinPool(java.util.concurrent.ForkJoinPool)

Example 8 with RecursiveTask

use of java.util.concurrent.RecursiveTask in project sandbox by irof.

the class ParallelStreamSample method sampleInForkJoinWorkerThread.

@Test
public void sampleInForkJoinWorkerThread() throws Exception {
    RecursiveTask<List<String>> task = new RecursiveTask<List<String>>() {

        @Override
        protected List<String> compute() {
            // ForkJoinPoolのThreadの中でparallelに実行します。
            return IntStream.rangeClosed(1, 5).parallel().mapToObj(i -> Thread.currentThread().getName()).collect(toList());
        }
    };
    ForkJoinPool forkJoinPool = new ForkJoinPool();
    List<String> collected = forkJoinPool.invoke(task);
    // ForkJoinPoolの中で実行されているので、すべてForkJoinPoolのスレッド名になっているはずです。
    assertThat(collected).are(new Condition<>(str -> str.startsWith("ForkJoinPool-"), "start with 'ForkJoinPool-'"));
    forkJoinPool.shutdown();
}
Also used : IntStream(java.util.stream.IntStream) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) RecursiveTask(java.util.concurrent.RecursiveTask) ForkJoinPool(java.util.concurrent.ForkJoinPool) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Condition(org.assertj.core.api.Condition) Test(org.junit.Test) RecursiveTask(java.util.concurrent.RecursiveTask) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) ForkJoinPool(java.util.concurrent.ForkJoinPool) Test(org.junit.Test)

Example 9 with RecursiveTask

use of java.util.concurrent.RecursiveTask in project tetrad by cmu-phil.

the class GFciMax method addColliders.

private void addColliders(Graph graph, final Graph fgesGraph) {
    List<Node> nodes = graph.getNodes();
    class Task extends RecursiveTask<Boolean> {

        int from;

        int to;

        int chunk = 20;

        List<Node> nodes;

        Graph graph;

        public Task(List<Node> nodes, Graph graph, int from, int to) {
            this.nodes = nodes;
            this.graph = graph;
            this.from = from;
            this.to = to;
        }

        @Override
        protected Boolean compute() {
            if (to - from <= chunk) {
                for (int i = from; i < to; i++) {
                    doNode(graph, fgesGraph, nodes.get(i));
                }
                return true;
            } else {
                int mid = (to + from) / 2;
                Task left = new Task(nodes, graph, from, mid);
                Task right = new Task(nodes, graph, mid, to);
                left.fork();
                right.compute();
                left.join();
                return true;
            }
        }
    }
    Task task = new Task(nodes, graph, 0, nodes.size());
    ForkJoinPoolInstance.getInstance().getPool().invoke(task);
}
Also used : RecursiveTask(java.util.concurrent.RecursiveTask) RecursiveTask(java.util.concurrent.RecursiveTask) List(java.util.List)

Example 10 with RecursiveTask

use of java.util.concurrent.RecursiveTask in project tetrad by cmu-phil.

the class FasStableConcurrentFdr method searchAtDepth.

// private boolean freeDegreeGreaterThanDepth(Map<Node, Set<Node>> adjacencies, int depth) {
// for (Node x : adjacencies.keySet()) {
// Set<Node> opposites = adjacencies.get(x);
// 
// if (opposites.size() - 1 > depth) {
// return true;
// }
// }
// 
// return false;
// }
private boolean searchAtDepth(final List<Node> nodes, final IndependenceTest test, final Map<Node, Set<Node>> adjacencies, final int depth) {
    if (verbose) {
        out.println("Searching at depth " + depth);
        System.out.println("Searching at depth " + depth);
    }
    final Map<Node, Set<Node>> adjacenciesCopy = new HashMap<>();
    for (Node node : adjacencies.keySet()) {
        adjacenciesCopy.put(node, new HashSet<>(adjacencies.get(node)));
    }
    final List<Double> sorted = new ArrayList<>();
    class DepthTask extends RecursiveTask<Boolean> {

        private int chunk;

        private int from;

        private int to;

        public DepthTask(int chunk, int from, int to) {
            this.chunk = chunk;
            this.from = from;
            this.to = to;
        }

        @Override
        protected Boolean compute() {
            if (to - from <= chunk) {
                for (int i = from; i < to; i++) {
                    if (verbose) {
                        if ((i + 1) % 1000 == 0)
                            System.out.println("i = " + (i + 1));
                    }
                    Node x = nodes.get(i);
                    List<Node> adjx = new ArrayList<>(adjacenciesCopy.get(x));
                    EDGE: for (Node y : adjx) {
                        if (!existsShortPath(x, y, 3, adjacencies)) {
                            continue;
                        }
                        List<Node> _adjx = new ArrayList<>(adjx);
                        _adjx.remove(y);
                        List<Node> ppx = possibleParents(x, _adjx, knowledge);
                        if (ppx.size() >= depth) {
                            ChoiceGenerator cg = new ChoiceGenerator(ppx.size(), depth);
                            int[] choice;
                            while ((choice = cg.next()) != null) {
                                List<Node> condSet = GraphUtils.asList(choice, ppx);
                                boolean independent;
                                try {
                                    numIndependenceTests++;
                                    independent = test.isIndependent(x, y, condSet);
                                } catch (Exception e) {
                                    independent = false;
                                }
                                boolean noEdgeRequired = knowledge.noEdgeRequired(x.getName(), y.getName());
                                if (independent && noEdgeRequired) {
                                    sorted.add(test.getPValue());
                                    continue EDGE;
                                }
                            }
                        }
                    }
                }
                return true;
            } else {
                final int mid = (to + from) / 2;
                DepthTask left = new DepthTask(chunk, from, mid);
                DepthTask right = new DepthTask(chunk, mid, to);
                left.fork();
                right.compute();
                left.join();
                return true;
            }
        }
    }
    pool.invoke(new DepthTask(chunk, 0, nodes.size()));
    Collections.sort(sorted);
    final double cutoff = StatUtils.fdrCutoff(test.getAlpha(), sorted, false, true);
    System.out.println();
    class DepthTask2 extends RecursiveTask<Boolean> {

        private int chunk;

        private int from;

        private int to;

        public DepthTask2(int chunk, int from, int to) {
            this.chunk = chunk;
            this.from = from;
            this.to = to;
        }

        @Override
        protected Boolean compute() {
            if (to - from <= chunk) {
                for (int i = from; i < to; i++) {
                    if (verbose) {
                        if ((i + 1) % 1000 == 0)
                            System.out.println("i = " + (i + 1));
                    }
                    Node x = nodes.get(i);
                    List<Node> adjx = new ArrayList<>(adjacenciesCopy.get(x));
                    EDGE: for (Node y : adjx) {
                        List<Node> _adjx = new ArrayList<>(adjx);
                        _adjx.remove(y);
                        List<Node> ppx = possibleParents(x, _adjx, knowledge);
                        if (ppx.size() >= depth) {
                            ChoiceGenerator cg = new ChoiceGenerator(ppx.size(), depth);
                            int[] choice;
                            while ((choice = cg.next()) != null) {
                                List<Node> condSet = GraphUtils.asList(choice, ppx);
                                try {
                                    numIndependenceTests++;
                                    test.isIndependent(x, y, condSet);
                                    if (test.getPValue() > cutoff) {
                                        adjacencies.get(x).remove(y);
                                        adjacencies.get(y).remove(x);
                                        if (recordSepsets) {
                                            getSepsets().set(x, y, condSet);
                                        }
                                        continue EDGE;
                                    }
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            }
                        }
                    }
                }
                return true;
            } else {
                final int mid = (to + from) / 2;
                DepthTask left = new DepthTask(chunk, from, mid);
                DepthTask right = new DepthTask(chunk, mid, to);
                left.fork();
                right.compute();
                left.join();
                return true;
            }
        }
    }
    pool.invoke(new DepthTask2(chunk, 0, nodes.size()));
    if (verbose) {
        System.out.println("Done with depth");
    }
    return freeDegree(nodes, adjacencies) > depth;
}
Also used : RecursiveTask(java.util.concurrent.RecursiveTask) ChoiceGenerator(edu.cmu.tetrad.util.ChoiceGenerator)

Aggregations

RecursiveTask (java.util.concurrent.RecursiveTask)12 List (java.util.List)5 ForkJoinPool (java.util.concurrent.ForkJoinPool)4 ArrayList (java.util.ArrayList)3 Test (org.junit.Test)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 ChoiceGenerator (edu.cmu.tetrad.util.ChoiceGenerator)1 ForkJoinPoolInstance (edu.cmu.tetrad.util.ForkJoinPoolInstance)1 ThreePidMapping (io.kamax.mxisd.lookup.ThreePidMapping)1 Link (io.perfmark.Link)1 Tag (io.perfmark.Tag)1 ArrayDeque (java.util.ArrayDeque)1 HashSet (java.util.HashSet)1 LinkedList (java.util.LinkedList)1 Random (java.util.Random)1 Collectors.toList (java.util.stream.Collectors.toList)1 IntStream (java.util.stream.IntStream)1 Well1024a (org.apache.commons.math3.random.Well1024a)1 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)1 Condition (org.assertj.core.api.Condition)1