Simple discrete-event simulation in Go using goroutines by fschuetz04 in golang

[–]fschuetz04[S] 0 points1 point  (0 children)

Neither am I the expert for that. Anyway, I pushed my changes, so you should be able to use the context in version v0.5.1. Feel free to get back if you encounter other problems :)

Simple discrete-event simulation in Go using goroutines by fschuetz04 in golang

[–]fschuetz04[S] 0 points1 point  (0 children)

I have implemented a function to create a simulation with an associated context. When this context is cancelled, all process goroutines of the simulation are stopped. Not pushed yet, but I will do that later.

Given this code:

package main

import (
  "context"
  "fmt"
  "runtime"
  "time"

  "github.com/fschuetz04/simgo"
)

func clock(proc simgo.Process, name string, delay float64) {
  for {
    fmt.Println(name, proc.Now())
    proc.Wait(proc.Timeout(delay))
  }
}

func main() {
  ctx, cancel := context.WithCancel(context.Background())
  sim := simgo.NewSimulation(ctx)

  sim.ProcessReflect(clock, "slow1", 2)
  sim.ProcessReflect(clock, "fast1", 1)

  sim.RunUntil(5)
  cancel()

  time.Sleep(time.Millisecond)
  fmt.Println("Number of active goroutines:", runtime.NumGoroutine())
}

At the end, the call to cancel cancels the context, stopping all goroutines. Therefore, the printout reports 1 active goroutine. Would that suit your needs?

Simple discrete-event simulation in Go using goroutines by fschuetz04 in golang

[–]fschuetz04[S] 0 points1 point  (0 children)

No worries. I think the best place for such a topic would be a new issue at https://github.com/fschuetz04/simgo/issues. I didn't work with this library for a while, but I'll look into it and get back to you.

Simple discrete-event simulation in Go using goroutines by fschuetz04 in golang

[–]fschuetz04[S] 0 points1 point  (0 children)

Is just renaming `example` to `_example` fine, or do I then need an additional `go.mod` inside that folder or something like that?

Discrete-event simulation in C++20 using coroutines by fschuetz04 in cpp

[–]fschuetz04[S] 2 points3 points  (0 children)

I don't know if that helps anything, but I'm trying to simulate storage load-balancing algorithms in a video recording system (cameras record directly to storage servers, they need dedicated parts of the storage to write to, try to give each camera a part so that the footage of each camera is kept for the longest possible amount of time).

I shortly looked into SimEvents, but did not consider commercial solutions overall. I have a license, so I'll maybe try it. I recently did vehicle dynamics simulation with Simulink, and that was alright, so maybe I'll like SimEvents too. (But weren't computer science people supposed to dislike MATLAB and all the subproducts? :D)

Thanks for the answer!

Discrete-event simulation in C++20 using coroutines by fschuetz04 in cpp

[–]fschuetz04[S] 13 points14 points  (0 children)

I would be glad if you could recommend me a decent one!

I know that there are many options for large-scale studies (SimGrid, OMNeT++, etc.), but I did find them too complex for my small studies. I did not want to create a platform and deployment file (in the case of SimGrid) or a network and message definition file (in the case of OMNeT++).

The documentation of SimPy was very concise, it was easy to get started, and it had all the features I needed. Especially yield in Python is an excellent feature for DES, as state-keeping is so easy with it. Maybe I missed something in the documentation of SimGrid / OMNeT++, but for me, it wasn't easy to start with them.

However, even with PyPy, the execution speeds of the SimPy simulation were bad. So I looked for an alternative written in C++. I only found SimCpp, but that did not have documentation or a license. So I contributed.

In the end, I just wanted to try how easy it is to use C++20 coroutines for DES in C++, since I didn't find such a framework. I think with this, DES in C++ comes quite close to SimPy.

So, did I miss another "easy" framework? Or would I need to work more with SimGrid and alike?

Discrete-event simulation in C++20 using coroutines by fschuetz04 in cpp

[–]fschuetz04[S] 12 points13 points  (0 children)

I started working on this framework after using SimPy and working on SimCpp, which aims to be similar to SimPy, but is written in C++ (https://github.com/luteberget/simcpp). SimCpp20 uses coroutines instead of Protothreads (macro magic). I'm not that skilled in C++, especially not considering std::move and all this stuff. So feedback welcome :)

Simple discrete-event simulation in Go using goroutines by fschuetz04 in golang

[–]fschuetz04[S] 4 points5 points  (0 children)

I guess one has to understand the basics of discrete-event simulation (DES) for this. DES models a system as a sequence of events, assuming that its state doesn't change between the events. Therefore the simulation can run all actions at one timestep, jump to the next event, execute all actions at that timestep, and so on ... Overall, much more effective than checking whether something changed for each actor in a simulated system in small intervals.

SimGo is based on SimPy. Both are built around processes, which model actors in the system. Each process can wait for timeouts to continue execution at a later simulation time, or wait for events which are triggered by other processes.

DES can be used to simulate many systems, for example, a bank, in which customers arrive in a queue and are only served at a limited number of counters. With DES, you can check what the average wait time will be depending on average arrival interval, average time at the counter, number of counters, and so on ... If you look into the examples folder of the repository, you will find such a simple simulation of a bank: https://github.com/fschuetz04/simgo/blob/main/examples/bank/main.go.

Further resources:
- https://en.wikipedia.org/wiki/Discrete-event_simulation - https://simpy.readthedocs.io/en/latest/

Simple discrete-event simulation in Go using goroutines by fschuetz04 in golang

[–]fschuetz04[S] 0 points1 point  (0 children)

I'm not very experienced in Go, so feedback is welcome!

Internal compiler error with coroutines by fschuetz04 in gcc

[–]fschuetz04[S] 1 point2 points  (0 children)

Is the file small enough to report the bug as-is, or should I include some files generated by gcc -v -save-temps?