0
\$\begingroup\$

I'm making a chess-like game with an AI that implements minimax algorithm with alpha beta pruning. The AI raw computation is effectively infinite, so I'm forced to limit the depth I traverse, thereby limiting the strength of the AI.

I'm extremely sensitive to the engine speed, as it is unacceptable for the player to have to wait even a few seconds for a move. Hence I must keep the depth pretty low.

If I launch multiple threads, will this effectively increase the speed? My concern is that I can't predict how strong the player's computer will be, so I kind of need to assume they have a weak machine (so the game is playable for everyone). That said, even if they have only 1 core on their entire cpu, there won't be much cost to launching a few extra threads (the overhead of launching <10 extra threads is frankly minimal). And in the case they have a CPU with 2 or 4 threads, they will go that much faster.

Do most computers have multiple cores that will work well for multiple threads (like, at least 2?)? I'm thinking of using either 1, 2, 4 or 8 threads. Seeking general advice.

Also, is the GPU relevant at all? (I tend only to think # of threads should equal # of cores ... )

\$\endgroup\$

2 Answers 2

1
\$\begingroup\$

The current state-of-the-art for multithreading in Unity is the jobs system using the burst compiler. It not only gives you far more performance than regular C# code for most situations (at the cost of being restricted to a subset of the language), it also gives you multithreading managed by the engine itself. So you can just schedule parallel jobs and let the engine manage the distribution of tasks to CPU threads.

The whole system is unfortunately too complex to explain it from start to finish in a single answer, so I would recommend you to read the documentation I linked above and ask a new question when you run into any roadblocks on your way.

Do most computers have multiple cores that will work well for multiple threads (like, at least 2?)?

The last single core CPUs made by Intel were the cheapest of the cheapest Celeron CPUs from the Sandy Bridge generation (10 years ago). And that was an outlier: The three generations prior had no single core chips at all. AMD CPUs only got relevant again when they started to outdo Intel in regards to core count.

The first iPhone with a multi-core CPU was the iPhone 6 from 2014.

The landscape of Android devices is too vast to evaluate completely, but multi-core CPUs also seem pretty ubiquitous for several years there too.

So yes, you can safely assume multiple CPU cores nowadays.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ As another data point (granted, biased toward gaming-focused users who tend to have higher-spec machines, so take it with a grain of salt), the latest Steam hardware survey reports only one in 400 of their users on any OS has only a single physical CPU core - and that doesn't factor in that some of those physical CPUs may still have more than one logical CPU via hyperthreading. \$\endgroup\$
    – DMGregory
    Jan 4, 2022 at 15:27
  • \$\begingroup\$ The nice thing about using Unity JOBS is that it'll tend to significantly speed things up even if users have a single thread as long as your [BurstCompile] them. This isn't only because BURST compiler can hyper optimize HPC# (high performance c#). It's also because the language is very constrained it'll tend to force you in a more performance-oriented way from the outset (no GC, encourages use of large contiguous buffers and therefore cache-coherence). \$\endgroup\$
    – Charly
    Jan 4, 2022 at 21:08
-1
\$\begingroup\$

You could use a ComputeShader to offload the ai logic to your GPU, which is better in running parallel tasks, like AI pathing and the sorts.

Or just teach your AI chess with unity ml-agents, so that fuzzy logic and neural network make the AI more human and choose the best move that "feels right", and you could just edit the thinking time and have different agents learning for different lengths of time.

\$\endgroup\$
4
  • \$\begingroup\$ Evaluating chess positions is not really a task well-suited for GPU. And OP already said they want to use good old Minimax with alpha/beta pruning instead of relying on AI experiments. \$\endgroup\$
    – Philipp
    Jan 4, 2022 at 9:35
  • \$\begingroup\$ Please highlight the bit where OP explicitly rules out ever using AI with neural networks - I can't find it. Anyway, I used A-Star Pathfinding with a compute shader in one of my games - with about 24 enemies on a board twice the size of a chessboard. It worked well, but for sure. Also, OP makes a chess-like game. So I think it's fair to relate this to his second question: Does the GPU matter? No, but certainly you could do the calculations on the GPU and it might be better, in fact it will be if the tasks are simple enough and can be run in parallel. \$\endgroup\$ Jan 4, 2022 at 12:44
  • \$\begingroup\$ It's the first sentence of the question: "I'm making a chess-like game with an AI that implements minimax algorithm with alpha beta pruning." If you look up "Minmax Algorithm" and "Alpha Beta Pruning", then you will see it's an algorithmic solution not based on machine learning. And A* isn't really useful for chess. Just because you successfully implemented A* on GPU does not mean that any algorithm is suitable for that. Evaluating chess positions is a mostly CPU-bound task. \$\endgroup\$
    – Philipp
    Jan 4, 2022 at 14:31
  • \$\begingroup\$ I'm not sure why you would want to employ neural nets to chess anyways in a game context. What would this improve? It would make the AI much harder to tune and understand, and much less strong without a ton of training data and tuning. MiniMax algo is well known, and can still beat some of the strongest players if they don't know how to exploit it. If you want chess AI to have different "personalities" so it feels more human, then there's easy ways to do as you can just modify the heuristic scoring that the algo uses. \$\endgroup\$
    – Charly
    Jan 4, 2022 at 21:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .