Go to the documentation of this file.00001 #include "SundanceFileIOChacoPartitioner.hpp"
00002 #include "Teuchos_StrUtils.hpp"
00003
00004 using namespace Sundance;
00005 using namespace Sundance;
00006
00007 using namespace Teuchos;
00008 using namespace Sundance;
00009
00010 using std::ofstream;
00011 using std::ifstream;
00012 using std::endl;
00013
00014
00015 FileIOChacoPartitioner::FileIOChacoPartitioner(const std::string& filename)
00016 : filename_(filename)
00017 {}
00018
00019 void FileIOChacoPartitioner::writeGraph(const Mesh& mesh) const
00020 {
00021 Array<Array<int> > neighbors;
00022 int nEdges;
00023
00024 getNeighbors(mesh, neighbors, nEdges);
00025
00026 std::string gf = filename_ + ".graph";
00027 ofstream os(gf.c_str());
00028
00029 os << neighbors.size() << " " << nEdges << std::endl;
00030
00031 for (int i=0; i<neighbors.size(); i++)
00032 {
00033 for (int j=0; j<neighbors[i].size(); j++)
00034 {
00035 if (j > 0) os << " ";
00036 os << neighbors[i][j]+1;
00037 }
00038 os << "\n";
00039 }
00040 }
00041
00042
00043 void FileIOChacoPartitioner::runChaco(int np) const
00044 {
00045 ofstream pf("User_Params");
00046 pf <<
00047 "OUTPUT_ASSIGN=true\n"
00048 "PROMPT=false\n"
00049 "ARCHITECTURE=1\n"
00050 "REFINE_PARTITION=4\n"
00051 "REFINE_MAP=true\n"
00052 "KL_BAD_MOVES=20\n"
00053 "KL_NTRIES_BAD=10\n"
00054 "KL_IMBALANCE=0.02\n"
00055 "INTERNAL_VERTICES=true\n"
00056 "MATCH_TYPE=2\n"
00057 "HEAVY_MATCH=true\n"
00058 "TERM_PROP=true\n"
00059 "COARSE_NLEVEL_KL=1\n"
00060 "COARSEN_RATIO_MIN=0.7\n"
00061 "CUT_TO_HOP_COST=1.0\n"
00062 "RANDOM_SEED=12345\n" << std::endl;
00063
00064 ofstream chIn("chacoInput");
00065 chIn << filename_ + ".graph\n" << filename_ + ".assign\n1\n100\n"
00066 << np << "\n1\nn" << std::endl;
00067
00068 int status = system("chaco < chacoInput");
00069 TEST_FOR_EXCEPTION(status < 0, RuntimeError, "error detected in system call to run chaco");
00070 }
00071
00072 bool FileIOChacoPartitioner::isEmptyLine(const std::string& x) const
00073 {
00074 return x.length()==0 || StrUtils::isWhite(x);
00075 }
00076
00077 bool FileIOChacoPartitioner::getNextLine(std::istream& is, std::string& line,
00078 Array<string>& tokens,
00079 char comment) const
00080 {
00081 bool rtn = false;
00082 while ((rtn=StrUtils::readLine(is, line)))
00083 {
00084 if (line.length() > 0) line = StrUtils::before(line,comment);
00085 if (isEmptyLine(line)) continue;
00086 if (line.length() > 0) break;
00087 }
00088 tokens = StrUtils::stringTokenizer(line);
00089 return rtn;
00090 }
00091
00092 void FileIOChacoPartitioner::getAssignments(const Mesh& mesh, int np,
00093 Array<int>& assignments) const
00094 {
00095 writeGraph(mesh);
00096 runChaco(np);
00097
00098 std::string af = filename_ + ".assign";
00099 ifstream is(af.c_str());
00100
00101 std::string line;
00102 Array<string> tokens;
00103
00104 while (getNextLine(is, line, tokens, '#'))
00105 {
00106 assignments.append(StrUtils::atoi(tokens[0]));
00107 }
00108 }
00109