src/Pure/Concurrent/multithreading.scala
author wenzelm
Wed, 14 Feb 2024 14:41:18 +0100
changeset 79604 0e8ac7db1f4d
parent 79603 9f002cdb6b8d
child 79605 83627a092fbf
permissions -rw-r--r--
clarified num_processors: follow Poly/ML (with its inaccuracies);
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
79603
9f002cdb6b8d clarified modules, following Isabelle/ML;
wenzelm
parents:
diff changeset
     1
/*  Title:      Pure/Concurrent/multithreading.scala
9f002cdb6b8d clarified modules, following Isabelle/ML;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
9f002cdb6b8d clarified modules, following Isabelle/ML;
wenzelm
parents:
diff changeset
     3
9f002cdb6b8d clarified modules, following Isabelle/ML;
wenzelm
parents:
diff changeset
     4
Multithreading in Isabelle/Scala.
9f002cdb6b8d clarified modules, following Isabelle/ML;
wenzelm
parents:
diff changeset
     5
*/
9f002cdb6b8d clarified modules, following Isabelle/ML;
wenzelm
parents:
diff changeset
     6
9f002cdb6b8d clarified modules, following Isabelle/ML;
wenzelm
parents:
diff changeset
     7
package isabelle
9f002cdb6b8d clarified modules, following Isabelle/ML;
wenzelm
parents:
diff changeset
     8
9f002cdb6b8d clarified modules, following Isabelle/ML;
wenzelm
parents:
diff changeset
     9
9f002cdb6b8d clarified modules, following Isabelle/ML;
wenzelm
parents:
diff changeset
    10
object Multithreading {
79604
0e8ac7db1f4d clarified num_processors: follow Poly/ML (with its inaccuracies);
wenzelm
parents: 79603
diff changeset
    11
  /* physical processors */
0e8ac7db1f4d clarified num_processors: follow Poly/ML (with its inaccuracies);
wenzelm
parents: 79603
diff changeset
    12
0e8ac7db1f4d clarified num_processors: follow Poly/ML (with its inaccuracies);
wenzelm
parents: 79603
diff changeset
    13
  def num_processors(ssh: SSH.System = SSH.Local): Int =
0e8ac7db1f4d clarified num_processors: follow Poly/ML (with its inaccuracies);
wenzelm
parents: 79603
diff changeset
    14
    if (ssh.isabelle_platform.is_macos) {
0e8ac7db1f4d clarified num_processors: follow Poly/ML (with its inaccuracies);
wenzelm
parents: 79603
diff changeset
    15
      val result = ssh.execute("sysctl -n hw.physicalcpu").check
0e8ac7db1f4d clarified num_processors: follow Poly/ML (with its inaccuracies);
wenzelm
parents: 79603
diff changeset
    16
      Library.trim_line(result.out) match {
0e8ac7db1f4d clarified num_processors: follow Poly/ML (with its inaccuracies);
wenzelm
parents: 79603
diff changeset
    17
        case Value.Int(n) => n
0e8ac7db1f4d clarified num_processors: follow Poly/ML (with its inaccuracies);
wenzelm
parents: 79603
diff changeset
    18
        case _ => 1
0e8ac7db1f4d clarified num_processors: follow Poly/ML (with its inaccuracies);
wenzelm
parents: 79603
diff changeset
    19
      }
0e8ac7db1f4d clarified num_processors: follow Poly/ML (with its inaccuracies);
wenzelm
parents: 79603
diff changeset
    20
    }
0e8ac7db1f4d clarified num_processors: follow Poly/ML (with its inaccuracies);
wenzelm
parents: 79603
diff changeset
    21
    else {
0e8ac7db1f4d clarified num_processors: follow Poly/ML (with its inaccuracies);
wenzelm
parents: 79603
diff changeset
    22
      val Core_Id = """^\s*core id\s*:\s*(\d+)\s*$""".r
0e8ac7db1f4d clarified num_processors: follow Poly/ML (with its inaccuracies);
wenzelm
parents: 79603
diff changeset
    23
      val cpuinfo = ssh.read(Path.explode("/proc/cpuinfo"))
0e8ac7db1f4d clarified num_processors: follow Poly/ML (with its inaccuracies);
wenzelm
parents: 79603
diff changeset
    24
      (for (case Core_Id(Value.Int(i)) <- Library.trim_split_lines(cpuinfo)) yield i).toSet.size
0e8ac7db1f4d clarified num_processors: follow Poly/ML (with its inaccuracies);
wenzelm
parents: 79603
diff changeset
    25
    }
0e8ac7db1f4d clarified num_processors: follow Poly/ML (with its inaccuracies);
wenzelm
parents: 79603
diff changeset
    26
0e8ac7db1f4d clarified num_processors: follow Poly/ML (with its inaccuracies);
wenzelm
parents: 79603
diff changeset
    27
79603
9f002cdb6b8d clarified modules, following Isabelle/ML;
wenzelm
parents:
diff changeset
    28
  /* max_threads */
9f002cdb6b8d clarified modules, following Isabelle/ML;
wenzelm
parents:
diff changeset
    29
9f002cdb6b8d clarified modules, following Isabelle/ML;
wenzelm
parents:
diff changeset
    30
  def max_threads(): Int = {
9f002cdb6b8d clarified modules, following Isabelle/ML;
wenzelm
parents:
diff changeset
    31
    val m = Value.Int.unapply(System.getProperty("isabelle.threads", "0")) getOrElse 0
79604
0e8ac7db1f4d clarified num_processors: follow Poly/ML (with its inaccuracies);
wenzelm
parents: 79603
diff changeset
    32
    if (m > 0) m else (num_processors() max 1) min 8
79603
9f002cdb6b8d clarified modules, following Isabelle/ML;
wenzelm
parents:
diff changeset
    33
  }
9f002cdb6b8d clarified modules, following Isabelle/ML;
wenzelm
parents:
diff changeset
    34
}