1 // Copyright (C) 2005, 2006 The Trustees of Indiana University.
3 // Use, modification and distribution is subject to the Boost Software
4 // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
7 // Authors: Douglas Gregor
9 #define PBGL_ACCOUNTING
11 #include <boost/graph/use_mpi.hpp>
12 #include <boost/config.hpp>
13 #include <boost/throw_exception.hpp>
14 #include <boost/graph/distributed/boman_et_al_graph_coloring.hpp>
15 #include <boost/graph/distributed/mpi_process_group.hpp>
16 #include <boost/lexical_cast.hpp>
17 #include <boost/graph/parallel/distribution.hpp>
18 #include <boost/graph/erdos_renyi_generator.hpp>
19 #include <boost/graph/distributed/adjacency_list.hpp>
20 #include <boost/graph/graphviz.hpp>
22 #include <boost/random.hpp>
23 #include <boost/test/minimal.hpp>
25 #ifdef BOOST_NO_EXCEPTIONS
27 boost::throw_exception(std::exception const& ex)
29 std::cout << ex.what() << std::endl;
34 using namespace boost;
35 using boost::graph::distributed::mpi_process_group;
38 test_distributed_graph_coloring(int n, double p, int s,
39 int seed, bool emit_dot_file)
41 typedef adjacency_list<listS,
42 distributedS<mpi_process_group, vecS>,
45 typedef property_map<Graph, vertex_index_t>::type vertex_index_map;
47 // Build a random number generator
51 // Build a random graph
52 Graph g(erdos_renyi_iterator<minstd_rand, Graph>(gen, n, p),
53 erdos_renyi_iterator<minstd_rand, Graph>(),
57 std::vector<int> colors_vec(num_vertices(g));
58 iterator_property_map<int*, vertex_index_map> color(&colors_vec[0],
59 get(vertex_index, g));
61 // Run the graph coloring algorithm
62 graph::boman_et_al_graph_coloring(g, color, s);
64 if (process_id(g.process_group()) == 0) {
65 graph::distributed::boman_et_al_graph_coloring_stats.print(std::cout);
68 if ( emit_dot_file ) {
69 if ( process_id(g.process_group()) == 0 ) {
70 for (int i = 0; i < n; ++i)
71 get(color, vertex(i, g));
77 write_graphviz("coloring.dot", g, paint_by_number(color));
81 int test_main(int argc, char* argv[])
83 mpi::environment env(argc, argv);
89 bool emit_dot_file = false;
91 if (argc > 1) n = lexical_cast<int>(argv[1]);
92 if (argc > 2) p = lexical_cast<double>(argv[2]);
93 if (argc > 3) s = lexical_cast<int>(argv[3]);
94 if (argc > 4) seed = lexical_cast<int>(argv[4]);
95 if (argc > 5) emit_dot_file = lexical_cast<bool>(argv[5]);
97 test_distributed_graph_coloring(n, p, s, seed, emit_dot_file);