Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
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;
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