#
# To build this image and run it with a shell, first change into the directory
# where this Dockerfile lives, then:
#
# sudo docker build -t pv-v5.6.0-egl .
#
# Or, to choose a different version:
#
# sudo docker build --build-arg PARAVIEW_TAG=master --build-arg SUPERBUILD_TAG=master -t pv-master-egl .
#
# Run the image with bash to look around:
#
# sudo docker run --runtime=nvidia -ti pv-v5.6.0-egl bash
#

#
# Build options
#
#   BASE_IMAGE
#
#     nvidia/opengl:1.0-glvnd-devel-ubuntu18.04
#     ubuntu:18.04
#
#   RENDERING
#
#     egl
#     osmesa
#
#   PARAVIEW_TAG
#
#     "master", "v5.6.0", <branch-name>, etc...
#
#   SUPERBUILD_REPO
#
#     allows cloning from a fork
#
#   SUPERBUILD_TAG
#
#     "master", "v5.6.0", <branch-name>, etc...
#
#   PYTHON_VERSION
#
#     2
#     3
#
#     (default is "2")
#
#   BUILD_TYPE
#
#     "Release", or "Debug" (Default: "Release")
#

FROM nvidia/cudagl:10.1-devel-ubuntu18.04

ARG RENDERING=egl
ARG PARAVIEW_TAG=v5.8.0
ARG SUPERBUILD_TAG=enable-rtx-local-build
ARG PARAVIEW_VERSION_STRING=paraview-5.8
ARG SUPERBUILD_REPO=https://gitlab.kitware.com/nleaf/paraview-superbuild.git
ARG PYTHON_VERSION=2
ARG BUILD_TYPE=Release

# The following environment variables help with the Python 2 vs 3 option while
# we still support both of them.  In order for this approach to work, we need
# the RUN command to use bash instead of sh for variable indirection.
SHELL ["/bin/bash", "-c"]

ENV SYSTEM_PYTHON_2_PIP pip
ENV SYSTEM_PYTHON_3_PIP pip3
ENV SYSTEM_PYTHON_PIP "SYSTEM_PYTHON_${PYTHON_VERSION}_PIP"

RUN apt-get update && apt-get install -y --no-install-recommends \
        autoconf \
        automake \
        build-essential \
        ca-certificates \
        chrpath \
        curl \
        gfortran \
        git \
        libtool \
        openssl \
        libglu1-mesa-dev \
        libnuma-dev \
        librdmacm-dev \
        libpmi2-0-dev \
        flex \
        pkg-config && \
    if [ "${PYTHON_VERSION}" = "2" ]; then apt-get install -y --no-install-recommends python2.7-dev python-pip python-setuptools; fi && \
    if [ "${PYTHON_VERSION}" = "3" ]; then apt-get install -y --no-install-recommends python3-dev python3-pip python3-setuptools; fi && \
    if [ "${PYTHON_VERSION}" = "3" ]; then update-alternatives --install /usr/bin/python python /usr/bin/python3 1; fi && \
    rm -rf /var/lib/apt/lists/* && \
    ${!SYSTEM_PYTHON_PIP} install mako


################################################################################
### 
# XPMEM/git:master
###

RUN cd /tmp \
 && git clone --depth 1 -b master https://gitlab.com/hjelmn/xpmem \
 && cd xpmem \
 && autoreconf --install \
 && ./configure --disable-kernel-module --prefix=/usr/local/xpmem \
 && make \
 && make install

RUN echo /usr/local/xpmem/lib >> /etc/ld.so.conf.d/nvidia.conf \
 && ldconfig

### 
# KNEM/knem-1.1.3
###

RUN cd /tmp \
 && git clone --depth 1 -b knem-1.1.3 https://gforge.inria.fr/git/knem/knem.git \
 && cd knem \
 && mkdir -p /usr/local/knem/include \
 && cp common/knem_io.h /usr/local/knem/include

RUN echo /usr/local/knem/lib >> /etc/ld.so.conf.d/nvidia.conf \
 && ldconfig

### 
# OFED/apt-get
###

### 
# UCX/v1.8.0
###

RUN cd /tmp \
 && mkdir -p ./usr/local/ucx \
 && cd ./usr/local/ucx \
 && git clone --depth 1 -b v1.8.0 https://github.com/openucx/ucx.git \
 && cd ucx \
 && ./autogen.sh \
 && CFLAGS="-Wl,-rpath=None/lib" ./configure --prefix=/usr/local/ucx \
    --enable-compiler-opt=3 \
    --with-avx \
    --disable-doxygen-doc \
    --disable-logging \
    --disable-debug \
    --disable-assertions \
    --disable-params-check \
    --without-cuda \
    --with-knem=/usr/local/knem \
    --with-xpmem=/usr/local/xpmem \
    --with-verbs \
    --with-rdmacm \
    --enable-shared \
    --enable-static=no \
    --enable-cma \
 && make -j $(nproc) all \
 && make install

RUN echo /usr/local/ucx/lib >> /etc/ld.so.conf.d/nvidia.conf \
 && ldconfig

### 
# OpenMPI/v4.0.3 with UCX and slurm pmi2 support
###

ENV MPI_DIRECTORY="/usr/local/openmpi"
RUN cd /tmp \
 && cp /usr/include/slurm-wlm/* /usr/include \
 && git clone --depth 1 -b v4.0.3 https://github.com/open-mpi/ompi.git \
 && cd ompi \
 && ./autogen.pl \
 && ./configure --prefix=$MPI_DIRECTORY \
    --with-slurm \
    --with-pmix \
    --with-libevent \
    --with-pmi \
    --disable-oshmem \
    --enable-orterun-prefix-by-default \
    --disable-debug \
    --disable-mem-debug \
    --disable-mem-profile \
    --disable-memchecker \
    --without-cuda \
    --with-ucx=/usr/local/ucx \
    --with-ucx-libdir=/usr/local/ucx/lib \
    --with-hwloc=internal \
    --enable-mpi1-compatibility \
    --enable-shared \
    --disable-static \
 && make -j $(nproc) \
 && make install

RUN echo /usr/local/openmpi/lib >> /etc/ld.so.conf.d/nvidia.conf \
 && ldconfig
################################################################################




# Create a non-root user
RUN groupadd pv-user && \
    useradd -g pv-user -d /home/pv-user pv-user && \
    mkdir /home/pv-user && chown -R pv-user:pv-user /home/pv-user && \
    mkdir -p /opt/paraview && chown -R pv-user:pv-user /opt/paraview

USER pv-user

# Get CMake
RUN mkdir -p /home/pv-user/cmake/3.16.4 && cd /home/pv-user/cmake/3.16.4 && \
    curl -L https://cmake.org/files/v3.16/cmake-3.16.4-Linux-x86_64.tar.gz | tar --strip-components=1 -xzv

# Make it easy to find CMake when build external projects and plugins
env CMAKE_EXECUTABLE /home/pv-user/cmake/3.16.4/bin/cmake

# Clone the superbuild
RUN mkdir -p /home/pv-user/pvsb/build && cd /home/pv-user/pvsb && \
    git clone --recursive --single-branch --branch ${SUPERBUILD_TAG} ${SUPERBUILD_REPO} src

WORKDIR /home/pv-user/pvsb/build

RUN mkdir -p /home/pv-user/pvsb/downloads && chown pv-user:pv-user /home/pv-user/pvsb/downloads
COPY NVIDIA-OptiX-SDK-6.0.0-linux64-25650775.tar.gz /home/pv-user/pvsb/downloads/NVIDIA-OptiX-SDK-6.0.0-linux64-25650775.tar.gz
COPY mdl-sdk-314800.830.tar.bz2 /home/pv-user/pvsb/downloads/mdl-sdk-314800.830.tar.bz2 

# Configure
RUN ${CMAKE_EXECUTABLE} -C /home/pv-user/pvsb/src/cmake/sites/Docker-Ubuntu-18_04.cmake "-GUnix Makefiles" ../src

# Build
RUN ${CMAKE_EXECUTABLE} --build .

# Install
RUN ${CMAKE_EXECUTABLE} --install .

# Make it easy to find ParaView when build external projects and plugins
env PARAVIEW_DIRECTORY /home/pv-user/pvsb/build/install/lib/cmake/${PARAVIEW_VERSION_STRING}

WORKDIR /opt/paraview
