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 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
|
/*
Copyright (c) 2009, David Cheng, Viral B. Shah.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#ifndef PSORT_SAMPLE_H
#define PSORT_SAMPLE_H
#include "psort_util.h"
#define MPI_PSORT_TAG 31337
namespace vpsort {
using namespace std;
// sample n_out elements from array in _without_ replacement
// done with two sweeps of size n_out
// in is touched, but restored
template<typename _RandomAccessIter, typename _ValueType, typename _Distance>
static void sample_splitters(_RandomAccessIter in, _Distance n_in,
_ValueType *out, long n_out) {
if (n_out >= n_in) {
// TODO
} else {
_Distance *picks = new _Distance[n_out];
for (int i=0; i<n_out; ++i) {
picks[i] = static_cast<_Distance> ((n_in - i) * drand48());
out[i] = *(in + picks[i]);
*(in + picks[i]) = *(in + (n_out - i - 1));
}
for (int i=n_out-1; i>=0; --i) {
*(in + (n_out - i - 1)) = *(in + picks[i]);
*(in + picks[i]) = out[i];
}
delete [] picks;
}
}
static inline void set_splitters(long *part_gsize, long n_parts, int nproc, long *dist, long *out) {
// impl 1: get all but last about correct size; last might pick up slack
long ind = 0, cum = 0;
for (long i=0; i<n_parts; ++i) {
cum += part_gsize[i];
if (cum > dist[ind]) {
out[ind] = i;
if (cum - dist[ind] > dist[ind] - cum + part_gsize[i]) {
--out[ind];
cum = part_gsize[i];
} else {
cum = 0;
}
if (++ind >= nproc - 1) break;
}
}
}
template<typename _ValueType>
static void redist (int *from_dist, _ValueType *current,
int *to_dist, _ValueType *final,
int rank, int nproc,
MPI_Datatype &MPI_valueType, MPI_Comm comm)
{
int ** starch = new int*[nproc];
for (int i = 0; i < nproc; ++i) {
starch[i] = new int[nproc];
for (int j = 0; j < nproc; ++j) {
starch[i][j] = 0;
}
}
int i = 0, j=0;
int from_delta = from_dist[0], to_delta = to_dist[0];
while ((i < nproc) && (j < nproc)) {
int diff = from_delta - to_delta;
if (diff == 0) {
starch[i][j] = from_delta;
++i;
++j;
from_delta = from_dist[i];
to_delta = to_dist[j];
} else if (diff > 0) {
starch[i][j] = to_delta;
++j;
from_delta -= to_delta;
to_delta = to_dist[j];
} else {
starch[i][j] = from_delta;
++i;
to_delta -= from_delta;
from_delta = from_dist[i];
}
}
MPI_Request send_req[nproc], recv_req[nproc];
MPI_Status status;
int sendl = 0, recvl = 0;
for (int p = 0; p <= rank; ++p) {
int torecv = starch[p][rank];
if (torecv) {
MPI_Irecv (&final[recvl], torecv,
MPI_valueType, p, MPI_PSORT_TAG,
comm, &recv_req[p]);
recvl += torecv;
}
int tosend = starch[rank][p];
if (tosend) {
MPI_Isend (¤t[sendl], tosend,
MPI_valueType, p, MPI_PSORT_TAG,
comm, &send_req[p]);
sendl += tosend;
}
}
int sendr = 0, recvr = 0;
for (int p = nproc-1; p > rank; --p) {
int torecv = starch[p][rank];
if (torecv) {
recvr += torecv;
MPI_Irecv (&final[to_dist[rank]-recvr], torecv,
MPI_valueType, p, MPI_PSORT_TAG,
comm, &recv_req[p]);
}
int tosend = starch[rank][p];
if (tosend) {
sendr += tosend;
MPI_Isend (¤t[from_dist[rank]-sendr], tosend,
MPI_valueType, p, MPI_PSORT_TAG,
comm, &send_req[p]);
}
}
for (int p = 0; p < nproc; ++p) {
if ( starch[rank][p] )
MPI_Wait(&send_req[p], &status);
if ( starch[p][rank] )
MPI_Wait(&recv_req[p], &status);
}
for (int i = 0; i < nproc; ++i)
delete [] starch[i];
delete [] starch;
}
template<typename _RandomAccessIter, typename _Compare,
typename _SeqSortType>
void parallel_samplesort(_RandomAccessIter first, _RandomAccessIter last,
_Compare comp, long *dist,
SeqSort<_SeqSortType> &mysort,
long s, long k,
MPI_Comm comm) {
typedef typename iterator_traits<_RandomAccessIter>::value_type _ValueType;
typedef typename iterator_traits<_RandomAccessIter>::difference_type _Distance;
int nproc, rank;
MPI_Comm_size(comm, &nproc);
MPI_Comm_rank(comm, &rank);
MPI_Datatype MPI_valueType, MPI_distanceType;
MPI_Type_contiguous (sizeof(_ValueType), MPI_CHAR, &MPI_valueType);
MPI_Type_commit (&MPI_valueType);
MPI_Type_contiguous (sizeof(_Distance), MPI_CHAR, &MPI_distanceType);
MPI_Type_commit (&MPI_distanceType);
_Distance n_loc = last - first;
progress (rank, 0, "Sample splitters", comm);
// randomly pick s*k sample splitters
long n_samp = s*k;
_ValueType *s_splitters = new _ValueType[n_samp];
sample_splitters(first, n_loc, s_splitters, n_samp);
// send the sample splitters to the root
long n_parts = nproc * k - 1;
_ValueType *p_splitters = new _ValueType[n_parts];
if (rank != 0) {
MPI_Gather(s_splitters, n_samp, MPI_valueType,
NULL, 0, MPI_valueType, 0, comm);
} else {
_ValueType *gath_splitters = new _ValueType[nproc * n_samp];
MPI_Gather(s_splitters, n_samp, MPI_valueType,
gath_splitters, n_samp, MPI_valueType,
0, comm);
// root sorts sample splitters serially
sort(gath_splitters, gath_splitters + (nproc * n_samp), comp);
// root picks pk-1 splitters, broadcasts
double stride = (double) (nproc * n_samp) / (nproc * k);
for (int i=0; i<n_parts; ++i) {
p_splitters[i] = gath_splitters[(int) (stride * (double) (i + 1))];
}
delete [] gath_splitters;
}
MPI_Bcast(p_splitters, n_parts, MPI_valueType, 0, comm);
delete [] s_splitters;
progress (rank, 1, "Partition", comm);
// apply the splitters
// impl1: binary search each element over the splitters
// TODO: impl2: recurse, partitioning dfs order over splitters
_Distance *part_ind = new _Distance[n_loc];
long *part_lsize = new long[n_parts + 1];
for (long i=0; i<=n_parts; ++i) part_lsize[i] = 0;
for (long i=0; i<n_loc; ++i) {
// TODO: load balance when splitters have same value
part_ind[i] = lower_bound(p_splitters, p_splitters + n_parts, *(first + i))
- p_splitters;
++part_lsize[part_ind[i]];
}
delete [] p_splitters;
long *boundaries = new long[nproc - 1];
if (rank != 0) {
MPI_Reduce(part_lsize, NULL, n_parts + 1, MPI_LONG, MPI_SUM, 0, comm);
} else {
// determine global partition sizes, starch-minimizing boundaries
long *part_gsize = new long[n_parts + 1];
MPI_Reduce(part_lsize, part_gsize, n_parts + 1, MPI_LONG, MPI_SUM, 0, comm);
set_splitters (part_gsize, n_parts + 1, nproc, dist, boundaries);
delete [] part_gsize;
}
MPI_Bcast(boundaries, nproc - 1, MPI_LONG, 0, comm);
// send data according to boundaries
// part_proc[i] says which processor partition i goes to
long *part_proc = new long[n_parts + 1];
int out_counts[nproc];
for (int i=0; i<nproc; ++i) out_counts[i] = 0;
long cur = 0;
for (long i=0; i<=n_parts; ++i) {
if (cur < nproc - 1 && i > boundaries[cur]) {
++cur;
}
part_proc[i] = cur;
out_counts[cur] += part_lsize[i];
}
delete [] boundaries;
delete [] part_lsize;
long curs[nproc];
_ValueType **s_bufs = new _ValueType*[nproc];
for (int i=0; i<nproc; ++i) {
curs[i] = 0;
s_bufs[i] = new _ValueType[out_counts[i]];
}
for (int i=0; i<n_loc; ++i) {
int dest = part_proc[part_ind[i]];
s_bufs[dest][curs[dest]++] = *(first + i);
}
delete [] part_ind;
delete [] part_proc;
progress (rank, 2, "Alltoall", comm);
// tell each processor how many elements to expect from m
int in_counts[nproc];
MPI_Alltoall(out_counts, 1, MPI_INT, in_counts, 1, MPI_INT, comm);
int offsets[nproc + 1];
offsets[0] = 0;
for (int i=1; i<=nproc; ++i) {
offsets[i] = offsets[i-1] + in_counts[i-1];
}
int n_loct = offsets[nproc];
MPI_Request send_reqs[nproc];
MPI_Request recv_reqs[nproc];
_ValueType *trans_data = new _ValueType[n_loct];
for (int i=0; i<nproc; ++i) {
MPI_Irecv(&trans_data[offsets[i]], in_counts[i],
MPI_valueType, i, MPI_PSORT_TAG, comm, &recv_reqs[i]);
}
for (int i=1; i<=nproc; ++i) {
int dest = (rank + i) % nproc;
MPI_Isend(s_bufs[dest], out_counts[dest], MPI_valueType,
dest, MPI_PSORT_TAG, comm, &send_reqs[dest]);
}
MPI_Waitall(nproc, send_reqs, MPI_STATUS_IGNORE);
for (int i=0; i < nproc; ++i) {
delete [] s_bufs[i];
}
delete [] s_bufs;
MPI_Waitall (nproc, recv_reqs, MPI_STATUS_IGNORE);
progress (rank, 3, "Sequential sort", comm);
mysort.seqsort (trans_data, trans_data + n_loct, comp);
progress (rank, 4, "Adjust boundaries", comm);
// starch
int trans_dist[nproc], dist_out[nproc];
MPI_Allgather(&n_loct, 1, MPI_INT, trans_dist, 1, MPI_INT, comm);
for (int i=0; i<nproc; ++i) dist_out[i] = dist[i];
redist(trans_dist, trans_data, dist_out, first, rank, nproc, MPI_valueType, comm);
delete [] trans_data;
progress (rank, 5, "Finish", comm);
MPI_Type_free (&MPI_valueType);
}
template<typename _RandomAccessIter>
void parallel_samplesort(_RandomAccessIter first, _RandomAccessIter last,
long *dist, MPI_Comm comm) {
typedef typename iterator_traits<_RandomAccessIter>::value_type _ValueType;
STLSort stl_sort;
int nproc;
MPI_Comm_size (comm, &nproc);
long s = nproc, k = nproc;
parallel_samplesort (first, last, less<_ValueType>(), dist, stl_sort, s, k, comm);
}
} /* namespace vpsort */
#endif /* PSORT_SAMPLE_H */
|