Skip to content

Commit e119f6c

Browse files
committed
Docker: optionally install OpenBLAS, numpy, & scipy from source
When building a `wcm-runtime` Docker Image, make the default be to install numpy & scipy from wheels with their embedded copies of OpenBLAS. It's **much faster** and it's the usual approach so we're less likely to hit installer bugs like #1040. The reason for compiling OpenBLAS from source was originally to get a bug fix and later to avoid the AVX2 problem building it in Docker-for-Mac. This does change Parca outputs but that's all part of #931.
1 parent 56359d2 commit e119f6c

File tree

2 files changed

+31
-20
lines changed

2 files changed

+31
-20
lines changed

cloud/build-containers-locally.sh

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77

88
set -eu
99

10-
# On macOS, build with `NO_AVX2=1` to avoid the OpenBLAS 0.3.6+ self-test failures
11-
# and bad results when building and running in Docker Desktop on macOS. This
12-
# heuristic won't help if you build an image on Linux then run it on macOS.
13-
# See the Dockerfile.
10+
# To avoid OpenBLAS self-test failures when compiling it in Docker Desktop on
11+
# macOS, build the Image with `COMPILE_BLAS=0` (=> don't compile OpenBLAS) or
12+
# with `NO_AVX2=1` (=> compile it to not use AVX2 vector instructions).
13+
COMPILE_BLAS=0
1414
if [ "$(uname -s)" == Darwin ]; then NO_AVX2=1; else NO_AVX2=0; fi
15-
echo NO_AVX2=$NO_AVX2
15+
echo COMPILE_BLAS=$COMPILE_BLAS, NO_AVX2=$NO_AVX2
1616

1717
WCM_RUNTIME=${USER}-wcm-runtime
1818
WCM_CODE=${USER}-wcm-code
@@ -22,7 +22,9 @@ TIMESTAMP=$(date '+%Y%m%d.%H%M%S')
2222

2323
# Docker image #1: The Python runtime environment.
2424
docker build -f cloud/docker/runtime/Dockerfile -t "${WCM_RUNTIME}" \
25-
--build-arg NO_AVX2=$NO_AVX2 .
25+
--build-arg COMPILE_BLAS=$COMPILE_BLAS \
26+
--build-arg NO_AVX2=$NO_AVX2 \
27+
.
2628

2729
# Docker image #2: The Whole Cell Model code on the runtime environment.
2830
# See this Dockerfile for usage instructions.

cloud/docker/runtime/Dockerfile

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,25 @@ RUN echo "alias ls='ls --color=auto'" >> ~/.bashrc \
2222
RUN apt-get update \
2323
&& apt-get install -y swig gfortran llvm cmake nano
2424

25-
# Option `--build-arg NO_AVX2=1` controls OpenBLAS' use of AVX2 vector instructions:
25+
# Option `--build-arg COMPILE_BLAS=1` makes this Dockerfile compile OpenBLAS and
26+
# also compile numpy and scipy from source code so they link to this OpenBLAS.
27+
# That can add 2 hours to the build time on Google Cloud Build and it might
28+
# produce more consistent results.
29+
ARG COMPILE_BLAS=0
30+
ENV COMPILE_BLAS="$COMPILE_BLAS"
31+
32+
RUN if [ "${COMPILE_BLAS}" != 0 ]; then \
33+
(echo "[openblas]" \
34+
&& echo "libraries = openblas" \
35+
&& echo "library_dirs = /usr/lib" \
36+
&& echo "include_dirs = /usr/include") > ~/.numpy-site.cfg; \
37+
fi
38+
39+
# If COMPILE_BLAS=1 then option `--build-arg NO_AVX2=1` compiles OpenBLAS' to
40+
# not use AVX2 vector instructions:
2641
#
27-
# * NO_AVX2=1 is needed to build properly in Docker-for-Mac due to a Docker bug,
28-
# but it computes slightly different results.
42+
# * NO_AVX2=1 is needed to compile OpenBLAS in Docker-for-Mac due to a Docker
43+
# bug, but it computes slightly different results.
2944
# * NO_AVX2=0 gets more consistent results and reportedly runs 20-30% faster
3045
# (although it only saves ~7% in a cell sim). Use this when building in
3146
# Docker-for-Linux. The built Images seem to run fine on Mac.
@@ -39,21 +54,15 @@ ENV NO_AVX2="$NO_AVX2"
3954

4055
# Install openblas (must be 0.3.5+) for numpy, scipy, and Theano.
4156
ENV OPENBLAS_LABEL=v0.3.13
42-
RUN (mkdir -p openblas && cd openblas \
57+
RUN if [ -f ~/.numpy-site.cfg ]; then \
58+
(mkdir -p openblas && cd openblas \
4359
&& curl -SL https://github.com/xianyi/OpenBLAS/archive/${OPENBLAS_LABEL}.tar.gz | tar -xz \
4460
&& cd OpenBLAS* \
4561
&& echo "Compiling OpenBLAS ${OPENBLAS_LABEL} with NO_AVX2=${NO_AVX2}" \
4662
&& make "NO_AVX2=${NO_AVX2}" FC=gfortran \
4763
&& make "NO_AVX2=${NO_AVX2}" PREFIX=/usr install) \
48-
&& rm -r openblas
49-
50-
# Build numpy and scipy from source (`--no-binary`) to link them to this
51-
# OpenBLAS. This can add 50 minutes to the build time to get more consistent
52-
# results and go faster at run time.
53-
RUN (echo "[openblas]" \
54-
&& echo "libraries = openblas" \
55-
&& echo "library_dirs = /usr/lib" \
56-
&& echo "include_dirs = /usr/include") > ~/.numpy-site.cfg
64+
&& rm -r openblas; \
65+
fi
5766

5867
# This gets more consistent results from openblas.
5968
ENV OPENBLAS_NUM_THREADS=1
@@ -66,7 +75,7 @@ ENV OPENBLAS_NUM_THREADS=1
6675
COPY requirements.txt /
6776
RUN (b1="" \
6877
&& if [ -f ~/.numpy-site.cfg ] ; then b1="--no-binary=numpy,scipy"; fi \
69-
&& echo "Installing pips with $b1" \
78+
&& echo "Installing pips with '$b1'" \
7079
&& pip install --no-cache-dir --upgrade pip setuptools wheel \
7180
&& pip install --no-cache-dir numpy==1.19.5 $b1 \
7281
&& pip install --no-cache-dir -r requirements.txt $b1 \

0 commit comments

Comments
 (0)