-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscan.java
More file actions
140 lines (121 loc) · 3.72 KB
/
scan.java
File metadata and controls
140 lines (121 loc) · 3.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import java.util.Arrays;
import java.util.Random;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.ForkJoinTask;
import java.util.concurrent.RecursiveAction;
import java.util.concurrent.RecursiveTask;
public class ParallelPrefix {
static int[] sums;
// Sequential prefix algorithm for testing
public static int[] prefixSequential(int[] arr) {
int[] res = new int[arr.length];
res[0] = arr[0];
for (int i = 1; i < arr.length; i++) {
res[i] = arr[i] + res[i - 1];
}
return res;
}
// Parallel prefix algorithm
public static int[] prefixParallel(int[] arr) {
ForkJoinPool pool = new ForkJoinPool();
int[] res = new int[arr.length];
sums = new int[arr.length * 2];
pool.invoke(new FirstPass(arr, res, 0, arr.length, 0));
pool.invoke(new SecondPass(res, 0, 0, arr.length, 0));
pool.shutdown();
return res;
}
// first pass task
static class FirstPass extends RecursiveTask<Integer> {
private final int CUTOFF = 500;
private int[] arr, res;
private int start, end, id;
FirstPass(int[] arr, int[] res, int start, int end, int id) {
this.arr = arr;
this.res = res;
this.start = start;
this.end = end;
this.id = id;
}
protected Integer compute() {
if (end - start <= CUTOFF) {
int sum = arr[start];
res[start] = arr[start];
for (int i = start + 1; i < end; i++) {
res[i] = res[i - 1] + arr[i];
sum += arr[i];
}
sums[id] = sum;
return sum;
} else {
int mid = (start + end) / 2;
FirstPass left = new FirstPass(arr, res, start, mid, 2 * id + 1);
FirstPass right = new FirstPass(arr, res, mid, end, 2 * id + 2);
left.fork();
right.fork();
int leftSum = left.join();
int rightSum = right.join();
sums[id] = leftSum + rightSum;
return leftSum + rightSum;
}
}
}
static class SecondPass extends RecursiveAction {
private final int CUTOFF = 500;
private int[] res;
private int start, end, correction, id;
public SecondPass(int[] res, int correction, int start, int end, int id) {
this.res = res;
this.correction = correction;
this.start = start;
this.end = end;
this.id = id;
}
protected void compute() {
if (end - start <= CUTOFF) {
for (int i = start; i < end; i++) {
res[i] += correction;
}
} else {
int mid = (start + end) / 2;
SecondPass left = new SecondPass(res, correction, start, mid, 2 * id + 1);
SecondPass right = new SecondPass(res, correction + sums[2 * id + 1], mid, end, 2 * id + 2);
left.fork();
right.fork();
left.join();
right.join();
}
}
}
// Test the algorithm
public static void main(String[] args) {
int length = 10000;
int[] arr = new int[length];
Random random = new Random();
for (int i = 0; i < length; i++) {
arr[i] = random.nextInt(100); // generate random numbers between 0 and 99
}
int[] expected = new int[length];
int[] res = new int[length];
res = prefixParallel(arr);
expected = prefixSequential(arr);
int count = compareArrays(res, expected);
if (count == 0) {
System.out.println("The arrays are the same.");
} else {
System.out.println("The arrays differ in " + count + " elements.");
}
}
public static int compareArrays(int[] arr1, int[] arr2) {
int count = 0;
if (arr1.length != arr2.length) {
throw new IllegalArgumentException("The arrays have different lengths.");
}
for (int i = 0; i < arr1.length; i++) {
if (arr1[i] != arr2[i]) {
count++;
}
}
return count;
}
}