Friday Facts #271 - Fluid optimisations & GUI Style inspector

Regular reports on Factorio development.
User avatar
Lubricus
Filter Inserter
Filter Inserter
Posts: 294
Joined: Sun Jun 04, 2017 12:13 pm
Contact:

Re: Friday Facts #271 - Fluid optimisations & GUI Style inspector

Post by Lubricus »

posila wrote:
Wed Dec 12, 2018 7:18 am
Nightinggale wrote:
Wed Dec 12, 2018 1:39 am
Looks like it is running an if-else structure on shaders. Does that mean shaders can handle conditional branching? If so then the shaders might end up in different places in the code. Does that mean shaders are not SIMD? Has the GPU system left the SIMD system? That doesn't make sense because both CUDA and OpenCL claims to be SIMD.
More accurate name for GPU architecture is SIMT (Single instruction, multiple threads). The article describes control flow handling following way
A downside of SIMT execution is the fact that thread-specific control-flow is performed using "masking", leading to poor utilization where a processor's threads follow different control-flow paths. For instance, to handle an IF-ELSE block where various threads of a processor execute different paths, all threads must actually process both paths (as all threads of a processor always execute in lock-step), but masking is used to disable and enable the various threads as appropriate. Masking is avoided when control flow is coherent for the threads of a processor, i.e. they all follow the same path of execution. The masking strategy is what distinguishes SIMT from ordinary SIMD, and has the benefit of inexpensive synchronization between the threads of a processor.
Interesting...
I realized it's possible to emulate logic operators with arithmetic operators doodling with "factorio combinators"
you can say 0 -> false, non 0 -> true
then the * operator behaves as the "and" operator.
Then I remembered i had done similar tricks to convert simple loops with if statements in them to vector operations (SIMD) with similar trick with the downside that all paths are evaluated but some of them evaluates to 0 and then don't contribute to the result.

Nightinggale
Fast Inserter
Fast Inserter
Posts: 120
Joined: Sun May 14, 2017 12:01 pm
Contact:

Re: Friday Facts #271 - Fluid optimisations & GUI Style inspector

Post by Nightinggale »

A new video came out explaining multithreading and the issues related to it. I think most if not all of it has been mentioned in this thread already, but it's explained a whole lot better and the visuals will certainly help understanding the issues. Multithreading Code - Computerphile. Keep in mind that this is mainly about what can go wrong related to not getting the correct calculation results (race conditions) and while performance is mentioned, gaining performance from using more than one core is significantly more complex than this video indicates.
Lubricus wrote:
Wed Dec 12, 2018 12:14 pm
Interesting...
I realized it's possible to emulate logic operators with arithmetic operators doodling with "factorio combinators"
you can say 0 -> false, non 0 -> true
then the * operator behaves as the "and" operator.
Then I remembered i had done similar tricks to convert simple loops with if statements in them to vector operations (SIMD) with similar trick with the downside that all paths are evaluated but some of them evaluates to 0 and then don't contribute to the result.
Sure this can be done, but will it be faster than masking instructions? For performance, the question is if the if statement is faster than multiplying with 1/0. (I suspect masking is fastest). Secondly I have a hunch that masking will make the core idle, hence idle power/heat. Multiplying with 0 is not an idle core, hence a hotter core.

Usually the coding standard for programming languages for using an int as a bool will make false = 0 and true != 0. This is fine when reading, but you have to be sure what you are doing when writing.

Code: Select all

int a = 1 > 0;
What is in a after this? Well it depends. If you are coding in C++ it should be 1. If you are coding in C, then the answer is it depends on the CPU. x86 will set it to 1 while PowerPC will set each bit to 1. This might have changed in the newest standards of C, but it was an issue back when I ran into bugs related to this.

Another good question:

Code: Select all

unsigned char a = 0xff;
a++;
What does a contain? The answer is if it's C, then again it can be CPU specific and the answer can be both 0xFF and 0.

In other words yes you can do a whole lot of stuff where you multiply with 0 or 1 to simulate conditional cases without actually have branches, but when doing so you better know completely how the computer will react to border cases like this.

Another important factor to consider is which approach is the most prone to bugs. Often it doesn't pay to make your code 5% faster if it makes the code much less readable. Unreadable code has a tendency to be buggy and hide bugs. Bugfree is usually way more attractive than the last percentage of performance boosting.

Honilno
Manual Inserter
Manual Inserter
Posts: 1
Joined: Wed Dec 19, 2018 4:01 pm
Contact:

Re: Friday Facts #271 - Fluid optimisations & GUI Style inspector

Post by Honilno »

Fluid system from point of view of industrial fluid system designer.

I can really agree that current system in Factorio for calculating fluid level in every pipe piece is really CPU consuming.

Approach to modeling should follow assumptions :
a) compressible/incompressible
b) pipe losses
c) boundary condition

Example

Simple physics based system

assumptions:
1) Incompressible fluid
2) No pressure losses in the pipes
3) Cross section area of the pipes is significantly larger than restrictions on inlets and outlets
Equation: Bernoulli + balancing multiple inputs/outputs


Modeling
1) As there is no loss in the pipes and metering restrictions are on inlet and outlets of the system there is no need to differentiate between pressure in a single pipe system. One pressure value for one system.
2) Boundary conditions:
a) Provider has internal tank with check valve. New produced batches are dropped to the tank independent from any pressure calculation. Until whole batch is not in the tank , production is stopped. Tank pressure linearly depend on the fluid level in tank. If internal tank pressure drops bellow pipe system pressure - there is no reverse flow.
b) Receiver has internal tank with check valve. Demand is supplied from internal tank independent of any pressure calculations. Tank pressure linearly depend on the fluid level in tank. If internal tank pressure rise above pipe system pressure - there is no reverse flow.
C) Tank. Tank pressure linearly depend on the fluid level in tank.
d) Pump. Tank with outlet pressure higher than inlet pressure by certain amount (constant)
3) Filling the piping system. Every system would have its volume dependent of number of pipes, lenght of the underground lines. Until pipe system is not filled it is not transferring flows and system pressure is 0.


Algorithm:

1) Add fluid to provider tanks (from production if applicable)
Remove fluid from receiver tanks (fulfill demand)
2) Solve set of equations for system to get all W and Ps:

Wp= ap*(Pp-Ps)^0.5 (for every provider)
Wr= ar*(Ps-Pr)^0.5 (for every receiver)
Wt= at*((Pt-Ps)/ |Pt-Ps|)*(|Pt-Ps|)^0.5 (for every tank)
Wp-Wr+ Wt =0 (include every provider, receiver and tank)

Where W is flow, P is pressure, a is abstract flow restriction. Subscripts p-provider, r-receiver, s-system, t-tank.



3) Modify all tanks levels based on calculated flows
4) recalculate all tank pressures



You can go out of the physics a little and make equations for flow linear W= a*(P1-P2).
Set constant level of pressure at the exit of pump.

You can go also into solutions closer to industrial standard 1D calculations where every intersection, connection to the building, tank etc. is a node where You solve pressure and balance the flow. And every section of pipes between nodes is element to calculate flow with its losses. I think it would be still feasible in the game for incompressible flow, and should be comparable to CPU load relative to current solution.

I will be glad to help if You need it.

Post Reply

Return to “News”