Zoltan2
Zoltan2_AlgAMD.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_ALGAMD_HPP_
51 #define _ZOLTAN2_ALGAMD_HPP_
52 
53 #include <Zoltan2_Algorithm.hpp>
54 #include <Zoltan2_GraphModel.hpp>
56 
57 
58 #ifdef HAVE_ZOLTAN2_AMD
59 #include "amd.h"
60 #endif
61 
62 namespace Zoltan2{
63 
64 #ifdef HAVE_ZOLTAN2_AMD
65 template <typename Ordinal>
66 class AMDTraits
67 {
68  public:
69  Ordinal order(Ordinal n, const Ordinal *Ap, const Ordinal *Ai,
70  Ordinal *perm, double *control, double *info);
71 };
72 
73 template <>
74 class AMDTraits<int>
75 {
76  public:
77  int order(int n, const int *Ap, const int *Ai, int *perm,
78  double *control, double *info)
79  {
80  return (amd_order(n, Ap, Ai, perm, control, info));
81  }
82 };
83 
84 template <>
85 class AMDTraits<long>
86 {
87  public:
88  long order(long n, const long *Ap, const long *Ai, long *perm,
89  double *control, double *info)
90  {
91  return (amd_l_order(n, Ap, Ai, perm, control, info));
92  }
93 };
94 #endif
95 
96 }
97 
98 
99 namespace Zoltan2{
100 
101 template <typename Adapter>
102 class AlgAMD : public Algorithm<Adapter>
103 {
104  private:
105 
106  const RCP<GraphModel<Adapter> > model;
107  const RCP<Teuchos::ParameterList> pl;
108  const RCP<Teuchos::Comm<int> > comm;
109 
110  public:
111 
113  const RCP<GraphModel<Adapter> > &model__,
114  const RCP<Teuchos::ParameterList> &pl__,
115  const RCP<Teuchos::Comm<int> > &comm__
116  ) : model(model__), pl(pl__), comm(comm__)
117  { }
118 
119  int order(const RCP<OrderingSolution<typename Adapter::lno_t,
120  typename Adapter::gno_t> > &solution)
121  {
122 #ifndef HAVE_ZOLTAN2_AMD
123  throw std::runtime_error(
124  "BUILD ERROR: AMD requested but not compiled into Zoltan2.\n"
125  "Please set CMake flag Zoltan2_ENABLE_AMD:BOOL=ON.");
126 #else
127  typedef typename Adapter::gno_t gno_t;
128  typedef typename Adapter::lno_t lno_t;
129  typedef typename Adapter::scalar_t scalar_t;
130 
131  int ierr= 0;
132 
133  const size_t nVtx = model->getLocalNumVertices();
134 
135  //cout << "Local num vertices" << nVtx << endl;
136  ArrayView<const gno_t> edgeIds;
137  ArrayView<const lno_t> offsets;
138  ArrayView<StridedData<lno_t, scalar_t> > wgts;
139 
140  // wgts are ignored in AMD
141  model->getEdgeList(edgeIds, offsets, wgts);
142 
143  AMDTraits<lno_t> AMDobj;
144  double Control[AMD_CONTROL];
145  double Info[AMD_INFO];
146 
147  amd_defaults(Control);
148  amd_control(Control);
149 
150  lno_t *perm;
151  perm = (lno_t *) (solution->getPermutationRCP().getRawPtr());
152 
153  lno_t result = AMDobj.order(nVtx, offsets.getRawPtr(),
154  edgeIds.getRawPtr(), perm, Control, Info);
155 
156  if (result != AMD_OK && result != AMD_OK_BUT_JUMBLED)
157  ierr = -1;
158 
159  solution->setHavePerm(true);
160  return ierr;
161 #endif
162  }
163 };
164 
165 }
166 
167 
168 
169 #endif
Defines the OrderingSolution class.
Adapter::scalar_t scalar_t
Algorithm defines the base class for all algorithms.
AlgAMD(const RCP< GraphModel< Adapter > > &model__, const RCP< Teuchos::ParameterList > &pl__, const RCP< Teuchos::Comm< int > > &comm__)
int order(const RCP< OrderingSolution< typename Adapter::lno_t, typename Adapter::gno_t > > &solution)
Ordering method.
GraphModel defines the interface required for graph models.
Defines the GraphModel interface.
The class containing ordering solutions.