1 / 183

INTRODUCTION TO CSIM 19 FOR C++ PROGRAMMERS

Mesquite Corporation. INTRODUCTION TO CSIM 19 FOR C++ PROGRAMMERS. http://www.mesquite.com/documentation/index.htm#startcpp. What is CSIM?. Simulation language developed by Mesquite Software in Austin, TX C/C++ based Has now a Java-based version

susane
Download Presentation

INTRODUCTION TO CSIM 19 FOR C++ PROGRAMMERS

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Mesquite Corporation INTRODUCTION TO CSIM 19 FOR C++ PROGRAMMERS http://www.mesquite.com/documentation/index.htm#startcpp

  2. What is CSIM? Simulation language developed byMesquite Software in Austin, TX C/C++ based Has now a Java-based version Allows user to create process-oriented, discrete-event simulation models Implemented as a library No need to learn a new language

  3. OUR FIRST CSIM PROGRAM

  4. A very simple example (I)‏ M/M/1 server with a FCFS queue Input parameters are Time intervals between arrivals Service times Server

  5. A very simple example (II)‏ We want to know Average customer response time:time of arrival to time of departure Customer throughput rate:customers served per unit time Server utilization:Fraction of elapsed time the server is busy Average queue length:number of customers at the facility

  6. The sim process #include <cpp.h> // CSIM C++ header file facility *f; // the service center extern "C" void sim() { // sim process create("sim"); // make this a processf = new facility("f"); // create service center fwhile (simtime() < 5000.0) { hold(exponential(1.0)); // delay customer(); // generate new customer } // while loopreport(); // output results }// sim

  7. The customer process void customer() {create("customer"); // make this a process f->use(exponential(0.5)); // get service } // customer Advantages of the approach • We can describe system through actions taken by each customer • Facility f manages its own queue

  8. Output CSIM Simulation Report (Version 19 for MSVC++)  Mon May 13 13:42:39 1996 Ending simulation time:10001.909  Elapsed simulation time:10001.909 CPU time used (seconds):0.490  FACILITY SUMMARY facility service service queue response compl name disc time util through-put length time count f fcfs 1.00954 0.512 0.506801. 019832.01229 5069 This is the default output. We can request much more specific data

  9. Program analysis: processes Two processes sim: the main process customer: describes customer behavior A CSIM process is a C++ function which executes the "create" statement Establishes the procedure as an independent, ready-to-run process, Returns control to the calling process

  10. Program analysis: facilities One facility f: the service center A facility is Declared with a “facility" declaration Initialized by the "facility()" function To request service from a facility f, process can call “f->use(Delta_t)”where “Delta_t” is the request duration

  11. Program analysis: simtime()‏ A global “clock” variable keeps track of the simulated time Double-precision floating point variable Value can be accessed through “simtime()” function

  12. Program analysis: hold()‏ “hold()” function causes time to pass for the process that calls it Deals with simulated time Do not use “sleep()”

  13. Program analysis: exponential()‏ “exponential(Delta_t)” function returns a value for a exponential random variable with mean “Delta_t” Many other functions of this type

  14. CSIM PROCESSES

  15. Processes Model the active elements of a system Created through a call to “create()” Can have multiple instances of the same process: while (simtime() < 5000.0) { hold(exponential(1.0)); // delay customer(); // generate new customer } // while

  16. Process attributes Each instance of a CSIM process has: Its own internal state A unique process id A process priority One of the following external states: Executing Waiting-to-execute Holding (for some time interval)‏ Waiting (for some event to occur)‏

  17. Warning CSIM processes look very much like conventional OS processes They are different: One OS process per CSIM simulation CSIM processes run within that process CSIM processes use simulated time Not physical time

  18. CSIM process creation When a function calls create()‏ CSIM creates a process control block (pcb) for the new process and puts it on the “next event list” Control is returned immediately to the process which invoked the function

  19. CSIM process scheduling Non-preemptive A new process will execute only after the currently running process leaves the running state by Executing a hold()‏ Going through a wait Terminating

  20. CSIM FACILITIES

  21. Facilities We can define Single server facilities:Can service one process at a time Multi-server facilities:Can service several processes at a time and have a single queue Arrays of single server facilities:each with its own queue

  22. Facility scheduling By default, a facility services processes in priority order Where several processes have the same priority, they will be served on FCFS basis Other service disciplines can be specified

  23. Example:single-server facility (I)‏ facility *ss; // declare facility ... ss = new facility("sngle srvr");// initialize ... ss->use(Delta_t); // use ss for duration Delta_t

  24. Example:single-server facility (II)‏ s->reserve(); // reserve facility ... hold(Delta_t); ... s->release(); // release facility ... Using a reserve/release pair lets us keep separate counts of time spent by customers (a) in queue and (b) being serviced

  25. Example:facility with three servers All three servers share the same queue const long NSERV = 3; // 3 servers ... facility_ms *ms; // declare facility ms = new facility_ms("multi", NSERV); ... ms->use(service_time); ....

  26. The post office problem revisited (I) #include <cpp.h> // CSIM C++ header file facility_ms *po; // the service center extern "C" void sim() { // sim process create("sim"); // make it a processpo = new facility_ms(“po“, 2); // create powhile (simtime() < 5000.0) { hold(exponential(1.0)); // delay customer(); // generate new customer } // while loopreport(); // output results }// sim

  27. The post office problem revisited (II) void customer() {create("customer"); // make it a process po->use(exponential(0.5)); // get service } // customer

  28. Example:array of single-server facilities Each facility now has its own queue const long NFACS = 10; facility_set *fa; // declare array fa = new facility_set("facs", NFACS); ... i = random(0, NFACS - 1); // pick one (*fa)[i].use(service_time); //use facility[i] Note the (*fa)[i]. without “->”

  29. Example:setting a maximum wait time const double MAXWAIT = 5.0; ... st = ss->timed_reserve(MAXWAIT); if (st < MAXWAIT) { // success hold(service_time); ss->release(); // done } else { //request timed out ... }

  30. Example:allowing preemption FACILITY cpu; // declare facility cpu cpu = new facility("cpu"); // initialize facility ... cpu->set_servicefunc(pre_res) // set service protocol to preempt-resume ... priority = 100; // raise process priority cpu->use(service_time);

  31. CSIM STORAGES

  32. Storages Like facilities but designed to be shared Each process gets some units of storage Used to simulate main memory Must have a name Only used to label report entries

  33. Example: single storage const long SIZE = 100; storage *mem; // declare storage mem = new storage("mem", SIZE); // initialize mem with 100 units ... amt = random(1, SIZE); mem->allocate(amt); // get storage ... mem->deallocate(amt); // release it ...

  34. Example: storage array const long NSTORES = 5; const long SIZE = 100; storage _set *mems; // declare array ... mems = new storage_set("mem", SIZE, NSTORES); //initialize ... (*mems)[3].allocate(amt); ... (*mems)[3].deallocate(amt);

  35. Example:setting a maximum wait time const double MAXWAIT = 1.0; st = mem->timed_allocate(amt, MAXWAIT); // maximum wait is one time unit if (st < MAXWAIT) { // success ... mem->deallocate(amt); // release } else { // failure ... }

  36. More options Can also specify faculties and storages that can only be allocated at regular points in time Clock ticks Must invoke “synchronous()” method before invoking “allocate()” method “synchronous()” method specifies phase and period of allocation policy

  37. CSIM BUFFERS

  38. Buffers A buffer consist of a counter indicating the amount of available capacity A queue for processes waiting to get some buffer space A typical producer behavior A queue for processes waiting to free some buffer space A typical consumer behavior

  39. Example:creating and accessing a buffer const long SIZE = 100; buffer *buf; buff = new buffer("buff", SIZE); ... amt = random(1, SIZE); buff->get(amt); ... buff->put(amt);

  40. Example:setting a maximum wait time st = buff->timed_get(amt, 1.0); if (st < TIMED_OUT) { // success ... buff->put(amt); } else { // failure ... }

  41. CSIM EVENTS

  42. Events Used to synchronize and control interactions between different processes Have two states Occurred (OCC) Not occurred (NOT_OCC).

  43. Processes and events A process can Set an event:mark it as occurred Wait for an event:when event occurs all waiting processes can continue occurred state Queue on an event:when event occurs one waiting processes can continue

  44. More details (I)‏ When a process waits for an event: If the event is in the not-occurred state, the process is suspended and placed in a queue of processes waiting for the event to occur. When the event occur All waiting processes are allowed to proceed Event is changed tonot occurred If the event is in the occurred state, The process continues to execute Event state is changed to not occurred

  45. More details (II)‏ When a process queues on an event: If the event is in the not-occurred state, the process is suspended and placed in a queueof processes queued on the event. When the event occur Only the first queued processes is allowed to proceed Event is changed to not occurred If the event is in the occurred state, The process continues to execute Event state is changed to not occurred

  46. CSIM events and semaphores Like binary semaphores, CSIM events can have two values:occurred/not-occurred Occurred correspond to value of 1 Not-occurred corresponds to value of 0 Set() method corresponds to V()‏ Queue() method corresponds to P()‏ Wait() method does not correspond to any semaphore operation

  47. Example:declaring and using an event event *ev; //declare event ev ev = new event("ev"); //initialize it ... ev->wait(); // wait for event ... ev->queue(); // queue on event ... ev->set(); // set event

  48. Example:arrays of events const long NEV = 25; event_set *eva; // declare array eva = new event_set("ev array", NEV); // initialize it events*/ ... (*eva)[5].wait(); ... (*eva)[5].set();

  49. Example: waiting/queuing foranyevent in an array i = eva->wait_any(); // i is index of event that occurred or i = eva->queue_any(); // i is index of event that occurred

  50. Example: setting a maximum wait time ... t = ev->timed_wait(MAXWAIT); //wait for up to MAXWAIT time units if (st ! = MAXWAIT) { // success ... }

More Related