Zoltan2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
Zoltan2_PartitioningHelpers.hpp
Go to the documentation of this file.
1 // @HEADER
2 //
3 // ***********************************************************************
4 //
5 // Zoltan2: A package of combinatorial algorithms for scientific computing
6 // Copyright 2012 Sandia Corporation
7 //
8 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
9 // the U.S. Government retains certain rights in this software.
10 //
11 // Redistribution and use in source and binary forms, with or without
12 // modification, are permitted provided that the following conditions are
13 // met:
14 //
15 // 1. Redistributions of source code must retain the above copyright
16 // notice, this list of conditions and the following disclaimer.
17 //
18 // 2. Redistributions in binary form must reproduce the above copyright
19 // notice, this list of conditions and the following disclaimer in the
20 // documentation and/or other materials provided with the distribution.
21 //
22 // 3. Neither the name of the Corporation nor the names of the
23 // contributors may be used to endorse or promote products derived from
24 // this software without specific prior written permission.
25 //
26 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
27 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
30 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 //
38 // Questions? Contact Karen Devine (kddevin@sandia.gov)
39 // Erik Boman (egboman@sandia.gov)
40 // Siva Rajamanickam (srajama@sandia.gov)
41 //
42 // ***********************************************************************
43 //
44 // @HEADER
45 
50 #ifndef _ZOLTAN2_PARTITIONINGHELPERS_HPP_
51 #define _ZOLTAN2_PARTITIONINGHELPERS_HPP_
52 
53 #ifdef _MSC_VER
54 #define NOMINMAX
55 #include <windows.h>
56 #endif
57 
58 
60 #include <Zoltan2_AlltoAll.hpp>
61 
62 namespace Zoltan2 {
63 
79 template <typename SolutionAdapter, typename DataAdapter>
82  const DataAdapter * const data,
83  ArrayRCP<typename DataAdapter::gno_t> &imports // output
84 )
85 {
87  typedef typename PartitioningSolution<SolutionAdapter>::gno_t gno_t;
88 
89  size_t numParts = solution.getActualGlobalNumberOfParts();
90  int numProcs = solution.getCommunicator()->getSize();
91 
92  if (numParts > size_t(numProcs)) {
93  char msg[256];
94  sprintf(msg, "Number of parts %lu exceeds number of ranks %d; "
95  "%s requires a MappingSolution for this case\n",
96  numParts, numProcs, __func__);
97  throw std::logic_error(msg);
98  }
99 
100  size_t localNumIds = data->getLocalNumIDs();
101  const typename DataAdapter::gno_t *gids = NULL;
102  data->getIDsView(gids);
103 
104  const part_t *parts = solution.getPartListView();
105 
106  // How many ids to each process?
107  Array<int> counts(numProcs, 0);
108  for (size_t i=0; i < localNumIds; i++)
109  counts[parts[i]]++;
110 
111  Array<gno_t> offsets(numProcs+1, 0);
112  for (int i=1; i <= numProcs; i++){
113  offsets[i] = offsets[i-1] + counts[i-1];
114  }
115 
116  Array<typename DataAdapter::gno_t> gidList(localNumIds);
117  for (size_t i=0; i < localNumIds; i++) {
118  gno_t idx = offsets[parts[i]];
119  gidList[idx] = gids[i];
120  offsets[parts[i]] = idx + 1;
121  }
122 
123  Array<int> recvCounts(numProcs, 0);
124  try {
125  AlltoAllv<typename DataAdapter::gno_t>(*(solution.getCommunicator()),
126  *(solution.getEnvironment()),
127  gidList(), counts(), imports, recvCounts());
128  }
130 
131  return imports.size();
132 }
133 
134 } // namespace Zoltan2
135 
136 #endif // _ZOLTAN2_PARTITIONINGHELPERS_HPP_
#define Z2_FORWARD_EXCEPTIONS
Forward an exception back through call stack.
Defines the PartitioningSolution class.
size_t getImportList(const PartitioningSolution< SolutionAdapter > &solution, const DataAdapter *const data, ArrayRCP< typename DataAdapter::gno_t > &imports)
From a PartitioningSolution, get a list of IDs to be imported. Assumes part numbers in PartitioningSo...
SparseMatrixAdapter_t::part_t part_t
A PartitioningSolution is a solution to a partitioning problem.
const RCP< const Comm< int > > & getCommunicator() const
Return the communicator associated with the solution.
const part_t * getPartListView() const
Returns the part list corresponding to the global ID list.
size_t getActualGlobalNumberOfParts() const
Returns the actual global number of parts provided in setParts().
const RCP< const Environment > & getEnvironment() const
Return the environment associated with the solution.
AlltoAll communication methods.