-
Notifications
You must be signed in to change notification settings - Fork 0
/
select_b_nas.sc
233 lines (201 loc) · 5.64 KB
/
select_b_nas.sc
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/* tab:8
*
* select_b_nas.sc - Parallel Selection Algorithm for NAS IS
*
*
* "Copyright (c) 1995,1996 The Regents of the University of Maryland.
* All rights reserved.
*
* Permission to use, copy, modify, and distribute this software and its
* documentation for any purpose, without fee, and without written agreement is
* hereby granted, provided that the above copyright notice and the following
* two paragraphs appear in all copies of this software.
*
* IN NO EVENT SHALL THE UNIVERSITY OF MARYLAND BE LIABLE TO ANY PARTY FOR
* DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
* OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
* MARYLAND HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* THE UNIVERSITY OF MARYLAND SPECIFICALLY DISCLAIMS ANY WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
* ON AN "AS IS" BASIS, AND THE UNIVERSITY OF MARYLAND HAS NO OBLIGATION TO
* PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS."
*
* Authors: David A. Bader <dbader@umiacs.umd.edu>
* Joseph F. Ja'Ja' <joseph@umiacs.umd.edu>
* Institute for Advanced Computer Studies
* Department of Electrical Engineering
* AV Williams Building
* College Park, MD 20742
*
* Version: 1.0
* Creation Date: April 17, 1995
* Filename: select_b_nas.sc
* History:
*/
#include "select_b_nas.h"
#define THRESH_SEL (PROCS*PROCS)
#define PROFILE_DETAILED 0
static int binsearch_gt(int * list, int min_idx, int max_idx, int target)
/* return the highest index of the array list with element <= target */
/* return min_idx-1 if all elements of list > target */
/* "THE PRICE IS RIGHT" */
{
register int mid_idx;
#if PROFILE_DETAILED
fprintf(outfile,"PE %2d: in binsearch_gt (%5d, %5d): %5d\n",
MYPROC, min_idx, max_idx, target);
#endif
if (max_idx < min_idx)
return (min_idx-1);
if (min_idx == max_idx)
if (list[min_idx] <= target)
return (min_idx);
else
return (min_idx-1);
mid_idx = min_idx + (max_idx - min_idx) / 2;
if (list[mid_idx] <= target) /* This person bid under */
return binsearch_gt(list, mid_idx+1, max_idx, target);
else /* This person bid over */
return binsearch_gt(list, min_idx, mid_idx-1, target);
}
/*************************************************************/
static int all_select_b_nas(int M, int *spread A,
int *spread N, int total_n, int i)
/*************************************************************/
{
int
meds[PROCS],
mom,
j, k, l,
t, v,
result,
*seqval;
int *locA,
*locN;
#if PROFILE_DETAILED
double secs;
#endif
barrier();
#if PROFILE_DETAILED
barrier();
on_one {
fprintf(outfile,"PE %2d: all_select_b_nas: total_n: %d i: %d\n",
MYPROC, total_n, i);
fflush(outfile);
}
barrier();
#endif
if (total_n < THRESH_SEL) {
#if PROFILE_DETAILED
barrier();
secs = get_seconds();
#endif
locN = (int*) malloc(PROCS * sizeof(int));
assert_malloc(locN);
locN[0] = *(N+MYPROC);
all_gather(locN);
on_one {
seqval = (int*)malloc(total_n*sizeof(int));
assert_malloc(seqval);
t = 0;
for (j=0; j<PROCS ; j++) {
bulk_get(seqval+t, (int *global)(A+j), locN[j]*sizeof(int));
t += locN[j];
}
#if 1
if (t != total_n)
fprintf(stderr,"ERROR: t != total_n\n");
#endif
sync();
fastsort_19(seqval,t);
result = seqval[i];
free(seqval);
}
#if PROFILE_DETAILED
barrier();
secs = get_seconds() - secs;
on_one {
fprintf(outfile,"Seq2 n: %12d ",total_n);
fprintf(outfile,"Time for Seq2: %9.6f\n",secs);
}
barrier();
#endif
result = all_bcast(result);
free(locN);
}
else {
#if PROFILE_DETAILED
barrier();
secs = get_seconds();
#endif
all_LoadBalance_b(M, A, N, total_n);
#if PROFILE_DETAILED
barrier();
secs = get_seconds() - secs;
on_one {
fprintf(outfile,"LB2 n: %12d ",total_n);
fprintf(outfile,"Time for LB2: %9.6f\n",secs);
}
barrier();
#endif
#if PROFILE_DETAILED
barrier();
secs = get_seconds();
#endif
locN = (int *)(N+MYPROC);
locA = (int *)(A+MYPROC);
fastsort_19(locA, *locN);
*meds = locA[(*locN + 1) >> 1];
all_gather(meds);
on_one fastsort_19(meds, PROCS);
mom = all_bcast(meds[PROCS>>1]);
k = binsearch_gt(locA, 0, *locN - 1, mom);
l = k+1;
t = all_reduce_to_all_add(l);
v = (i <= t);
j = (v ? t : (total_n - t) );
total_n = j;
if (v)
*locN = l ;
else {
*locN -= l;
#if 1
bcopy(locA+l , locA, (*locN) * sizeof(int));
#else
for (j=0 ; j< *locN ; j++)
locA[j] = locA[j+l];
#endif
}
#if PROFILE_DETAILED
barrier();
secs = get_seconds() - secs;
on_one {
fprintf(outfile,"Sel2 n: %12d ",total_n);
fprintf(outfile,"Time for Sel2: %9.6f\n",secs);
}
barrier();
#endif
result = all_select_b_nas(M, A, N, total_n, (v ? i : (i-t)));
}
return (result);
}
int all_select_median_b_nas(int M, int *spread A)
{
int *spread N;
int t;
N = all_spread_malloc(PROCS,sizeof(int));
assert_spread_malloc(N);
*(N+MYPROC) = M;
t = PROCS*M;
#if PROFILE_DETAILED
barrier();
on_one {
fprintf(outfile,"PE %2d: all_select_median_b_nas (%d)\n",MYPROC,t);
fflush(outfile);
}
barrier();
#endif
return all_select_b_nas(M, A, N, t, (t+1)>>1);
}