mesh_generator_test.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // Copyright (C) 2004-2008 The Trustees of Indiana University.
  2. // Use, modification and distribution is subject to the Boost Software
  3. // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. // Authors: Douglas Gregor
  6. // Andrew Lumsdaine
  7. #include <boost/graph/use_mpi.hpp>
  8. #include <boost/config.hpp>
  9. #include <boost/throw_exception.hpp>
  10. #include <boost/graph/mesh_graph_generator.hpp>
  11. #include <boost/test/minimal.hpp>
  12. #include <boost/graph/distributed/adjacency_list.hpp>
  13. #include <boost/graph/distributed/mpi_process_group.hpp>
  14. #include <boost/graph/distributed/graphviz.hpp>
  15. #include <boost/graph/iteration_macros.hpp>
  16. #include <iostream>
  17. #include <sstream>
  18. #include <iomanip>
  19. #include <string>
  20. #include <boost/test/minimal.hpp>
  21. #ifdef BOOST_NO_EXCEPTIONS
  22. void
  23. boost::throw_exception(std::exception const& ex)
  24. {
  25. std::cout << ex.what() << std::endl;
  26. abort();
  27. }
  28. #endif
  29. using namespace boost;
  30. using boost::graph::distributed::mpi_process_group;
  31. /****************************************************************************
  32. * Timing
  33. ****************************************************************************/
  34. typedef double time_type;
  35. inline time_type get_time()
  36. {
  37. return MPI_Wtime();
  38. }
  39. std::string print_time(time_type t)
  40. {
  41. std::ostringstream out;
  42. out << std::setiosflags(std::ios::fixed) << std::setprecision(2) << t;
  43. return out.str();
  44. }
  45. /****************************************************************************
  46. * Edge weight generator iterator *
  47. ****************************************************************************/
  48. template<typename F>
  49. class generator_iterator
  50. {
  51. public:
  52. typedef std::input_iterator_tag iterator_category;
  53. typedef typename F::result_type value_type;
  54. typedef const value_type& reference;
  55. typedef const value_type* pointer;
  56. typedef void difference_type;
  57. explicit generator_iterator(const F& f = F()) : f(f) { value = this->f(); }
  58. reference operator*() const { return value; }
  59. pointer operator->() const { return &value; }
  60. generator_iterator& operator++()
  61. {
  62. value = f();
  63. return *this;
  64. }
  65. generator_iterator operator++(int)
  66. {
  67. generator_iterator temp(*this);
  68. ++(*this);
  69. return temp;
  70. }
  71. bool operator==(const generator_iterator& other) const
  72. { return f == other.f; }
  73. bool operator!=(const generator_iterator& other) const
  74. { return !(*this == other); }
  75. private:
  76. F f;
  77. value_type value;
  78. };
  79. template<typename F>
  80. inline generator_iterator<F> make_generator_iterator(const F& f)
  81. { return generator_iterator<F>(f); }
  82. int test_main(int argc, char* argv[])
  83. {
  84. mpi::environment env(argc, argv);
  85. if (argc < 5) {
  86. std::cerr << "Usage: mesh_generator_test <x> <y> <toroidal> <emit dot file>\n";
  87. exit(-1);
  88. }
  89. int x(atoi(argv[1])), y(atoi(argv[2]));
  90. bool toroidal(argv[3] == std::string("true"));
  91. bool emit_dot_file(argv[4] == std::string("true"));
  92. typedef adjacency_list<listS,
  93. distributedS<mpi_process_group, vecS>,
  94. undirectedS> Graph;
  95. Graph g(mesh_iterator<Graph>(x, y, toroidal),
  96. mesh_iterator<Graph>(),
  97. x*y);
  98. synchronize(g);
  99. BGL_FORALL_VERTICES(v, g, Graph)
  100. if (toroidal)
  101. assert(out_degree(v, g) == 4);
  102. else
  103. assert(out_degree(v, g) >= 2 && out_degree(v, g) <= 4);
  104. if ( emit_dot_file )
  105. write_graphviz("mesh.dot", g);
  106. return 0;
  107. }