File: SyncStates.C

package info (click to toggle)
maria 1.3.5-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,068 kB
  • sloc: cpp: 43,408; yacc: 8,080; ansic: 436; sh: 404; lisp: 395; makefile: 228; perl: 21
file content (442 lines) | stat: -rw-r--r-- 13,206 bytes parent folder | download | duplicates (6)
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
// Collection of synchronisation states -*- c++ -*-

#ifdef __GNUC__
# pragma implementation
#endif // __GNUC__
#include "SyncStates.h"
#include "Net.h"
#include "Transition.h"
#include "GlobalMarking.h"
#include "StateReporter.h"

#ifdef ADD_CACHE
# include "FullSet.h"
#endif // ADD_CACHE

/** @file SyncStates.C
 * Collection of synchronisation states for modular analysis
 */

/* Copyright  2002-2003 Marko Mkel (msmakela@tcs.hut.fi).

   This file is part of MARIA, a reachability analyzer and model checker
   for high-level Petri nets.

   MARIA is free software; you can redistribute it and/or modify it
   under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2, or (at your option)
   any later version.

   MARIA is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   The GNU General Public License is often shipped with GNU software, and
   is generally kept in a file called COPYING or LICENSE.  If you do not
   have a copy of the license, write to the Free Software Foundation,
   59 Temple Place, Suite 330, Boston, MA 02111 USA. */

SyncStates::SyncStates (const class Net& net) :
  myNumModules (net.getNumChildren ()),
  myTransitionMap (),
#ifdef SYNC_CACHE
  myStateMap (new StateMap[myNumModules]),
  myStateSet (0), myStateIndex (new unsigned[myNumModules]),
  myStateUsed (0), myStateAlloc (0)
#else // SYNC_CACHE
  myStateSet (0), myEnabled (0)
#endif // SYNC_CACHE
#ifdef ADD_CACHE
  , mySync (net.getParent () ? 0 : new class FullSet ())
#endif // ADD_CACHE
{
  assert (myNumModules > 0);
  unsigned i = net.getNumTransitions (), numLabels = 0;
  while (i--) {
    const class Transition& t = net.getTransition (i);
    if (t.getNumChildren ())
      const_cast<TransitionMap&>(myTransitionMap).insert
	(TransitionMap::value_type (&t, numLabels++));
  }
#ifdef SYNC_CACHE
  for (i = myNumModules; i--; )
    myStateIndex[i] = UINT_MAX;
#else // SYNC_CACHE
  *const_cast<StateSet***>(&myStateSet) =
    new StateSet*[myNumModules * numLabels];
  memset (myStateSet, 0, myNumModules * numLabels * sizeof *myStateSet);
  *const_cast<bool**>(&myEnabled) = new bool[numLabels];
  memset (myEnabled, 0, numLabels * sizeof *myEnabled);
#endif // SYNC_CACHE
#ifdef ADD_CACHE
  if (mySync && !mySync->init (false))
    assert (false);
#endif // ADD_CACHE
}

SyncStates::~SyncStates ()
{
#ifdef SYNC_CACHE
  for (StateMap* sm = myStateMap + myNumModules; sm-- > myStateMap; )
    for (StateMap::iterator si = sm->begin (); si != sm->end (); si++)
      delete[] si->first;
  delete[] myStateMap;
  if (myStateSet) {
    for (unsigned i = myStateUsed; i--; ) {
      if (!myStateSet[i])
	continue;
      for (StateSet::iterator j = myStateSet[i]->begin ();
	   j != myStateSet[i]->end (); j++)
	delete[] *j;
      delete myStateSet[i];
    }
    delete[] myStateSet;
  }
  else
    assert (!myStateUsed);
  delete[] myStateIndex;
#else // SYNC_CACHE
  for (unsigned i = myNumModules * myTransitionMap.size (); i--; ) {
    if (!myStateSet[i])
      continue;
    for (StateSet::iterator j = myStateSet[i]->begin ();
	 j != myStateSet[i]->end (); j++)
      delete[] *j;
    delete myStateSet[i];
  }
  delete[] myStateSet;
  delete[] myEnabled;
#endif // SYNC_CACHE
#ifdef ADD_CACHE
  delete mySync;
#endif // ADD_CACHE
}

/** Copy a state
 * @param buf	the state
 * @param size	length of the state in words
 * @return	a copy of the state, tagged with its length
 */
static word_t*
copy (const word_t* buf,
      word_t size)
{
  word_t* s = new word_t[size + 1];
  *s = size;
  memcpy (s + 1, buf, size * sizeof *buf);
  return s;
}

#ifdef SYNC_CACHE
bool
SyncStates::lookup (unsigned module,
		    const word_t* buf,
		    word_t size)
{
  word_t* state = ::copy (buf, size);
  std::pair<StateMap::iterator,bool> p =
    myStateMap[module].insert (StateMap::value_type (state, myStateUsed));
  if (!p.second) {
    delete[] state;
    myStateIndex[module] = p.first->second;
    return true;
  }
  // new source state: add it to the cache
  myStateIndex[module] = myStateUsed;
  myStateUsed += myTransitionMap.size ();
  // expand myStateSet[] if needed
  if (myStateAlloc < myStateUsed) {
    unsigned ssize = myStateAlloc ? myStateAlloc << 1 : 1;
    while (ssize < myStateUsed) ssize <<= 1;
    StateSet** s = new StateSet*[ssize];
    memcpy (s, myStateSet, myStateAlloc * sizeof *s);
    memset (s + myStateAlloc, 0, (ssize - myStateAlloc) * sizeof *s);
    delete[] myStateSet;
    myStateSet = s;
    myStateAlloc = ssize;
  }
  return false;
}
#else // SYNC_CACHE
void
SyncStates::cleanup ()
{
  memset (myEnabled, 0, myTransitionMap.size () * sizeof *myEnabled);
  for (unsigned i = myNumModules * myTransitionMap.size (); i--; ) {
    if (myStateSet[i]) {
      for (StateSet::iterator j = myStateSet[i]->begin ();
	   j != myStateSet[i]->end (); j++)
	delete[] *j;
      delete myStateSet[i];
      myStateSet[i] = 0;
    }
  }
}
#endif // SYNC_CACHE

void
SyncStates::add (const class Transition& transition,
		 const word_t* buf,
		 word_t size)
{
  assert (transition.getNumParents ());
  assert (&transition.getNet ()->getParent ()->getChild
	  (transition.getNet ()->getParentIndex ()) == transition.getNet ());
  assert (transition.getNet ()->getParentIndex () < myNumModules);

  for (unsigned parent = transition.getNumParents (); parent--; ) {
    TransitionMap::const_iterator i =
      myTransitionMap.find (&transition.getParent (parent));
    assert (i != myTransitionMap.end ());
#ifdef SYNC_CACHE
    StateSet*& s =
      myStateSet[myStateIndex[transition.getNet ()->getParentIndex ()] +
		 i->second];
#else // SYNC_CACHE
    const unsigned ti = i->second;
    myEnabled[ti] = true;
    StateSet*& s =
      myStateSet[transition.getNet ()->getParentIndex () + myNumModules * ti];
#endif // SYNC_CACHE
    if (!s)
      s = new StateSet ();
    // for simplicity, copy the state for each label the transition exports
    word_t* state = ::copy (buf, size);
    std::pair<StateSet::iterator,bool> p = s->insert (state);
    if (!p.second)
      delete[] state;
  }
}

/** Synchronise on a label
 * @param child		number of the child transition being checked
 * @param transition	the synchronisation label
 * @param ss		the local synchronisation states
 * @param ssi		base indexes to ss[], indexed by module
 * @param ti		index number of the synchronisation label
 * @param m		the source marking
 * @param tibits	number of bits for representing sync labels
 * @param syncset	set of already encountered sync states
 * @param buf		buffer for encoding states
 * @param reporter	the interface for reporting successor states
 */
static void
sync (unsigned child,
      const class Transition& transition,
      SyncStates::StateSet** ss,
#ifdef SYNC_CACHE
      const unsigned* ssi,
      unsigned ti,
#elif defined ADD_CACHE
      unsigned ti,
#endif // SYNC_CACHE
      class GlobalMarking& m,
#ifdef ADD_CACHE
      unsigned tibits,
      class FullSet* syncset,
#endif // ADD_CACHE
      class BitPacker& buf,
      class StateReporter& reporter)
{
  if (child < transition.getNumChildren ()) {
    const class Net& net = *transition.getChild (child).getNet ();
    assert (&net.getParent ()->getChild (net.getParentIndex ()) == &net);

#ifdef SYNC_CACHE
    if (ssi[net.getParentIndex ()] == UINT_MAX);
    else if (const SyncStates::StateSet* sset =
	     ss[ssi[net.getParentIndex ()] + ti])
#else // SYNC_CACHE
    if (const SyncStates::StateSet* sset = ss[net.getParentIndex ()])
#endif // SYNC_CACHE
    {
      for (SyncStates::StateSet::const_iterator i = sset->begin ();
	   i != sset->end (); i++) {
	m.decode (net, (*i) + 1);
	sync (child + 1, transition, ss,
#ifdef SYNC_CACHE
	      ssi, ti,
#elif defined ADD_CACHE
	      ti,
#endif // SYNC_CACHE
	      m,
#ifdef ADD_CACHE
	      tibits, syncset,
#endif // ADD_CACHE
	      buf, reporter);
      }
    }
  }
  else {
#ifdef ADD_CACHE
    if (syncset) {
      buf.clear ();
      if (!m.encode (buf, *transition.getNet ()->getInitMarking (), 0))
	assert (false); // rejected source state
      buf.append (ti, tibits);
      buf.deflate ();
      if (!syncset->add (buf.getBuf (), buf.getNumBytes ())) {
	reporter.enabled (transition);
	return; // this transition has been analysed in the state
      }
      else {
	// we do not need a search queue: we just want to eliminate duplicates
	size_t size;
	delete[] syncset->pop (false, size);
	assert (!syncset->pop (false, size));
      }
    }
#endif // ADD_CACHE
    buf.clear ();
    if (!m.encode (buf, *transition.getNet ()->getInitMarking (), 0))
      assert (false); // rejected source state
    word_t* s = new word_t[buf.getNumWords ()];
    memcpy (s, buf.getBuf (), buf.getNumWords () * sizeof *s);
    reporter.setSyncSource (s, buf.getNumBytes ());
    transition.analyze (m, reporter);
  }
}

void
SyncStates::sync (const class GlobalMarking& m,
		  const class Transition& transition,
		  class StateReporter& reporter) const
{
  assert (transition.getNumChildren () > 0);
  assert (myTransitionMap.find (&transition) != myTransitionMap.end ());
  class GlobalMarking succ (m);
  class BitPacker buf;
#ifndef SYNC_CACHE
  if (!myEnabled[myTransitionMap.find (&transition)->second])
    return;
#endif // SYNC_CACHE
  ::sync (0, transition,
#ifdef SYNC_CACHE
	  myStateSet, myStateIndex,
	  myTransitionMap.find (&transition)->second,
#else // SYNC_CACHE
	  myStateSet + myNumModules *
	  myTransitionMap.find (&transition)->second,
# ifdef ADD_CACHE
	  myTransitionMap.find (&transition)->second,
# endif // ADD_CACHE
#endif // SYNC_CACHE
	  succ,
#ifdef ADD_CACHE
	  mySync ? ::log2 (myTransitionMap.size ()) : 0, mySync,
#endif // ADD_CACHE
	  buf, reporter);
}

#ifdef EXPR_COMPILE
# include "Compilation.h"

/** Synchronise on a label
 * @param child		number of the child transition being checked
 * @param transition	the synchronisation label
 * @param ss		the local synchronisation states
 * @param ssi		base indexes to ss[], indexed by module
 * @param ti		index number of the synchronisation label
 * @param tibits	number of bits for representing sync labels
 * @param syncset	set of already encountered sync states
 * @param reporter	the interface for reporting successor states
 * @param srcs		encoded deflated states (for restoring the state)
 */
static void
sync (unsigned child,
      const class Transition& transition,
      SyncStates::StateSet** ss,
#ifdef SYNC_CACHE
      const unsigned* ssi,
      unsigned ti,
#elif defined ADD_CACHE
      unsigned ti,
#endif // SYNC_CACHE
#ifdef ADD_CACHE
      unsigned tibits,
      class FullSet* syncset,
#endif // ADD_CACHE
      class StateReporter& reporter,
      word_t** srcs)
{
  if (child < transition.getNumChildren ()) {
    const class Net& net = *transition.getChild (child).getNet ();
    assert (&net.getParent ()->getChild (net.getParentIndex ()) == &net);

#ifdef SYNC_CACHE
    if (ssi[net.getParentIndex ()] == UINT_MAX);
    else if (const SyncStates::StateSet* sset =
	     ss[ssi[net.getParentIndex ()] + ti])
#else // SYNC_CACHE
    if (const SyncStates::StateSet* sset = ss[net.getParentIndex ()])
#endif // SYNC_CACHE
    {
      for (SyncStates::StateSet::const_iterator i = sset->begin ();
	   i != sset->end (); i++) {
	reporter.myCompilation->stateDecode (net.getIndex (), (*i) + 1, 0);
	sync (child + 1, transition, ss,
#ifdef SYNC_CACHE
	      ssi, ti,
#elif defined ADD_CACHE
	      ti,
#endif // SYNC_CACHE
#ifdef ADD_CACHE
	      tibits, syncset,
#endif // ADD_CACHE
	      reporter, srcs);
      }
      reporter.myCompilation->stateDecode (net.getIndex (),
					   srcs[net.getParentIndex ()], 0);
    }
  }
  else {
#ifdef ADD_CACHE
    if (syncset) {
      unsigned bytes;
      const void* buf = reporter.myCompilation->stateProject
	(transition.getNet ()->getIndex (), ti, tibits, bytes);
      if (!syncset->add (buf, bytes)) {
	reporter.enabled (transition);
	return; // this transition has been analysed in the state
      }
      else {
	// we do not need a search queue: we just want to eliminate duplicates
	size_t size;
	delete[] syncset->pop (false, size);
	assert (!syncset->pop (false, size));
      }
    }
#endif // ADD_CACHE
    reporter.sync (transition);
  }
}

void
SyncStates::sync (const class Transition& transition,
		  class StateReporter& reporter,
		  word_t** srcs) const
{
  assert (transition.getNumChildren () > 0);
  assert (myTransitionMap.find (&transition) != myTransitionMap.end ());
#ifndef SYNC_CACHE
  if (!myEnabled[myTransitionMap.find (&transition)->second])
    return;
#endif // SYNC_CACHE
  ::sync (0, transition,
#ifdef SYNC_CACHE
	  myStateSet, myStateIndex,
	  myTransitionMap.find (&transition)->second,
#else // SYNC_CACHE
	  myStateSet + myNumModules *
	  myTransitionMap.find (&transition)->second,
# ifdef ADD_CACHE
	  myTransitionMap.find (&transition)->second,
# endif // ADD_CACHE
#endif // SYNC_CACHE
#ifdef ADD_CACHE
	  mySync ? ::log2 (myTransitionMap.size ()) : 0, mySync,
#endif // ADD_CACHE
	  reporter, srcs);
}
#endif // EXPR_COMPILE