Skip to content

Instantly share code, notes, and snippets.

@lexich
Created July 2, 2012 07:22
Show Gist options
  • Save lexich/3031658 to your computer and use it in GitHub Desktop.
Save lexich/3031658 to your computer and use it in GitHub Desktop.
Integration in threads
import java.util.Scanner;
/**
* User: lexich Date: 02.07.12 Time: 10:24
*/
class Func{
/**
* Integration function
*
* @param x input arg
*
* @return result
*/
public static double f(double x) {
return Math.pow(x, 5) + 5 * Math.pow(x, 4) - 3 * Math.log10(x);
}
}
class Worker implements Runnable{
private double result = 0;
private final double from;
private final double to;
private final int steps;
/**
* get integration result
*
* @return result
*/
public double getResult() {
return result;
}
public Worker(final double aFrom, final double aTo, final int aSteps) {
from = aFrom;
to = aTo;
steps = aSteps;
}
public void run() {
result = 0;
final double delta = (to - from) / steps;
double start = Func.f(from);
double end = 0;
System.out.println("Run: from:" + from + " to:" + to + " delta:" + delta + " steps:" + steps + " thread:" + Thread.currentThread().getId());
for (double i = from + delta; i <= to; i += delta) {
end = Func.f(i);
double res = (start + end) * delta / 2;
result += res;
//System.out.println( "start:" + start + "\tend:" + end + "\tdelta:" + delta );
//System.out.println("res:" + res + " result:" + result + " thread:" + Thread.currentThread().getId());
start = end;
}
}
}
public class Integration{
private static double integration(double from, double to, int steps, boolean aInThreads) throws InterruptedException {
if (from > to) {
throw new IllegalArgumentException("from > to");
}
if (steps <= 0) {
throw new IllegalArgumentException("steps != 0");
}
double result = 0;
if (aInThreads) {
int processorCount = Runtime.getRuntime().availableProcessors();
Worker workers[] = new Worker[processorCount];
Thread threads[] = new Thread[processorCount];
final double delta = (to - from) / processorCount;
for (int i = 0; i < processorCount; i++) {
Worker w = new Worker(from + i * delta, from + (i + 1) * delta, steps / processorCount);
workers[i] = w;
threads[i] = new Thread(w);
}
for (Thread th : threads)
th.start();
for (Thread th : threads)
th.join();
for (Worker w : workers)
result += w.getResult();
} else {
Worker w = new Worker(from, to, steps);
w.run();
result = w.getResult();
}
return result;
}
public static void main(String[] args) {
double from, to;
int steps;
if (args.length >= 3) {
from = Double.parseDouble(args[0]);
to = Double.parseDouble(args[1]);
steps = Integer.parseInt(args[2]);
} else {
Scanner scanner = new Scanner(System.in);
System.out.println("Input start of integration interval:");
from = scanner.nextDouble();
System.out.println("Input end of integration interval:");
to = scanner.nextDouble();
System.out.println("Input steps counts:");
steps = scanner.nextInt();
}
try {
System.out.println("Integration result: " + integration(from, to, steps, false));
System.out.println("Integration result in threads: " + integration(from, to, steps, true));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment