src/benchmark/bfs-bench.cc
author marci
Mon, 23 Aug 2004 11:26:09 +0000
changeset 769 eb61fbc64c16
parent 742 235fd36336b7
child 774 4297098d9677
permissions -rw-r--r--
.
alpar@742
     1
// -*- mode:C++ -*-
alpar@742
     2
alpar@742
     3
#include<math.h>
alpar@742
     4
#include<hugo/list_graph.h>
alpar@742
     5
#include<hugo/smart_graph.h>
alpar@742
     6
#include<hugo/dijkstra.h>
alpar@742
     7
#include<hugo/max_flow.h>
alpar@742
     8
alpar@742
     9
#include"bench_tools.h"
alpar@742
    10
alpar@742
    11
using namespace std;
alpar@742
    12
using namespace hugo;
alpar@742
    13
alpar@742
    14
inline int numOfOnes(int n,int dim)
alpar@742
    15
{
alpar@742
    16
  int s=0;
alpar@742
    17
  for(int i=0;i<dim;i++) {
alpar@742
    18
    s+=n%2;
alpar@742
    19
    n>>=1;
alpar@742
    20
  }
alpar@742
    21
  return s;
alpar@742
    22
}
alpar@742
    23
alpar@742
    24
inline int numOfZeros(int n,int dim)
alpar@742
    25
{
alpar@742
    26
  int s=dim;
alpar@742
    27
  for(int i=0;i<dim;i++) {
alpar@742
    28
    s-=n&1;
alpar@742
    29
    n>>=1;
alpar@742
    30
  }
alpar@742
    31
  return s;
alpar@742
    32
}
alpar@742
    33
alpar@742
    34
template<class Graph>
alpar@742
    35
void bfsStlQueue(Graph &G,typename Graph::Node source)
alpar@742
    36
{
alpar@742
    37
  GRAPH_TYPEDEF_FACTORY(Graph);
alpar@742
    38
alpar@742
    39
  using namespace std;
alpar@742
    40
  
alpar@751
    41
  typename Graph::template NodeMap<bool> visited(G,false);
alpar@742
    42
    
alpar@742
    43
  queue<typename Graph::Node> Q;
alpar@742
    44
    
alpar@742
    45
  Q.push(source);
alpar@742
    46
  visited[source]=true;
alpar@742
    47
  do {
alpar@742
    48
    Node n(Q.front());
alpar@742
    49
    Node m;
alpar@742
    50
    Q.pop();
alpar@742
    51
    for(OutEdgeIt e(G,n);G.valid(e);G.next(e))
alpar@742
    52
      if(!visited[m=G.head(e)]) {
alpar@742
    53
	Q.push(m);
alpar@742
    54
	visited.set(m,true);
alpar@742
    55
      }
alpar@742
    56
  } while(!Q.empty());
alpar@742
    57
}
alpar@742
    58
alpar@742
    59
template<class Graph>
alpar@742
    60
void bfsOwnQueue(Graph &G,typename Graph::Node source)
alpar@742
    61
{
alpar@742
    62
  GRAPH_TYPEDEF_FACTORY(Graph);
alpar@742
    63
alpar@742
    64
  using namespace std;
alpar@742
    65
  
alpar@751
    66
  typename Graph::template NodeMap<bool> visited(G,false);
alpar@742
    67
  
alpar@742
    68
  int N=G.nodeNum();
alpar@742
    69
  vector<typename Graph::Node> Q(N);
alpar@742
    70
  int Qh=0;
alpar@742
    71
  int Qt=0;
alpar@742
    72
  
alpar@742
    73
alpar@742
    74
  Q[Qh++]=source;
alpar@742
    75
  visited.set(source,true);
alpar@742
    76
  do {
alpar@742
    77
    Node m;
alpar@742
    78
    Node n=Q[Qt++];
alpar@742
    79
    for(OutEdgeIt e(G,n);G.valid(e);G.next(e))
alpar@742
    80
      if(!visited[m=G.head(e)]) {
alpar@742
    81
	Q[Qh++]=m;
alpar@742
    82
	visited.set(m,true);
alpar@742
    83
      }
alpar@742
    84
  } while(Qt!=Qh);
alpar@742
    85
}
alpar@742
    86
alpar@751
    87
template<class Graph>
alpar@751
    88
void iteratorBench(Graph &G)
alpar@751
    89
{
alpar@751
    90
  GRAPH_TYPEDEF_FACTORY(Graph);
alpar@751
    91
alpar@751
    92
  int i=0;
alpar@751
    93
  
alpar@751
    94
  for(NodeIt n(G);G.valid(n);G.next(n))
alpar@751
    95
    for(OutEdgeIt e(G,n);G.valid(e);G.next(e))
alpar@751
    96
      i++;
alpar@751
    97
}
alpar@751
    98
alpar@742
    99
int main(int argc, char *argv[])
alpar@742
   100
{
alpar@742
   101
  //  typedef ListGraph Graph;
alpar@742
   102
  typedef SmartGraph Graph;
alpar@742
   103
alpar@742
   104
  ///\bug GRAPH_TYPEDEF_FACTORY(Graph);
alpar@742
   105
  GRAPH_TYPEDEF_FACTORY_NOTYPENAME(Graph);
alpar@742
   106
alpar@742
   107
  Graph G;
alpar@742
   108
  
alpar@742
   109
  Timer T;
alpar@742
   110
  
alpar@751
   111
  if(argc!=3) {
alpar@751
   112
    cout << "Usage: " << argv[0] << " dim mul\n";
alpar@742
   113
    return 1;
alpar@742
   114
  }
alpar@742
   115
  
alpar@742
   116
  int dim=atoi(argv[1]);
alpar@751
   117
  int mul=atoi(argv[2]);
alpar@742
   118
  
alpar@742
   119
//   cout << "Creating Hipercube ("<< (1<<dim) << " nodes, "
alpar@742
   120
//        << dim*(1<<dim) << " edges):";
alpar@742
   121
alpar@742
   122
  T.reset();
alpar@742
   123
  vector<Node> nodes;
alpar@742
   124
  addBiDirHiperCube(G,dim,nodes);
alpar@742
   125
alpar@742
   126
  PrintTime("GENGRAPH",T);
alpar@742
   127
alpar@742
   128
  T.reset();
alpar@742
   129
  {
alpar@751
   130
    for(int i=0;i<mul;i++)
alpar@742
   131
      bfsStlQueue(G,nodes[0]);
alpar@742
   132
  }
alpar@742
   133
  PrintTime("BFS-STL",T);
alpar@742
   134
  T.reset();
alpar@742
   135
  {
alpar@751
   136
    for(int i=0;i<mul;i++)
alpar@742
   137
      bfsOwnQueue(G,nodes[0]);
alpar@742
   138
  }
alpar@742
   139
  PrintTime("BFS-OWN",T);
alpar@751
   140
  T.reset();
alpar@751
   141
  {
alpar@751
   142
    for(int i=0;i<mul;i++)
alpar@751
   143
      iteratorBench(G);
alpar@751
   144
  }
alpar@751
   145
  PrintTime("ITERATE",T);
alpar@751
   146
alpar@751
   147
alpar@742
   148
}