ARG BASE_IMAGE=nvidia/cuda:11.6.0-devel-ubuntu20.04
FROM ${BASE_IMAGE}

RUN echo Etc/UTC > /etc/timezone && ln -s /usr/share/zoneinfo/Etc/UTC /etc/localtime
RUN apt-get update && apt-get install -y --no-install-recommends \
    automake \
    cmake \
    git \
    gfortran-10 \
    gcc-10 \
    g++-10 \
    libblis-serial-dev \
    liblapack-dev \
    libtool \
    pkg-config \
    python3-distutils \
    python3-pip \
    ssh \
    zlib1g-dev \
  && rm -rf /var/lib/apt/lists/*

RUN useradd -ms /bin/bash review
USER review
WORKDIR /home/review
ENV PETSC_DIR=/home/review/petsc PETSC_ARCH=ompi-cuda
RUN git clone --depth=1 --branch=release https://gitlab.com/petsc/petsc.git
RUN cd petsc && \
  OPT='-O3 -funsafe-math-optimizations -march=haswell'; \
  python3 configure \
    --with-debugging=0 CC=gcc-10 CXX=g++-10 FC=gfortran-10 COPTFLAGS="$OPT" CXXOPTFLAGS="$OPT" FOPTFLAGS="$OPT" \
    --with-cuda \
    --with-cuda-arch=80 \
    --with-fortran-bindings=0 \
    --download-openmpi \
    --download-kokkos \
    --download-kokkos-kernels \
    --download-metis \
    --download-mumps \
    --download-hypre \
    --download-parmetis \
    --download-scalapack \
    --with-zlib \
    && \
  make \
    && \
  rm -rf $PETSC_ARCH/externalpackages $PETSC_ARCH/obj $PETSC_ARCH/tests

RUN git clone https://github.com/CEED/libCEED.git
RUN cd libCEED && \
  git checkout v0.10.1 && \
  make configure CC=gcc-10 CXX=g++-10 OPT='-O3 -funsafe-math-optimizations -march=haswell' CUDA_DIR=/usr/local/cuda && \
  make -j8

RUN git clone https://gitlab.com/micromorph/ratel.git
RUN cd ratel && \
  git checkout v0.1.1 && \
  make -j8 LDFLAGS='-L/home/review/ratel/lib -Wl,-rpath,/home/review/ratel/lib -L/home/review/petsc/ompi-cuda/lib -L/home/review/libCEED/lib -Wl,-rpath,/home/review/petsc/ompi-cuda/lib -Wl,-rpath,/home/review/libCEED/lib -L/usr/local/cuda/lib64/stubs -lcuda'

ENV PATH=/home/review/petsc/ompi-cuda/bin:/home/review/ratel/bin:$PATH
RUN python3 -m pip install pandas seaborn

ADD --chown=review:review runs/ /home/review/runs/
ADD --chown=review:review figures/ /home/review/figures/
ADD --chown=review:review meshes/ /home/review/meshes/
ADD --chown=review:review Welcome.md Dockerfile /home/review/

LABEL maintainer='Jed Brown <jed@jedbrown.org>'
LABEL description='PETSc with CUDA and GPU-aware Open MPI'
