lucasart / Demolito

UCI Chess Engine
GNU General Public License v3.0
45 stars 9 forks source link

Include HashTable Memory Allocation on MacOS #144

Closed Spill-The-Tea closed 5 years ago

lucasart commented 5 years ago

Are you saying that aligned_alloc() does not exist on MacOS ? That's odd, because MacOS is supposed to be (at least somewhat0 POSIX compliant.

Also, we need aligned memory, so that hash entries never sit across 2 cache lines. calloc() (or rather malloc because we don't need zero init here) isn't good enough.

lucasart commented 5 years ago

Sorry, aligned_alloc() is C11, not POSIX. So your compiler is not C11 compliant. https://en.cppreference.com/w/c/memory/aligned_alloc clang or gcc work fine on linux and have C11 support for that. So I find it really strange that they wouldn't have it on MacOS.

Spill-The-Tea commented 5 years ago

Hmmm... Right, MacOS is posix compliant, but is not entirely C11 compliant (i.e. does not recognize aligned_alloc()). I looked through other chess engines written in c, including Ethereal, and the only memory allocation schemes it uses is malloc(), and memset().

At first I thought you were right about calloc(), because after playing around with the compiled executable, I wasn't able to change any uci options (specifically threads and hash); That was, until I realized that setoption [name] is not UCI Compliant, because it is case sensitive, e.g.: setoption Hash value 256 (this works) setoption hash value 256 (does nothing)

I did end up changing from calloc() to malloc(), to look like this instead: HashTable = malloc(sizeof(HashEntry) * (hashMB << 20));

Anyways, this is the only way I am able to compile it and have a working executable.

lucasart commented 5 years ago

which compiler are you using and what exactly do you type in the terminal to compile? And what error do you get?

Spill-The-Tea commented 5 years ago

I'm Just Using the makefile as is (where cc= clang), I get the following error:

ld: warning: option -s is obsolete and being ignored undef: _aligned_alloc Undefined symbols for architecture x86_64: "_aligned_alloc", referenced from: _main in cc-a12996.o _uci_loop in cc-a12996.o ld: symbol(s) not found for architecture x86_64 clang-8: fatal error: linker command failed with exit code 1 (use -v to see invocation) make: *** [all] Error 1

I also get the identical error if I use gcc instead.

lucasart commented 5 years ago

What about this?

posix_memalign(&HashTable, sizeof(HashEntry), hashMB << 20);

instead of

HashTable = aligned_alloc(sizeof(HashEntry), hashMB << 20);
Spill-The-Tea commented 5 years ago

I tried that previously, and while it does compile, the resulting executable fails to start due to segmentation fault 11.

lucasart commented 5 years ago

I find that hard to believe. This is kernel API. I know that MacOS sucks, but it can't suck to the point where a basic memory allocation function of the kernel is broken…

lucasart commented 5 years ago

Looks like I need to get my hands dirty, and write my own aligned memory allocation routines, to cater for these broken OS and/or C libraries…

lucasart commented 5 years ago

done

Spill-The-Tea commented 5 years ago

Thank you! It compiles and functions without a problem now.