[gnome-photos/wip/max-threads: 77/77] gegl: Don't exceed arbitrary maximum number of threads




commit 5d4e0a63cd071ead997da4fe8fc7582a91303dbe
Author: Simon McVittie <smcv debian org>
Date:   Wed Dec 9 12:46:04 2020 +0000

    gegl: Don't exceed arbitrary maximum number of threads
    
    GEGL has an arbitrary maximum value for GeglConfig:threads, currently
    64, and exceeding it causes CRITCALs. This can make the build-time
    tests fail on massively parallel hardware. For example, on a dual
    POWER8 system used to build Debian ppc64el packages (2 CPUs, 10 cores
    per CPU and 8 threads per core, for a total of 160 threads), we ask
    GEGL to use 80 threads, which is more than 64.
    
    The simple approach to this would be to clamp to the range
    [1, GEGL_MAX_THREADS], but this would not be robust against possible
    future changes to the value of GEGL_MAX_THREADS, so this commit
    introspects the property to find out the true maximum at runtime.
    
    https://gitlab.gnome.org/GNOME/gnome-photos/-/merge_requests/154

 src/photos-gegl.c | 23 ++++++++++++++++++++---
 1 file changed, 20 insertions(+), 3 deletions(-)
---
diff --git a/src/photos-gegl.c b/src/photos-gegl.c
index c2e12af3..762bf9c3 100644
--- a/src/photos-gegl.c
+++ b/src/photos-gegl.c
@@ -680,21 +680,38 @@ void
 photos_gegl_init (void)
 {
   GeglConfig *config;
+  GParamSpec *threads_pspec;
+  GParamSpecInt *threads_pspec_int;
+  gint max_threads;
   gint threads;
   guint n_processors;
 
   n_processors = g_get_num_processors ();
-  g_return_if_fail (n_processors > 0);
+  config = gegl_config ();
+
+  /* GEGL has an arbitrary maximum for the number of threads. The
+   * maximum for the version of GEGL we were compiled against is given
+   * by the GEGL_MAX_THREADS macro, but there's no guarantee that the
+   * version we are eventually run against will be the same, so
+   * introspect the property to get the current maximum.
+   */
+  threads_pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (config), "threads");
+  g_return_if_fail (threads_pspec != NULL);
+  g_return_if_fail (G_IS_PARAM_SPEC_INT (threads_pspec));
+  threads_pspec_int = G_PARAM_SPEC_INT (threads_pspec);
+  g_return_if_fail (threads_pspec_int->maximum >= 1);
+  max_threads = threads_pspec_int->maximum;
 
   /* The number of threads should match the number of physical CPU
    * cores, not the number of virtual hyper-threading cores. In the
    * absence of an API to get the number of physical CPU cores, we
    * assume that a number higher than one is indicative of
    * hyper-threading, and hence divide by two.
+   *
+   * Make sure it's in the allowed range for the threads property.
    */
-  threads = (gint) (n_processors > 1 ? n_processors / 2 : n_processors);
+  threads = (gint) CLAMP (n_processors / 2, 1U, (guint) max_threads);
 
-  config = gegl_config ();
   g_object_set (config, "application-license", "GPL3", NULL);
   g_object_set (config, "threads", threads, NULL);
   g_object_set (config, "use-opencl", FALSE, NULL);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]