Bootstrap: localimage
From: ../04-python-scilib/python-scilib.sif

%files
    ./prg/mandelbrot-mpi.py /exec/
    ./prg/default.conf /exec/
	
%environment
    # general
    export LC_ALL=C
    export PATH=/exec:${PATH}

    # UCX
    export LD_LIBRARY_PATH=/opt/ucx-1.10.0/lib:${LD_LIBRARY_PATH}
    export PATH=/opt/ucx-1.10.0/bin:${PATH}

    # OpenMPI
    export LD_LIBRARY_PATH=/opt/openmpi/lib:${LD_LIBRARY_PATH}
    export PATH=/opt/openmpi/bin:${PATH}
    export OMPI_DIR=/opt/openmpi/build

%post
    export LC_ALL=C
    export DEBIAN_FRONTEND=noninteractive

    # install tools
    apt update -y
    apt install -y wget
    apt install -y openssh-client
    apt install -y libpmix-dev
    apt install -y libnuma-dev
    apt install -y rdma-core    

    # download and build UCX
    cd /opt
    wget https://github.com/openucx/ucx/releases/download/v1.18.1-rc1/ucx-1.18.1.tar.gz
    tar xvf ucx-1.18.1.tar.gz
    cd ucx-1.18.1
    ./configure --prefix=/opt/ucx-1.18.1 --enable-mt
    make -j 10
    make install
	
    # download and build OpenMPI
    export MPI_VER_MAIN=4.1
    export MPI_VER_SUB=1
    export MPI_VER=${MPI_VER_MAIN}.${MPI_VER_SUB}
    cd /opt
    wget https://download.open-mpi.org/release/open-mpi/v${MPI_VER_MAIN}/openmpi-${MPI_VER}.tar.gz
    tar xvzf openmpi-${MPI_VER}.tar.gz
    rm -rf /opt/openmpi-${MPI_VER}.tar.gz
    mv openmpi-${MPI_VER} openmpi
    cd openmpi
    ./configure --prefix=/opt/openmpi --with-slurm --with-pmix --with-ucx=/opt/ucx-1.18.1
    make all install
    cd ..
    export LD_LIBRARY_PATH=/opt/openmpi/lib:${LD_LIBRARY_PATH}
    export PATH=/opt/openmpi/bin:${PATH}

    # install mpi4py
    pip3 install mpi4py --break-system-packages

    # set file permissions
    chmod a+rx /exec/*.py
    chmod a+r /exec/*.conf
	
%runscript
    echo "Usage: "
    echo "  singularity exec mb-mpi.sif mandelbrot-mpi.py"
    echo "  singularity exec mb-mpi.sif mandelbrot-mpi.py --config <config file>"
    echo
    echo "  <config file> example:"
    echo "--------------------------"
    cat /exec/default.conf
    echo "--------------------------"

%test
    echo "Mandelbrot set python scripts and config file:" 
    ls -lr /exec/*

%labels
    Author      uros (dot) lotric (at) fri (dot) uni (dash) lj (dot) si
				barbara (dot) krasovec (at) ijs (dot) si
    Container   Mandelbrot set with MPI
    Version     1.1
    Description Workshop advanced supercomputing (Superračunalištvo bolj zares)

%help
    For details run:
        ./mpi-mb.sif
