changeset 5978:1569a1d140ae

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.
author Markus Mützel <markus.muetzel@gmx.de>
date Thu, 25 Nov 2021 18:37:52 +0100
parents fe9c2aa80854
children e617e117f94d
files installer-files/octave-launch.c
diffstat 1 files changed, 55 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- 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++)
     {