-
Notifications
You must be signed in to change notification settings - Fork 0
/
select_randsamp.c
208 lines (171 loc) · 4.91 KB
/
select_randsamp.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
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
/* tab:8
*
* select_randsamp.c - Parallel Selection Algorithm
*
*
* "Copyright (c) 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: February 6, 1996
* Filename: select_randsamp.c
* History:
*/
#include "select_randsamp.h"
#define DEBUG 0
#define BUNDLE 4
int all_random_pick_exact_i(int *A, int *B, int M, int n) {
double mscale;
int *B_ptr, *E_ptr;
int i, j, k;
if (M>0) {
k = (int)ceil((double)M * pow((double)n, PICK_EPS-1.0));
B_ptr = B;
E_ptr = B_ptr + k;
mscale = (double)M/2147483648.0;
/* for (i=0 ; i<k ; i++) */
while (B_ptr < E_ptr)
{
j = (int)floor((double)rrandom()*mscale);
#if 0
if ((j >= M) || (j < 0))
fprintf(stderr,"ERROR: all_random_pick_exact_i j: %d M: %d\n",j,M);
#endif
*B_ptr++ = A[j];
}
}
else
k = 0;
return (k);
}
int all_random_pick_exact_d(double *A, double *B, int M, int n) {
double mscale;
double *B_ptr, *E_ptr;
int i, j, k;
if (M>0) {
B_ptr = B;
k = (int)ceil((double)M * pow((double)n, PICK_EPS-1.0));
mscale = (double)M/2147483648.0;
/* for (i=0 ; i<k ; i++) */
E_ptr = B_ptr + k;
while (B_ptr < E_ptr)
{
j = (int)floor((double)rrandom()*mscale);
#if 0
if ((j >= M) || (j < 0))
fprintf(stderr,"ERROR: all_random_pick_exact_d j: %d M: %d\n",j,M);
#endif
*B_ptr++ = A[j];
}
}
else
k = 0;
return (k);
}
int all_random_pick_exactbund_i(int *A, int *B, int M, int n) {
double mscale;
int *B_ptr;
int i, j, k;
int times, rem, ebs;
if (M>0) {
k = (int)ceil((double)M * pow((double)n, PICK_EPS-1.0));
times = k / BUNDLE;
rem = k - (times*BUNDLE);
ebs = BUNDLE*sizeof(int);
B_ptr = B;
mscale = (double)M/2147483648.0;
for (i=0 ; i<times ; i++) {
j = (int)floor((double)rrandom()*mscale);
if (j+BUNDLE-1 >= M) {
j = M - BUNDLE;
#if 0
fprintf(stderr,"ERROR: all_random_pick_exact_i j: %d M: %d\n",j,M);
#endif
}
memcpy(B_ptr, A+j, ebs);
B_ptr += BUNDLE;
}
if (rem>0) {
j = (int)floor((double)rrandom()*mscale);
if (j+BUNDLE-1 >= M) {
j = M - BUNDLE;
#if 0
fprintf(stderr,"ERROR: all_random_pick_exact_i j: %d M: %d\n",j,M);
#endif
}
memcpy(B_ptr, A+j, rem*sizeof(int));
}
#if 0
if (k != B_ptr+rem - B)
fprintf(stderr,"ERROR: k: %d B_ptr-rem-B: %d\n",k, B_ptr+rem-B);
#endif
}
else
k = 0;
return (k);
}
int all_random_pick_exactbund_d(double *A, double *B, int M, int n) {
double mscale;
double *B_ptr;
int i, j, k;
int times, rem, ebs;
if (M>0) {
k = (int)ceil((double)M * pow((double)n, PICK_EPS-1.0));
times = k / BUNDLE;
rem = k - (times*BUNDLE);
ebs = BUNDLE*sizeof(double);
B_ptr = B;
mscale = (double)M/2147483648.0;
for (i=0 ; i<times ; i++) {
j = (int)floor((double)rrandom()*mscale);
if (j+BUNDLE-1 >= M) {
j = M - BUNDLE;
#if 0
fprintf(stderr,"ERROR: all_random_pick_exact_d j: %d M: %d\n",j,M);
#endif
}
memcpy(B_ptr, A+j, ebs);
B_ptr += BUNDLE;
}
if (rem>0) {
j = (int)floor((double)rrandom()*mscale);
if (j+BUNDLE-1 >= M) {
j = M - BUNDLE;
#if 0
fprintf(stderr,"ERROR: all_random_pick_exact_d j: %d M: %d\n",j,M);
#endif
}
memcpy(B_ptr, A+j, rem*sizeof(double));
}
#if 0
if (k != B_ptr+rem - B)
fprintf(stderr,"ERROR: k: %d B_ptr-rem-B: %d\n",k, B_ptr+rem-B);
#endif
}
else
k = 0;
return (k);
}