|
| 1 | +#!/usr/bin/env sage-system-python |
| 2 | +# |
| 3 | +# Determine the number of threads to be used by Sage. |
| 4 | +# |
| 5 | +# Outputs three space-separated numbers: |
| 6 | +# 1) The number of threads to use for Sage, based on MAKE, MAKEFLAGS |
| 7 | +# and SAGE_NUM_THREADS |
| 8 | +# 2) The number of threads to use when parallel execution is explicitly |
| 9 | +# asked for (e.g. sage -tp) |
| 10 | +# 3) The number of CPU cores in the system, as determined by |
| 11 | +# multiprocessing.cpu_count() |
| 12 | +# |
| 13 | +# AUTHOR: Jeroen Demeyer (2011-12-08): Trac ticket #12016 |
| 14 | +# |
| 15 | + |
| 16 | +from __future__ import print_function |
| 17 | + |
| 18 | +import os |
| 19 | +import multiprocessing |
| 20 | +import re |
| 21 | +import math |
| 22 | + |
| 23 | + |
| 24 | +def number_of_cores(): |
| 25 | + """ |
| 26 | + Try to determine the number of CPU cores in this system. |
| 27 | + If successful return that number. Otherwise return 1. |
| 28 | + """ |
| 29 | + # If the environment variable SAGE_NUM_CORES exists, use that value. |
| 30 | + # This is useful for testing purposes. |
| 31 | + try: |
| 32 | + n = int(os.environ["SAGE_NUM_CORES"]) |
| 33 | + if n > 0: |
| 34 | + return n |
| 35 | + except (ValueError, KeyError): |
| 36 | + pass |
| 37 | + |
| 38 | + try: |
| 39 | + n = multiprocessing.cpu_count() |
| 40 | + if n > 0: |
| 41 | + return n |
| 42 | + except NotImplementedError: |
| 43 | + pass |
| 44 | + |
| 45 | + try: # Solaris fix |
| 46 | + from subprocess import Popen, PIPE |
| 47 | + p = Popen(['sysctl', '-n', 'hw.ncpu'], |
| 48 | + stdin=PIPE, stdout=PIPE, stderr=PIPE, close_fds=True) |
| 49 | + n = int(p.stdout.read().strip()) |
| 50 | + if n > 0: |
| 51 | + return n |
| 52 | + except (ValueError, OSError): |
| 53 | + pass |
| 54 | + |
| 55 | + return 1 |
| 56 | + |
| 57 | + |
| 58 | +def parse_jobs_from_MAKE(MAKE, unlimited=999999): |
| 59 | + """ |
| 60 | + Parse an environment variable like :envvar:`MAKE` for the number of |
| 61 | + jobs specified. This looks at arguments ``-j``, ``--jobs``, ``-l``, |
| 62 | + ``--load-average``. |
| 63 | + |
| 64 | + INPUT: |
| 65 | + |
| 66 | + - ``MAKE`` -- The value of :envvar:`MAKE` or :envvar:`MAKEFLAGS`. |
| 67 | + |
| 68 | + - ``unlimited`` -- The value to return when ``MAKE`` contains ``-j`` |
| 69 | + without argument and no ``-l`` option. Normally this is interpreted |
| 70 | + as "unlimited". |
| 71 | + |
| 72 | + OUTPUT: |
| 73 | + |
| 74 | + The number of jobs specified by that variable. Raise ``KeyError`` |
| 75 | + if no number of jobs is specified in ``MAKE``. |
| 76 | + """ |
| 77 | + # First, find value of -j |
| 78 | + # Since this is doing a greedy match on the left and non-greedy on the right, |
| 79 | + # we find the last -j or --jobs |
| 80 | + (j, num) = re.subn(r'^(.* )?(-j *|--jobs(=(?=[0-9])| +))([0-9]*)( .*?)?$', r'\4', MAKE, count=1) |
| 81 | + if num < 1: |
| 82 | + # No replacement done, i.e. no -j option found |
| 83 | + raise KeyError("No number of jobs specified") |
| 84 | + elif j == "": |
| 85 | + # j is empty: unlimited number of jobs! :-) |
| 86 | + j = unlimited |
| 87 | + else: |
| 88 | + j = int(j) |
| 89 | + if j <= 0: |
| 90 | + raise ValueError("Non-positive value specified for -j") |
| 91 | + |
| 92 | + # Next, find the value of -l |
| 93 | + # If it is specified, use this as an upper bound on j |
| 94 | + (l, num) = re.subn(r'^(.* )?(-l *|--(load-average|max-load)(=(?=[0-9])| +))([0-9.]*)( .*?)?$', r'\5', MAKE, count=1) |
| 95 | + if num < 1: |
| 96 | + # No replacement done, i.e. no -l option found |
| 97 | + pass |
| 98 | + elif not l: |
| 99 | + # No load limit specified |
| 100 | + pass |
| 101 | + else: |
| 102 | + l = int(math.ceil(float(l))) |
| 103 | + # A load limit will never prevent starting at least one job |
| 104 | + if l <= 1: |
| 105 | + l = 1 |
| 106 | + j = min(j, l) |
| 107 | + |
| 108 | + return j |
| 109 | + |
| 110 | + |
| 111 | +def num_threads(): |
| 112 | + """ |
| 113 | + Determine the number of threads from the environment variables |
| 114 | + (in decreasing priority) :envvar:`SAGE_NUM_THREADS`, :envvar:`MAKE`, |
| 115 | + :envvar:`MAKEFLAGS` and :envvar:`MFLAGS`. |
| 116 | + |
| 117 | + If :envvar:`SAGE_NUM_THREADS` is 0 and neither :envvar:`MAKE` nor |
| 118 | + :envvar:`MAKEFLAGS` specifies a number of jobs, the use a default |
| 119 | + of ``min(8, number_of_cores)``. |
| 120 | + |
| 121 | + OUTPUT: |
| 122 | + |
| 123 | + a tuple (num_threads, num_threads_parallel, num_cores) |
| 124 | + """ |
| 125 | + num_cores = number_of_cores() |
| 126 | + |
| 127 | + num_threads = None |
| 128 | + # Handle MFLAGS only for backwards compatibility |
| 129 | + try: |
| 130 | + num_threads = parse_jobs_from_MAKE(os.environ["MFLAGS"], unlimited=2) |
| 131 | + except (ValueError, KeyError): |
| 132 | + pass |
| 133 | + |
| 134 | + try: |
| 135 | + # Prepend hyphen to MAKEFLAGS if it does not start with one |
| 136 | + MAKEFLAGS = os.environ["MAKEFLAGS"] |
| 137 | + if MAKEFLAGS[0] != '-': |
| 138 | + MAKEFLAGS = '-' + MAKEFLAGS |
| 139 | + # In MAKEFLAGS, "-j" does not mean unlimited. It probably |
| 140 | + # means an inherited number of jobs, let us use 2 for safety. |
| 141 | + num_threads = parse_jobs_from_MAKE(MAKEFLAGS, unlimited=2) |
| 142 | + except (ValueError, KeyError, IndexError): |
| 143 | + pass |
| 144 | + |
| 145 | + try: |
| 146 | + num_threads = parse_jobs_from_MAKE(os.environ["MAKE"]) |
| 147 | + except (ValueError, KeyError): |
| 148 | + pass |
| 149 | + |
| 150 | + # Number of threads to use when parallel execution is explicitly |
| 151 | + # asked for |
| 152 | + num_threads_parallel = num_threads |
| 153 | + if num_threads_parallel is None: |
| 154 | + num_threads_parallel = max(min(8, num_cores), 2) |
| 155 | + |
| 156 | + try: |
| 157 | + sage_num_threads = int(os.environ["SAGE_NUM_THREADS"]) |
| 158 | + if sage_num_threads == 0: |
| 159 | + # If SAGE_NUM_THREADS is 0, use the default only |
| 160 | + # if none of the above variables specified anything. |
| 161 | + if num_threads is None: |
| 162 | + num_threads = min(8, num_cores) |
| 163 | + elif sage_num_threads > 0: |
| 164 | + # SAGE_NUM_THREADS overrides everything |
| 165 | + num_threads = sage_num_threads |
| 166 | + num_threads_parallel = sage_num_threads |
| 167 | + except (ValueError, KeyError): |
| 168 | + pass |
| 169 | + |
| 170 | + # If we still don't know, use 1 thread |
| 171 | + if num_threads is None: |
| 172 | + num_threads = 1 |
| 173 | + |
| 174 | + # Finally, use SAGE_NUM_THREADS_PARALLEL if set. A user isn't |
| 175 | + # likely to set this, but it ensures that sage-env is idempotent |
| 176 | + # if called more than once. |
| 177 | + try: |
| 178 | + sage_num_threads = int(os.environ["SAGE_NUM_THREADS_PARALLEL"]) |
| 179 | + if sage_num_threads > 0: |
| 180 | + num_threads_parallel = sage_num_threads |
| 181 | + except (ValueError, KeyError): |
| 182 | + pass |
| 183 | + |
| 184 | + return (num_threads, num_threads_parallel, num_cores) |
| 185 | + |
| 186 | + |
| 187 | +print(*num_threads()) |
0 commit comments