forked from tjknoth/GGDMS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquickMultiSelect.cpp
More file actions
202 lines (158 loc) · 5.11 KB
/
quickMultiSelect.cpp
File metadata and controls
202 lines (158 loc) · 5.11 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
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
//
// quickSelect.c
//
// Created by Patrick Slough on 11/15/14.
//
// gcc -std=c99 -o qs quickSelect.c
//
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
//#define VERBOSE
template<typename T>
void swap(T * x, T * y){
//generic pointer swap
T temp = *x;
*x = *y;
*y = temp;
}
template<typename T>
uint* mypartition(T * vec, uint length, T pivot){
// Dutch National flag partition algorithm
// inspired by algorithm discussed here:
// http://www.math.grin.edu/~rebelsky/Courses/CSC207/2014F/eboards/eboard.24.html
// index of where elements smaller than the pivot end
uint s = 0;
//index of where equal elements end
uint e = 0;
//index of where bigger elements start
uint b = length;
//make array to return s and b
uint* returnVals;
returnVals = (uint *)malloc(sizeof(uint)*2);
while (e < b){
if (vec[e] < pivot){
swap<T>(&vec[s++], &vec[e++]);
}
else if (vec[e] == pivot){
e++;
}
else{
swap<T>(&vec[e], &vec[b - 1]);
b--;
}
}
returnVals[0] = s;
returnVals[1] = b;
return returnVals;
}
template<typename T>
T myqs(T * vec, uint orderStat, uint length){
if (length == 1){
return vec[0];
} else {
uint mid = length/2;
T pivot = vec[mid];
uint *bounds = mypartition<T>(vec, length, pivot);
if(orderStat >= bounds[0] && orderStat < bounds[1]){
return vec[orderStat];
}
else if(orderStat < bounds[0]){
return myqs<T>(vec, orderStat, bounds[0]);
}
else{
return myqs<T>(vec+bounds[1], orderStat-bounds[1], length-bounds[1]);
}
}
}
template<typename T>
T myquickSelect(T* vec, uint orderStat, uint length){
if(orderStat > length){
return -1;
}
else{
return myqs<T>(vec, orderStat, length);
}
}
template<typename T>
T* naiveQuickMultiSelect(T* vec, uint* orderStats, uint vecLength, uint orderStatsLength){
//vector to hold returned orderStats
T* returnVals = malloc(sizeof(T)*orderStatsLength);
//run quickselect on every orderStat wanted.
//vec should become more sorted on every run, making selection faster
for(int i = 0; i < orderStatsLength; i++){
returnVals[i] = myquickSelect<T>(vec, orderStats[i], vecLength);
}
return returnVals;
}
template<typename T>
void myqms(T * vec, uint * orderStats, T * results, uint vecLength, uint osLength){
if(osLength <= 1){
results[0] = myqs<T>(vec, orderStats[0], vecLength);
//printf("result = %d \n", results[0]);
} else {
uint mid = (osLength)/2;
// *** median of threes ***
if (vecLength>10){
if (vec[0] > vec[vecLength-1]){
swap<T>(&vec[0], &vec[vecLength-1]);
}
if (vec[0] > vec[mid]){
swap<T>(&vec[0], &vec[mid]);
}
if (vec[vecLength-1] < vec[mid]){
swap<T>(&vec[0], &vec[mid]);
}
}
uint w = orderStats[mid];
results[mid] = myqs<T>(vec, w, vecLength);
//printf("w = %d, result[%d] = %d\n", w, mid, results[mid]);
// if there are order statistcs smaller than w, recurse
if (mid>0) {
myqms<T>(vec, orderStats, results, w, mid);
}
// if there are oder statistics larger than w, recurse
if (osLength > mid + 1){
// update the order statistics for the new vector
for (int i=mid+1; i<osLength; i++){
orderStats[i] -= (w+1);
}
myqms<T>(vec + w + 1, orderStats + mid + 1, results + mid + 1, vecLength - w - 1, osLength - mid -1);
} // end if osLength>mid+1
} // end if {osLength<=1} else {
}
/*
template<typename T>
void quickMultiSelect(T * vec, uint * orderStats, T * results, uint vecLength, uint orderStatsLength){
//based on mselect algorithm from
//http://www.ccse.kfupm.edu.sa/~suwaiyel/publications/multiselection_parCom.pdf
uint *osIndices;
osIndices = (uint *) malloc(sizeof(uint)*orderStatsLength);
for (register int i=0; i< orderStatsLength; i++){
osIndices[i]=i;
}
// sort the given indices
thrust::host_ptr<uint>kVals_ptr(orderStats);
thrust::host_ptr<uint>kIndices_ptr(osIndices);
thrust::sort_by_key(kVals_ptr, kVals_ptr + numKs, kIndices_ptr);
myqms<T>(vec, orderStats, results, vecLength, orderStatsLength);
free(osIndices);
}
*/
/* Wrapper function around the multi-selection fucntion that inverts the given k indices.
*/
template <typename T>
T quickMultiselectWrapper (T * h_vector, uint length, uint * kVals_ori, uint numKs
, T * outputs, int blocks, int threads) {
uint kVals[numKs];
// fix zero indexing
for (register int i = 0; i < numKs; i++)
kVals[numKs - i - 1] = length - kVals_ori[i];
// quickMultiSelect<T>(h_vector, kVals, outputs, length, numKs);
myqms<T>(h_vector, kVals, outputs, length, numKs);
for (register int i=0; i<numKs/2; i++)
swap<T>(&outputs[i],&outputs[numKs-i-1]);
//outputs = quickMultiSelect<T>(h_vector, length, kVals, numKs);
return 1;
}