src/benchmark/hcube.cc
author alpar
Wed, 21 Jul 2004 17:39:49 +0000
changeset 722 be8712e1fe07
parent 711 b6c56353832c
child 729 a9b1c49440f7
permissions -rw-r--r--
For the sake of icc.
alpar@708
     1
// -*- mode:C++ -*-
alpar@708
     2
alpar@708
     3
#include<math.h>
alpar@708
     4
#include<hugo/list_graph.h>
alpar@708
     5
#include<hugo/smart_graph.h>
alpar@708
     6
#include<hugo/dijkstra.h>
alpar@718
     7
#include<../work/jacint/max_flow_no_stack.h>
alpar@711
     8
#include"bench_tools.h"
alpar@708
     9
alpar@708
    10
using namespace std;
alpar@708
    11
using namespace hugo;
alpar@708
    12
alpar@708
    13
template<class Graph>
alpar@708
    14
void addHiperCube(Graph &G,int dim,vector<typename Graph::Node> &nodes)
alpar@708
    15
{
alpar@708
    16
  GRAPH_TYPEDEF_FACTORY(Graph);
alpar@708
    17
  
alpar@708
    18
  vector<int> bits(dim+1);
alpar@708
    19
  bits[0]=1;
alpar@708
    20
  for(int i=1;i<=dim;i++) bits[i]=2*bits[i-1];
alpar@708
    21
  
alpar@708
    22
  for(int i=0;i<bits[dim];i++) {
alpar@708
    23
    nodes.push_back(G.addNode());
alpar@718
    24
    for(int j=0;j<dim;j++) if(i&bits[j]) G.addEdge(nodes[i-bits[j]],nodes[i]);
alpar@708
    25
  }
alpar@708
    26
}
alpar@708
    27
alpar@708
    28
template<class Graph>
alpar@708
    29
void addBiDirHiperCube(Graph &G,int dim,vector<typename Graph::Node> &nodes)
alpar@708
    30
{
alpar@708
    31
  GRAPH_TYPEDEF_FACTORY(Graph);
alpar@708
    32
  
alpar@708
    33
  vector<int> bits(dim+1);
alpar@708
    34
  bits[0]=1;
alpar@708
    35
  for(int i=1;i<=dim;i++) bits[i]=2*bits[i-1];
alpar@708
    36
  
alpar@708
    37
  for(int i=0;i<bits[dim];i++) {
alpar@708
    38
    nodes.push_back(G.addNode());
alpar@708
    39
    for(int j=0;j<dim;j++) if(i&bits[j]) {
alpar@708
    40
      G.addEdge(nodes[i-bits[j]],nodes[i]);
alpar@708
    41
      G.addEdge(nodes[i],nodes[i-bits[j]]);
alpar@708
    42
    }
alpar@708
    43
    
alpar@708
    44
  }
alpar@708
    45
}
alpar@708
    46
alpar@718
    47
inline int numOfOnes(int n,int dim)
alpar@718
    48
{
alpar@718
    49
  int s=0;
alpar@718
    50
  for(int i=0;i<dim;i++) {
alpar@718
    51
    s+=n%2;
alpar@718
    52
    n>>=1;
alpar@718
    53
  }
alpar@718
    54
  return s;
alpar@718
    55
}
alpar@718
    56
alpar@718
    57
inline int numOfZeros(int n,int dim)
alpar@718
    58
{
alpar@718
    59
  int s=dim;
alpar@718
    60
  for(int i=0;i<dim;i++) {
alpar@718
    61
    s-=n&1;
alpar@718
    62
    n>>=1;
alpar@718
    63
  }
alpar@718
    64
  return s;
alpar@718
    65
}
alpar@718
    66
alpar@708
    67
int main(int argc, char *argv[])
alpar@708
    68
{
alpar@708
    69
  //  typedef ListGraph Graph;
alpar@708
    70
  typedef SmartGraph Graph;
alpar@708
    71
alpar@708
    72
  ///\bug GRAPH_TYPEDEF_FACTORY(Graph);
alpar@708
    73
  GRAPH_TYPEDEF_FACTORY_NOTYPENAME(Graph);
alpar@708
    74
alpar@708
    75
  Graph G;
alpar@708
    76
  
alpar@708
    77
  Timer T;
alpar@708
    78
  
alpar@708
    79
  if(argc!=2) {
alpar@708
    80
    cout << "Usage: " << argv[0] << " dim\n";
alpar@708
    81
    return 1;
alpar@708
    82
  }
alpar@708
    83
  
alpar@708
    84
  int dim=atoi(argv[1]);
alpar@708
    85
  
alpar@718
    86
//   cout << "Creating Hipercube ("<< (1<<dim) << " nodes, "
alpar@718
    87
//        << dim*(1<<dim) << " edges):";
alpar@708
    88
alpar@718
    89
  T.reset();
alpar@708
    90
  vector<Node> nodes;
alpar@708
    91
  addBiDirHiperCube(G,dim,nodes);
alpar@718
    92
alpar@718
    93
  PrintTime("GENGRAPH",T);
alpar@718
    94
alpar@708
    95
  T.reset();
alpar@708
    96
  Graph::EdgeMap<int> map(G);
alpar@708
    97
  {
alpar@708
    98
    Primes P;
alpar@708
    99
    for(int i=0;i<dim*(1<<dim);i++) P();
alpar@708
   100
    
alpar@708
   101
    //  for(EdgeIt e(G);G.valid(e);G.next(e)) map[e]=P();
alpar@708
   102
    for(int i=0;i<dim*(1<<dim);i++)
alpar@708
   103
      //    map[Edge(((long long int)(i)*2987)%(dim*(1<<dim)))]=P();
alpar@708
   104
      map[Edge(((long long int)(i)*93505)%(dim*(1<<dim)))]=P();
alpar@718
   105
  
alpar@718
   106
//     for(int i=0;i<(1<<dim);i++) {
alpar@718
   107
//       int mul= (1<<(numOfZeros(i,dim)/4));
alpar@718
   108
//       for(OutEdgeIt e(G,nodes[i]);G.valid(e);G.next(e))
alpar@718
   109
// 	map[e]*=mul;
alpar@718
   110
//     }
alpar@708
   111
  }
alpar@708
   112
  
alpar@718
   113
  PrintTime("GENLENGTHS",T);
alpar@718
   114
alpar@708
   115
  T.reset();
alpar@708
   116
  {
alpar@708
   117
    Dijkstra<Graph> Dij(G,map);
alpar@708
   118
    Dij.run(nodes[0]);
alpar@708
   119
  }
alpar@718
   120
  PrintTime("DIJKSTRA",T);
alpar@718
   121
alpar@718
   122
  T.reset();
alpar@718
   123
  {
alpar@718
   124
   Graph::EdgeMap<int> flow(G);
alpar@708
   125
   
alpar@718
   126
    MaxFlowNoStack<Graph,int> MF(G,nodes[0],nodes[1<<dim-1],map,flow);
alpar@718
   127
    MF.run(MF.NO_FLOW);
alpar@718
   128
  }
alpar@718
   129
  PrintTime("PREFLOW",T);
alpar@718
   130
alpar@708
   131
}