From patchwork Fri Oct 15 10:27:00 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 46267 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 8003F3857C64 for ; Fri, 15 Oct 2021 10:28:32 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 8003F3857C64 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1634293712; bh=ufrkahc4o6atH44YZAu1rNxP2Jhzcc3mLwL52kAu+X4=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=RtgJCRXIN1j2WBdPvJ4w5J0E0hR7JAiuNz1N9SQEvjDMZAd89fi+ZrWYZP+1KOyvj 2YnU6zkSYEzlVUhWLqefGvg0ZaU7I8bDQ6Lc4KNAUvlLRVpWBSR3o+zcpl3+NmAFC3 bzzNVutYE91lumHoJBMfqZncl/R9ySP1YGNPBd6k= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by sourceware.org (Postfix) with ESMTP id 482893858015 for ; Fri, 15 Oct 2021 10:28:03 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 482893858015 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-480-F6J-M-wCPMSH8NRZrq-GxQ-1; Fri, 15 Oct 2021 06:27:05 -0400 X-MC-Unique: F6J-M-wCPMSH8NRZrq-GxQ-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id DDF7B824FB9; Fri, 15 Oct 2021 10:27:04 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.39.193.172]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 8870478319; Fri, 15 Oct 2021 10:27:04 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.16.1/8.16.1) with ESMTPS id 19FAR1R14132786 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Fri, 15 Oct 2021 12:27:02 +0200 Received: (from jakub@localhost) by tucnak.zalov.cz (8.16.1/8.16.1/Submit) id 19FAR12o4132785; Fri, 15 Oct 2021 12:27:01 +0200 Date: Fri, 15 Oct 2021 12:27:00 +0200 To: gcc-patches@gcc.gnu.org Subject: [committed] openmp: Add support for OMP_PLACES=numa_domains Message-ID: <20211015102700.GC304296@tucnak> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-5.5 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Jakub Jelinek via Gcc-patches From: Jakub Jelinek Reply-To: Jakub Jelinek Cc: Tobias Burnus Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" Hi! This adds support for numa_domains abstract name in OMP_PLACES, also new in OpenMP 5.1. Way to test this is OMP_PLACES=numa_domains OMP_DISPLAY_ENV=true LD_PRELOAD=.libs/libgomp.so.1 /bin/true and see what it prints on OMP_PLACES line. For non-NUMA machines it should print a single place that covers all CPUs, for NUMA machine one place for each NUMA node with corresponding CPUs. Bootstrapped/regtested on x86_64-linux and i686-linux and tested on powerpc64le-linux too, committed to trunk. 2021-10-15 Jakub Jelinek * env.c (parse_places_var): Handle numa_domains as level 5. * config/linux/affinity.c (gomp_affinity_init_numa_domains): New function. (gomp_affinity_init_level): Use it instead of gomp_affinity_init_level_1 for level == 5. * testsuite/libgomp.c/places-5.c: New test. Jakub --- libgomp/env.c.jj 2021-10-14 15:25:48.816212823 +0200 +++ libgomp/env.c 2021-10-14 18:53:34.238432698 +0200 @@ -701,6 +701,11 @@ parse_places_var (const char *name, bool env += 9; level = 4; } + else if (strncasecmp (env, "numa_domains", 12) == 0) + { + env += 12; + level = 5; + } if (level) { count = ULONG_MAX; --- libgomp/config/linux/affinity.c.jj 2021-10-14 17:13:12.811302863 +0200 +++ libgomp/config/linux/affinity.c 2021-10-14 19:22:37.820259007 +0200 @@ -355,6 +355,102 @@ gomp_affinity_init_level_1 (int level, i free (line); } +static void +gomp_affinity_init_numa_domains (unsigned long count, cpu_set_t *copy, + char *name) +{ + FILE *f; + char *nline = NULL, *line = NULL; + size_t nlinelen = 0, linelen = 0; + char *q; + size_t prefix_len = sizeof ("/sys/devices/system/node/") - 1; + + strcpy (name, "/sys/devices/system/node/online"); + f = fopen (name, "r"); + if (f == NULL || getline (&nline, &nlinelen, f) <= 0) + { + if (f) + fclose (f); + return; + } + fclose (f); + q = nline; + while (*q && *q != '\n' && gomp_places_list_len < count) + { + unsigned long nfirst, nlast; + + errno = 0; + nfirst = strtoul (q, &q, 10); + if (errno) + break; + nlast = nfirst; + if (*q == '-') + { + errno = 0; + nlast = strtoul (q + 1, &q, 10); + if (errno || nlast < nfirst) + break; + } + for (; nfirst <= nlast; nfirst++) + { + sprintf (name + prefix_len, "node%lu/cpulist", nfirst); + f = fopen (name, "r"); + if (f == NULL) + continue; + if (getline (&line, &linelen, f) > 0) + { + char *p = line; + void *pl = NULL; + + while (*p && *p != '\n') + { + unsigned long first, last; + bool seen = false; + + errno = 0; + first = strtoul (p, &p, 10); + if (errno) + break; + last = first; + if (*p == '-') + { + errno = 0; + last = strtoul (p + 1, &p, 10); + if (errno || last < first) + break; + } + for (; first <= last; first++) + { + if (!CPU_ISSET_S (first, gomp_cpuset_size, copy)) + continue; + if (pl == NULL) + { + pl = gomp_places_list[gomp_places_list_len]; + gomp_affinity_init_place (pl); + } + if (gomp_affinity_add_cpus (pl, first, 1, 0, true)) + { + CPU_CLR_S (first, gomp_cpuset_size, copy); + if (!seen) + { + gomp_places_list_len++; + seen = true; + } + } + } + if (*p == ',') + ++p; + } + } + fclose (f); + } + if (*q == ',') + ++q; + } + free (line); + free (nline); +} + bool gomp_affinity_init_level (int level, unsigned long count, bool quiet) { @@ -377,8 +473,11 @@ gomp_affinity_init_level (int level, uns copy = gomp_alloca (gomp_cpuset_size); strcpy (name, "/sys/devices/system/cpu/cpu"); memcpy (copy, gomp_cpusetp, gomp_cpuset_size); - gomp_affinity_init_level_1 (level, level > 3 ? level : 3, count, copy, name, - quiet); + if (level == 5) + gomp_affinity_init_numa_domains (count, copy, name); + else + gomp_affinity_init_level_1 (level, level > 3 ? level : 3, count, copy, + name, quiet); if (gomp_places_list_len == 0) { if (!quiet) --- libgomp/testsuite/libgomp.c/places-5.c.jj 2021-10-15 11:31:58.161312737 +0200 +++ libgomp/testsuite/libgomp.c/places-5.c 2021-10-15 11:32:05.498206896 +0200 @@ -0,0 +1,10 @@ +/* { dg-set-target-env-var OMP_PLACES "numa_domains" } */ + +#include + +int +main () +{ + omp_display_env (0); + return 0; +}