Standalone-Simulation

For stand-alone execution of simulation models, adevs provides the devssim class. This class is a wrapper around the basic Parallel DEVS simulation objects (see the pdevs.h header) and provides some simple options for driving your simulation. The most direct means of starting a simulation is to create a devssim object, provided with a model via the constructor, and then call its run method.

int main()
{
     myModel model;
     devssim sim(&model);
     sim.run();
     return 0;
}

This code snippet will simulate the model until its time advance is INFINITY. That is, until the model is passive. Optionally, an end time could be passed to the run method which would cause the simulation to halt when the time of next event is larger than the requested termination time.

The devvsim class can be sub-classed to provide specialized behavior. The class has two virtual member functions that can be overridden. The halt() method is called at the end of every simulation cycle. It returns true if the simulation has reached a stopping condition and false otherwise. When the halt() method returns true, the simulation stops and the run() method will return. The other virtual method is the reset() member function. The default implementation causes each model’s init() function to be called with the time t0. Simulation classes derived from devssim should always call the base devssim reset() method as part of any override of the reset() method. An example of a specialized simulation driver is shown below.

class sim: public devssim 
{
     public:
          sim(myModel* model, int count):
          devssim(model),
          model_(model),
          count_(count),
          cycles_(0)
          {}
          // Overrides the halt method from devssim.
          bool halt()
          {
               return (model_->stopSimulation() || cycles_++ > count_);
          } 
          // Resets the simulator. 
          void reset(stime_t t0 = 0)
          {
               devssim::reset(t0);
               cycles_ = 0;
          }
          ~sim(){}
     private:   
          myModel* model_;
          int count_;
          int cycles_;
};

This sim class will run until the model indicates that it has reached a stopping point or the number of simulation cycles exceeds some count. The next snippet shows how this new simulation class might be used. The only significant change from the use of the devssim class is in the constructor call.

int main()
{
     myModel model;
     sim sim_(&model,1000);
     sim_.run();
     return 0;
}

Parallel Simulation

Adevs has support for parallel simulation using POSIX threads. This feature is enabled by compiling the adevs library with the USE_THREADS option defined. Adevs uses a direct implementation of the Parallel DEVS algorithm in which simultaneous state changes, output functions, and (this is adevs specific) the end of cycle function being computed in parallel. To use the parallel simulation feature you must ensure that your state transition functions, output functions, and end of cycle functions can execute simultaneously without breaking your simulation program. To use this feature effectively, you will need a simulation that has relatively compute intensive state changes, output functions, or end of cycle methods and relatively large numbers of models that undergo simultaneous state changes.

Adevs maintains a pool of threads that are used to compute state changes, output functions, and execute the end of cycle methods of models. The number of threads in the pool will grow so long as the simulation engine is able to generate tasks (i.e., models undergoing state changes) faster than the thread pool can complete the tasks assigned to it. In most applications, the size of the thread pool will grow to some small number that can be kept busy by the simulation engine. However, adevs does not place an upper limit on the size of the thread pool. This can be done by using the method adevs_threadpool::set_thread_limit(int limit), where the limit argument indicates that maximum number of threads that can be created by the simulation engine. This method is only available if adevs was compiled with the USE_THREADS option set.