distributed_graph_coloring_test.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // Copyright (C) 2005, 2006 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. #define PBGL_ACCOUNTING
  8. #include <boost/graph/use_mpi.hpp>
  9. #include <boost/config.hpp>
  10. #include <boost/throw_exception.hpp>
  11. #include <boost/serialization/vector.hpp>
  12. #include <boost/graph/distributed/boman_et_al_graph_coloring.hpp>
  13. #include <boost/graph/distributed/mpi_process_group.hpp>
  14. #include <boost/lexical_cast.hpp>
  15. #include <boost/graph/parallel/distribution.hpp>
  16. #include <boost/graph/erdos_renyi_generator.hpp>
  17. #include <boost/graph/distributed/adjacency_list.hpp>
  18. #include <boost/graph/graphviz.hpp>
  19. #include <iostream>
  20. #include <boost/random.hpp>
  21. #include <boost/test/minimal.hpp>
  22. #ifdef BOOST_NO_EXCEPTIONS
  23. void
  24. boost::throw_exception(std::exception const& ex)
  25. {
  26. std::cout << ex.what() << std::endl;
  27. abort();
  28. }
  29. #endif
  30. using namespace boost;
  31. using boost::graph::distributed::mpi_process_group;
  32. void
  33. test_distributed_graph_coloring(int n, double p, int s,
  34. int seed, bool emit_dot_file)
  35. {
  36. typedef adjacency_list<listS,
  37. distributedS<mpi_process_group, vecS>,
  38. undirectedS> Graph;
  39. typedef property_map<Graph, vertex_index_t>::type vertex_index_map;
  40. // Build a random number generator
  41. minstd_rand gen;
  42. gen.seed(seed);
  43. // Build a random graph
  44. Graph g(erdos_renyi_iterator<minstd_rand, Graph>(gen, n, p),
  45. erdos_renyi_iterator<minstd_rand, Graph>(),
  46. n);
  47. // Set up color map
  48. std::vector<int> colors_vec(num_vertices(g));
  49. iterator_property_map<int*, vertex_index_map> color(&colors_vec[0],
  50. get(vertex_index, g));
  51. // Run the graph coloring algorithm
  52. graph::boman_et_al_graph_coloring(g, color, s);
  53. if (process_id(g.process_group()) == 0) {
  54. graph::distributed::boman_et_al_graph_coloring_stats.print(std::cout);
  55. }
  56. if ( emit_dot_file ) {
  57. if ( process_id(g.process_group()) == 0 ) {
  58. for (int i = 0; i < n; ++i)
  59. get(color, vertex(i, g));
  60. synchronize(color);
  61. } else {
  62. synchronize(color);
  63. }
  64. write_graphviz("coloring.dot", g, paint_by_number(color));
  65. }
  66. }
  67. int test_main(int argc, char* argv[])
  68. {
  69. mpi::environment env(argc, argv);
  70. int n = 1000;
  71. double p = 0.01;
  72. int s = 100;
  73. int seed = 1;
  74. bool emit_dot_file = false;
  75. if (argc > 1) n = lexical_cast<int>(argv[1]);
  76. if (argc > 2) p = lexical_cast<double>(argv[2]);
  77. if (argc > 3) s = lexical_cast<int>(argv[3]);
  78. if (argc > 4) seed = lexical_cast<int>(argv[4]);
  79. if (argc > 5) emit_dot_file = lexical_cast<bool>(argv[5]);
  80. test_distributed_graph_coloring(n, p, s, seed, emit_dot_file);
  81. return 0;
  82. }