University stuff.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Parallel.java 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. class Parallel extends Oblig4 {
  2. int nThreads = Math.min(
  3. Runtime.getRuntime().availableProcessors(),
  4. 16);
  5. Parallel(int[] x, int[] y, int n) {
  6. super(x, y, n);
  7. name = "Parallel";
  8. }
  9. @Override
  10. void solve() {
  11. MinMaxXY minmax = findMinMax();
  12. MAX_X = x[minmax.maxX];
  13. MAX_Y = y[minmax.maxY];
  14. IntList indexes = new IntList(n);
  15. for (int i = 0; i < n; ++i)
  16. indexes.add(i);
  17. PointsRightOfWorker w1 = new PointsRightOfWorker(
  18. minmax.maxX, minmax.minX, indexes, 1);
  19. w1.start();
  20. PointsRightOfWorker w2 = new PointsRightOfWorker(
  21. minmax.minX, minmax.maxX, indexes, 1);
  22. w2.start();
  23. try { w1.join(); } catch (InterruptedException ex) {}
  24. try { w2.join(); } catch (InterruptedException ex) {}
  25. coHull = w1.result;
  26. coHull.append(w2.result);
  27. }
  28. class MinMaxXY {
  29. int minX, maxX, maxY;
  30. }
  31. class FindMinMaxWorker extends Thread {
  32. int start, end;
  33. int minX, maxX, maxY;
  34. FindMinMaxWorker(int start, int end) {
  35. this.start = start;
  36. this.end = end;
  37. this.minX = start;
  38. this.maxX = start;
  39. this.maxY = start;
  40. }
  41. public void run() {
  42. for (int i = start + 1; i < end; ++i) {
  43. if (x[i] < x[minX])
  44. minX = i;
  45. if (x[i] > x[maxX])
  46. maxX = i;
  47. if (y[i] > y[maxY])
  48. maxY = i;
  49. }
  50. }
  51. }
  52. MinMaxXY findMinMax() {
  53. FindMinMaxWorker[] workers = new FindMinMaxWorker[nThreads];
  54. int d = n / nThreads;
  55. for (int i = 0; i < nThreads; ++i) {
  56. workers[i] = new FindMinMaxWorker(
  57. d * i,
  58. (i == nThreads - 1
  59. ? n - 1
  60. : d * (i + 1)));
  61. workers[i].start();
  62. }
  63. MinMaxXY minmax = new MinMaxXY();
  64. for (FindMinMaxWorker w: workers) {
  65. try { w.join(); } catch (InterruptedException ex) {}
  66. if (x[w.minX] < x[minmax.minX])
  67. minmax.minX = w.minX;
  68. if (x[w.maxX] > x[minmax.maxX])
  69. minmax.maxX = w.maxX;
  70. if (y[w.maxY] > y[minmax.maxY])
  71. minmax.maxY = w.maxY;
  72. }
  73. return minmax;
  74. }
  75. class PointsRightOfWorker extends Thread {
  76. int p1, p2;
  77. IntList indexes;
  78. int depth;
  79. IntList result;
  80. PointsRightOfWorker(int p1, int p2, IntList indexes, int depth) {
  81. this.p1 = p1;
  82. this.p2 = p2;
  83. this.indexes = indexes;
  84. this.depth = depth;
  85. }
  86. public void run() {
  87. result = pointsRightOf(p1, p2, indexes, depth);
  88. }
  89. }
  90. IntList pointsRightOf(int p1, int p2, IntList indexes, int depth) {
  91. int p3 = -1;
  92. double p3dist = 0;
  93. int a = lineA(p1, p2);
  94. int b = lineB(p1, p2);
  95. int c = lineC(p1, p2);
  96. IntList nwIndexes = new IntList();
  97. for (int i = 0; i < indexes.size(); ++i) {
  98. int idx = indexes.get(i);
  99. if (idx == p1 || idx == p2)
  100. continue;
  101. double line = lineEquation(a, b, c, idx);
  102. if (line > 0)
  103. continue;
  104. nwIndexes.add(idx);
  105. double dist = Math.abs(dist(line, a, b));
  106. if (dist > p3dist) {
  107. p3dist = dist;
  108. p3 = idx;
  109. }
  110. }
  111. if (p3 == -1) {
  112. IntList l = addPointsOnLine(nwIndexes, a, b, c, p1);
  113. l.add(p2);
  114. return l;
  115. }
  116. if (Math.pow(2, depth) >= nThreads) {
  117. IntList l = pointsRightOf(p1, p3, nwIndexes, depth);
  118. l.append(pointsRightOf(p3, p2, nwIndexes, depth));
  119. return l;
  120. } else {
  121. PointsRightOfWorker w1 = new PointsRightOfWorker(p1, p3, nwIndexes, depth + 1);
  122. w1.start();
  123. PointsRightOfWorker w2 = new PointsRightOfWorker(p3, p2, nwIndexes, depth + 1);
  124. w2.start();
  125. try { w1.join(); } catch (InterruptedException ex) {}
  126. try { w2.join(); } catch (InterruptedException ex) {}
  127. IntList l = w1.result;
  128. l.append(w2.result);
  129. return l;
  130. }
  131. }
  132. }