Page 1 of 1

OpenMP for chess programming

Posted: Fri Jun 28, 2019 1:12 am
by BeyondCritics
I have a simple question: Would you consider to use OpenMP for chess programming? If i look a certain chess programs, this would be possible. Can you back up your opinion with facts? Thanks in advance.

Re: OpenMP for chess programming

Posted: Fri Jun 28, 2019 2:17 am
by Dann Corbit

Re: OpenMP for chess programming

Posted: Fri Jun 28, 2019 5:52 pm
by Daniel Shawul
BeyondCritics wrote: Fri Jun 28, 2019 1:12 am I have a simple question: Would you consider to use OpenMP for chess programming? If i look a certain chess programs, this would be possible. Can you back up your opinion with facts? Thanks in advance.
OpenMP has task based parallelism since 4.0 i think, so why not. It is easier if you use easy algorithms such as
MCTS, LazySMP, ABDADA etc but should be possible with YBW too. All you need from the library is to be able
to fork threads and provide some primitive synchronizations.

Sample code from my MCTS chess and Hex engine that uses OpenMP on the CPU and CUDA on the GPU.

Code: Select all

//
// locks
//
#ifdef GPU
#	define LOCK          int
#	define l_create(x)   ((x) = 0)
#	define l_trylock(x)  (atomicExch(&(x),1))
#	define l_lock(x)     while(l_trylock(x) != 0);
#	define l_unlock(x)   (atomicExch(&(x),0))
#	define l_add(x,v)	 (atomicAdd(&x,v))
#	define l_sub(x,v)	 (atomicSub(&x,v))
#else
#	define LOCK          omp_lock_t
#	define l_create(x)   omp_init_lock(&x)
#	define l_trylock(x)  omp_test_lock(&x)
#	define l_lock(x)     omp_set_lock(&x)
#	define l_unlock(x)   omp_unset_lock(&x)   
template <class T>
inline void l_add(T x,T v) { 
	#pragma omp atomic 
		x+=v;
}
template <class T>
inline void l_sub(T x,T v) { 
	#pragma omp atomic 
		x-=v;
}
#endif