Zoltan2
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 
00057 #ifdef HAVE_ZOLTAN2_AMD
00058 #include "amd.h"
00059 #endif
00060 
00061 namespace Zoltan2{
00062 
00063 #ifdef HAVE_ZOLTAN2_AMD
00064 template <typename Ordinal>
00065 class AMDTraits
00066 {
00067     public:
00068     Ordinal order(Ordinal n, const Ordinal *Ap, const Ordinal *Ai,
00069                 Ordinal *perm, double *control, double *info);
00070 };
00071 
00072 template <>
00073 class AMDTraits<int>
00074 {
00075     public:
00076     int order(int n, const int *Ap, const int *Ai, int *perm,
00077                 double *control, double *info)
00078     {
00079         return (amd_order(n, Ap, Ai, perm, control, info));
00080     }
00081 };
00082 
00083 template <>
00084 class AMDTraits<long>
00085 {
00086     public:
00087     long order(long n, const long *Ap, const long *Ai, long *perm,
00088                 double *control, double *info)
00089     {
00090         return (amd_l_order(n, Ap, Ai, perm, control, info));
00091     }
00092 };
00093 #endif
00094 
00095 }
00096 
00097 
00098 namespace Zoltan2{
00099 
00100 template <typename Adapter>
00101 class AlgAMD
00102 {
00103     public:
00104 
00105     AlgAMD()
00106     {
00107     }
00108 
00109     int order ( const RCP<GraphModel<Adapter> > &model,
00110     const RCP<OrderingSolution<typename Adapter::gid_t,
00111     typename Adapter::lno_t> > &solution, const RCP<Teuchos::ParameterList> &pl,
00112     const RCP<Teuchos::Comm<int> > &comm )
00113     {
00114 #ifndef HAVE_ZOLTAN2_AMD
00115   throw std::runtime_error(
00116         "BUILD ERROR: AMD requested but not compiled into Zoltan2.\n"
00117         "Please set CMake flag Zoltan2_ENABLE_AMD:BOOL=ON.");
00118 #else
00119       typedef typename Adapter::lno_t lno_t;
00120       typedef typename Adapter::gno_t gno_t;
00121       typedef typename Adapter::gid_t gid_t;
00122       typedef typename Adapter::scalar_t scalar_t;
00123 
00124       int ierr= 0;
00125 
00126       if (comm->getSize() != 1)
00127       {
00128           throw std::runtime_error(
00129             "ERROR: AMD requested with distributed matrix.\n"
00130             "This feature is not supported yet. Please use a local matrix.");
00131       }
00132 
00133       const size_t nVtx = model->getLocalNumVertices();
00134 
00135       //cout << "Local num vertices" << nVtx << endl;
00136       ArrayView<const gno_t> edgeIds;
00137       ArrayView<const int> procIds;
00138       ArrayView<const lno_t> offsets;
00139       ArrayView<StridedData<lno_t, scalar_t> > wgts;
00140 
00141       //const size_t nEdgs = model->getEdgeList( edgeIds,
00142       //                      procIds, offsets, wgts);
00143       // TODO: Should use the local graph
00144       model->getEdgeList( edgeIds, procIds, offsets, wgts);
00145 
00146       AMDTraits<lno_t> AMDobj;
00147       double Control[AMD_CONTROL];
00148       double Info[AMD_INFO];
00149 
00150       amd_defaults(Control);
00151       amd_control(Control);
00152 
00153       lno_t *perm;
00154       perm = (lno_t *) (solution->getPermutationRCP().getRawPtr());
00155 
00156       lno_t result = AMDobj.order(nVtx, offsets.getRawPtr(),
00157                              edgeIds.getRawPtr(), perm, Control, Info);
00158 
00159       if (result != AMD_OK && result != AMD_OK_BUT_JUMBLED)
00160           ierr = -1; // TODO: Change return value to lno_t
00161 
00162       solution->setHavePerm(true);
00163       return ierr;
00164 #endif
00165     }
00166 };
00167 
00168 }
00169 
00170 
00171 
00172 #endif