Zoltan2 Version of the Day
Zoltan2_AlgAMD.hpp
Go to the documentation of this file.
00001 // @HEADER
00002 //
00003 // ***********************************************************************
00004 //
00005 //   Zoltan2: A package of combinatorial algorithms for scientific computing
00006 //                  Copyright 2012 Sandia Corporation
00007 //
00008 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
00009 // the U.S. Government retains certain rights in this software.
00010 //
00011 // Redistribution and use in source and binary forms, with or without
00012 // modification, are permitted provided that the following conditions are
00013 // met:
00014 //
00015 // 1. Redistributions of source code must retain the above copyright
00016 // notice, this list of conditions and the following disclaimer.
00017 //
00018 // 2. Redistributions in binary form must reproduce the above copyright
00019 // notice, this list of conditions and the following disclaimer in the
00020 // documentation and/or other materials provided with the distribution.
00021 //
00022 // 3. Neither the name of the Corporation nor the names of the
00023 // contributors may be used to endorse or promote products derived from
00024 // this software without specific prior written permission.
00025 //
00026 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00027 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00028 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00029 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00030 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00031 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00032 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00033 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00034 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00035 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00036 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00037 //
00038 // Questions? Contact Karen Devine      (kddevin@sandia.gov)
00039 //                    Erik Boman        (egboman@sandia.gov)
00040 //                    Siva Rajamanickam (srajama@sandia.gov)
00041 //
00042 // ***********************************************************************
00043 //
00044 // @HEADER
00045 
00050 #ifndef _ZOLTAN2_ALGAMD_HPP_
00051 #define _ZOLTAN2_ALGAMD_HPP_
00052 
00053 #include <Zoltan2_GraphModel.hpp>
00054 #include <Zoltan2_OrderingSolution.hpp>
00055 
00056 #ifndef HAVE_ZOLTAN2_AMD
00057 
00058 namespace Zoltan2{
00059 
00060 template <typename Adapter>
00061 int AlgAMD(
00062   const RCP<GraphModel<Adapter> > &model,
00063   const RCP<OrderingSolution<typename Adapter::gid_t,
00064                              typename Adapter::lno_t> > &solution,
00065   const RCP<Teuchos::ParameterList> &pl,
00066   const RCP<Teuchos::Comm<int> > &comm
00067 )
00068 {
00069     // We don't need to check comm size here as we throw an exception always.
00070   throw std::runtime_error(
00071         "BUILD ERROR: AMD requested but not compiled into Zoltan2.\n"
00072         "Please set CMake flag Zoltan2_ENABLE_AMD:BOOL=ON.");
00073 }
00074 
00075 }
00076 
00077 #else
00078 
00079 #include "amd.h"
00080 
00081 namespace Zoltan2{
00082 
00083 template <typename Ordinal>
00084 class AMDTraits
00085 {
00086     public:
00087     Ordinal order(Ordinal n, const Ordinal *Ap, const Ordinal *Ai,
00088                 Ordinal *perm, double *control, double *info);
00089 };
00090 
00091 template <>
00092 class AMDTraits<int>
00093 {
00094     public:
00095     int order(int n, const int *Ap, const int *Ai, int *perm,
00096                 double *control, double *info)
00097     {
00098         return (amd_order(n, Ap, Ai, perm, control, info));
00099     }
00100 };
00101 
00102 template <>
00103 class AMDTraits<long>
00104 {
00105     public:
00106     long order(long n, const long *Ap, const long *Ai, long *perm,
00107                 double *control, double *info)
00108     {
00109         return (amd_l_order(n, Ap, Ai, perm, control, info));
00110     }
00111 };
00112 
00113 template <typename Adapter>
00114 int AlgAMD(
00115   const RCP<GraphModel<Adapter> > &model,
00116   const RCP<OrderingSolution<typename Adapter::gid_t,
00117                              typename Adapter::lno_t> > &solution,
00118   const RCP<Teuchos::ParameterList> &pl,
00119   const RCP<Teuchos::Comm<int> > &comm
00120 )
00121 {
00122   typedef typename Adapter::lno_t lno_t;
00123   typedef typename Adapter::gno_t gno_t;
00124   typedef typename Adapter::gid_t gid_t;
00125   typedef typename Adapter::scalar_t scalar_t;
00126 
00127   int ierr= 0;
00128 
00129   if (comm->getSize() != 1)
00130   {
00131       throw std::runtime_error(
00132         "ERROR: AMD requested with distributed matrix.\n"
00133         "This feature is not supported yet. Please use a local matrix.");
00134   }
00135 
00136   const size_t nVtx = model->getLocalNumVertices();
00137 
00138   cout << "Local num vertices" << nVtx << endl;
00139   ArrayView<const gno_t> edgeIds;
00140   ArrayView<const int> procIds;
00141   ArrayView<const lno_t> offsets;
00142   ArrayView<StridedData<lno_t, scalar_t> > wgts;
00143 
00144   //const size_t nEdgs = model->getEdgeList( edgeIds,
00145   //                      procIds, offsets, wgts);
00146   // TODO: Should use the local graph
00147   model->getEdgeList( edgeIds, procIds, offsets, wgts);
00148 
00149   AMDTraits<lno_t> AMDobj;
00150   double Control[AMD_CONTROL];
00151   double Info[AMD_INFO];
00152 
00153   amd_defaults(Control);
00154   amd_control(Control);
00155 
00156   lno_t *perm;
00157   perm = (lno_t *) (solution->getPermutationRCP().getRawPtr());
00158 
00159   lno_t result = AMDobj.order(nVtx, offsets.getRawPtr(),
00160                          edgeIds.getRawPtr(), perm, Control, Info);
00161 
00162   if (result != AMD_OK && result != AMD_OK_BUT_JUMBLED)
00163       ierr = -1; // TODO: Change return value to lno_t
00164 
00165   return ierr;
00166 }
00167 
00168 }
00169 
00170 #endif // ZOLTAN2_HAVE_AMD
00171 
00172 #endif