Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sage
Path: blob/develop/src/bin/sage-num-threads.py
4052 views
1
#!/usr/bin/env python3
2
#
3
# Determine the number of threads to be used by Sage.
4
# This is a simplified version of SAGE_ROOT/build/bin/sage-build-num-threads.py
5
#
6
# Outputs three space-separated numbers:
7
# 1) The number of threads to use for Sage, based on MAKE, MAKEFLAGS
8
# and SAGE_NUM_THREADS
9
# 2) The number of threads to use when parallel execution is explicitly
10
# asked for (e.g. sage -tp)
11
# 3) The number of CPU cores in the system, as determined by
12
# multiprocessing.cpu_count()
13
#
14
# AUTHOR: Jeroen Demeyer (2011-12-08): Github issue #12016
15
#
16
import os
17
import multiprocessing
18
19
20
def number_of_cores():
21
"""
22
Try to determine the number of CPU cores in this system.
23
If successful return that number. Otherwise return 1.
24
"""
25
# If the environment variable SAGE_NUM_CORES exists, use that value.
26
# This is useful for testing purposes.
27
try:
28
n = int(os.environ["SAGE_NUM_CORES"])
29
if n > 0:
30
return n
31
except (ValueError, KeyError):
32
pass
33
34
try:
35
n = multiprocessing.cpu_count()
36
if n > 0:
37
return n
38
except NotImplementedError:
39
pass
40
41
try: # Solaris fix
42
from subprocess import Popen, PIPE
43
p = Popen(['sysctl', '-n', 'hw.ncpu'],
44
stdin=PIPE, stdout=PIPE, stderr=PIPE, close_fds=True)
45
n = int(p.stdout.read().strip())
46
if n > 0:
47
return n
48
except (ValueError, OSError):
49
pass
50
51
return 1
52
53
54
def num_threads():
55
"""
56
Determine the number of threads from the environment variable
57
:envvar:`SAGE_NUM_THREADS`. If it is 0 or not provided, use a default
58
of ``min(8, number_of_cores)``.
59
60
OUTPUT:
61
62
a tuple (num_threads, num_threads_parallel, num_cores)
63
"""
64
num_cores = number_of_cores()
65
66
num_threads = None
67
68
# Number of threads to use when parallel execution is explicitly
69
# asked for
70
num_threads_parallel = num_threads
71
if num_threads_parallel is None:
72
num_threads_parallel = max(min(8, num_cores), 2)
73
74
try:
75
sage_num_threads = int(os.environ["SAGE_NUM_THREADS"])
76
if sage_num_threads == 0:
77
# If SAGE_NUM_THREADS is 0, use the default only
78
# if none of the above variables specified anything.
79
if num_threads is None:
80
num_threads = min(8, num_cores)
81
elif sage_num_threads > 0:
82
# SAGE_NUM_THREADS overrides everything
83
num_threads = sage_num_threads
84
num_threads_parallel = sage_num_threads
85
except (ValueError, KeyError):
86
pass
87
88
# If we still don't know, use 1 thread
89
if num_threads is None:
90
num_threads = 1
91
92
# Finally, use SAGE_NUM_THREADS_PARALLEL if set. A user isn't
93
# likely to set this, but it ensures that sage-env is idempotent
94
# if called more than once.
95
try:
96
sage_num_threads = int(os.environ["SAGE_NUM_THREADS_PARALLEL"])
97
if sage_num_threads > 0:
98
num_threads_parallel = sage_num_threads
99
except (ValueError, KeyError):
100
pass
101
102
return (num_threads, num_threads_parallel, num_cores)
103
104
105
print(*num_threads())
106
107