summaryrefslogtreecommitdiff
path: root/cacount.cpp
blob: 7b59563226e3c18771735d2eb99ae5e8e9cb7efc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#include <assert.h>
#include <stdlib.h>
#include <string.h>

#include <array>
#include <bitset>
#include <functional>
#include <iostream>
#include <list>
#include <unordered_map>
#include <unordered_set>
#include <thread>
#include <boost/integer.hpp>

#include "cachepad.hpp"
#include "mmalloc.hpp"
#include "timer.hpp"

using namespace std;
using boost::optional;

#ifndef BIT_WIDTH
#define BIT_WIDTH 16
#endif

const uint8_t logState = BIT_WIDTH;
typedef typename boost::uint_t<logState    >::least State;
typedef typename boost::uint_t<logState + 1>::least StateIter;
const StateIter numState  = (StateIter) 1 << logState;
const StateIter nullState = ~((StateIter) 0);
const State maxState = ~((State) 0);

bitset<8> rule(110);

State update(State s) {
  State r(0);
  bitset<logState> b(s);
  for (unsigned i=0; i<logState; i++)
    r |= rule[1 * b[(i + logState - 1) % logState]
	    + 2 * b[i]                
	    + 4 * b[(i + 1) % logState]] << i;
  return r;
}

typedef array<State,   numState> Trans;
typedef array<uint8_t, numState> pbitset;

bool iterStateP(function<void(State, bool&)> f, optional<string> msg = optional<string>(), bool parallel = false, bool skipWorkTest = false) {
  PerfPrinter perfPrinter(msg);
  int numThreads = 1;
  if (parallel)
    numThreads = min<uint64_t>(thread::hardware_concurrency(), numState);
  cache_pad<bool> *perThreadWorked = new cache_pad<bool>[numThreads];
  list<thread*> tasks;
  for (int t=0; t<numThreads; t++) {
    perThreadWorked[t]() = false;
    tasks.push_front(new thread([=]{
	  for (StateIter s = numState / numThreads * t;
	       s < ((t == numThreads - 1) ? numState : (numState / numThreads * (t+1)));
	       s++)
	    f(s, perThreadWorked[t]());
	}));
  }
  for (; !tasks.empty(); tasks.front()->join(), delete tasks.front(), tasks.pop_front());
  bool worked = skipWorkTest;
  for (int t=0; t<numThreads; t++)
    worked |= perThreadWorked[t];
  return worked;
}

bool iterState(function<void(State)> f, optional<string> msg = optional<string>(), bool parallel = false) {
  return iterStateP([=](State s, bool &) { f(s); }, msg, parallel, true);
}

void iterTransP(int times, function<void(State, bool&)> f, optional<string> msg = optional<string>(), bool parallel = false, bool skipWorkTest = false) {
  PerfPrinter perfPrinter(msg);
  auto msg2 = [=,&msg] (int i) {
    return msg ? (*msg + string(" ") + to_string(times-i) + string("/") + to_string(times)) : msg; };
  while (times--)
    if (not iterStateP(f, msg2(times), parallel, skipWorkTest)) return;
}

void iterTrans(int times, function<void(State)> f, optional<string> msg = optional<string>(), bool parallel = false) {
  iterTransP(times, [=](State s, bool &) { f(s); }, msg, parallel, true);
}

void init(Trans &t, Trans &c, pbitset &reachable) {
  iterState([&](State s) {
      t[s] = update(s);
      c[s] = s;
      reachable[t[s]] = 1;
    }, (string) "single step transition table", true);
}

void findCycle(Trans &t, Trans &c) {
  // forward to t=numState; now every state is in a cycle
  iterTransP(logState, [&](State s, bool &worked) {
      State n = t[s];
      t[s] = t[n];
      c[s] = min<State>(c[s], c[n]);
      if (n != t[s]) worked = true;
    }, (string) "fwd time", true);
  // Transients may have a cycle id (minimum state) that is on the
  // transient (not in the cycle) and thus different from the cycle id
  // in the cycle. Thus we copy the cycle id from a state in the cycle
  // to all states in the basin.
  iterState([&](State s) {
      c[s] = c[t[s]];
    }, (string) "fix transient cycle id", true);
}

State canonize(State s) {
  // TODO: test if a variant base on clz is faster
  State cs = s;
  for (int i=0; i<logState; i++)
    cs = min<State>(cs, (((s<<i) | (s>>(logState-i))) & maxState));
  return cs;
};

void cycleStat(Trans &t, Trans &c, pbitset &reachable) {
  struct Stat {
    State basin, len, eden;
    StateIter totalBasin;
    explicit Stat() : basin(0), len(0), eden(0), totalBasin(0) {}
  };
  unordered_map<State, Stat> cycStat;
  unordered_set<State> redCycleCounted;

  // Is this cycle the canonical one?
  // How big is the basin of attraction?
  // How many garden of eden states does it contain?
  { PerfPrinter perfPrinter((string) "cycle count");
  iterState([&](State s) {
    auto cyc = c[s];
    auto canonCyc = canonize(cyc);
    auto &stat = cycStat[canonCyc];
    stat.totalBasin++;
    if (cyc == canonCyc) {
      stat.basin++;
      if (!reachable[s])
	stat.eden++;
    }
  }, (string) "basin & eden"); }

  // how long is the cycle, what is the actual minimal state
  { PerfPrinter perfPrinter((string) "cycle length");
  for (auto &i : cycStat) {
    Stat &stat = i.second;
    State cur, start;
    cur = start = t[i.first];
    do {
      stat.len++;
      cur = update(cur);
    } while (cur != start);
    // TODO: parallelize loop
  }}

  // print it
  for (auto &i : cycStat) {
    auto &s = i.second;
    assert(s.totalBasin % s.basin == 0);
    cout << bitset<logState>(i.first) << "\t"
	 << (s.totalBasin / s.basin) << "\t"
	 << s.len << "\t"
	 << s.basin << "\t"
	 << s.eden  << "\t"
	 << i.first << endl;
  }
}

void print(Trans &t) {
  for (auto s : t)
    cout << bitset<logState>(s) << endl;
}

void printTraj(State s, int count) {
  while (count--) {
    cout << bitset<logState>(s) << endl;
    s = update(s);
  }
}

int main(int argc, char **argv) {
  assert(argc >= 2);
  rule = atoi(argv[1]);
  if (!strcmp(argv[2], "traj")) {
    assert(argc == 5);
    printTraj(atoi(argv[3]), atoi(argv[4]));
  }
  if (!strcmp(argv[2], "cycle")) {
    Trans *t, *c;
    pbitset *r;
    { PerfPrinter perfPrinter((string) "allocating memory");
      t = mmalloc<Trans>();
      c = mmalloc<Trans>();
      r = mmalloc<pbitset>();
    }
    init(*t, *c, *r);
    findCycle(*t, *c);
    cycleStat(*t, *c, *r);
  }
  return 0;
}
contact: Jan Huwald // Impressum