mirror of
https://github.com/sockspls/badfish
synced 2025-04-30 16:53:09 +00:00
[NNUE] Wrap aligned_alloc
For some systems std::aligned_alloc is not available even if c++17 is specified. Wrap the function and use specific solutions. Update macosx-version-min to the required minimum. No functional change.
This commit is contained in:
parent
2b0ba70436
commit
98ffe0cd97
5 changed files with 31 additions and 4 deletions
|
@ -363,8 +363,8 @@ endif
|
||||||
endif
|
endif
|
||||||
|
|
||||||
ifeq ($(KERNEL),Darwin)
|
ifeq ($(KERNEL),Darwin)
|
||||||
CXXFLAGS += -arch $(arch) -mmacosx-version-min=10.9
|
CXXFLAGS += -arch $(arch) -mmacosx-version-min=10.15
|
||||||
LDFLAGS += -arch $(arch) -mmacosx-version-min=10.9
|
LDFLAGS += -arch $(arch) -mmacosx-version-min=10.15
|
||||||
endif
|
endif
|
||||||
|
|
||||||
### Travis CI script uses COMPILER to overwrite CXX
|
### Travis CI script uses COMPILER to overwrite CXX
|
||||||
|
|
24
src/misc.cpp
24
src/misc.cpp
|
@ -46,6 +46,7 @@ typedef bool(*fun3_t)(HANDLE, CONST GROUP_AFFINITY*, PGROUP_AFFINITY);
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include <cstdlib>
|
||||||
|
|
||||||
#if defined(__linux__) && !defined(__ANDROID__)
|
#if defined(__linux__) && !defined(__ANDROID__)
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
@ -293,6 +294,29 @@ void prefetch(void* addr) {
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
/// Wrappers for systems where the c++17 implementation doesn't guarantee the availability of aligned_alloc.
|
||||||
|
/// Memory allocated with std_aligned_alloc must be freed with std_aligned_free.
|
||||||
|
///
|
||||||
|
|
||||||
|
void* std_aligned_alloc(size_t alignment, size_t size) {
|
||||||
|
#if defined(__APPLE__)
|
||||||
|
return aligned_alloc(alignment, size);
|
||||||
|
#elif defined(_MSC_VER)
|
||||||
|
return _aligned_alloc(size, alignment);
|
||||||
|
#else
|
||||||
|
return std::aligned_alloc(alignment, size);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
void std_aligned_free(void* ptr) {
|
||||||
|
#if defined(__APPLE__)
|
||||||
|
free(ptr);
|
||||||
|
#elif defined(_MSC_VER)
|
||||||
|
_aligned_free(ptr);
|
||||||
|
#else
|
||||||
|
free(ptr);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
/// aligned_ttmem_alloc() will return suitably aligned memory, and if possible use large pages.
|
/// aligned_ttmem_alloc() will return suitably aligned memory, and if possible use large pages.
|
||||||
/// The returned pointer is the aligned one, while the mem argument is the one that needs
|
/// The returned pointer is the aligned one, while the mem argument is the one that needs
|
||||||
|
|
|
@ -33,6 +33,8 @@ const std::string engine_info(bool to_uci = false);
|
||||||
const std::string compiler_info();
|
const std::string compiler_info();
|
||||||
void prefetch(void* addr);
|
void prefetch(void* addr);
|
||||||
void start_logger(const std::string& fname);
|
void start_logger(const std::string& fname);
|
||||||
|
void* std_aligned_alloc(size_t alignment, size_t size);
|
||||||
|
void std_aligned_free(void* ptr);
|
||||||
void* aligned_ttmem_alloc(size_t size, void*& mem);
|
void* aligned_ttmem_alloc(size_t size, void*& mem);
|
||||||
void aligned_ttmem_free(void* mem); // nop if mem == nullptr
|
void aligned_ttmem_free(void* mem); // nop if mem == nullptr
|
||||||
|
|
||||||
|
|
|
@ -57,7 +57,7 @@ namespace Eval::NNUE {
|
||||||
template <typename T>
|
template <typename T>
|
||||||
void Initialize(AlignedPtr<T>& pointer) {
|
void Initialize(AlignedPtr<T>& pointer) {
|
||||||
|
|
||||||
pointer.reset(reinterpret_cast<T*>(std::aligned_alloc(alignof(T), sizeof(T))));
|
pointer.reset(reinterpret_cast<T*>(std_aligned_alloc(alignof(T), sizeof(T))));
|
||||||
std::memset(pointer.get(), 0, sizeof(T));
|
std::memset(pointer.get(), 0, sizeof(T));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -5,6 +5,7 @@
|
||||||
|
|
||||||
#include "nnue_feature_transformer.h"
|
#include "nnue_feature_transformer.h"
|
||||||
#include "nnue_architecture.h"
|
#include "nnue_architecture.h"
|
||||||
|
#include "../misc.h"
|
||||||
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
|
||||||
|
@ -19,7 +20,7 @@ namespace Eval::NNUE {
|
||||||
struct AlignedDeleter {
|
struct AlignedDeleter {
|
||||||
void operator()(T* ptr) const {
|
void operator()(T* ptr) const {
|
||||||
ptr->~T();
|
ptr->~T();
|
||||||
std::free(ptr);
|
std_aligned_free(ptr);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue