Page 1 of 1

Idea for threading

Posted: Thu May 04, 2017 8:40 am
by burner
I am currently at situation where I need run my factory with speed 0.3 with 3.6Ghz i7 so I start thinking that there has to be way. I cannot solve that with buying faster processor because there is no processor at market what can run that simulation at realtime with single core. Anyway there is processors where is enough cores to run it. With unlimited science that problem come more and more relevant.

I am not familiar with factorio code but I am very familiar with problems and development pain what multi threading brings. Locking all objects is out of options. There has been some kind "clusterio" idea where is point but it smells like glue. So combining these two ideas here is my proposal. Sorry that I dont know what terms you use in house so I fist clarify my terms
- Item: Item what interacts other items (belt, factory, inserter etc)
- tag: Id, pointer or some other one byte identifier given for each item (explained more at later)
- Thread intermediate item. Item what have special properties regarding to threading.

So idea is that example once at 10 minutes or even one in one hour(if it is very slow process) there run optimizer what start walk item interaction tree. It take one item and check what interaction items it have. Example inserter have usual near by belt and factory. It give tag 1 to each item. When that tree walk is reached limits it stops. Then it find next orphan item. It start walking that tree and give tag 2 for them. So it do that so many times that there is no orphan items.
Now we have interaction groups what we can give to separated worker threads. Each thread will keep tick sync with lock. So they can pick interaction groups from pool as fat there is none. Then they need wait until all other threads release their tick lock. That way all groups will be calculated once per tick and after that there is common calculations what are explained That will not cause changes for current simulation code.

So now we have problem that we can put separated factors for one map but you cannot move your products between groups and if 2 group touch threading will blow up....
For blow up problem my suggestion is that if you plant new item it will mark it as new and it is not counted to current tick. After tick it will check that if there is new items it will check witch group it will interact and add tag for that new item. If item touch to 2 different group then it will take larger group tag and add it to smaller group items. So groups will be merged. If that item is removed there groups will keep merged until next optimizer round.

And because we want big factory, not many small ones we need interaction for groups. For that we can use trains. If trains are exception, we can do that each time when train stop it will check what interaction groups it have and migrate that train to that group. If that train approach feels too complicated then one way to do it is make some kind transfer dock item. It can be example 2x16 item or even larger what include internally 2 huge chests. Both chests have own thread tag. When you put items inside of it it will balance internal chests after each tick. So then it can informed for players that do not put thousands of them, it will kill your performance.
For power grid there can be "HVDC station (check wikipedia if you dont know what it is)" what basically contains two accumulator without current limit or with huge current limit at it got balanced after each tick.

So that way game is still one instance with multiple threads. Threading areas are dynamic and most probably impact for code is quite limited (depending how that rendered engine have connected to simulation engine)

And for multiplayer lag I suggest feature what allow set allow server throttle setting up. That will do so that each client will report to server that if they are behind of server at simulation. If there is client connected what is behind then server will slow down that simulation. If not then it will speed up that simulation. Probably min and max speed is also good parameters. That probably helps to lag spikes at client and is quite trivial to implement.

Re: Idea for threading

Posted: Thu May 04, 2017 8:50 am
by Engimage
Multithreading was discussed pretty in-depth with devs as it is a real issue for many of us.

Multithreading is a really complex thing and currently most of things that could be using multithreading are using them.
There are a lot of different complexities in this as the data processed by threads does not intersect that leads to the issue that you can't just separate calculations while keeping the cross-thread interactions. But most of stuff in the game does interact with each other so you just can't define such separate blocks.

The only way currently able to aid you is the planned belt optimizations that got postponed to 0.16 and did not make it in 0.15 as was originally planned.

Please do understand that Factorio devs ARE real pros. Like REAL. They are aware of these issues and are desperately trying to optimize stuff and offload tasks to threads. Without in-depth knowledge of game functioning you just can't assist them. Believe me many of us tried but we got a number of in-depth explanations from Harkonnen where we just realized he is more than confident to make it on his own.

Re: Idea for threading

Posted: Thu May 04, 2017 11:20 am
by Tekky
Multithreading has been discussed extensively in this thread. I especially found this post by Factorio developer Harkonnen on page 4 of the thread to be very interesting.

Re: Idea for threading

Posted: Thu May 04, 2017 12:53 pm
by Deadly-Bagel
Jeez, guess Harkonnen got bored of people implying his code wasn't up to scratch lol. I understood about half of that. As I've been saying you jut can't make assumptions on how Factorio code works and suggest improvements based on that. We're not talking some Ubisoft trash hack job here, this is pioneering performance by updating the entirety of a very complex sandbox game 60 times a second with such reliability it can run concurrently on 400 computers (all interacting with each other) and always have the exact same result at the end. Name any other game that comes even remotely close to being this efficient.

It's never been done before, meaning Factorio utilises new and revolutionary techniques that have never been done before, meaning even if you are as brilliant as the Wube devs (hah) you could only come close to suggesting improvements by working on such new pioneering techniques and associated logic basically non-stop for over 4 years. I'm going to take a guess that nobody on the forum here (Wube aside) has done that, let alone in the current context (as doing so in 1996 would have been drastically different).

By all means go harass Ubisoft devs to write their games properly (they certainly seem to need the help) but I think Wube have earned the right not to be questioned on their expertise =)

Re: Idea for threading

Posted: Fri May 05, 2017 4:52 am
by BenSeidel
Deadly-Bagel wrote:but I think Wube have earned the right not to be questioned on their expertise =)
Any developer that believes that they have earnt the right not to be questioned is not a good developer.

Re: Idea for threading

Posted: Fri May 05, 2017 9:43 am
by Deadly-Bagel
Good thing I'm not a Wube developer then. Besides, any good developer would be more than happy to be questioned - by their peers. And they have been questioned regardless, so what's the sense in flogging this dead horse by questioning again and again? I know they know what they're doing, you know they know what they're doing, they've proven they know what they're doing, why do we keep coming back to this?

Re: Idea for threading

Posted: Sat May 06, 2017 4:05 am
by BenSeidel
Deadly-Bagel wrote:proven they know what they're doing, why do we keep coming back to this?
Because new perspectives may give you an idea or an insight into some aspect you have never thought of. I can't count the number of times a solution to an issue has been thought of because some "noob" came up with a "solution" to a problem, even after we had flogged the issue to death. Discussion is always a good thing. The idea put forward of identifying non-interacting trees is a really interesting take on the problem, and if this technique had already been considered by Wube, then good on them.

Applying a garbage collector to multithreading entities... Cool.

Re: Idea for threading

Posted: Sun May 07, 2017 8:06 pm
by ssilk
burner wrote:- Item: Item what interacts other items (belt, factory, inserter etc)
- tag: Id, pointer or some other one byte identifier given for each item (explained more at later)
- Thread intermediate item. Item what have special properties regarding to threading.
Items are coal, iron, etc. Something, that has no own execution code. What you mean are entities.
http://lua-api.factorio.com/latest/LuaEntity.html

I understand your idea like the idea here:
viewtopic.php?f=5&t=8670&start=10
The second post on that page is by me and it is about your idea: Split the world into different threads.
And Kovarex's answer some posts later:
viewtopic.php?f=5&t=8670&start=10#p69290

And then you should read that:
viewtopic.php?f=5&t=39893&hilit=multith ... 60#p238247
What Harkonnen says is: Factorio is so optimized, that the bottleneck is not the CPU, but the memory access.