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