3

I read about Looking for Magics for Magic Bitboard, they mentioned the method "Feeding in Randoms".

As I understand, this method works like this:

label RETRY;
magic = random();//trial

for (all cases of current square: from first case to last case)
{
  if (magic fails at any case)
    go to label RETRY;//and error
}
//Passed all cases -> success! now we have the magic for square 1, 63 more squares to go

The author said

On my Core Duo 2.8 GHz, it takes less than a second to find magic numbers for rooks and bishops for all squares

If we successfully get magic numbers with this "Randoms" method, why not just save the magic numbers to disk and load it from disk instead of running the "Feeding in Randoms" code every time at startup (that the author cares about how fast this method, isn't it faster just read the magic from disk instead of trial and error with random. If load from disk, it doesn't matter if the method takes whole day to find magic)?

Maybe I misunderstand something about this. So I want to ask Why have to find magic instead of just loading from disk?

Brian Towers
  • 92,895
  • 10
  • 227
  • 372
123iamking
  • 181
  • 2
  • I don't truly understand much about these magic numbers, but on current architectures it's a truth that memory movement is incredibly slow relative to the speed of number crunching or computation time. So in general, loading anything from disk would be impossibly slow relative to nearly any other method of computing those results. – NoseKnowsAll Mar 23 '20 at 15:51
  • 1
    I am not even remotely qualified to answer this as I am not a software engineer, but +1 to make up for the downvote since there is no valid reason for it that they were willing to own up to. – PhishMaster Mar 23 '20 at 20:29
  • 1
    Since this is a one time action, the time involved shouldn't even be a consideration. Loading the 128 numbers is easier (and probable faster) than computing them at startup. – Mike Jones Mar 24 '20 at 22:58
  • 2
    @PhishMaster So your whole justification for an up vote is because you perceive the OP was wrongfully down voted? Talk about not having a valid reason. – Mike Jones Mar 24 '20 at 23:00
  • @SmallChess You are entitled to do what you want as am I. The OP is a new user, and it seems like a totally reasonable question to me, even if it is tough to answer. THAT was my main reason for upvoting (some other people can't read), and I like for new people not to feel attacked, and welcomed. I would be interested as to why you think it was a bad question, especially since you have now taken the time to actually answer it. – PhishMaster Mar 24 '20 at 23:35
  • @PhishMaster Yes, it was me who downvoted it, because I thought it was silly question given that it only took like a second to compute the magic numbers. – ABCD Mar 25 '20 at 04:14
  • 1
    @SmallChess It is simple for you, but to someone, who does not know, it is not simple. Think about the simple pure chess questions that pop up here, like yesterday, they guy who asked why in the Ragozin (he did not know it was the Ragozin) Qa4+ did not win the Bb4, when the simple Nc6 saved it. There are no silly questions when you do not know, and are trying to be genuine. Of course, you can have people, who try to be that way on purpose, but I did not sense that was the case. I really hate the downvote system here as it is really just so negative beyond the -2 points. – PhishMaster Mar 25 '20 at 10:55

1 Answers1

1

Computing magics at statup is fine because it's done only once and not very slow.

https://github.com/official-stockfish/Stockfish/blob/master/src/bitboard.cpp

// init_magics() computes all rook and bishop attacks at startup. Magic
// bitboards are used to look up attacks of sliding pieces. As a reference see
// www.chessprogramming.org/Magic_Bitboards. In particular, here we use the so
// called "fancy" approach.

Stockfish did it. Leaving the constants in the code is also fine. It's all about implementation.

ABCD
  • 22,419
  • 2
  • 43
  • 82
  • I see, I've successfully implemented the const approach, works great and easier to understand (I prefer less & solid code). I personally against the computing magics at startup because it will take time to read & understand that computing for other people (or me 3 months later). – 123iamking Mar 28 '20 at 08:47