SundanceFileIOChacoPartitioner.cpp
Go to the documentation of this file.
00001 /* @HEADER@ */
00002 // ************************************************************************
00003 // 
00004 //                             Sundance
00005 //                 Copyright 2011 Sandia Corporation
00006 // 
00007 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
00008 // the U.S. Government retains certain rights in this software.
00009 //
00010 // Redistribution and use in source and binary forms, with or without
00011 // modification, are permitted provided that the following conditions are
00012 // met:
00013 //
00014 // 1. Redistributions of source code must retain the above copyright
00015 // notice, this list of conditions and the following disclaimer.
00016 //
00017 // 2. Redistributions in binary form must reproduce the above copyright
00018 // notice, this list of conditions and the following disclaimer in the
00019 // documentation and/or other materials provided with the distribution.
00020 //
00021 // 3. Neither the name of the Corporation nor the names of the
00022 // contributors may be used to endorse or promote products derived from
00023 // this software without specific prior written permission.
00024 //
00025 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00026 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00028 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00029 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00030 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00031 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00032 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00033 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00034 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00035 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00036 //
00037 // Questions? Contact Kevin Long (kevin.long@ttu.edu)
00038 // 
00039 
00040 /* @HEADER@ */
00041 
00042 
00043 #include "SundanceFileIOChacoPartitioner.hpp"
00044 #include "Teuchos_StrUtils.hpp"
00045 
00046 using namespace Sundance;
00047 using namespace Sundance;
00048 
00049 using namespace Teuchos;
00050 using namespace Sundance;
00051 
00052 using std::ofstream;
00053 using std::ifstream;
00054 using std::endl;
00055 
00056 
00057 FileIOChacoPartitioner::FileIOChacoPartitioner(const std::string& filename,
00058   bool ignoreGhosts)
00059   : SerialPartitionerBase(ignoreGhosts), filename_(filename)
00060 {}
00061 
00062 void FileIOChacoPartitioner::writeGraph(const Mesh& mesh) const 
00063 {
00064   Array<Array<int> > neighbors;
00065   int nEdges;
00066 
00067   getNeighbors(mesh, neighbors, nEdges);
00068 
00069   std::string gf = filename_ + ".graph";
00070   ofstream os(gf.c_str());
00071 
00072   os << neighbors.size() << " " << nEdges << std::endl;
00073 
00074   for (int i=0; i<neighbors.size(); i++)
00075   {
00076     for (int j=0; j<neighbors[i].size(); j++) 
00077     {
00078       if (j > 0) os << " ";
00079       os << neighbors[i][j]+1; // need unit offset here for Chaco
00080     }
00081     os << "\n";
00082   }
00083 }
00084 
00085 
00086 void FileIOChacoPartitioner::runChaco(int np) const 
00087 {
00088   ofstream pf("User_Params");
00089   pf << 
00090     "OUTPUT_ASSIGN=true\n"
00091     "PROMPT=false\n"
00092     "ARCHITECTURE=1\n"
00093     "REFINE_PARTITION=4\n"
00094     "REFINE_MAP=true\n"
00095     "KL_BAD_MOVES=20\n"
00096     "KL_NTRIES_BAD=10\n"
00097     "KL_IMBALANCE=0.02\n"
00098     "INTERNAL_VERTICES=true\n"
00099     "MATCH_TYPE=2\n"
00100     "HEAVY_MATCH=true\n"
00101     "TERM_PROP=true\n"
00102     "COARSE_NLEVEL_KL=1\n"
00103     "COARSEN_RATIO_MIN=0.7\n"
00104     "CUT_TO_HOP_COST=1.0\n"
00105     "RANDOM_SEED=12345\n" << std::endl;
00106 
00107   ofstream chIn("chacoInput");
00108   chIn << filename_ + ".graph\n" << filename_ + ".assign\n1\n100\n"
00109        << np << "\n1\nn" << std::endl;
00110 
00111   int status = system("chaco < chacoInput");
00112   TEUCHOS_TEST_FOR_EXCEPTION(status < 0, std::runtime_error, "error detected in system call to run chaco");
00113 }
00114 
00115 bool FileIOChacoPartitioner::isEmptyLine(const std::string& x) const 
00116 {
00117   return x.length()==0 || StrUtils::isWhite(x);
00118 }
00119 
00120 bool FileIOChacoPartitioner::getNextLine(std::istream& is, std::string& line,
00121                                          Array<string>& tokens,
00122                                          char comment) const 
00123 {
00124   bool rtn = false;
00125   while ((rtn=StrUtils::readLine(is, line)))
00126     {
00127       if (line.length() > 0) line = StrUtils::before(line,comment);
00128       if (isEmptyLine(line)) continue;
00129       if (line.length() > 0) break;
00130     }
00131   tokens = StrUtils::stringTokenizer(line);
00132   return rtn;
00133 }
00134 
00135 void FileIOChacoPartitioner::getAssignments(const Mesh& mesh, int np,
00136   Array<int>& assignments) const 
00137 {
00138   writeGraph(mesh);
00139   runChaco(np);
00140 
00141   std::string af = filename_ + ".assign";
00142   ifstream is(af.c_str());
00143 
00144   std::string line;
00145   Array<string> tokens;
00146     
00147   while (getNextLine(is, line, tokens, '#'))
00148   {
00149     assignments.append(StrUtils::atoi(tokens[0]));
00150   }
00151 }
00152 

Site Contact