use of java.util.concurrent.RecursiveTask in project tetrad by cmu-phil.
the class FciMax method addColliders.
private void addColliders(Graph graph) {
final Map<Triple, Double> scores = new ConcurrentHashMap<>();
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, Map<Triple, Double> scores, 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, scores, nodes.get(i));
}
return true;
} else {
int mid = (to + from) / 2;
Task left = new Task(nodes, graph, scores, from, mid);
Task right = new Task(nodes, graph, scores, mid, to);
left.fork();
right.compute();
left.join();
return true;
}
}
}
Task task = new Task(nodes, graph, scores, 0, nodes.size());
ForkJoinPoolInstance.getInstance().getPool().invoke(task);
List<Triple> tripleList = new ArrayList<>(scores.keySet());
// Most independent ones first.
Collections.sort(tripleList, new Comparator<Triple>() {
@Override
public int compare(Triple o1, Triple o2) {
return Double.compare(scores.get(o2), scores.get(o1));
}
});
for (Triple triple : tripleList) {
Node a = triple.getX();
Node b = triple.getY();
Node c = triple.getZ();
graph.setEndpoint(a, b, Endpoint.ARROW);
graph.setEndpoint(c, b, Endpoint.ARROW);
}
}
use of java.util.concurrent.RecursiveTask in project tetrad by cmu-phil.
the class LargeScaleSimulation method simulateDataRecursive.
/**
* This simulates data by picking random values for the exogenous terms and
* percolating this information down through the SEM, assuming it is
* acyclic. Works, but will hang for cyclic models, and is very slow for
* large numbers of variables (probably due to the heavyweight lookups of
* various values--could be improved). The model must be acyclic, or else
* this will spin.
*/
public DataSet simulateDataRecursive(int sampleSize) {
if (tierIndices == null) {
List<Node> nodes = graph.getNodes();
tierIndices = new int[nodes.size()];
for (int j = 0; j < nodes.size(); j++) {
tierIndices[j] = j;
}
}
int size = variableNodes.size();
setupModel(size);
class SimulateTask extends RecursiveTask<Boolean> {
private final int from;
private final int to;
private double[][] all;
private int chunk;
public SimulateTask(int from, int to, double[][] all, int chunk) {
this.from = from;
this.to = to;
this.all = all;
this.chunk = chunk;
}
@Override
protected Boolean compute() {
if (from - to > chunk) {
int mid = from + to / 2;
SimulateTask left = new SimulateTask(from, mid, all, chunk);
SimulateTask right = new SimulateTask(mid, to, all, chunk);
left.fork();
right.compute();
left.join();
return true;
} else {
for (int i = from; i < to; i++) {
// sqrt(errorVars[col]));
NormalDistribution normal = new NormalDistribution(new Well1024a(++seed), 0, 1);
normal.sample();
if (verbose && (i + 1) % 50 == 0) {
System.out.println("Simulating " + (i + 1));
}
for (int col : tierIndices) {
double value = normal.sample() * sqrt(errorVars[col]);
for (int j = 0; j < parents[col].length; j++) {
value += all[parents[col][j]][i] * coefs[col][j];
}
value += means[col];
all[col][i] = value;
}
}
return true;
}
}
}
if (graph instanceof TimeLagGraph) {
sampleSize += 200;
}
double[][] all = new double[variableNodes.size()][sampleSize];
int chunk = sampleSize / ForkJoinPoolInstance.getInstance().getPool().getParallelism() + 1;
ForkJoinPoolInstance.getInstance().getPool().invoke(new SimulateTask(0, sampleSize, all, chunk));
if (graph instanceof TimeLagGraph) {
int[] rem = new int[200];
for (int i = 0; i < 200; ++i) {
rem[i] = i;
}
BoxDataSet dat = new BoxDataSet(new VerticalDoubleDataBox(all), variableNodes);
dat.removeRows(rem);
return dat;
}
return new BoxDataSet(new VerticalDoubleDataBox(all), variableNodes);
}
use of java.util.concurrent.RecursiveTask in project sandbox by irof.
the class ForkJoinSample method サンプル.
@Test
public void サンプル() throws Exception {
// 今回は値を返すのでRecursiveTaskを使用します。
class MyTask extends RecursiveTask<List<String>> {
private final List<?> list;
private MyTask(List<?> list) {
this.list = list;
}
@Override
protected List<String> compute() {
// タスクをFork/Joinするか、そのまま実行するかを判断します。
if (list.size() < 2) {
// 十分に小さければそのまま実行します。
return computeDirectly();
}
int halfSize = list.size() / 2;
// forkすると非同期で実行されます。
MyTask taskA = new MyTask(list.subList(0, halfSize));
taskA.fork();
// computeは同期で実行されます。
MyTask taskB = new MyTask(list.subList(halfSize, list.size()));
List<String> resultB = taskB.compute();
// forkした結果をjoinで取得します。
List<String> resultA = taskA.join();
// 結果をマージします。
ArrayList<String> result = new ArrayList<>();
result.addAll(resultA);
result.addAll(resultB);
return result;
}
private List<String> computeDirectly() {
// 実行されたスレッド名を返します。
String name = Thread.currentThread().getName();
return Collections.singletonList(name);
}
}
// 5件の任意のリスト
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
MyTask task = new MyTask(list);
// ForkJoinPoolのinvokeにForkJoinTaskを渡すと、
// ForkJoinPoolの持つスレッドで実行されます。
ForkJoinPool forkJoinPool = new ForkJoinPool();
List<String> result = forkJoinPool.invoke(task);
// ForkJoinTaskが実行されたすべてのスレッド名と現在のスレッド名は異なります。
String name = Thread.currentThread().getName();
assertThat(result).doesNotContain(name);
System.out.println("this thread : " + name);
System.out.println("other threads: " + result);
}
use of java.util.concurrent.RecursiveTask in project tetrad by cmu-phil.
the class OrientCollidersMaxP method addColliders.
// ======================================== PRIVATE METHODS ====================================//
private void addColliders(Graph graph) {
final Map<Triple, Double> scores = new ConcurrentHashMap<>();
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, Map<Triple, Double> scores, 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++) {
if (Thread.currentThread().isInterrupted()) {
break;
}
doNode(graph, scores, nodes.get(i));
}
return true;
} else {
int mid = (to + from) / 2;
Task left = new Task(nodes, graph, scores, from, mid);
Task right = new Task(nodes, graph, scores, mid, to);
left.fork();
right.compute();
left.join();
return true;
}
}
}
Task task = new Task(nodes, graph, scores, 0, nodes.size());
ForkJoinPoolInstance.getInstance().getPool().invoke(task);
List<Triple> tripleList = new ArrayList<>(scores.keySet());
// Most independent ones first.
Collections.sort(tripleList, new Comparator<Triple>() {
@Override
public int compare(Triple o1, Triple o2) {
return Double.compare(scores.get(o2), scores.get(o1));
}
});
for (Triple triple : tripleList) {
Node a = triple.getX();
Node b = triple.getY();
Node c = triple.getZ();
// if (!(graph.getEndpoint(b, a) == Endpoint.ARROW || graph.getEndpoint(b, c) == Endpoint.ARROW)) {
// graph.setEndpoint(a, b, Endpoint.ARROW);
// graph.setEndpoint(c, b, Endpoint.ARROW);
orientCollider(graph, a, b, c, getConflictRule());
// }
}
}
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();
}
Aggregations