angel_io.hpp

Go to the documentation of this file.
00001 // $Id: angel_io.hpp,v 1.14 2008/02/28 16:21:08 gottschling Exp $
00002 
00003 #ifndef         _angel_io_include_
00004 #define         _angel_io_include_
00005 
00006 
00007 //
00008 //
00009 //
00010 
00011 // #include "boost/graph/graphviz.hpp"
00012 
00013 #include <vector>
00014 #include <iostream>
00015 #include <fstream>
00016 #include <sstream>
00017 #include <string>
00018 #include <ctime>
00019 
00020 #include "angel_types.hpp"
00021 #include "angel_tools.hpp"
00022 
00023 namespace angel {
00024 
00025   using std::string; using std::vector; using std::ostream;
00026   using std::ofstream; using std::cout; using std::endl;
00027   using boost::tie; using boost::graph_traits;
00028   using boost::property_map;
00029 
00038 int read_graph_eliad (const string& file_name, c_graph_t& cg, bool retry= true);
00039 
00040 // =====================================================
00041 // output
00042 // =====================================================
00043 
00045 void write_face (ostream& stream, 
00046                  line_graph_t::face_t face,
00047                  const line_graph_t& lg);
00048 
00050 inline void write_face (line_graph_t::face_t face,
00051                         const line_graph_t& lg) {
00052   write_face (cout, face, lg);
00053 }
00054 
00056 void write_face_vector (ostream& stream, const string& s, 
00057                         const vector<line_graph_t::face_t>& v,
00058                         const line_graph_t& lg);
00059 
00061 inline void write_face_vector (const string& s, 
00062                                const vector<line_graph_t::face_t>& v,
00063                                const line_graph_t& lg) {
00064   write_face_vector (cout, s, v, lg);
00065 }
00066 
00067 // -----------------------------------------------------
00068 // general templates
00069 // -----------------------------------------------------
00070 
00072 template <typename T1, typename T2>
00073 ostream& operator<< (ostream& stream, const std::pair<T1,T2>& p) {
00074   return stream << "(" << p.first << ", " << p.second << ")"; }
00075 
00077 template <typename Scalar_t>
00078 inline void write_vector (ostream& stream, const string& s, 
00079                           const vector<Scalar_t>& v);
00080 
00082 template <typename Scalar_t>
00083 inline void write_vector (const string& s, const vector<Scalar_t>& v) {
00084   write_vector (cout, s, v);
00085 }
00086 
00093 template <typename Scalar_t, typename Op_t>
00094 inline void write_vector (ostream& stream, const string& s, 
00095                           const vector<Scalar_t>& v, Op_t op);
00096 
00102 template <typename Scalar_t, typename Op_t>
00103 inline void write_vector (const string& s, const vector<Scalar_t>& v, 
00104                           Op_t op) {
00105   write_vector (cout, s, v, op);
00106 }
00107 
00108 // -----------------------------------------------------
00109 // general graph output
00110 // -----------------------------------------------------
00111 
00118 template <typename Ad_graph_t> 
00119 void write_graph (ostream& stream, const string& s, const Ad_graph_t& adg,
00120                   bool write_edge_weight);
00121 
00127 template <typename Ad_graph_t> 
00128 inline void write_graph (const string& s, const Ad_graph_t& adg,
00129                          bool write_edge_weight) {      
00130   write_graph (cout, s, adg, write_edge_weight);
00131 }
00132 
00139 template <typename Ad_graph_t> 
00140 inline void write_graph (const string& file_name, 
00141                          const string& s, const Ad_graph_t& adg,
00142                          bool write_edge_weight) {       
00143   ofstream fout (file_name.c_str());
00144   write_graph (fout, s, adg, write_edge_weight);
00145 }
00146 
00147 
00153 template <typename Ad_graph_t> 
00154 void write_graph (ostream& stream, const string& s, const Ad_graph_t& adg);
00155 
00160 template <typename Ad_graph_t> 
00161 inline void write_graph (const string& s, const Ad_graph_t& adg) {      
00162   write_graph (cout, s, adg);
00163 }
00164 
00170 template <typename Ad_graph_t> 
00171 inline void write_graph (const string& file_name, 
00172                          const string& s, const Ad_graph_t& adg) {       
00173   ofstream fout (file_name.c_str());
00174   write_graph (fout, s, adg);
00175 }
00176 
00177 // -----------------------------------------------------
00178 // write graph like EliAD tools does
00179 // -----------------------------------------------------
00180 
00186 template <typename Ad_graph_t> 
00187 inline void write_graph_eliad (ostream& stream, const Ad_graph_t& adg);
00188 
00193 template <typename Ad_graph_t> 
00194 inline void write_graph_eliad (const Ad_graph_t& adg) {
00195   write_graph_eliad (cout, adg);
00196 }
00197 
00203 template <typename Ad_graph_t> 
00204 inline void write_graph_eliad (const string& file_name, const Ad_graph_t& adg) {
00205   ofstream fout (file_name.c_str());
00206   write_graph_eliad (fout, adg);
00207 }
00208 
00210 class write_edge_eliad_op_t {
00211   ostream& stream;
00212 public:
00213   write_edge_eliad_op_t (ostream& s) : stream (s) {}
00214   template <typename Ad_graph_t> 
00215   void operator() (typename Ad_graph_t::edge_descriptor e, const Ad_graph_t& adg) {
00216     stream << "   " << target (e, adg) + 1 << "   " << source (e, adg) + 1 << "   2\n";
00217   }
00218 };
00219 
00220 template <typename Ad_graph_t> 
00221 inline void write_graph_eliad (ostream& stream, const Ad_graph_t& adg) {
00222 
00223   stream << "n = " << adg.v() << "\n" << "CG = [\n";
00224   write_edge_eliad_op_t write_edge (stream);
00225   for_all_edges (adg, write_edge);
00226   stream << "]\n";
00227 }
00228 
00229 // -----------------------------------------------------
00230 // write internal graph properties
00231 // -----------------------------------------------------
00232 
00233 
00240 template <typename Prop_t, typename Ad_graph_t> 
00241 void write_vertex_property (ostream& stream, const string& s, 
00242                             const Prop_t& prop, const Ad_graph_t& adg) {
00243   stream << s << " are {";
00244 
00245   typename Ad_graph_t::vertex_iterator vi, vend;
00246   boost::tie (vi, vend) = vertices (adg);
00247   // write first if exist
00248   if (vi != vend) stream << prop[*vi++];
00249 
00250   for (; vi != vend; ++vi) 
00251     stream << ", " << prop[*vi];
00252   stream << '}' << endl;
00253 }
00254 
00261 template <typename Prop_t, typename Ad_graph_t> 
00262 void write_edge_property (ostream& stream, const string& s, 
00263                           const Prop_t& prop, const Ad_graph_t& adg);
00264 
00265 
00266 
00267 // =====================================================
00268 // lengthy implementations 
00269 // =====================================================
00270 
00271 
00272 template <typename Scalar_t>
00273 inline void write_vector (ostream& stream, const string& s, 
00274                           const vector<Scalar_t>& v) {
00275 
00276   stream << s << " (size = " << v.size() << ") is {";
00277 
00278   typename vector<Scalar_t>::const_iterator i= v.begin();
00279   // write first if exist
00280   if (i != v.end()) stream << *i++; 
00281 
00282   // from second to last (if exist)
00283   for (; i != v.end(); ++i)
00284     stream << ", " << *i;
00285   stream << '}' << endl;
00286 }
00287 
00288 template <typename Scalar_t, typename Op_t>
00289 inline void write_vector (ostream& stream, const string& s, 
00290                           const vector<Scalar_t>& v, Op_t op) {
00291 
00292   stream << s << " (size = " << v.size() << ") is {";
00293 
00294   typename vector<Scalar_t>::const_iterator i= v.begin();
00295   // write first if exist
00296   if (i != v.end()) op (stream, *i++); 
00297 
00298   // from second to last (if exist)
00299   for (; i != v.end(); ++i)
00300     stream << ", ", op (stream, *i);
00301   stream << '}' << endl;
00302 }
00303 
00304 // template <typename Ad_graph_t> 
00305 // void write_graph_as_bool_matrix (const string& file_name, const Ad_graph_t& adg,
00306 //                               bool write_transposed) {
00307 //   // typedef typename Ad_graph_t::pure_graph_t                         pure_graph_t;
00308 //   typedef typename graph_traits<Ad_graph_t>::vertex_iterator      vi_t;
00309 //   typedef typename graph_traits<Ad_graph_t>::edge_iterator        ei_t;
00310 //   typedef typename graph_traits<Ad_graph_t>::adjacency_iterator   ai_t;
00311 //   typedef typename property_map<Ad_graph_t, boost::vertex_index_t>::type id_t;
00312 //   // typedef typename pure_graph_t::edge_type                          ed_t;
00313 
00314 //   // const pure_graph_t& g (adg.pure_graph);
00315 //   vi_t i, end;
00316 //   id_t id = get(vertex_index, adg);
00317 
00318 //   // tie(i, end) = vertices(g);
00319 //   int gsize= num_vertices (adg);
00320 
00321 //   ofstream fout (file_name.c_str());
00322 //   fout << adg.x() << endl << adg.z() << endl << adg.y() << endl;
00323 
00324 //   if (write_transposed) {
00325 //     vector<bool> bool_line (gsize, false);
00326 //     vector<vector<bool> > bool_matrix (gsize, bool_line);
00327 //     for (tie(i, end) = vertices(adg); i != end; ++i) {
00328 //       ai_t ai, a_end;
00329 //       for (tie(ai, a_end) = adjacent_vertices(*i, adg); ai != a_end; ++ai)
00330 //      bool_matrix[get(id, *ai)][get(id, *i)]= true;
00331 //     }
00332 //     for (tie(i, end) = vertices(adg); i != end; ++i) {
00333 //       const vector<bool>& line_ref (bool_matrix[get(id, *i)]);
00334 //       for (int j= 0; j < gsize; j++)
00335 //      fout << (line_ref[j] ? 1 : 0);
00336 //       fout << endl;
00337 //     }
00338 //   } else 
00339 //     for (tie(i, end) = vertices(adg); i != end; ++i) {
00340 //       vector<int> bool_line (gsize, 0);
00341 //       ai_t ai, a_end;
00342 //       for (tie(ai, a_end) = adjacent_vertices(*i, adg); ai != a_end; ++ai)
00343 //        bool_line[get(id, *ai)]= 1;
00344 //       for (int j= 0; j < gsize; j++)
00345 //      fout << bool_line[j];
00346 //       fout << endl;
00347 //     }
00348 // }
00349 
00350 template <typename Ad_graph_t> 
00351 void write_graph (ostream& stream, const string& s, const Ad_graph_t& adg,
00352                   bool write_edge_weight) {     
00353   stream << s << " has " << num_vertices (adg) << " vertices: "
00354          << adg.x() << " independent, " << adg.z() << " intermediate and "
00355          << adg.y() << " dependent\n";
00356   write_vector (stream, "the dependent vertices are", adg.dependents);
00357   stream << "the adjacencies are:\n";
00358 
00359 
00360   if (write_edge_weight) {
00361     typename property_map<Ad_graph_t, boost::edge_weight_t>::const_type 
00362              ew= get(boost::edge_weight, adg);
00363     typename graph_traits<Ad_graph_t>::vertex_iterator  i, end;
00364     for (tie (i, end)= vertices (adg); i != end; ++i) {
00365       typename graph_traits<Ad_graph_t>::out_edge_iterator  ei, e_end;
00366       tie(ei, e_end) = out_edges(*i, adg);
00367       stream << "vertex " << *i << " has ";
00368       if (ei == e_end) stream << "no successor";
00369       else stream << "successors ";
00370       for (; ei != e_end; ++ei)
00371         stream << target (*ei, adg) << '[' << ew[*ei] << "]  ";
00372       stream << endl;
00373     }
00374     stream << endl;
00375   } else {
00376     typename graph_traits<Ad_graph_t>::vertex_iterator  i, end;
00377     for (tie (i, end)= vertices (adg); i != end; ++i) {
00378       typename graph_traits<Ad_graph_t>::adjacency_iterator  ai, a_end;
00379       tie(ai, a_end) = adjacent_vertices(*i, adg);
00380       stream << "vertex " << *i << " has ";
00381       if (ai == a_end) stream << "no successor";
00382       else stream << "successors ";
00383       for (; ai != a_end; ++ai)
00384         stream << *ai << "  ";
00385       stream << endl;
00386     }
00387     stream << endl;
00388   }
00389 }
00390 
00391 template <typename Ad_graph_t> 
00392 void write_graph (ostream& stream, const string& s, const Ad_graph_t& adg) {    
00393   stream << s << " has " << num_vertices (adg) << " vertices: "
00394          << adg.x() << " independent, " << adg.z() << " intermediate and "
00395          << adg.y() << " dependent\n";
00396   write_vector (stream, "the dependent vertices are", adg.dependents);
00397   stream << "the adjacencies are:\n";
00398 
00399     typename graph_traits<Ad_graph_t>::vertex_iterator  i, end;
00400     for (tie (i, end)= vertices (adg); i != end; ++i) {
00401       typename graph_traits<Ad_graph_t>::adjacency_iterator  ai, a_end;
00402       tie(ai, a_end) = adjacent_vertices(*i, adg);
00403       stream << "vertex " << *i << " has ";
00404       if (ai == a_end) stream << "no successor";
00405       else stream << "successors ";
00406       for (; ai != a_end; ++ai)
00407         stream << *ai << "  ";
00408       stream << endl;
00409     }
00410     stream << endl;
00411 }
00412 
00413 
00414 
00415 
00416 template <typename Prop_t, typename Ad_graph_t> 
00417 void write_edge_property (ostream& stream, const string& s, 
00418                           const Prop_t& prop, const Ad_graph_t& adg) {
00419   stream << s << " is {";
00420 
00421   typename Ad_graph_t::edge_iterator ei, eend;
00422   boost::tie (ei, eend) = edges (adg);
00423   // write first if exist
00424   if (ei != eend) {
00425     stream << '(' << source (*ei, adg) << ", " << target (*ei, adg) 
00426            << ")=" << prop[*ei];
00427     ++ei;}
00428   for (; ei != eend; ++ei) 
00429     stream << ", (" << source (*ei, adg) << ", " << target (*ei, adg) 
00430            << ")=" << prop[*ei];
00431   stream << '}' << endl;
00432 }
00433 
00434 // template <typename Ad_graph_t> 
00435 // void graphviz_display (const Ad_graph_t& adg) {
00436 //   string aFilename("/tmp/GraphVizDisplay.dot");
00437 //   ofstream anOutFileStream;
00438 //   anOutFileStream.open(aFilename.c_str(),std::ios::out);
00439 //   boost::write_graphviz(anOutFileStream, adg); 
00440 //   anOutFileStream.close(); 
00441 //   string commandString("dot -Tgif " + aFilename + " > " + aFilename + ".gif ; xv " + aFilename + ".gif" ); 
00442 //   system(commandString.c_str()); 
00443 // }
00444 
00445 extern ofstream log_file;
00446 
00447 #ifdef USE_MPI
00448 inline void open_log_file (int& argc, char**& argv, int proc) {
00449   std::ostringstream log_file_name;
00450   log_file_name << "log_file_proc_" << proc << "_" << time (0);
00451   log_file.open (log_file_name.str().c_str());
00452   log_file << "argv" << endl;
00453   for (int i= 0; i < argc; i++)
00454     log_file << argv[i] << endl;
00455   log_file << "----- end of argv -----" << endl;
00456 }
00457 #else
00458 inline void open_log_file (int& argc, char**& argv) {
00459   std::ostringstream log_file_name;
00460   log_file_name << "log_file_" << time (0);
00461   log_file.open (log_file_name.str().c_str());
00462   log_file << "argv" << endl;
00463   for (int i= 0; i < argc; i++)
00464     log_file << argv[i] << endl;
00465   log_file << "----- end of argv -----" << endl;
00466 }
00467 #endif
00468 
00469 inline void close_log_file () {
00470   log_file.close();
00471 }   
00472       
00473 string numbered_filename (const string& basename, const string& suffix, 
00474                         int number, int width= 4);
00475 
00476 struct no_output_t {
00477   void operator() (const std::string&) {}
00478   void operator() (const std::ostringstream&) {}
00479   template <class Ad_graph_t> 
00480   void write_graph (const std::string&, const Ad_graph_t&) {}
00481 };
00482 extern no_output_t no_output;
00483 
00484 template <class Value_t>
00485 no_output_t& operator<< (no_output_t& out, const Value_t&) {
00486   return out;}
00487 
00488 class string_stream_output_t : public no_output_t {
00489 protected:
00490   ostream& mystream;
00491 public:
00492   string_stream_output_t (std::ostream& s) : mystream (s) {}
00493   void operator() (const std::string& str) {
00494     mystream << str;}
00495   void operator() (const std::ostringstream& sstr) {
00496     mystream << sstr.str();}
00497   template <class Value_t> 
00498   friend string_stream_output_t& operator<< (string_stream_output_t&, const Value_t&);
00499 };
00500 extern string_stream_output_t cout_string_output;
00501 
00502 template <class Value_t>
00503 string_stream_output_t& operator<< (string_stream_output_t& out, const Value_t& value) {
00504   out.mystream << value; return out;}
00505 
00506 struct stream_output_t : public string_stream_output_t {
00507   stream_output_t (std::ostream& s) : string_stream_output_t (s) {}
00508   template <class Ad_graph_t> 
00509   void write_graph (const std::string& str, const Ad_graph_t& adg) {
00510     angel::write_graph (mystream, str, adg);}
00511 };
00512 // stream_output_t cout_output (std::cout);
00513 
00514 struct vis_display_output_t : public string_stream_output_t {
00515   vis_display_output_t (std::ostream& s) : string_stream_output_t (s) {}
00516   template <class Ad_graph_t> 
00517   void write_graph (const std::string& str, const Ad_graph_t& adg) {
00518     mystream << str; graphviz_display (adg);}
00519 };
00520 extern vis_display_output_t cout_vis_display_output;
00521 
00522 
00523 // struct vis_store_output_t : public no_output_t {
00524 //   std::string filenamebase;
00525 //   int         filecounter;
00526 // public:
00527 //   vis_store_output_t (const std::string& f) : filenamebase(f), filecounter(0) {}
00528 //   template <class Ad_graph_t> 
00529 //   void write_graph (const std::string& , const Ad_graph_t& adg) {
00530 //     string dot_filename (numbered_filename (filenamebase, "dot", filecounter));
00531 //     ofstream dot_file (dot_filename.c_str());
00532 //     write_graphviz(dot_file, adg); 
00533 //     dot_file.close();
00534 //     string command ("dot -Tgif " + dot_filename + " > " 
00535 //                  + numbered_filename (filenamebase, "gif", filecounter++)
00536 //                  + "; rm " + dot_filename);
00537 //     system (command.c_str());
00538 //   }
00539 // };
00540 
00541 #ifdef USEXAIFBOOSTER
00542 
00543 void write_refillDependences (ostream& stream,
00544                               const refillDependenceMap_t& refillDependences);
00545 
00546 void writeVertexAndEdgeTypes (ostream& stream,
00547                               c_graph_t& angelLCG);
00548 
00549 #endif // USEXAIFBOOSTER
00550 
00551 } // namespace angel
00552 
00553 #endif //       _angel_io_include_
00554 
00555 
00556 
00557 
00558 
00559 
00560 
00561 
00562 
00563 
00564 
00565 
00566 
00567 
00568 
00569 
00570 
00571 
00572 
00573 
00574 
00575 
00576 
00577 
00578 
00579 
00580 
00581 
00582 
00583 
00584 
00585 
00586 
00587 
00588 
00589 
00590 
00591 
00592 
00593 
00594 
00595 
00596 
00597 
00598 
00599 
00600 
00601 
00602 
00603 
00604 
00605 
00606 
00607 
00608 

Generated on Wed Mar 11 10:33:11 2009 for angel by  doxygen 1.5.3