Zoltan2  Version of the Day
Zoltan2_AlgScotch.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 #ifndef _ZOLTAN2_ALGSCOTCH_HPP_
46 #define _ZOLTAN2_ALGSCOTCH_HPP_
47 
48 #include <Zoltan2_GraphModel.hpp>
50 #include <Zoltan2_Util.hpp>
51 
53 
54 #ifndef HAVE_ZOLTAN2_SCOTCH
55 
56 // Error handling for when Scotch is requested
57 // but Zoltan2 not built with Scotch.
58 
59 namespace Zoltan2 {
60 
72 template <typename Adapter>
74  const RCP<const Environment> &env,
75  const RCP<const Comm<int> > &problemComm,
76 #ifdef HAVE_ZOLTAN2_MPI
77  MPI_Comm mpicomm,
78 #endif
80  RCP<PartitioningSolution<Adapter> > &solution
81 
82 )
83 {
84  throw std::runtime_error(
85  "BUILD ERROR: Scotch requested but not compiled into Zoltan2.\n"
86  "Please set CMake flag Zoltan2_ENABLE_Scotch:BOOL=ON.");
87 }
88 }
89 
90 #else //HAVE_ZOLTAN2_SCOTCH
91 
92 
93 // stdint.h for int64_t in scotch header
94 
95 #include <stdint.h>
96 #ifndef HAVE_ZOLTAN2_MPI
97 #include "scotch.h"
98 #else
99 #include "ptscotch.h"
100 #endif
101 
102 #ifdef SHOW_ZOLTAN2_SCOTCH_MEMORY
103 //
104 // Scotch keeps track of memory high water mark, but doesn't
105 // provide a way to get that number. So add this function:
106 // "size_t SCOTCH_getMemoryMax() { return memorymax;}"
107 // to src/libscotch/common_memory.c
108 //
109 // and this macro:
110 // "#define HAVE_SCOTCH_GETMEMORYMAX
111 // to include/ptscotch.h
112 //
113 // and compile scotch with -DCOMMON_MEMORY_TRACE
114 //
115 #ifdef HAVE_SCOTCH_GETMEMORYMAX
116 
117 extern "C"{
118 extern size_t SCOTCH_getMemoryMax();
119 }
120 
121 #else
122 
123 #error "Either turn off ZOLTAN2_ENABLE_SCOTCH_MEMORY_REPORT in cmake configure, or see SHOW_ZOLTAN2_SCOTCH_MEMORY comment in Zoltan2_AlgScotch.hpp"
124 
125 #endif
126 
127 #endif
128 
129 
133 
134 namespace Zoltan2{
135 
137 // Traits struct to handle conversions between gno_t/lno_t and SCOTCH_Num.
139 
140 // General case: SCOTCH_Num and gno_t/lno_t (called zno_t here) differ.
141 template <typename zno_t>
142 struct SCOTCH_Num_Traits {
143 
144  static inline SCOTCH_Num ASSIGN_TO_SCOTCH_NUM(
145  SCOTCH_Num &a,
146  zno_t b,
147  const RCP<const Environment> &env)
148  {
149  // Assign a = b; make sure SCOTCH_Num is large enough to accept zno_t.
150  if (b <= SCOTCH_NUMMAX) a = b;
151  else
152  env->localInputAssertion(__FILE__, __LINE__,
153  "Value too large for SCOTCH_Num, Rebuild Scotch with larger SCOTCH_Num",
154  false, BASIC_ASSERTION);
155  return a;
156  }
157 
158  static inline void ASSIGN_SCOTCH_NUM_ARRAY(
159  SCOTCH_Num **a,
160  ArrayView<const zno_t> &b,
161  const RCP<const Environment> &env)
162  {
163  // Allocate array a; copy b values into a.
164  size_t size = b.size();
165  *a = new SCOTCH_Num[size];
166  for (size_t i = 0; i < size; i++) ASSIGN_TO_SCOTCH_NUM((*a)[i], b[i], env);
167  }
168 
169  static inline void DELETE_SCOTCH_NUM_ARRAY(SCOTCH_Num *a)
170  {
171  // Delete the copy made in ASSIGN_SCOTCH_NUM_ARRAY.
172  delete [] a;
173  }
174 
175  //TODO static inline ASSIGN_FROM_SCOTCH_NUM(zno_t b, SCOTCH_Num a)
176  //TODO {
177  //TODO Make sure zno_t is large enough to accept SCOTCH_Num.
178  //TODO NOT NEEDED AS LONG AS PARTS ARE size_t.
179  //TODO }
180 };
181 
182 
183 // Special case: zno_t == SCOTCH_Num. No error checking or copies needed.
184 template <>
185 struct SCOTCH_Num_Traits<SCOTCH_Num> {
186  static inline SCOTCH_Num ASSIGN_TO_SCOTCH_NUM(
187  SCOTCH_Num &a,
188  SCOTCH_Num b,
189  const RCP<const Environment> &env)
190  {
191  a = b;
192  return a;
193  }
194  static inline void ASSIGN_SCOTCH_NUM_ARRAY(
195  SCOTCH_Num **a,
196  ArrayView<const SCOTCH_Num> &b,
197  const RCP<const Environment> &env)
198  {
199  *a = const_cast<SCOTCH_Num *> (b.getRawPtr());
200  }
201  static inline void DELETE_SCOTCH_NUM_ARRAY(SCOTCH_Num *a) { }
202 };
203 
204 
206 // Now, the actual Scotch algorithm.
208 
220 template <typename Adapter>
221 void AlgPTScotch(
222  const RCP<const Environment> &env, // parameters & app comm
223  const RCP<const Comm<int> > &problemComm, // problem comm
224 #ifdef HAVE_ZOLTAN2_MPI
225  MPI_Comm mpicomm,
226 #endif
227  const RCP<GraphModel<typename Adapter::base_adapter_t> > &model, // the graph
228  RCP<PartitioningSolution<Adapter> > &solution
229 )
230 {
231  HELLO;
232 
233  typedef typename Adapter::lno_t lno_t;
234  typedef typename Adapter::gno_t gno_t;
235  typedef typename Adapter::scalar_t scalar_t;
236 
237  int ierr = 0;
238 
239  size_t numGlobalParts = solution->getTargetGlobalNumberOfParts();
240 
241  SCOTCH_Num partnbr;
242  SCOTCH_Num_Traits<size_t>::ASSIGN_TO_SCOTCH_NUM(partnbr, numGlobalParts, env);
243 
244 #ifdef HAVE_ZOLTAN2_MPI
245 
246  const SCOTCH_Num baseval = 0; // Base value for array indexing.
247  // GraphModel returns GNOs from base 0.
248 
249  SCOTCH_Strat stratstr; // Strategy string
250  // TODO: Set from parameters
251  SCOTCH_stratInit(&stratstr);
252 
253  // Allocate & initialize PTScotch data structure.
254  //SCOTCH_Dgraph *gr = SCOTCH_dgraphAlloc(); // Scotch distributed graph
255  SCOTCH_Dgraph *gr = new SCOTCH_Dgraph; // Scotch distributed graph
256  ierr = SCOTCH_dgraphInit(gr, mpicomm);
257 
258  env->globalInputAssertion(__FILE__, __LINE__, "SCOTCH_dgraphInit",
259  !ierr, BASIC_ASSERTION, problemComm);
260 
261  // Get vertex info
262  ArrayView<const gno_t> vtxID;
263  ArrayView<StridedData<lno_t, scalar_t> > xyz;
264  ArrayView<StridedData<lno_t, scalar_t> > vtxWt;
265  size_t nVtx = model->getVertexList(vtxID, xyz, vtxWt);
266  SCOTCH_Num vertlocnbr;
267  SCOTCH_Num_Traits<size_t>::ASSIGN_TO_SCOTCH_NUM(vertlocnbr, nVtx, env);
268  SCOTCH_Num vertlocmax = vertlocnbr; // Assumes no holes in global nums.
269 
270  // Get edge info
271  ArrayView<const gno_t> edgeIds;
272  ArrayView<const int> procIds;
273  ArrayView<const lno_t> offsets;
274  ArrayView<StridedData<lno_t, scalar_t> > ewgts;
275 
276  size_t nEdges = model->getEdgeList(edgeIds, procIds, offsets, ewgts);
277 
278  SCOTCH_Num edgelocnbr;
279  SCOTCH_Num_Traits<size_t>::ASSIGN_TO_SCOTCH_NUM(edgelocnbr, nEdges, env);
280  const SCOTCH_Num edgelocsize = edgelocnbr; // Assumes adj array is compact.
281 
282  SCOTCH_Num *vertloctab; // starting adj/vtx
283  SCOTCH_Num_Traits<lno_t>::ASSIGN_SCOTCH_NUM_ARRAY(&vertloctab, offsets, env);
284 
285  SCOTCH_Num *edgeloctab; // adjacencies
286  SCOTCH_Num_Traits<gno_t>::ASSIGN_SCOTCH_NUM_ARRAY(&edgeloctab, edgeIds, env);
287 
288  // We don't use these arrays, but we need them as arguments to Scotch.
289  SCOTCH_Num *vendloctab = NULL; // Assume consecutive storage for adj
290  SCOTCH_Num *vlblloctab = NULL; // Vertex label array
291  SCOTCH_Num *edgegsttab = NULL; // Array for ghost vertices
292 
293  // Get weight info.
294  // TODO: Actually get the weights; for now, not using weights.
295  SCOTCH_Num *veloloctab = NULL; // Vertex weights
296  SCOTCH_Num *edloloctab = NULL; // Edge weights
297  //TODO int vwtdim = model->getVertexWeightDim();
298  //TODO int ewtdim = model->getEdgeWeightDim();
299  //TODO if (vwtdim) veloloctab = new SCOTCH_Num[nVtx];
300  //TODO if (ewtdim) edloloctab = new SCOTCH_Num[nEdges];
301  //TODO scale weights to SCOTCH_Nums.
302 
303  // Build PTScotch distributed data structure
304  ierr = SCOTCH_dgraphBuild(gr, baseval, vertlocnbr, vertlocmax,
305  vertloctab, vendloctab, veloloctab, vlblloctab,
306  edgelocnbr, edgelocsize,
307  edgeloctab, edgegsttab, edloloctab);
308 
309  env->globalInputAssertion(__FILE__, __LINE__, "SCOTCH_dgraphBuild",
310  !ierr, BASIC_ASSERTION, problemComm);
311 
312  // Create array for Scotch to return results in.
313  ArrayRCP<partId_t> partList(new partId_t [nVtx], 0, nVtx,true);
314  SCOTCH_Num *partloctab;
315  if (sizeof(SCOTCH_Num) == sizeof(partId_t)) {
316  // Can write directly into the solution's memory
317  partloctab = (SCOTCH_Num *) partList.getRawPtr();
318  }
319  else {
320  // Can't use solution memory directly; will have to copy later.
321  partloctab = new SCOTCH_Num[nVtx];
322  }
323 
324  // Call partitioning; result returned in partloctab.
325  // TODO: Use SCOTCH_dgraphMap so can include a machine model in partitioning
326  ierr = SCOTCH_dgraphPart(gr, partnbr, &stratstr, partloctab);
327 
328  env->globalInputAssertion(__FILE__, __LINE__, "SCOTCH_dgraphPart",
329  !ierr, BASIC_ASSERTION, problemComm);
330 
331  // TODO - metrics
332 #ifdef SHOW_ZOLTAN2_SCOTCH_MEMORY
333  int me = env->comm_->getRank();
334 #endif
335 
336 #ifdef HAVE_SCOTCH_ZOLTAN2_GETMEMORYMAX
337  if (me == 0){
338  size_t scotchBytes = SCOTCH_getMemoryMax();
339  std::cout << "Rank " << me << ": Maximum bytes used by Scotch: ";
340  std::cout << scotchBytes << std::endl;
341  }
342 #endif
343 
344  // Clean up PTScotch
345  SCOTCH_dgraphExit(gr);
346  delete gr;
347  SCOTCH_stratExit(&stratstr);
348 
349  // Load answer into the solution.
350 
351  if (sizeof(SCOTCH_Num) != sizeof(partId_t)) {
352  for (size_t i = 0; i < nVtx; i++) partList[i] = partloctab[i];
353  }
354 
355  ArrayRCP<const gno_t> gnos = arcpFromArrayView(vtxID);
356 
357  solution->setParts(gnos, partList, true);
358 
359  env->memory("Zoltan2-Scotch: After creating solution");
360 
361  //if (me == 0) cout << " done." << endl;
362  // Clean up Zoltan2
363  //TODO if (vwtdim) delete [] velotab;
364  //TODO if (ewtdim) delete [] edlotab;
365 
366  // Clean up copies made due to differing data sizes.
367  if (sizeof(lno_t) != sizeof(SCOTCH_Num)) delete [] vertloctab;
368  if (sizeof(gno_t) != sizeof(SCOTCH_Num)) delete [] edgeloctab;
369 
370 #else // DO NOT HAVE_MPI
371 
372  // TODO: Handle serial case with calls to Scotch.
373  // TODO: For now, assign everything to rank 0 and assume only one part.
374  // TODO: Can probably use the code above for loading solution,
375  // TODO: instead of duplicating it here.
376  // TODO
377  // TODO: Actual logic should call Scotch when number of processes == 1.
378  ArrayView<const gno_t> vtxID;
379  ArrayView<StridedData<lno_t, scalar_t> > xyz;
380  ArrayView<StridedData<lno_t, scalar_t> > vtxWt;
381  size_t nVtx = model->getVertexList(vtxID, xyz, vtxWt);
382 
383  for (size_t i = 0; i < nVtx; i++) partList[i] = 0;
384 
385 
386 #endif // DO NOT HAVE_MPI
387 }
388 
389 }
390 #endif // HAVE_ZOLTAN2_SCOTCH
391 #endif
#define HELLO
fast typical checks for valid arguments
Defines the PartitioningSolution class.
zoltan2_partId_t partId_t
void AlgPTScotch(const RCP< const Environment > &env, const RCP< const Comm< int > > &problemComm, const RCP< GraphModel< typename Adapter::base_adapter_t > > &model, RCP< PartitioningSolution< Adapter > > &solution)
int zoltan2_partId_t
a data type for part IDs
A PartitioningSolution is a solution to a partitioning problem.
double scalar_t
GraphModel defines the interface required for graph models.
Defines the GraphModel interface.
A gathering of useful namespace methods.