Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  

Author Topic: multithreading, just a short question for a short answer  (Read 788 times)

Raven

  • Bay Watcher
    • View Profile
multithreading, just a short question for a short answer
« on: January 06, 2023, 02:01:06 pm »

joking, you can give a more extensive answer

question1) is it possible?

2) to which extent? I think that applying it to the world creation would speed things immensely and permit to have bigger older world even on less powerful computers

3) is it hard thing to do?

thanks, love to putnam and adams brothers
Logged
http://dwarffortressitalia.tumblr.com/

Italian blog about Dwarf Fortress

Il primo blog italiano su Dwarf Fortress

Telgin

  • Bay Watcher
  • Professional Programmer
    • View Profile
Re: multithreading, just a short question for a short answer
« Reply #1 on: January 06, 2023, 02:31:07 pm »

A fairly short answer:

With a game with a codebase as huge as DF's, it would be very hard.  Essentially, every code block that touches anything that more than one thread might want to operate on needs new multithreading guards to be added to it.  This is notoriously error prone, and if done badly can make the game run slower than it already does.  If done wrong it can also make the game behave in interesting and unpredictable ways, not all of which are obvious like crashes.

The fundamental problem with DF is that it uses a ton of huge central and global data structures that would have to be protected this way, and it limits what multithreading can do to benefit it without a significant amount of architectural changes.

And even then, it's hard to get it right in a way that speeds things up.  Pathfinding is actually one thing that might benefit from multithreading, but as Putnam recently discovered it's really not a major contributor to the game's speed so there wouldn't be much point..  You can use it to do things like temperature calculations on tiles in parallel, but then you have to handle complex border cases where your divided workload touches on bits from other threads and that may nullify any speedup.  The examples are endless.

I'm not sure about worldgen.  I'd guess that some of its work could be broken up, but most of the time is spent in history generation, which is another example of something that is hard to split up because of how interconnected the pieces are.  You can't hand two separate threads the job of generating historical events and just let them go, since they might create events that impact what the other threads are working on, so you're back to needing synchronization.

I'm sure there are parts of the game that could be sped up with multithreading, and probably even in its current state, but it also comes down to a question of how the programmers' time should be spent.  So far, Toady has decided it wasn't a good use of his time, and that's probably reasonable since he has a better understanding of the code than anyone.  If the game were rewritten from the ground up with parallel work loads in mind it might be a different story.
Logged
Through pain, I find wisdom.

Criperum

  • Bay Watcher
    • View Profile
Re: multithreading, just a short question for a short answer
« Reply #2 on: January 06, 2023, 03:06:44 pm »

As a programmer with almost 15 years of experience i'll try to answer:
1. I believe it is possible
2. It heavily depends on subsystem, mechanics you are trying ot multithread. Multithreading is not a thing you can just turn on. Most of the things in DF are interconnected and it requires a lot of effort and complete rework to make a possibility (just possibility) to parallelise it. For example  pathfinding cannot be mutithreaded since A* algorithm depends on previous steps. But if several agents are moving on exactly hte same map and don't interact upon meeting (walking through each other) several pathfindings can be done in parralel. But in DF all agents are interacting.
3. Extremely hard. Basically as hard as making half of the game from scratch or even worse.
Logged

Putnam

  • Bay Watcher
  • DAT WIZARD
    • View Profile
Re: multithreading, just a short question for a short answer
« Reply #3 on: January 06, 2023, 06:58:05 pm »

1. yes
2. I have successfully done possibly-stable (it did not crash within two minutes and I did not see any blow-ups) multithreading on temperature, which sped up a fort from 28 to 31 with occasional massive drops (I assume due to weird concurrency issues). mostly it's extremely difficult. Even the most basic kind (sending various steps of processing to other threads in the main loop) is not feasible without a major rewrite, because e.g. map block processing and unit processing both use the pathfinder (for connectivity graph checking and pathfinding respectively)
3. yes, very

Raven

  • Bay Watcher
    • View Profile
Re: multithreading, just a short question for a short answer
« Reply #4 on: January 08, 2023, 06:49:39 am »

thank you all for your precious time in replying, I understood the short and long answers provided <3
Logged
http://dwarffortressitalia.tumblr.com/

Italian blog about Dwarf Fortress

Il primo blog italiano su Dwarf Fortress