all messages for Guix-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: "Ludovic Courtès" <ludo@gnu.org>
To: 52283@debbugs.gnu.org
Cc: "Ludovic Courtès" <ludo@gnu.org>
Subject: [bug#52283] [PATCH v2 01/12] Add (guix cpu).
Date: Thu, 16 Dec 2021 18:58:16 +0100	[thread overview]
Message-ID: <20211216175827.2077-2-ludo@gnu.org> (raw)
In-Reply-To: <20211216175827.2077-1-ludo@gnu.org>

* guix/cpu.scm: New file.
* Makefile.am (MODULES): Add it.
---
 Makefile.am  |   1 +
 guix/cpu.scm | 143 +++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 144 insertions(+)
 create mode 100644 guix/cpu.scm

diff --git a/Makefile.am b/Makefile.am
index c4ccee65f1..dba9f4da82 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -81,6 +81,7 @@ MODULES =					\
   guix/base64.scm				\
   guix/ci.scm					\
   guix/cpio.scm					\
+  guix/cpu.scm					\
   guix/deprecation.scm				\
   guix/docker.scm	   			\
   guix/records.scm				\
diff --git a/guix/cpu.scm b/guix/cpu.scm
new file mode 100644
index 0000000000..e1911f52a8
--- /dev/null
+++ b/guix/cpu.scm
@@ -0,0 +1,143 @@
+;;; GNU Guix --- Functional package management for GNU
+;;; Copyright © 2021 Ludovic Courtès <ludo@gnu.org>
+;;;
+;;; This file is part of GNU Guix.
+;;;
+;;; GNU Guix is free software; you can redistribute it and/or modify it
+;;; under the terms of the GNU General Public License as published by
+;;; the Free Software Foundation; either version 3 of the License, or (at
+;;; your option) any later version.
+;;;
+;;; GNU Guix is distributed in the hope that it will be useful, but
+;;; WITHOUT ANY WARRANTY; without even the implied warranty of
+;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+;;; GNU General Public License for more details.
+;;;
+;;; You should have received a copy of the GNU General Public License
+;;; along with GNU Guix.  If not, see <http://www.gnu.org/licenses/>.
+
+(define-module (guix cpu)
+  #:use-module (guix sets)
+  #:use-module (guix memoization)
+  #:use-module (srfi srfi-1)
+  #:use-module (srfi srfi-9)
+  #:use-module (ice-9 match)
+  #:use-module (ice-9 rdelim)
+  #:export (current-cpu
+            cpu?
+            cpu-architecture
+            cpu-family
+            cpu-model
+            cpu-flags
+
+            cpu->gcc-architecture))
+
+;;; Commentary:
+;;;
+;;; This module provides tools to determine the micro-architecture supported
+;;; by the CPU and to map it to a name known to GCC's '-march'.
+;;;
+;;; Code:
+
+;; CPU description.
+(define-record-type <cpu>
+  (cpu architecture family model flags)
+  cpu?
+  (architecture cpu-architecture)                 ;string, from 'uname'
+  (family       cpu-family)                       ;integer
+  (model        cpu-model)                        ;integer
+  (flags        cpu-flags))                       ;set of strings
+
+(define current-cpu
+  (mlambda ()
+    "Return a <cpu> record representing the host CPU."
+    (define (prefix? prefix)
+      (lambda (str)
+        (string-prefix? prefix str)))
+
+    (call-with-input-file "/proc/cpuinfo"
+      (lambda (port)
+        (let loop ((family #f)
+                   (model #f))
+          (match (read-line port)
+            ((? eof-object?)
+             #f)
+            ((? (prefix? "cpu family") str)
+             (match (string-tokenize str)
+               (("cpu" "family" ":" family)
+                (loop (string->number family) model))))
+            ((? (prefix? "model") str)
+             (match (string-tokenize str)
+               (("model" ":" model)
+                (loop family (string->number model)))
+               (_
+                (loop family model))))
+            ((? (prefix? "flags") str)
+             (match (string-tokenize str)
+               (("flags" ":" flags ...)
+                (cpu (utsname:machine (uname))
+                     family model (list->set flags)))))
+            (_
+             (loop family model))))))))
+
+(define (cpu->gcc-architecture cpu)
+  "Return the architecture name, suitable for GCC's '-march' flag, that
+corresponds to CPU, a record as returned by 'current-cpu'."
+  (match (cpu-architecture cpu)
+    ("x86_64"
+     ;; Transcribed from GCC's 'host_detect_local_cpu' in driver-i386.c.
+     (or (and (= 6 (cpu-family cpu))              ;the "Pentium Pro" family
+              (letrec-syntax ((model (syntax-rules (=>)
+                                       ((_) #f)
+                                       ((_ (candidate => integers ...) rest
+                                           ...)
+                                        (or (and (= (cpu-model cpu) integers)
+                                                 candidate)
+                                            ...
+                                            (model rest ...))))))
+                (model ("bonnel" => #x1c #x26)
+                       ("silvermont" => #x37 #x4a #x4d #x5a #x5d)
+                       ("core2" => #x0f #x17 #x1d)
+                       ("nehalem" => #x1a #x1e #x1f #x2e)
+                       ("westmere" => #x25 #x2c #x2f)
+                       ("sandybridge" => #x2a #x2d)
+                       ("ivybridge" => #x3a #x3e)
+                       ("haswell" => #x3c #x3f #x45 #x46)
+                       ("broadwell" => #x3d #x47 #x4f #x56)
+                       ("skylake" => #x4e #x5e #x8e #x9e)
+                       ("skylake-avx512" => #x55) ;TODO: cascadelake
+                       ("knl" => #x57)
+                       ("cannonlake" => #x66)
+                       ("knm" => #x85))))
+
+         ;; Fallback case for non-Intel processors or for Intel processors not
+         ;; recognized above.
+         (letrec-syntax ((if-flags (syntax-rules (=>)
+                                     ((_)
+                                      #f)
+                                     ((_ (flags ... => name) rest ...)
+                                      (if (every (lambda (flag)
+                                                   (set-contains? (cpu-flags cpu)
+                                                                  flag))
+                                                 '(flags ...))
+                                          name
+                                          (if-flags rest ...))))))
+           (if-flags ("avx512" => "knl")
+                     ("adx" => "broadwell")
+                     ("avx2" => "haswell")
+                     ;; TODO: tigerlake, cooperlake, etc.
+                     ("avx" => "sandybridge")
+                     ("sse4_2" "gfni" => "tremont")
+                     ("sse4_2" "sgx" => "goldmont-plus")
+                     ("sse4_2" "xsave" => "goldmont")
+                     ("sse4_2" "movbe" => "silvermont")
+                     ("sse4_2" => "nehalem")
+                     ("ssse3" "movbe" => "bonnell")
+                     ("ssse3" => "core2")))
+
+         ;; TODO: Recognize AMD models (bdver*, znver*, etc.)?
+
+         "x86_64"))
+    (architecture
+     ;; TODO: AArch64.
+     architecture)))
-- 
2.33.0





  reply	other threads:[~2021-12-16 17:59 UTC|newest]

Thread overview: 41+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-12-04 20:34 [bug#52283] [PATCH 00/10] Tuning packages for CPU micro-architectures Ludovic Courtès
2021-12-04 20:49 ` [bug#52283] [PATCH 01/10] Add (guix cpu) Ludovic Courtès
2021-12-04 20:49   ` [bug#52283] [PATCH 02/10] transformations: Add '--tune' Ludovic Courtès
2021-12-06 23:18     ` Thiago Jung Bauermann via Guix-patches via
2021-12-07  8:04       ` Ludovic Courtès
2021-12-07 10:32         ` zimoun
2021-12-07 14:52           ` Ludovic Courtès
2021-12-07 15:52             ` zimoun
2021-12-09  9:19               ` Ludovic Courtès
2021-12-09 10:35                 ` zimoun
2021-12-10  8:49                   ` Ludovic Courtès
2021-12-04 20:49   ` [bug#52283] [PATCH 03/10] ci: Add extra jobs for tunable packages Ludovic Courtès
2021-12-04 20:49   ` [bug#52283] [PATCH 04/10] gnu: Add eigen-benchmarks Ludovic Courtès
2021-12-04 20:49   ` [bug#52283] [PATCH 05/10] gnu: Add xsimd-benchmark Ludovic Courtès
2021-12-04 20:49   ` [bug#52283] [PATCH 06/10] gnu: Add xtensor-benchmark Ludovic Courtès
2021-12-04 20:49   ` [bug#52283] [PATCH 07/10] gnu: ceres-solver: Mark as tunable Ludovic Courtès
2021-12-04 20:49   ` [bug#52283] [PATCH 08/10] gnu: Add ceres-solver-benchmarks Ludovic Courtès
2021-12-04 20:49   ` [bug#52283] [PATCH 09/10] gnu: libfive: Mark as tunable Ludovic Courtès
2021-12-04 20:49   ` [bug#52283] [PATCH 10/10] gnu: prusa-slicer: " Ludovic Courtès
2021-12-05  8:37   ` [bug#52283] [PATCH 00/10] Tuning packages for CPU micro-architectures Mathieu Othacehe
2021-12-06 10:38     ` Ludovic Courtès
2021-12-06 12:47       ` zimoun
2021-12-07  8:39       ` Mathieu Othacehe
2021-12-07  9:02         ` Ludovic Courtès
2021-12-06 16:48     ` Ludovic Courtès
2021-12-04 21:11 ` Ludovic Courtès
2021-12-07  9:13 ` Ludovic Courtès
2021-12-16 17:58   ` [bug#52283] [PATCH v2 00/12] " Ludovic Courtès
2021-12-16 17:58     ` Ludovic Courtès [this message]
2021-12-16 17:58     ` [bug#52283] [PATCH v2 02/12] gnu: gcc: Add 'compiler-cpu-architectures' property Ludovic Courtès
2021-12-16 17:58     ` [bug#52283] [PATCH v2 03/12] gnu: clang: " Ludovic Courtès
2021-12-16 17:58     ` [bug#52283] [PATCH v2 04/12] transformations: Add '--tune' Ludovic Courtès
2021-12-16 17:58     ` [bug#52283] [PATCH v2 05/12] ci: Add extra jobs for tunable packages Ludovic Courtès
2021-12-16 17:58     ` [bug#52283] [PATCH v2 06/12] gnu: Add eigen-benchmarks Ludovic Courtès
2021-12-16 17:58     ` [bug#52283] [PATCH v2 07/12] gnu: Add xsimd-benchmark Ludovic Courtès
2021-12-16 17:58     ` [bug#52283] [PATCH v2 08/12] gnu: Add xtensor-benchmark Ludovic Courtès
2021-12-16 17:58     ` [bug#52283] [PATCH v2 09/12] gnu: ceres-solver: Mark as tunable Ludovic Courtès
2021-12-16 17:58     ` [bug#52283] [PATCH v2 10/12] gnu: Add ceres-solver-benchmarks Ludovic Courtès
2021-12-16 17:58     ` [bug#52283] [PATCH v2 11/12] gnu: libfive: Mark as tunable Ludovic Courtès
2021-12-16 17:58     ` [bug#52283] [PATCH v2 12/12] gnu: prusa-slicer: " Ludovic Courtès
2022-01-01 14:59     ` bug#52283: [PATCH 00/10] Tuning packages for CPU micro-architectures Ludovic Courtès

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20211216175827.2077-2-ludo@gnu.org \
    --to=ludo@gnu.org \
    --cc=52283@debbugs.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/guix.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.