Stockfish haswell optimized build

Discussion of anything and everything relating to chess playing software and machines.

Moderator: Ras

j_romang
Posts: 99
Joined: Mon May 16, 2011 2:52 am

Re: Stockfish haswell optimized build

Post by j_romang »

Here is the attack code, with no changes elsewhere :
https://github.com/jromang/Stockfish/bl ... ard.h#L271
Gerd Isenberg
Posts: 2251
Joined: Wed Mar 08, 2006 8:47 pm
Location: Hattingen, Germany

Re: Stockfish haswell optimized build

Post by Gerd Isenberg »

j_romang wrote:Here is the attack code, with no changes elsewhere :
https://github.com/jromang/Stockfish/bl ... ard.h#L271
Thanks, yes Ronald's PDEP/PEXT 210.5k lookup approach. Wow, 4% is a huge speedup considering only attack-getters are changed, but of course affecting other memory and cache issues in other areas of the program. Assuming you also tried the PEXT only approach with 4 times greater tables, Ronald had the right sense, congratulations!

http://www.talkchess.com/forum/viewtopi ... 11&start=3

Cheers,
Gerd
phenri
Posts: 284
Joined: Tue Aug 13, 2013 9:44 am

Re: Stockfish haswell optimized build

Post by phenri »

I would probably have no utility of this patch before some months, or even some years.
But thank you for the future.

I have a question that has nothing to do with BMI2.
I'm asking you because I do not really expect a response from Marco.

I probably be wrong, but I want to understand.
Why in the makefile, POPCNT comes with the flag -msse3 instead of -msse4.2 while POPCNT is present only for architectures with a minimum SSE4.2.

And why the flag -mpopcnt is not included?

Code: Select all

### 3.9 popcnt
ifeq ($(popcnt),yes)
	CXXFLAGS += -msse3 -DUSE_POPCNT
endif

Same for preftech why the flag is so low?

Code: Select all

### 3.7 prefetch
ifeq ($(prefetch),yes)
	ifeq ($(sse),yes)
		CXXFLAGS += -msse
		DEPENDFLAGS += -msse
	endif
else
	CXXFLAGS += -DNO_PREFETCH
endif

Regards,

Paul
syzygy
Posts: 6052
Joined: Tue Feb 28, 2012 11:56 pm

Re: Stockfish haswell optimized build

Post by syzygy »

It might be interesting to try this.
In bitboard.h change

Code: Select all

struct BMI2Info {
  unsigned short *data;
  uint64_t mask1;
  uint64_t mask2;
};

extern unsigned short attack_table[107648];
extern struct BMI2Info bishop_bmi2[64];
extern struct BMI2Info rook_bmi2[64];

template<PieceType Pt>
inline Bitboard attacks_bb(Square s, Bitboard occ) {
  struct BMI2Info *info = (Pt == ROOK ? &rook_bmi2[s] : &bishop_bmi2[s]);
  return _pdep_u64(info->data[_pext_u64(occ, info->mask1)], info->mask2);
}
into

Code: Select all

struct BMI2Info {
  uint64_t *data;
  uint64_t mask;
};

extern struct BMI2Info bishop_bmi2[64];
extern struct BMI2Info rook_bmi2[64];

template<PieceType Pt>
inline Bitboard attacks_bb(Square s, Bitboard occ) {
  struct BMI2Info *info = (Pt == ROOK ? &rook_bmi2[s] : &bishop_bmi2[s]);
  return info->data[_pext_u64(occ, info->mask)];
}
In bitboard.cpp change the lines

Code: Select all

static unsigned short attacks_table[107648];
...
        info[sq].mask1 = bb
...
          if (i == 0)
        info[sq].mask2 = bb2;
          attacks_table[idx++] = _pext_u64(bb2, info[sq].mask2);
into

Code: Select all

static uint64_t attacks_table[107648];
...
        info[sq].mask = bb
...
          attacks_table[idx++] = bb2;
I did not test this, so maybe something is wrong or missing.

This might be faster and this might be slower, but it would be interesting to know.
syzygy
Posts: 6052
Joined: Tue Feb 28, 2012 11:56 pm

Re: Stockfish haswell optimized build

Post by syzygy »

Gerd Isenberg wrote:Assuming you also tried the PEXT only approach with 4 times greater tables, Ronald had the right sense, congratulations!
I don't think he tried, but we can now find out :-)
Gerd Isenberg
Posts: 2251
Joined: Wed Mar 08, 2006 8:47 pm
Location: Hattingen, Germany

Re: Stockfish haswell optimized build

Post by Gerd Isenberg »

syzygy wrote:
Gerd Isenberg wrote:Assuming you also tried the PEXT only approach with 4 times greater tables, Ronald had the right sense, congratulations!
I don't think he tried, but we can now find out :-)
Would be nice if Jean-Francois or you could try and report. U64 versus pdep_u64(U16, mask2).
j_romang
Posts: 99
Joined: Mon May 16, 2011 2:52 am

Re: Stockfish haswell optimized build

Post by j_romang »

I didn't try :wink:
j_romang
Posts: 99
Joined: Mon May 16, 2011 2:52 am

Re: Stockfish haswell optimized build

Post by j_romang »

Gerd Isenberg wrote: Wow, 4% is a huge speedup considering only attack-getters are changed, but of course affecting other memory and cache issues in other areas of the program.
According to my profiling experiments stockfish spends about 5-6% of time computing attack bitboards, that's we I wanted to give a try to the pext solution.
syzygy
Posts: 6052
Joined: Tue Feb 28, 2012 11:56 pm

Re: Stockfish haswell optimized build

Post by syzygy »

phenri wrote:Why in the makefile, POPCNT comes with the flag -msse3 instead of -msse4.2 while POPCNT is present only for architectures with a minimum SSE4.2.
It is not entirely true that POPCNT needs SSE4.2. On AMD native popcnt came with SSE4a.

I tried compiling with -msse4.2. It did not improve the speed.
And why the flag -mpopcnt is not included?

Code: Select all

### 3.9 popcnt
ifeq ($(popcnt),yes)
	CXXFLAGS += -msse3 -DUSE_POPCNT
endif
Because stockfish uses inline assembly for native popcount, so the compiler does not need to be told it can generate the popcnt instruction. It would be different if stockfish used the __builtin_popcountll() compiler intrinsic.
Same for preftech why the flag is so low?

Code: Select all

### 3.7 prefetch[/quote]
I did not check, but I suppose the prefetch instruction is available on systems with sse. So no need to generate executables that don't work on systems that do not have sse2 or higher.
syzygy
Posts: 6052
Joined: Tue Feb 28, 2012 11:56 pm

Re: Stockfish haswell optimized build

Post by syzygy »

j_romang wrote:I didn't try :wink:
See the suggested code changes I posted above. They try.