# HG changeset patch # User Markus Mützel # Date 1637861872 -3600 # Node ID 1569a1d140aeacf5830bfc3d47c2106034d41cc1 # Parent fe9c2aa80854006b6961ba6c5192461d9c545e5e octave-launch: Set number of OpenBLAS threads to number of cores (bug #61208). * installer-files/octave-launch.c (get_num_physical_cores): New function to get number of physical processor cores. (wmain): Set OPENBLAS_NUM_THREADS to number of physical processor cores for performance. diff -r fe9c2aa80854 -r 1569a1d140ae installer-files/octave-launch.c --- a/installer-files/octave-launch.c Thu Nov 25 17:05:38 2021 +0100 +++ b/installer-files/octave-launch.c Thu Nov 25 18:37:52 2021 +0100 @@ -30,7 +30,7 @@ return len; } -wchar_t * FilePart (wchar_t *dir) +static wchar_t * FilePart (wchar_t *dir) { int len = lstrlenW (dir); while (len > 0 && dir[len-1] != L'\\') @@ -38,6 +38,43 @@ return &dir[len]; } +static size_t get_num_physical_cores (void) +{ + DWORD length; + PSYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX lpi; + BOOL res; + size_t num_physical_cores; + size_t offset; + + length = 0; + GetLogicalProcessorInformationEx (RelationProcessorCore, NULL, &length); + if (GetLastError () != ERROR_INSUFFICIENT_BUFFER) + return 0; + + lpi = malloc (length); + res = GetLogicalProcessorInformationEx (RelationProcessorCore, lpi, &length); + if (! res) + { + free (lpi); + return 0; + } + + num_physical_cores = 0; + offset = 0; + do + { + const PSYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX cur_lpi = + (PSYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX) (lpi + offset); + offset += cur_lpi->Size; + num_physical_cores++; + } + while (offset < length); + + free (lpi); + + return num_physical_cores; +} + // Set up environment and launch octave.exe with appropriate // arguments. NOTE: There are corresponding VBS and BAT script // versions of this program so any changes here may need to be made in @@ -220,6 +257,23 @@ SetEnvironmentVariableW (L"HOME", newhome); } + /* set number of OpenBLAS threads */ + nSize = GetEnvironmentVariableW (L"OPENBLAS_NUM_THREADS", NULL, 0); + if (nSize == 0) + { + /* Only set if it wasn't already set in the environment */ + size_t num_threads; + num_threads = get_num_physical_cores (); + + if (num_threads > 0) + { +#define THREADS_SZ 64 + wchar_t buffer[THREADS_SZ]; + StringCchPrintfW (buffer, THREADS_SZ, L"%zu", num_threads); + SetEnvironmentVariableW (L"OPENBLAS_NUM_THREADS", buffer); + } + } + /* check for gui mode */ for (i = 1; i < argc; i++) {