// Copyright (C) 2004-2008 The Trustees of Indiana University.

// Use, modification and distribution is subject to the Boost Software
// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)

//  Authors: Douglas Gregor
//           Andrew Lumsdaine

// Example usage of breadth_first_search algorithm

// Enable PBGL interfaces to BGL algorithms
#include <boost/graph/use_mpi.hpp>

// Communicate via MPI
#include <boost/graph/distributed/mpi_process_group.hpp>

// Breadth-first search algorithm
#include <boost/graph/breadth_first_search.hpp>

// Distributed adjacency list
#include <boost/graph/distributed/adjacency_list.hpp>

// METIS Input
#include <boost/graph/metis.hpp>

// Graphviz Output
#include <boost/graph/distributed/graphviz.hpp>

// Standard Library includes
#include <fstream>
#include <string>
#include <boost/foreach.hpp>

#include <boost/graph/graph_utility.hpp>
#include <boost/property_map/parallel/distributed_property_map.hpp>

#ifdef BOOST_NO_EXCEPTIONS
void
boost::throw_exception(std::exception const& ex)
{
    std::cout << ex.what() << std::endl;
    abort();
}
#endif

using namespace boost;
using boost::graph::distributed::mpi_process_group;


template<typename DistanceMap>
struct bfs_discovery_visitor : bfs_visitor<> 
{
  bfs_discovery_visitor(DistanceMap distance) : distance(distance) 
  {
    set_property_map_role(vertex_distance, distance);
  }

  template<typename Edge, typename Graph>
  void tree_edge(Edge e, const Graph& g)
  {
    typename property_map<Graph, vertex_index_t>::type vidxx = get(vertex_index, g);
    typename graph_traits<Graph>::vertex_descriptor src = source(e,g);
    typename graph_traits<Graph>::vertex_descriptor des = target(e,g);
    
    std::size_t new_distance = get(distance, source(e, g)) + 1;
    std::cout<<"NowVisiting: ("<<owner(src)<<":"<<local(src)<<")--> ("<<owner(des)<<":"<<local(des)<<")"<<" distance "<<new_distance<<std::endl;
    put(distance, target(e, g), new_distance);
  }
  
private:
  DistanceMap distance;
};

typedef adjacency_list_traits<vecS, distributedS<mpi_process_group, vecS>, directedS >::vertex_descriptor vertex_descriptor;

/* An undirected graph with distance values stored on the vertices. */
typedef adjacency_list<vecS, distributedS<mpi_process_group, vecS>, directedS,
                       /*Vertex properties=*/property<vertex_distance_t, std::size_t>
		       // property<vertex_predecessor_t, vertex_descriptor>
		       >
  Graph;

int main(int argc, char* argv[])
{
  boost::mpi::environment env(argc,argv);

  // Parse command-line options
    
  int n = 8;
  Graph g(n);
  if(process_id(g.process_group()) == 0){

    std::vector<std::pair<int, int> > allEdges;
    allEdges.push_back(std::pair<int, int>(0,1));
    allEdges.push_back(std::pair<int, int>(1,2));
    allEdges.push_back(std::pair<int, int>(2,3));
    allEdges.push_back(std::pair<int, int>(0,4));
    allEdges.push_back(std::pair<int, int>(4,3));
    
    std::vector<std::pair<int, int> >::iterator git = allEdges.begin();
    for( ; git != allEdges.end(); git++)
      {
	add_edge(vertex(git->first, g), vertex(git->second, g), g);
      }
    
    //   print_graph(g);
  }

  synchronize(g.process_group());
  typedef  boost::graph::parallel::process_group_type<Graph>::type process_group_type;
  typedef property_map<Graph, vertex_distance_t>::type VertexDistanceMap;
  typedef graph_traits<Graph>::vertex_descriptor key_type;
  typedef property_map<Graph, vertex_index_t>::type VertexIndexMap;
  property_map<Graph, vertex_distance_t>::type distance =
    get(vertex_distance, g);
  property_map<Graph, vertex_global_t>::type vertex_global_map = get(vertex_global, g); 

  //  property_map<Graph, vertex_predecessor_t>::type predecessor =
  //  get(vertex_predecessor, g);
  
  typedef graph_traits<Graph>::vertex_descriptor Vertex;
  typedef Graph::local_vertex_descriptor local_vertex;

  
  std::map<local_vertex, Vertex> pred_map_gd;
  boost::associative_property_map< std::map< local_vertex, Vertex> >
    pred_pmap_gd(pred_map_gd);
 
  

  // Get vertex 0 in the graph
  graph_traits<Graph>::vertex_descriptor start = vertex(0, g);

  // Compute BFS levels from vertex 0
  

  
  property_map<Graph, vertex_index_t>::type vidx = get(vertex_index, g);

  put(distance, start, 0);
  
  distance.set_consistency_model(boost::parallel::cm_forward);
  
   boost::breadth_first_search
     (g, start, 
      boost::visitor
      (
       boost::make_bfs_visitor
       (
	std::make_pair
	(
	 boost::record_distances(distance, boost::on_tree_edge()),
	 boost::record_predecessors(boost::parallel::make_distributed_property_map(g.process_group(), vertex_global_map, pred_pmap_gd ), boost::on_tree_edge())
	 )
	)
       )
      );
   
   
    synchronize(g.process_group());
    synchronize(g.process_group());
    synchronize(g.process_group());
    synchronize(g.process_group());
    //synchronize(distance);
      

  
  
  
  typedef  property_map<Graph, vertex_index_t>::const_type
    VertexIndexMap;
  typedef  property_map<Graph, vertex_global_t>::const_type
    VertexGlobalMap;
  
  if(process_id(g.process_group()) == 0){
  
    using boost::graph::parallel::process_group;
    process_group_type pg = process_group(g);
    std::cout<<"Finished Parallel BFS "<<std::endl;
    std::cout<<"Node ID    :  Level "<<std::endl;
    for(int i = 0; i < n; i++)
      {
	graph_traits<Graph>::vertex_descriptor vtx = vertex(i, g);
	std::cout<<i<<"  :  "<<get(distance, vtx)<<" "<<std::endl;
	
      }
  }

  std::string outfile("edges.dot");
  //write_graphviz(outfile, g, make_label_writer(distance));

  //synchronize(distance);
 synchronize(g.process_group());
 synchronize(g.process_group());
 synchronize(g.process_group());

  return 0;
}
