abassign wrote in Wed Jul 11, 2018 7:45 am:Flightgear has three official programming languages:
JSBSim,
property tree XML,
NASAL ! They are obviously too many and not only, but almost all of them are executed on one thread.
...
However it would be interesting to be able to put this part in separate threads (I do not know if there are any side effects) ... do not think there will be problems using JSBSim for all the animations, and if this is possible we will have the chance to "throw away" the NASAL section in all our airplanes!
Probably you meant JSBSim, autopilot/property rules and NASAL. There is a potential small performance benefit from running on multiple threads, for the F-15 this would be a maximum of 6ms, but I am aware that the cockpit mesh could be improved to avoid state changes which would probably have a similar impact.
Before I get into what will be probably a long message about multi-threading the summary is yes, you certainly should move all Nasal code that you can into JSBsim systems. As Thorsten said Nasal is good at managing this, handling property listeners and gluing it all together as an event based system, but Nasal should not have the minimum amount of code that is run each frame, limited to logic that can't be done in JSBSim, Canvas updates etc. My F-15 has too much Nasal and I'm not citing the F-15 as an example of how to use Nasal.
Anyway, onto multi-threading. Yes, you're right there are side effects of multi-threading and it would require a lot of work to make FG multithreaded.
Multi-threading is something that seems like it should help, but it doesn't help as much as it should; primarily because you can't get more performance out of OpenGL or your GPU by using multiple threads. What we could get is around 10% of the total (for most aircraft).
It's been said before, but multi-threading in C++ is very hard to make stable, I know this because the SwiftIOS kernel has to handle multiple threads and processes and it took me a lot of time to make this stable, working with code that was designed to be multi-threaded. Even now it isn't 100% right as each process has to sit on a single CPU (CPU affinity) otherwise after a while it deadlocks. This is another case where using all of the multicore CPU doesn't make any real difference as SwiftIOS is more than fast enough as it is. Yeah it'd be nice to get it using all cores, but long term reliability is way more important than improving something that is frame locked at 20hz; it's no good if the sim had one lockup every 100hours as that impacts training.
I've done a lot of analysis and work on multithreading in FG; I've even had a prototype multithreaded version of FG[1] that uses two threads, one for the rendering and another for everything else. There is pretty much no point have more than one thread for everything else, as even at 120hz there is still idle time (for the F-15).
My F-15 has around 6ms of total non visual rendering time at a JSBSim frequency of 120hz; around 3ms of this is the total time for the JSBSim model; the rest is Nasal.
However most of the time in FG is taken up with the rendering. an extra 1 or 2ms isn't going to dramatically impact your frame rate. Trying to optimise the JSBSim systems time is rather like painting your coal cellar black; you could do it but actually it's not likely anyone will notice.
This is how the frame time breaks down for the F-15 on my system[2]
3d aircraft 11.44 ms
3d terrain 15.60 ms
Nasal 2.99 ms
JSBSim 3.10 ms
Total= 33.12ms
FPS= 30.19
A theoretically optimum multithreaded FG would give you
If you could make FG reliably multithreaded you'd get a maximum FPS boost of 6.8 for the F-15 (see below); which is good and ideally someone would be doing this; but if it makes FG less reliable those extra FPS don't matter a jot. Reliability is more important to FG (IMO) than performance. It's no fun to be 2 hours into a flight and have FG crash.
CPU0
3d aircraft 11.44 fps
3d terrain 15.60 fps
CPU1
Nasal 2.99 fps
JSBSim 3.10 fps
3d time (ms) 27.03 fps
FPS 36.99 fps
Increase 6.6 fps
--------------------------------------
[1]
https://imgur.com/HC7NgqZ[2] my system is R9-290, i7-4790K, 24gb ram.