angel  mercurial changeset:
include/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 #############################################################
00004 # This file is part of angel released under the BSD license #
00005 # The full COPYRIGHT notice can be found in the top         #
00006 # level directory of the angel distribution                 #
00007 #############################################################
00008 */
00009 
00010 #ifndef         _angel_io_include_
00011 #define         _angel_io_include_
00012 
00013 
00014 //
00015 //
00016 //
00017 
00018 // #include "boost/graph/graphviz.hpp"
00019 
00020 #include <vector>
00021 #include <iostream>
00022 #include <fstream>
00023 #include <sstream>
00024 #include <string>
00025 #include <ctime>
00026 
00027 #include "angel_types.hpp"
00028 #include "angel_tools.hpp"
00029 
00030 namespace angel {
00031 
00032   using std::string; using std::vector; using std::ostream;
00033   using std::ofstream; using std::cout; using std::endl;
00034   using boost::tie; using boost::graph_traits;
00035   using boost::property_map;
00036 
00045 int read_graph_eliad (const string& file_name, c_graph_t& cg, bool retry= true);
00046 
00047 // =====================================================
00048 // output
00049 // =====================================================
00050 
00052 void write_face (ostream& stream, 
00053                  line_graph_t::face_t face,
00054                  const line_graph_t& lg);
00055 
00057 inline void write_face (line_graph_t::face_t face,
00058                         const line_graph_t& lg) {
00059   write_face (cout, face, lg);
00060 }
00061 
00063 void write_face_vector (ostream& stream, const string& s, 
00064                         const vector<line_graph_t::face_t>& v,
00065                         const line_graph_t& lg);
00066 
00068 inline void write_face_vector (const string& s, 
00069                                const vector<line_graph_t::face_t>& v,
00070                                const line_graph_t& lg) {
00071   write_face_vector (cout, s, v, lg);
00072 }
00073 
00074 // -----------------------------------------------------
00075 // general templates
00076 // -----------------------------------------------------
00077 
00079 template <typename T1, typename T2>
00080 ostream& operator<< (ostream& stream, const std::pair<T1,T2>& p) {
00081   return stream << "(" << p.first << ", " << p.second << ")"; }
00082 
00084 template <typename Scalar_t>
00085 inline void write_vector (ostream& stream, const string& s, 
00086                           const vector<Scalar_t>& v);
00087 
00089 template <typename Scalar_t>
00090 inline void write_vector (const string& s, const vector<Scalar_t>& v) {
00091   write_vector (cout, s, v);
00092 }
00093 
00100 template <typename Scalar_t, typename Op_t>
00101 inline void write_vector (ostream& stream, const string& s, 
00102                           const vector<Scalar_t>& v, Op_t op);
00103 
00109 template <typename Scalar_t, typename Op_t>
00110 inline void write_vector (const string& s, const vector<Scalar_t>& v, 
00111                           Op_t op) {
00112   write_vector (cout, s, v, op);
00113 }
00114 
00115 // -----------------------------------------------------
00116 // general graph output
00117 // -----------------------------------------------------
00118 
00125 template <typename Ad_graph_t> 
00126 void write_graph (ostream& stream, const string& s, const Ad_graph_t& adg,
00127                   bool write_edge_weight);
00128 
00134 template <typename Ad_graph_t> 
00135 inline void write_graph (const string& s, const Ad_graph_t& adg,
00136                          bool write_edge_weight) {      
00137   write_graph (cout, s, adg, write_edge_weight);
00138 }
00139 
00146 template <typename Ad_graph_t> 
00147 inline void write_graph (const string& file_name, 
00148                          const string& s, const Ad_graph_t& adg,
00149                          bool write_edge_weight) {       
00150   ofstream fout (file_name.c_str());
00151   write_graph (fout, s, adg, write_edge_weight);
00152 }
00153 
00154 
00160 template <typename Ad_graph_t> 
00161 void write_graph (ostream& stream, const string& s, const Ad_graph_t& adg);
00162 
00167 template <typename Ad_graph_t> 
00168 inline void write_graph (const string& s, const Ad_graph_t& adg) {      
00169   write_graph (cout, s, adg);
00170 }
00171 
00177 template <typename Ad_graph_t> 
00178 inline void write_graph (const string& file_name, 
00179                          const string& s, const Ad_graph_t& adg) {       
00180   ofstream fout (file_name.c_str());
00181   write_graph (fout, s, adg);
00182 }
00183 
00184 // -----------------------------------------------------
00185 // write graph like EliAD tools does
00186 // -----------------------------------------------------
00187 
00193 template <typename Ad_graph_t> 
00194 inline void write_graph_eliad (ostream& stream, const Ad_graph_t& adg);
00195 
00200 template <typename Ad_graph_t> 
00201 inline void write_graph_eliad (const Ad_graph_t& adg) {
00202   write_graph_eliad (cout, adg);
00203 }
00204 
00210 template <typename Ad_graph_t> 
00211 inline void write_graph_eliad (const string& file_name, const Ad_graph_t& adg) {
00212   ofstream fout (file_name.c_str());
00213   write_graph_eliad (fout, adg);
00214 }
00215 
00217 class write_edge_eliad_op_t {
00218   ostream& stream;
00219 public:
00220   write_edge_eliad_op_t (ostream& s) : stream (s) {}
00221   template <typename Ad_graph_t> 
00222   void operator() (typename Ad_graph_t::edge_descriptor e, const Ad_graph_t& adg) {
00223     stream << "   " << target (e, adg) + 1 << "   " << source (e, adg) + 1 << "   2\n";
00224   }
00225 };
00226 
00227 template <typename Ad_graph_t> 
00228 inline void write_graph_eliad (ostream& stream, const Ad_graph_t& adg) {
00229 
00230   stream << "n = " << adg.v() << "\n" << "CG = [\n";
00231   write_edge_eliad_op_t write_edge (stream);
00232   for_all_edges (adg, write_edge);
00233   stream << "]\n";
00234 }
00235 
00236 // -----------------------------------------------------
00237 // write internal graph properties
00238 // -----------------------------------------------------
00239 
00240 
00247 template <typename Prop_t, typename Ad_graph_t> 
00248 void write_vertex_property (ostream& stream, const string& s, 
00249                             const Prop_t& prop, const Ad_graph_t& adg) {
00250   stream << s << " are {";
00251 
00252   typename Ad_graph_t::vertex_iterator vi, vend;
00253   boost::tie (vi, vend) = vertices (adg);
00254   // write first if exist
00255   if (vi != vend) stream << prop[*vi++];
00256 
00257   for (; vi != vend; ++vi) 
00258     stream << ", " << prop[*vi];
00259   stream << '}' << endl;
00260 }
00261 
00268 template <typename Prop_t, typename Ad_graph_t> 
00269 void write_edge_property (ostream& stream, const string& s, 
00270                           const Prop_t& prop, const Ad_graph_t& adg);
00271 
00272 
00273 
00274 // =====================================================
00275 // lengthy implementations 
00276 // =====================================================
00277 
00278 
00279 template <typename Scalar_t>
00280 inline void write_vector (ostream& stream, const string& s, 
00281                           const vector<Scalar_t>& v) {
00282 
00283   stream << s << " (size = " << v.size() << ") is {";
00284 
00285   typename vector<Scalar_t>::const_iterator i= v.begin();
00286   // write first if exist
00287   if (i != v.end()) stream << *i++; 
00288 
00289   // from second to last (if exist)
00290   for (; i != v.end(); ++i)
00291     stream << ", " << *i;
00292   stream << '}' << endl;
00293 }
00294 
00295 template <typename Scalar_t, typename Op_t>
00296 inline void write_vector (ostream& stream, const string& s, 
00297                           const vector<Scalar_t>& v, Op_t op) {
00298 
00299   stream << s << " (size = " << v.size() << ") is {";
00300 
00301   typename vector<Scalar_t>::const_iterator i= v.begin();
00302   // write first if exist
00303   if (i != v.end()) op (stream, *i++); 
00304 
00305   // from second to last (if exist)
00306   for (; i != v.end(); ++i)
00307     stream << ", ", op (stream, *i);
00308   stream << '}' << endl;
00309 }
00310 
00311 // template <typename Ad_graph_t> 
00312 // void write_graph_as_bool_matrix (const string& file_name, const Ad_graph_t& adg,
00313 //                               bool write_transposed) {
00314 //   // typedef typename Ad_graph_t::pure_graph_t                         pure_graph_t;
00315 //   typedef typename graph_traits<Ad_graph_t>::vertex_iterator      vi_t;
00316 //   typedef typename graph_traits<Ad_graph_t>::edge_iterator        ei_t;
00317 //   typedef typename graph_traits<Ad_graph_t>::adjacency_iterator   ai_t;
00318 //   typedef typename property_map<Ad_graph_t, boost::vertex_index_t>::type id_t;
00319 //   // typedef typename pure_graph_t::edge_type                          ed_t;
00320 
00321 //   // const pure_graph_t& g (adg.pure_graph);
00322 //   vi_t i, end;
00323 //   id_t id = get(vertex_index, adg);
00324 
00325 //   // tie(i, end) = vertices(g);
00326 //   int gsize= num_vertices (adg);
00327 
00328 //   ofstream fout (file_name.c_str());
00329 //   fout << adg.x() << endl << adg.z() << endl << adg.y() << endl;
00330 
00331 //   if (write_transposed) {
00332 //     vector<bool> bool_line (gsize, false);
00333 //     vector<vector<bool> > bool_matrix (gsize, bool_line);
00334 //     for (tie(i, end) = vertices(adg); i != end; ++i) {
00335 //       ai_t ai, a_end;
00336 //       for (tie(ai, a_end) = adjacent_vertices(*i, adg); ai != a_end; ++ai)
00337 //      bool_matrix[get(id, *ai)][get(id, *i)]= true;
00338 //     }
00339 //     for (tie(i, end) = vertices(adg); i != end; ++i) {
00340 //       const vector<bool>& line_ref (bool_matrix[get(id, *i)]);
00341 //       for (int j= 0; j < gsize; j++)
00342 //      fout << (line_ref[j] ? 1 : 0);
00343 //       fout << endl;
00344 //     }
00345 //   } else 
00346 //     for (tie(i, end) = vertices(adg); i != end; ++i) {
00347 //       vector<int> bool_line (gsize, 0);
00348 //       ai_t ai, a_end;
00349 //       for (tie(ai, a_end) = adjacent_vertices(*i, adg); ai != a_end; ++ai)
00350 //        bool_line[get(id, *ai)]= 1;
00351 //       for (int j= 0; j < gsize; j++)
00352 //      fout << bool_line[j];
00353 //       fout << endl;
00354 //     }
00355 // }
00356 
00357 template <typename Ad_graph_t> 
00358 void write_graph (ostream& stream, const string& s, const Ad_graph_t& adg,
00359                   bool write_edge_weight) {     
00360   stream << s << " has " << num_vertices (adg) << " vertices: "
00361          << adg.x() << " independent, " << adg.z() << " intermediate and "
00362          << adg.y() << " dependent\n";
00363   write_vector (stream, "the dependent vertices are", adg.dependents);
00364   stream << "the adjacencies are:\n";
00365 
00366 
00367   if (write_edge_weight) {
00368     typename property_map<Ad_graph_t, boost::edge_weight_t>::const_type 
00369              ew= get(boost::edge_weight, adg);
00370     typename graph_traits<Ad_graph_t>::vertex_iterator  i, end;
00371     for (tie (i, end)= vertices (adg); i != end; ++i) {
00372       typename graph_traits<Ad_graph_t>::out_edge_iterator  ei, e_end;
00373       tie(ei, e_end) = out_edges(*i, adg);
00374       stream << "vertex " << *i << " has ";
00375       if (ei == e_end) stream << "no successor";
00376       else stream << "successors ";
00377       for (; ei != e_end; ++ei)
00378         stream << target (*ei, adg) << '[' << ew[*ei] << "]  ";
00379       stream << endl;
00380     }
00381     stream << endl;
00382   } else {
00383     typename graph_traits<Ad_graph_t>::vertex_iterator  i, end;
00384     for (tie (i, end)= vertices (adg); i != end; ++i) {
00385       typename graph_traits<Ad_graph_t>::adjacency_iterator  ai, a_end;
00386       tie(ai, a_end) = adjacent_vertices(*i, adg);
00387       stream << "vertex " << *i << " has ";
00388       if (ai == a_end) stream << "no successor";
00389       else stream << "successors ";
00390       for (; ai != a_end; ++ai)
00391         stream << *ai << "  ";
00392       stream << endl;
00393     }
00394     stream << endl;
00395   }
00396 }
00397 
00398 template <typename Ad_graph_t> 
00399 void write_graph (ostream& stream, const string& s, const Ad_graph_t& adg) {    
00400   stream << s << " has " << num_vertices (adg) << " vertices: "
00401          << adg.x() << " independent, " << adg.z() << " intermediate and "
00402          << adg.y() << " dependent\n";
00403   write_vector (stream, "the dependent vertices are", adg.dependents);
00404   stream << "the adjacencies are:\n";
00405 
00406     typename graph_traits<Ad_graph_t>::vertex_iterator  i, end;
00407     for (tie (i, end)= vertices (adg); i != end; ++i) {
00408       typename graph_traits<Ad_graph_t>::adjacency_iterator  ai, a_end;
00409       tie(ai, a_end) = adjacent_vertices(*i, adg);
00410       stream << "vertex " << *i << " has ";
00411       if (ai == a_end) stream << "no successor";
00412       else stream << "successors ";
00413       for (; ai != a_end; ++ai)
00414         stream << *ai << "  ";
00415       stream << endl;
00416     }
00417     stream << endl;
00418 }
00419 
00420 
00421 
00422 
00423 template <typename Prop_t, typename Ad_graph_t> 
00424 void write_edge_property (ostream& stream, const string& s, 
00425                           const Prop_t& prop, const Ad_graph_t& adg) {
00426   stream << s << " is {";
00427 
00428   typename Ad_graph_t::edge_iterator ei, eend;
00429   boost::tie (ei, eend) = edges (adg);
00430   // write first if exist
00431   if (ei != eend) {
00432     stream << '(' << source (*ei, adg) << ", " << target (*ei, adg) 
00433            << ")=" << prop[*ei];
00434     ++ei;}
00435   for (; ei != eend; ++ei) 
00436     stream << ", (" << source (*ei, adg) << ", " << target (*ei, adg) 
00437            << ")=" << prop[*ei];
00438   stream << '}' << endl;
00439 }
00440 
00441 // template <typename Ad_graph_t> 
00442 // void graphviz_display (const Ad_graph_t& adg) {
00443 //   string aFilename("/tmp/GraphVizDisplay.dot");
00444 //   ofstream anOutFileStream;
00445 //   anOutFileStream.open(aFilename.c_str(),std::ios::out);
00446 //   boost::write_graphviz(anOutFileStream, adg); 
00447 //   anOutFileStream.close(); 
00448 //   string commandString("dot -Tgif " + aFilename + " > " + aFilename + ".gif ; xv " + aFilename + ".gif" ); 
00449 //   system(commandString.c_str()); 
00450 // }
00451 
00452 extern ofstream log_file;
00453 
00454 #ifdef USE_MPI
00455 inline void open_log_file (int& argc, char**& argv, int proc) {
00456   std::ostringstream log_file_name;
00457   log_file_name << "log_file_proc_" << proc << "_" << time (0);
00458   log_file.open (log_file_name.str().c_str());
00459   log_file << "argv" << endl;
00460   for (int i= 0; i < argc; i++)
00461     log_file << argv[i] << endl;
00462   log_file << "----- end of argv -----" << endl;
00463 }
00464 #else
00465 inline void open_log_file (int& argc, char**& argv) {
00466   std::ostringstream log_file_name;
00467   log_file_name << "log_file_" << time (0);
00468   log_file.open (log_file_name.str().c_str());
00469   log_file << "argv" << endl;
00470   for (int i= 0; i < argc; i++)
00471     log_file << argv[i] << endl;
00472   log_file << "----- end of argv -----" << endl;
00473 }
00474 #endif
00475 
00476 inline void close_log_file () {
00477   log_file.close();
00478 }   
00479       
00480 string numbered_filename (const string& basename, const string& suffix, 
00481                         int number, int width= 4);
00482 
00483 struct no_output_t {
00484   void operator() (const std::string&) {}
00485   void operator() (const std::ostringstream&) {}
00486   template <class Ad_graph_t> 
00487   void write_graph (const std::string&, const Ad_graph_t&) {}
00488 };
00489 extern no_output_t no_output;
00490 
00491 template <class Value_t>
00492 no_output_t& operator<< (no_output_t& out, const Value_t&) {
00493   return out;}
00494 
00495 class string_stream_output_t : public no_output_t {
00496 protected:
00497   ostream& mystream;
00498 public:
00499   string_stream_output_t (std::ostream& s) : mystream (s) {}
00500   void operator() (const std::string& str) {
00501     mystream << str;}
00502   void operator() (const std::ostringstream& sstr) {
00503     mystream << sstr.str();}
00504   template <class Value_t> 
00505   friend string_stream_output_t& operator<< (string_stream_output_t&, const Value_t&);
00506 };
00507 extern string_stream_output_t cout_string_output;
00508 
00509 template <class Value_t>
00510 string_stream_output_t& operator<< (string_stream_output_t& out, const Value_t& value) {
00511   out.mystream << value; return out;}
00512 
00513 struct stream_output_t : public string_stream_output_t {
00514   stream_output_t (std::ostream& s) : string_stream_output_t (s) {}
00515   template <class Ad_graph_t> 
00516   void write_graph (const std::string& str, const Ad_graph_t& adg) {
00517     angel::write_graph (mystream, str, adg);}
00518 };
00519 // stream_output_t cout_output (std::cout);
00520 
00521 struct vis_display_output_t : public string_stream_output_t {
00522   vis_display_output_t (std::ostream& s) : string_stream_output_t (s) {}
00523   template <class Ad_graph_t> 
00524   void write_graph (const std::string& str, const Ad_graph_t& adg) {
00525     mystream << str; graphviz_display (adg);}
00526 };
00527 extern vis_display_output_t cout_vis_display_output;
00528 
00529 
00530 // struct vis_store_output_t : public no_output_t {
00531 //   std::string filenamebase;
00532 //   int         filecounter;
00533 // public:
00534 //   vis_store_output_t (const std::string& f) : filenamebase(f), filecounter(0) {}
00535 //   template <class Ad_graph_t> 
00536 //   void write_graph (const std::string& , const Ad_graph_t& adg) {
00537 //     string dot_filename (numbered_filename (filenamebase, "dot", filecounter));
00538 //     ofstream dot_file (dot_filename.c_str());
00539 //     write_graphviz(dot_file, adg); 
00540 //     dot_file.close();
00541 //     string command ("dot -Tgif " + dot_filename + " > " 
00542 //                  + numbered_filename (filenamebase, "gif", filecounter++)
00543 //                  + "; rm " + dot_filename);
00544 //     system (command.c_str());
00545 //   }
00546 // };
00547 
00548 #ifdef USEXAIFBOOSTER
00549 
00550 void write_refillDependences (ostream& stream,
00551                               const refillDependenceMap_t& refillDependences);
00552 
00553 void writeVertexAndEdgeTypes (ostream& stream,
00554                               c_graph_t& angelLCG);
00555 
00556 #endif // USEXAIFBOOSTER
00557 
00558 } // namespace angel
00559 
00560 #endif //       _angel_io_include_
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 
00609 
00610 
00611 
00612 
00613 
00614 
00615 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines