16#include <mkl_service.h>
49 std::ifstream f(
"/sys/devices/system/cpu/cpu0/cache/index2/size");
56 GetLogicalProcessorInformation(
nullptr, &bufSize);
63 const size_t count =
static_cast<size_t>(bufSize) /
sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION);
64 std::vector<SYSTEM_LOGICAL_PROCESSOR_INFORMATION> buf(count);
65 GetLogicalProcessorInformation(buf.data(), &bufSize);
67 for (
size_t i = 0; i < count; ++i)
69 if (buf[i].Relationship == RelationCache && buf[i].Cache.Level == 2)
70 return buf[i].Cache.Size;
87 size_t wSize = inSize * outSize *
sizeof(float);
88 size_t bytesPerBatch = (outSize + inSize + inSize) *
sizeof(
float);
90 size_t remaining = (l2 > wSize) ? l2 - wSize : l2 / 2;
91 size_t B = remaining / bytesPerBatch;
116 static int currentThreads = -1;
118 const int THRESHOLD = 32;
119 int maxProcs = omp_get_num_procs();
130 int scaled = 8 + (batchSize / 1024) * 4;
131 const int MAX_USEFUL_THREADS = std::min(scaled, 16);
134 int targetThreads = (batchSize < THRESHOLD) ? 1 : std::min(maxProcs, MAX_USEFUL_THREADS);
136 if (currentThreads != targetThreads)
138 omp_set_num_threads(targetThreads);
139#ifdef DEEPITY_USE_MKL
140 mkl_set_num_threads(targetThreads);
144 currentThreads = targetThreads;
151 __m128 lo = _mm256_castps256_ps128(x);
152 __m128 hi = _mm256_extractf128_ps(x, 1);
153 lo = _mm_add_ps(lo, hi);
155 __m128 shuf = _mm_movehdup_ps(lo);
156 __m128 sums = _mm_add_ps(lo, shuf);
157 shuf = _mm_movehl_ps(shuf, sums);
158 sums = _mm_add_ss(sums, shuf);
160 return _mm_cvtss_f32(sums);
166 __m128 shuf = _mm_movehdup_ps(x);
167 __m128 sums = _mm_add_ps(x, shuf);
168 shuf = _mm_movehl_ps(shuf, sums);
169 sums = _mm_add_ss(sums, shuf);
170 return _mm_cvtss_f32(sums);
void openblas_set_num_threads(int num_threads)
OpenBLAS's own thread-count setter, declared directly since OpenBLAS ships no public header for it in...
This file exists to provide both Tensor.h and IComputeBackend.h with device types without introducing...
Definition Backend.cpp:10
static void DynamicThread(int batchSize) noexcept
Sets OpenMP's (and the BLAS library's) thread count for the given batch size, memoized so it's a no-o...
Definition Optimize.h:114
size_t GetL2CacheBytes()
Queries the size of this CPU's L2 cache (per-core, level 2), falling back to 512 KiB on platforms whe...
Definition Optimize.h:46
static float hsum128_ps(__m128 x)
Horizontal sum of all 4 lanes of an SSE register.
Definition Optimize.h:164
size_t AutoBatchSize(size_t inSize, size_t outSize)
Picks a batch size (a power of 2 clamped to [64, 512]) sized so a layer's weights plus one batch's ac...
Definition Optimize.h:83
static float hsum256_ps(__m256 x)
Horizontal sum of all 8 lanes of an AVX2 register.
Definition Optimize.h:149