COMBINATORIAL_BLAS  1.6
psort_alltoall.h
Go to the documentation of this file.
1 /*
2 Copyright (c) 2009, David Cheng, Viral B. Shah.
3 
4 Permission is hereby granted, free of charge, to any person obtaining a copy
5 of this software and associated documentation files (the "Software"), to deal
6 in the Software without restriction, including without limitation the rights
7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 copies of the Software, and to permit persons to whom the Software is
9 furnished to do so, subject to the following conditions:
10 
11 The above copyright notice and this permission notice shall be included in
12 all copies or substantial portions of the Software.
13 
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 THE SOFTWARE.
21 */
22 
23 #ifndef PSORT_ALLTOALL_H
24 #define PSORT_ALLTOALL_H
25 
26 #include "psort_util.h"
27 
28 namespace vpsort {
29  using namespace std;
30 
31  template <typename _RandomAccessIter, typename _ValueType, typename _Distance>
32  static void alltoall (vector< vector<_Distance> > &right_ends,
33  _RandomAccessIter first,
34  _RandomAccessIter last,
35  _ValueType *trans_data,
36  _Distance *boundaries,
37  MPI_Datatype &MPI_valueType,
38  MPI_Datatype &MPI_distanceType,
39  MPI_Comm comm) {
40 
41  int nproc, rank;
42  MPI_Comm_size (comm, &nproc);
43  MPI_Comm_rank (comm, &rank);
44 
45  // Should be _Distance, but MPI wants ints
46  char errMsg[] = "32-bit limit for MPI has overflowed";
47  _Distance n_loc_ = last - first;
48  if (n_loc_ > INT_MAX) throw std::overflow_error(errMsg);
49  int n_loc = static_cast<int> (n_loc_);
50 
51  // Calculate the counts for redistributing data
52  int *send_counts = new int[nproc];
53  int *send_disps = new int[nproc];
54  for (int i = 0; i < nproc; ++i) {
55  _Distance scount = right_ends[i + 1][rank] - right_ends[i][rank];
56  if (scount > INT_MAX) throw std::overflow_error(errMsg);
57  send_counts[i] = static_cast<int> (scount);
58  }
59  send_disps[0] = 0;
60  partial_sum (send_counts, send_counts + nproc - 1, send_disps + 1);
61 
62  int *recv_counts = new int[nproc];
63  int *recv_disps = new int[nproc];
64  for (int i = 0; i < nproc; ++i) {
65  _Distance rcount = right_ends[rank + 1][i] - right_ends[rank][i];
66  if (rcount > INT_MAX) throw std::overflow_error(errMsg);
67  recv_counts[i] = static_cast<int> (rcount);
68  }
69 
70  recv_disps[0] = 0;
71  partial_sum (recv_counts, recv_counts + nproc - 1, recv_disps + 1);
72 
73  assert (accumulate (recv_counts, recv_counts + nproc, 0) == n_loc);
74 
75  // Do the transpose
76  MPI_Alltoallv (first, send_counts, send_disps, MPI_valueType,
77  trans_data, recv_counts, recv_disps, MPI_valueType, comm);
78 
79  for (int i=0; i<nproc; ++i) boundaries[i] = (_Distance) recv_disps[i];
80  boundaries[nproc] = (_Distance) n_loc; // for the merging
81 
82  delete [] recv_counts;
83  delete [] recv_disps;
84 
85  delete [] send_counts;
86  delete [] send_disps;
87  return;
88  }
89 
90 }
91 
92 
93 #endif /* PSORT_ALLTOALL_H */
Definition: psort.h:28
int rank