Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sage
Path: blob/develop/docker/setup-make-parallelity.sh
4055 views
1
#!/bin/sh
2
3
# Source this to set CPUTHREADS (the number of apparent cores) and RAMTHREADS
4
# (free RAM divided by the maximum amount needed per thread typically)
5
# From this this script infers reasonable defaults for SAGE_NUM_THREADS and
6
# MAKEFLAGS.
7
8
# We do exactly the same for CPUTHREADS_DOCBUILD, RAMTHREADS_DOCBUILD,
9
# SAGE_NUM_THREADS_DOCBUILD, MAKEFLAGS_DOCBUILD. As the docbuild needs
10
# substantially more RAM as of May 2018.
11
12
# ****************************************************************************
13
# Copyright (C) 2018 Julian Rüth <[email protected]>
14
#
15
# This program is free software: you can redistribute it and/or modify
16
# it under the terms of the GNU General Public License as published by
17
# the Free Software Foundation, either version 2 of the License, or
18
# (at your option) any later version.
19
# http://www.gnu.org/licenses/
20
# ****************************************************************************
21
22
set -ex
23
24
if [ -z "$CPUTHREADS" ]; then
25
# Determine the number of threads that can run simultaneously on this system
26
# (we might not have nproc available.)
27
# Note that this value is incorrect for some CI providers (notably CircleCI:
28
# https://circleci.com/docs/2.0/configuration-reference/#resource_class) which
29
# provision fewer vCPUs than shown in /proc/cpuinfo. So it is probably better
30
# to set CPUTHREADS manuall in your CI configuration.
31
CPUTHREADS=`docker run docker cat /proc/cpuinfo | grep -E '^processor' | wc -l`
32
fi
33
if [ -z "$CPUTHREADS_DOCBUILD" ]; then
34
CPUTHREADS_DOCBUILD=$CPUTHREADS
35
fi
36
37
if [ -z "$RAMTHREADS" ]; then
38
RAMTHREADS=$(( `docker run docker cat /proc/meminfo | grep MemTotal | awk '{ print $2 }'` / 1048576 ))
39
if [ $RAMTHREADS = 0 ];then
40
RAMTHREADS=1;
41
fi
42
fi
43
if [ -z "$RAMTHREADS_DOCBUILD" ]; then
44
RAMTHREADS_DOCBUILD=$(( `docker run docker cat /proc/meminfo | grep MemTotal | awk '{ print $2 }'` / 2097152 ))
45
if [ $RAMTHREADS_DOCBUILD = 0 ];then
46
RAMTHREADS_DOCBUILD=1;
47
fi
48
fi
49
50
# On CI machines with their virtual CPUs, it seems to be quite beneficial to
51
# overcommit on CPU usage. We only need to make sure that we do not exceed RAM
52
# (as there is no swap.)
53
if [ $CPUTHREADS -lt $RAMTHREADS ]; then
54
export SAGE_NUM_THREADS=$((CPUTHREADS + 1))
55
else
56
export SAGE_NUM_THREADS=$RAMTHREADS
57
fi
58
if [ $CPUTHREADS_DOCBUILD -lt $RAMTHREADS_DOCBUILD ]; then
59
export SAGE_NUM_THREADS_DOCBUILD=$((CPUTHREADS_DOCBUILD + 1))
60
else
61
export SAGE_NUM_THREADS_DOCBUILD=$RAMTHREADS_DOCBUILD
62
fi
63
# Set -j and -l for make (though -l is probably ignored by Sage)
64
export MAKEFLAGS="-j $SAGE_NUM_THREADS -l $((CPUTHREADS - 1)).8"
65
export MAKEFLAGS_DOCBUILD="-j $SAGE_NUM_THREADS_DOCBUILD -l $((CPUTHREADS_DOCBUILD - 1)).8"
66
67