Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/benchmark.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#include "benchmark.h"
#include "numa.h"
#include "misc.h"

#include <cstdlib>
#include <fstream>
Expand Down Expand Up @@ -483,7 +484,7 @@ BenchmarkSetup setup_benchmark(std::istream& is) {

float totalTime = 0;
for (const auto& game : BenchmarkPositions)
for (size_t i = 0; i < game.size(); ++i)
for (usize i = 0; i < game.size(); ++i)
totalTime += float(getCorrectedTime(i + 1));

float timeScaleFactor = static_cast<float>(desiredTimeS * 1000) / totalTime;
Expand Down
38 changes: 19 additions & 19 deletions src/memory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ namespace Stockfish {
// 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) {
void* std_aligned_alloc(usize alignment, usize size) {
#if defined(_ISOC11_SOURCE)
return aligned_alloc(alignment, size);
#elif defined(POSIXALIGNEDALLOC)
Expand Down Expand Up @@ -102,19 +102,19 @@ void std_aligned_free(void* ptr) {

#if defined(_WIN32)

static void* aligned_large_pages_alloc_windows([[maybe_unused]] size_t allocSize) {
static void* aligned_large_pages_alloc_windows([[maybe_unused]] usize allocSize) {

return windows_try_with_large_page_priviliges(
[&](size_t largePageSize) {
[&](usize largePageSize) {
// Round up size to full pages and allocate
allocSize = (allocSize + largePageSize - 1) & ~size_t(largePageSize - 1);
allocSize = (allocSize + largePageSize - 1) & ~usize(largePageSize - 1);
return VirtualAlloc(nullptr, allocSize, MEM_RESERVE | MEM_COMMIT | MEM_LARGE_PAGES,
PAGE_READWRITE);
},
[]() { return (void*) nullptr; });
}

void* aligned_large_pages_alloc_with_hint(size_t allocSize, bool) {
void* aligned_large_pages_alloc_with_hint(usize allocSize, bool) {

// Try to allocate large pages
void* mem = aligned_large_pages_alloc_windows(allocSize);
Expand All @@ -131,13 +131,13 @@ void* aligned_large_pages_alloc_with_hint(size_t allocSize, bool) {
#if defined(__linux__) && defined(MAP_HUGE_SHIFT) && defined(__x86_64__)
#define HAS_HUGE_PAGES

static std::map<void*, size_t> huge_pages;
static std::mutex huge_pages_mtx;
static std::map<void*, usize> huge_pages;
static std::mutex huge_pages_mtx;

static void* try_huge_pages_alloc(size_t allocSize) {
size_t size = ((allocSize + HugePageSize - 1) / HugePageSize) * HugePageSize;
void* mem = mmap(NULL, size, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB | (30 << MAP_HUGE_SHIFT), -1, 0);
static void* try_huge_pages_alloc(usize allocSize) {
usize size = ((allocSize + HugePageSize - 1) / HugePageSize) * HugePageSize;
void* mem = mmap(NULL, size, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB | (30 << MAP_HUGE_SHIFT), -1, 0);

if (mem == MAP_FAILED)
return nullptr;
Expand All @@ -148,7 +148,7 @@ static void* try_huge_pages_alloc(size_t allocSize) {
}
#endif // defined(__linux__) && defined(MAP_HUGE_SHIFT) && defined(__x86_64__)

void* aligned_large_pages_alloc_with_hint(size_t allocSize, [[maybe_unused]] bool hugePageHint) {
void* aligned_large_pages_alloc_with_hint(usize allocSize, [[maybe_unused]] bool hugePageHint) {
#ifdef HAS_HUGE_PAGES
if (hugePageHint && allocSize >= HugePageSize)
{
Expand All @@ -159,14 +159,14 @@ void* aligned_large_pages_alloc_with_hint(size_t allocSize, [[maybe_unused]] boo
#endif

#if defined(__linux__)
constexpr size_t alignment = 2 * 1024 * 1024; // 2MB page size assumed
constexpr usize alignment = 2 * 1024 * 1024; // 2MB page size assumed
#else
constexpr size_t alignment = 4096; // small page size assumed
constexpr usize alignment = 4096; // small page size assumed
#endif

// Round up to multiples of alignment
size_t size = ((allocSize + alignment - 1) / alignment) * alignment;
void* mem = std_aligned_alloc(alignment, size);
usize size = ((allocSize + alignment - 1) / alignment) * alignment;
void* mem = std_aligned_alloc(alignment, size);
#if defined(MADV_HUGEPAGE)
madvise(mem, size, MADV_HUGEPAGE);
#endif
Expand All @@ -175,16 +175,16 @@ void* aligned_large_pages_alloc_with_hint(size_t allocSize, [[maybe_unused]] boo

#endif

void* aligned_large_pages_alloc(size_t size) {
void* aligned_large_pages_alloc(usize size) {
return aligned_large_pages_alloc_with_hint(size, false);
}

bool has_large_pages() {

#if defined(_WIN32)

constexpr size_t page_size = 2 * 1024 * 1024; // 2MB page size assumed
void* mem = aligned_large_pages_alloc_windows(page_size);
constexpr usize page_size = 2 * 1024 * 1024; // 2MB page size assumed
void* mem = aligned_large_pages_alloc_windows(page_size);
if (mem == nullptr)
{
return false;
Expand Down
35 changes: 18 additions & 17 deletions src/memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include <cstring>

#include "types.h"
#include "misc.h"

#if defined(_WIN64)

Expand Down Expand Up @@ -61,14 +62,14 @@ using AdjustTokenPrivileges_t =

namespace Stockfish {

constexpr size_t HugePageSize = size_t(1) << 30;
constexpr usize HugePageSize = usize(1) << 30;

void* std_aligned_alloc(size_t alignment, size_t size);
void* std_aligned_alloc(usize alignment, usize size);
void std_aligned_free(void* ptr);

// Memory aligned by page size, min alignment: 4096 bytes
void* aligned_large_pages_alloc_with_hint(size_t size, bool hugePageHint);
void* aligned_large_pages_alloc(size_t size);
void* aligned_large_pages_alloc_with_hint(usize size, bool hugePageHint);
void* aligned_large_pages_alloc(usize size);
void aligned_large_pages_free(void* mem);

bool has_large_pages();
Expand Down Expand Up @@ -96,15 +97,15 @@ void memory_deleter_array(T* ptr, FREE_FUNC free_func) {


// Move back on the pointer to where the size is allocated
const size_t array_offset = std::max(sizeof(size_t), alignof(T));
char* raw_memory = reinterpret_cast<char*>(ptr) - array_offset;
const usize array_offset = std::max(sizeof(usize), alignof(T));
char* raw_memory = reinterpret_cast<char*>(ptr) - array_offset;

if constexpr (!std::is_trivially_destructible_v<T>)
{
const size_t size = *reinterpret_cast<size_t*>(raw_memory);
const usize size = *reinterpret_cast<usize*>(raw_memory);

// Explicitly call the destructor for each element in reverse order
for (size_t i = size; i-- > 0;)
for (usize i = size; i-- > 0;)
ptr[i].~T();
}

Expand All @@ -123,19 +124,19 @@ inline std::enable_if_t<!std::is_array_v<T>, T*> memory_allocator(ALLOC_FUNC all
// Allocates memory for an array of unknown bound and places it there with placement new
template<typename T, typename ALLOC_FUNC>
inline std::enable_if_t<std::is_array_v<T>, std::remove_extent_t<T>*>
memory_allocator(ALLOC_FUNC alloc_func, size_t num) {
memory_allocator(ALLOC_FUNC alloc_func, usize num) {
using ElementType = std::remove_extent_t<T>;

const size_t array_offset = std::max(sizeof(size_t), alignof(ElementType));
const usize array_offset = std::max(sizeof(usize), alignof(ElementType));

// Save the array size in the memory location
char* raw_memory =
reinterpret_cast<char*>(alloc_func(array_offset + num * sizeof(ElementType)));
ASSERT_ALIGNED(raw_memory, alignof(T));

new (raw_memory) size_t(num);
new (raw_memory) usize(num);

for (size_t i = 0; i < num; ++i)
for (usize i = 0; i < num; ++i)
new (raw_memory + array_offset + i * sizeof(ElementType)) ElementType();

// Need to return the pointer at the start of the array so that
Expand Down Expand Up @@ -178,7 +179,7 @@ std::enable_if_t<!std::is_array_v<T>, LargePagePtr<T>> make_unique_large_page(Ar

// make_unique_large_page for arrays of unknown bound
template<typename T>
std::enable_if_t<std::is_array_v<T>, LargePagePtr<T>> make_unique_large_page(size_t num) {
std::enable_if_t<std::is_array_v<T>, LargePagePtr<T>> make_unique_large_page(usize num) {
using ElementType = std::remove_extent_t<T>;

static_assert(alignof(ElementType) <= 4096,
Expand Down Expand Up @@ -214,18 +215,18 @@ using AlignedPtr =
// make_unique_aligned for single objects
template<typename T, typename... Args>
std::enable_if_t<!std::is_array_v<T>, AlignedPtr<T>> make_unique_aligned(Args&&... args) {
const auto func = [](size_t size) { return std_aligned_alloc(alignof(T), size); };
const auto func = [](usize size) { return std_aligned_alloc(alignof(T), size); };
T* obj = memory_allocator<T>(func, std::forward<Args>(args)...);

return AlignedPtr<T>(obj);
}

// make_unique_aligned for arrays of unknown bound
template<typename T>
std::enable_if_t<std::is_array_v<T>, AlignedPtr<T>> make_unique_aligned(size_t num) {
std::enable_if_t<std::is_array_v<T>, AlignedPtr<T>> make_unique_aligned(usize num) {
using ElementType = std::remove_extent_t<T>;

const auto func = [](size_t size) { return std_aligned_alloc(alignof(ElementType), size); };
const auto func = [](usize size) { return std_aligned_alloc(alignof(ElementType), size); };
ElementType* memory = memory_allocator<T>(func, num);

return AlignedPtr<T>(memory);
Expand Down Expand Up @@ -256,7 +257,7 @@ auto windows_try_with_large_page_priviliges([[maybe_unused]] FuncYesT&& fyes, Fu
HANDLE hProcessToken{};
LUID luid{};

const size_t largePageSize = GetLargePageMinimum();
const usize largePageSize = GetLargePageMinimum();
if (!largePageSize)
return fno();

Expand Down
2 changes: 1 addition & 1 deletion src/misc.h
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ template<typename T>
class RelaxedAtomic {
static constexpr bool UseAtomic =
#ifdef USE_SLOPPY_ATOMICS
!std::atomic<T>::is_always_lock_free || sizeof(T) > sizeof(size_t);
!std::atomic<T>::is_always_lock_free || sizeof(T) > sizeof(usize);
#else
true;
#endif
Expand Down
4 changes: 2 additions & 2 deletions src/movegen.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
#define MOVEGEN_H_INCLUDED

#include <algorithm> // IWYU pragma: keep
#include <cstddef>

#include "misc.h"
#include "types.h"

namespace Stockfish {
Expand Down Expand Up @@ -61,7 +61,7 @@ struct MoveList {
last(generate<T>(pos, moveList)) {}
const Move* begin() const { return moveList; }
const Move* end() const { return last; }
size_t size() const { return last - moveList; }
usize size() const { return last - moveList; }
bool contains(Move move) const { return std::find(begin(), end(), move) != end(); }

private:
Expand Down
4 changes: 2 additions & 2 deletions src/nnue/nnz_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

namespace Stockfish::Eval::NNUE {

template<size_t Dimensions>
template<usize Dimensions>
struct NNZInfo {

#if defined(USE_AVX512)
Expand Down Expand Up @@ -146,7 +146,7 @@ struct NNZInfo {
}
else
{
size_t bytes = sizeof(neurons1) / 32;
usize bytes = sizeof(neurons1) / 32;
memcpy(out, &m1, bytes);
out += bytes;
memcpy(out, &m2, bytes);
Expand Down
3 changes: 1 addition & 2 deletions src/nnue/simd.h
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,7 @@ inline __m128i _mm_cvtsi64_si128(i64 val) {
#ifdef __wasm__
#define vec_convert_8_16(a) wasm_i16x8_load8x8(reinterpret_cast<const void*>(&a))
#else
#define vec_convert_8_16(a) \
_mm_cvtepi8_epi16(_mm_cvtsi64_si128(static_cast<int64_t>(a)))
#define vec_convert_8_16(a) _mm_cvtepi8_epi16(_mm_cvtsi64_si128(static_cast<i64>(a)))
#endif
#else
// Credit: Yoshie2000
Expand Down
2 changes: 1 addition & 1 deletion src/search.h
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ class Worker {

LimitsType limits;

size_t pvIdx, pvLast;
usize pvIdx, pvLast;
RelaxedAtomic<u64> nodes, tbHits, bestMoveChanges;
int selDepth, nmpMinPly;

Expand Down
2 changes: 1 addition & 1 deletion src/thread_win32_osx.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ namespace Stockfish {
class NativeThread {
pthread_t thread;

static constexpr size_t TH_STACK_SIZE = 8 * 1024 * 1024;
static constexpr usize TH_STACK_SIZE = 8 * 1024 * 1024;

public:
template<class Function, class... Args>
Expand Down
7 changes: 4 additions & 3 deletions src/tune.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@
#ifndef TUNE_H_INCLUDED
#define TUNE_H_INCLUDED

#include <cstddef>
#include <memory>
#include <string>
#include <type_traits> // IWYU pragma: keep
#include <utility>
#include <vector>

#include "misc.h"

namespace Stockfish {

class OptionsMap;
Expand Down Expand Up @@ -132,9 +133,9 @@ class Tune {
}

// Template specialization for arrays: recursively handle multi-dimensional arrays
template<typename T, size_t N, typename... Args>
template<typename T, usize N, typename... Args>
int add(const SetRange& range, std::string&& names, T (&value)[N], Args&&... args) {
for (size_t i = 0; i < N; i++)
for (usize i = 0; i < N; i++)
add(range, next(names, i == N - 1) + "[" + std::to_string(i) + "]", value[i]);
return add(range, std::move(names), args...);
}
Expand Down
Loading