Skip to content

Commit c54539a

Browse files
committed
completed
1 parent 7e42023 commit c54539a

File tree

107 files changed

+1603
-3754
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

107 files changed

+1603
-3754
lines changed

Parallel Programming/miniproject_1/src/main/java/edu/coursera/parallel/ReciprocalArraySum.java

+29-50
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ private static class ReciprocalArraySumTask extends RecursiveAction {
102102
* Intermediate value produced by this task.
103103
*/
104104
private double value;
105-
static int SEQ_FACTOR = 4;
105+
private static final int SEQUENTIAL_THRESHOLD = 1000;
106106

107107
/**
108108
* Constructor.
@@ -128,30 +128,18 @@ public double getValue() {
128128

129129
@Override
130130
protected void compute() {
131-
int range = endIndexExclusive - startIndexInclusive;
132-
if (range <= (input.length / SEQ_FACTOR)) {
133-
System.out.println("Computing " + startIndexInclusive + " " + endIndexExclusive);
134-
double result = 0;
131+
if ((endIndexExclusive - startIndexInclusive) <= SEQUENTIAL_THRESHOLD) {
135132
for (int i = startIndexInclusive; i < endIndexExclusive; i++) {
136-
result += 1 / input[i];
133+
value += 1 / input[i];
137134
}
138-
value = result;
139-
System.out.println("Computing done");
140135
} else {
141-
int half = (endIndexExclusive - startIndexInclusive) / 2;
142-
int a = startIndexInclusive;
143-
int b = startIndexInclusive + half;
144-
int c = endIndexExclusive;
145-
System.out.println(("Forking" + a + " " + b + " " + c));
146-
ReciprocalArraySumTask left = new ReciprocalArraySumTask(a, b, input);
147-
ReciprocalArraySumTask right = new ReciprocalArraySumTask(b, c, input);
136+
final int midIndex = (startIndexInclusive + endIndexExclusive) / 2;
137+
final ReciprocalArraySumTask left = new ReciprocalArraySumTask(startIndexInclusive, midIndex, input);
138+
final ReciprocalArraySumTask right = new ReciprocalArraySumTask(midIndex, endIndexExclusive, input);
148139
left.fork();
149-
right.fork();
140+
right.compute();
150141
left.join();
151-
right.join();
152-
System.out.println("Joined");
153-
double result = left.getValue() + right.getValue();
154-
value = result;
142+
value = left.value + right.value;
155143
}
156144
}
157145
}
@@ -168,27 +156,10 @@ protected void compute() {
168156
protected static double parArraySum(final double[] input) {
169157
assert input.length % 2 == 0;
170158

171-
return parManyTaskArraySum(input, 2);
172-
173-
// double sum = 0;
174-
//
175-
// ReciprocalArraySumTask left = new ReciprocalArraySumTask(0, input.length / 2,
176-
// input);
177-
// ReciprocalArraySumTask right = new ReciprocalArraySumTask(input.length / 2,
178-
// input.length, input);
179-
//
180-
// left.fork();
181-
// right.compute();
182-
// left.join();
183-
//
184-
// sum = left.getValue() + right.getValue();
185-
//
186-
//// // Compute sum of reciprocals of array elements
187-
//// for (int i = 0; i < input.length; i++) {
188-
//// sum += 1 / input[i];
189-
//// }
190-
//
191-
// return sum;
159+
double sum=0;
160+
161+
sum = parManyTaskArraySum(input, 2);
162+
return sum;
192163
}
193164

194165
/**
@@ -202,19 +173,27 @@ protected static double parArraySum(final double[] input) {
202173
* @return The sum of the reciprocals of the array input
203174
*/
204175
protected static double parManyTaskArraySum(final double[] input, final int numTasks) {
176+
205177
double sum = 0;
206178

207-
System.setProperty("java.util.concurrent.ForkJoinPool.common.parallelism", "4");
208-
System.out.println("numTasks" + numTasks);
179+
// Compute sum of reciprocals of array elements
180+
final ReciprocalArraySumTask[] tasks = new ReciprocalArraySumTask[numTasks];
209181

210-
ReciprocalArraySumTask task = new ReciprocalArraySumTask(0, input.length, input);
211-
ForkJoinPool.commonPool().invoke(task);
212-
sum = task.getValue();
182+
for (int i = 0; i < numTasks; i++) {
183+
final int startIndexInclusive = getChunkStartInclusive(i, numTasks, input.length);
184+
final int endIndexExclusive = getChunkEndExclusive(i, numTasks, input.length);
185+
tasks[i] = new ReciprocalArraySumTask(startIndexInclusive, endIndexExclusive, input);
186+
}
213187

214-
// Compute sum of reciprocals of array elements
215-
// for (int i = 0; i < input.length; i++) {
216-
// sum += 1 / input[i];
217-
// }
188+
for (int i = 0; i < (numTasks - 1); i++) {
189+
tasks[i].fork();
190+
}
191+
tasks[numTasks - 1].compute();
192+
for (int i = 0; i < (numTasks - 1); i++) {
193+
tasks[i].join();
194+
sum += tasks[i].getValue();
195+
}
196+
sum += tasks[numTasks - 1].getValue();
218197

219198
return sum;
220199
}

0 commit comments

Comments
 (0)