-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathoddevenbsp.c
59 lines (52 loc) · 1.58 KB
/
oddevenbsp.c
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
void odd_even_par(float *array, int n){
//array is a pointer to the array to sort.
//n = length of the array and amount of used processors.
bsp_begin(n);
//Initialisation and data distribution superstep 0
int p = n;
int s = bsp_pid();
float *localelem, *otherelem;
localelem = array[s];
bsp_push_reg(localelem, sizeof(float));
bsp_sync();
for(int i=1; i<=n; i++){ // n times the superstep 1 and 2
if(!isEven(i)){ // i is odd
if(!isEven(s)){ // pid is odd
comp_exch_min(s+1, localelem, otherelem);
} else { // pid is even
comp_exch_max(s-1, localelem, otherelem);
}
} else { // i is even
if(isEven(s)){ // pid is even
comp_exch_min(s+1, localelem, otherelem);
} else { // pid is odd
comp_exch_max(s-1, localelem, otherelem);
}
}
}
}
int isEven(int i){
return i % 2;
}
//keep the only the minima in the local array
void comp_exch_min(int otherpid, float *myelem, float *otherelem){
//Communication superstep 1: exchanging with neighboring processors.
bsp_get(otherpid, localelem, 0, otherelem, sizeof(float));
bsp_sync();
//Calcuclation superstep 2: comparing and updating the localarray.
if(*myelem > *otherelem ){
*myelem = *otherelem;
}
bsp_sync();
}
//keep the only the maxima in the local array
void comp_exch_max(int otherpid, float *myelem, float *otherelem){
//Communication superstep 1: exchanging with neighboring processors.
bsp_get(otherpid, localelem, 0, otherelem, sizeof(float));
bsp_sync();
//Calcuclation superstep 2: comparing and updating the localarray.
if(*myelem < *otherelem ){
*myelem = *otherelem;
}
bsp_sync();
}