Browse Source

sequential

master
mortie 7 years ago
parent
commit
8e74c1257d
4 changed files with 111 additions and 11 deletions
  1. 1
    0
      inf2440/hw4/Main.java
  2. 25
    6
      inf2440/hw4/Oblig4.java
  3. 85
    0
      inf2440/hw4/Parallel.java
  4. 0
    5
      inf2440/hw4/Sequential.java

+ 1
- 0
inf2440/hw4/Main.java View File

@@ -13,6 +13,7 @@ class Main {
run(par, tseq);

seq.draw();
par.draw();
}

static Timer run(Oblig4 o, Timer base) {

+ 25
- 6
inf2440/hw4/Oblig4.java View File

@@ -27,18 +27,37 @@ abstract class Oblig4 {
// It's responsible for filling in MAX_X, MAX_Y, and coHull
abstract void solve();

/*
* The rest is utility methods which any implementation will use.
*/
// We don't want to draw on the main thread.
class DrawThread extends Thread {
Oblig4 d;

DrawThread(Oblig4 d) {
this.d = d;
}

public void run() {
try {
TegnUt t = new TegnUt(d, coHull, name);
t.setVisible(true);
} catch (Exception ex) {
System.out.println(name+": Couldn't draw window.");
}
}
}

// Draw points using TegnUt.
// Requires x, y, n, MAX_X, MAX_Y, and coHull
// to be filled out.
void draw() {
TegnUt t = new TegnUt(this, coHull, name);
t.setVisible(true);
Thread draw() {
DrawThread t = new DrawThread(this);
t.start();
return t;
}

/*
* The rest is utility methods which any implementation will use.
*/

// a, b, and c will be used in the line equation.
// It doesn't make sense to recalculate them all the time,
// as they only depend on p1 and p2.

+ 85
- 0
inf2440/hw4/Parallel.java View File

@@ -17,6 +17,19 @@ class Parallel extends Oblig4 {
IntList indexes = new IntList(n);
for (int i = 0; i < n; ++i)
indexes.add(i);

PointsRightOfWorker w1 = new PointsRightOfWorker(
minmax.maxX, minmax.minX, indexes, 1);
w1.start();
PointsRightOfWorker w2 = new PointsRightOfWorker(
minmax.minX, minmax.maxX, indexes, 1);
w2.start();

try { w1.join(); } catch (InterruptedException ex) {}
try { w2.join(); } catch (InterruptedException ex) {}

coHull = w1.result;
coHull.append(w2.result);
}

class MinMaxXY {
@@ -73,4 +86,76 @@ class Parallel extends Oblig4 {

return minmax;
}

class PointsRightOfWorker extends Thread {
int p1, p2;
IntList indexes;
int depth;
IntList result;

PointsRightOfWorker(int p1, int p2, IntList indexes, int depth) {
this.p1 = p1;
this.p2 = p2;
this.indexes = indexes;
this.depth = depth;
}

public void run() {
result = pointsRightOf(p1, p2, indexes, depth);
}
}

IntList pointsRightOf(int p1, int p2, IntList indexes, int depth) {
int p3 = -1;
double p3dist = 0;

int a = lineA(p1, p2);
int b = lineB(p1, p2);
int c = lineC(p1, p2);

IntList nwIndexes = new IntList();

for (int i = 0; i < indexes.size(); ++i) {
int idx = indexes.get(i);
if (idx == p1 || idx == p2)
continue;

double line = lineEquation(a, b, c, idx);
if (line > 0)
continue;

nwIndexes.add(idx);

double dist = Math.abs(dist(line, a, b));
if (dist > p3dist) {
p3dist = dist;
p3 = idx;
}
}

if (p3 == -1) {
IntList l = addPointsOnLine(nwIndexes, a, b, c, p1);
l.add(p2);

return l;
}

if (Math.pow(2, depth) >= nThreads) {
IntList l = pointsRightOf(p1, p3, nwIndexes, depth);
l.append(pointsRightOf(p3, p2, nwIndexes, depth));
return l;
} else {
PointsRightOfWorker w1 = new PointsRightOfWorker(p1, p3, nwIndexes, depth + 1);
w1.start();
PointsRightOfWorker w2 = new PointsRightOfWorker(p3, p2, nwIndexes, depth + 1);
w2.start();

try { w1.join(); } catch (InterruptedException ex) {}
try { w2.join(); } catch (InterruptedException ex) {}

IntList l = w1.result;
l.append(w2.result);
return l;
}
}
}

+ 0
- 5
inf2440/hw4/Sequential.java View File

@@ -17,11 +17,6 @@ class Sequential extends Oblig4 {

coHull = pointsRightOf(minmax.maxX, minmax.minX, indexes);
coHull.append(pointsRightOf(minmax.minX, minmax.maxX, indexes));

for (int i = 0; i < coHull.size(); ++i) {
System.out.print(coHull.get(i)+" ");
}
System.out.println("");
}

class MinMaxXY {

Loading…
Cancel
Save