diff --git a/.cscs-ci/base.Dockerfile b/.cscs-ci/base.Dockerfile new file mode 100644 index 000000000..927ae6ca9 --- /dev/null +++ b/.cscs-ci/base.Dockerfile @@ -0,0 +1,86 @@ +ARG UBUNTU_VERSION=24.04 +ARG CUDA_VERSION +FROM docker.io/nvidia/cuda:${CUDA_VERSION}-devel-ubuntu${UBUNTU_VERSION} +ENV LANG C.UTF-8 +ENV LC_ALL C.UTF-8 + +ARG DEBIAN_FRONTEND=noninteractive +RUN apt-get update -qq && \ + apt-get install -qq -y --no-install-recommends \ + gfortran \ + g++ \ + gcc \ + strace \ + build-essential \ + tar \ + wget \ + curl \ + cmake \ + ca-certificates \ + zlib1g-dev \ + libssl-dev \ + libbz2-dev \ + libsqlite3-dev \ + llvm \ + libncurses5-dev \ + libncursesw5-dev \ + xz-utils \ + tk-dev \ + libffi-dev \ + libevent-dev \ + libhwloc-dev \ + liblzma-dev \ + libreadline-dev \ + python3-dev \ + python3-pip \ + git \ + rustc \ + htop && \ + rm -rf /var/lib/apt/lists/* + +# CUDA-aware Open MPI, without the Slingshot stack (libfabric/cxi, xpmem, gdrcopy): the MPI +# tests run on a single node, launched with the container's own mpiexec, so the shared-memory +# transport is enough. +# CUDA support lives in run-time loadable components (accelerator-cuda, btl-smcuda, ...), +# so libmpi does not depend on the CUDA driver, which is not available at image build time +# (the driver stubs are only used to build those components). +ARG OMPI_VERSION=5.0.10 +# SHA-256 published on https://www.open-mpi.org/software/ompi/v5.0/ (update with OMPI_VERSION) +ARG OMPI_SHA256=5692cc80554a7117c99eaa725d35100edd8bbf73423a5e265ff867979192df7d +ARG MPI_PATH=/usr/local +RUN set -eux; \ + curl -fsSL "https://download.open-mpi.org/release/open-mpi/v${OMPI_VERSION%.*}/openmpi-${OMPI_VERSION}.tar.gz" -o /tmp/ompi.tar.gz; \ + echo "${OMPI_SHA256} /tmp/ompi.tar.gz" | sha256sum -c -; \ + tar -C /tmp -xzf /tmp/ompi.tar.gz; \ + cd "/tmp/openmpi-${OMPI_VERSION}"; \ + ./configure \ + --prefix=${MPI_PATH} \ + --disable-mpi-fortran \ + --with-libevent=external \ + --with-hwloc=external \ + --with-pmix=internal \ + --with-prrte=internal \ + --with-cuda=/usr/local/cuda \ + --with-cuda-libdir=/usr/local/cuda/lib64/stubs; \ + make -j"$(nproc)" install; \ + cd /; \ + rm -rf "/tmp/openmpi-${OMPI_VERSION}" /tmp/ompi.tar.gz; \ + ldconfig; \ + # fail the image build if the CUDA component was not built (it is only loadable on + # GPU nodes, so the run-time check with ci/check_mpi_cuda.c is done in the test job) + test -f ${MPI_PATH}/lib/openmpi/mca_accelerator_cuda.so; \ + # libmpi must not depend on driver libraries that only exist as stubs at build time + if readelf -d ${MPI_PATH}/lib/libmpi.so | grep -E 'libcuda|libnvidia-ml'; then \ + echo "libmpi depends on NVIDIA driver libraries" >&2; exit 1; \ + fi; \ + ompi_info --version + +ENV NVIDIA_VISIBLE_DEVICES=all +ENV NVIDIA_DRIVER_CAPABILITIES=compute,utility + +ENV CXX=${MPI_PATH}/bin/mpicxx +ENV CC=${MPI_PATH}/bin/mpicc + +ENV CUDA_HOME /usr/local/cuda +ARG CUDA_ARCH +ENV CUDA_ARCH=${CUDA_ARCH} diff --git a/ci/build.Dockerfile b/.cscs-ci/build.Dockerfile similarity index 92% rename from ci/build.Dockerfile rename to .cscs-ci/build.Dockerfile index 624441968..88de8b3c0 100644 --- a/ci/build.Dockerfile +++ b/.cscs-ci/build.Dockerfile @@ -10,7 +10,7 @@ ENV GTCMAKE_GT_TESTS_REQUIRE_FORTRAN_COMPILER=ON ENV GTCMAKE_GT_TESTS_REQUIRE_C_COMPILER=ON ENV GTCMAKE_GT_TESTS_REQUIRE_OpenMP=ON ENV GTCMAKE_GT_TESTS_REQUIRE_GPU=ON -ENV GTCMAKE_GT_TESTS_MPI_WITH_MPI_EXECUTABLE=OFF +ENV GTCMAKE_GT_TESTS_MPI_WITH_MPI_EXECUTABLE=ON ENV GTCMAKE_GT_TESTS_REQUIRE_Python=ON ENV GT_ENABLE_STENCIL_DUMP=ON ENV GTCMAKE_CMAKE_EXPORT_NO_PACKAGE_REGISTRY=ON diff --git a/.cscs-ci/check_mpi_cuda.c b/.cscs-ci/check_mpi_cuda.c new file mode 100644 index 000000000..16871d71c --- /dev/null +++ b/.cscs-ci/check_mpi_cuda.c @@ -0,0 +1,32 @@ +/* + * GridTools + * + * Copyright (c) 2014-2023, ETH Zurich + * All rights reserved. + * + * Please, refer to the LICENSE file in the root directory. + * SPDX-License-Identifier: BSD-3-Clause + */ + +/* + * Checks that Open MPI's CUDA support is usable at run time, i.e. that the CUDA accelerator + * component was loaded and selected (this needs the CUDA driver, only available on GPU nodes). + * `ompi_info`'s mpi_built_with_cuda_support only reflects the configure-time setting. + */ +#include +#include +#if defined(OPEN_MPI) && OPEN_MPI +#include +#endif + +#if !defined(MPIX_CUDA_AWARE_SUPPORT) || !MPIX_CUDA_AWARE_SUPPORT +#error "MPI was not built with CUDA support" +#endif + +int main(int argc, char **argv) { + MPI_Init(&argc, &argv); + int supported = MPIX_Query_cuda_support(); + printf("MPI run-time CUDA support: %s\n", supported ? "yes" : "no"); + MPI_Finalize(); + return supported ? 0 : 1; +} diff --git a/ci/cscs.yml b/.cscs-ci/default.yml similarity index 62% rename from ci/cscs.yml rename to .cscs-ci/default.yml index 8e01c9efa..031cc509f 100644 --- a/ci/cscs.yml +++ b/.cscs-ci/default.yml @@ -8,7 +8,7 @@ stages: .build_baseimage: stage: baseimage - # we create a tag that depends on the SHA value of ci/base.Dockerfile, this way + # we create a tag that depends on the SHA value of .cscs-ci/base.Dockerfile, this way # a new base image is only built when the SHA of this file changes # If there are more dependency files that should change the tag-name of the base container # image, they can be added too. @@ -23,17 +23,18 @@ stages: reports: dotenv: build.env variables: - DOCKERFILE: ci/base.Dockerfile + DOCKERFILE: .cscs-ci/base.Dockerfile # change to 'always' if you want to rebuild, even if target tag exists already (if-not-exists is the default, i.e. we could also skip the variable) CSCS_REBUILD_POLICY: if-not-exists - DOCKER_BUILD_ARGS: '["CUDA_VERSION=$CUDA_VERSION", "UBUNTU_VERSION=$UBUNTU_VERSION"]' + DOCKER_BUILD_ARGS: '["CUDA_VERSION=$CUDA_VERSION", "CUDA_ARCH=$CUDA_ARCH", "UBUNTU_VERSION=$UBUNTU_VERSION"]' build_baseimage_aarch64: extends: [.container-builder-cscs-gh200, .build_baseimage] variables: CUDA_VERSION: 12.6.2 CUDA_ARCH: sm_90 UBUNTU_VERSION: 24.04 - SLURM_TIMELIMIT: 10 + # building Open MPI from source does not fit in 10 minutes + SLURM_TIMELIMIT: 60 .build_image: @@ -42,7 +43,7 @@ build_baseimage_aarch64: # make sure we use a unique name here, otherwise we could create a race condition, when multiple pipelines # are running. PERSIST_IMAGE_NAME: $CSCS_REGISTRY_PATH/public/$ARCH/gridtools/gridtools-ci:$CI_COMMIT_SHA - DOCKERFILE: ci/build.Dockerfile + DOCKERFILE: .cscs-ci/build.Dockerfile DOCKER_BUILD_ARGS: '["BASE_IMAGE=${BASE_IMAGE}", "BUILD_TYPE=release"]' build_image_aarch64: extends: [.container-builder-cscs-gh200, .build_image] @@ -68,14 +69,24 @@ test_aarch64: test_aarch64_mpi: extends: [.container-runner-daint-gh200, .test_helper] script: - - export LD_LIBRARY_PATH=/usr/lib64:$LD_LIBRARY_PATH - - export LD_PRELOAD=/usr/lib64/libmpi_gtl_cuda.so + # fail early if Open MPI's CUDA support is not usable on the GPU node (run-time check: the + # CUDA accelerator component must be loadable, which needs the CUDA driver) + - mpicc /gridtools/ci/check_mpi_cuda.c -o /tmp/check_mpi_cuda && mpiexec -n 1 /tmp/check_mpi_cuda - cd /build && ctest -L mpi --output-on-failure variables: + # a single Slurm task runs ctest, each MPI test is launched with `mpiexec -n 4` + # (GT_TESTS_MPI_WITH_MPI_EXECUTABLE=ON) on the local node + SLURM_NTASKS: 1 + SLURM_GPUS_PER_NODE: 4 NVIDIA_VISIBLE_DEVICES: all - SLURM_NTASKS: 4 - SLURM_GPUS_PER_TASK: 1 - MPICH_GPU_SUPPORT_ENABLED: 1 USE_MPI: "YES" - SLURM_MPI_TYPE: cray_shasta - CSCS_ADDITIONAL_MOUNTS: '["/opt/cray/pe/mpich/8.1.28/ofi/gnu/12.3/lib/libmpi.so:/usr/local/lib/libmpi.so.12.1.8", "/opt/cray/pe/lib64/libpmi.so.0:/usr/lib64/libpmi.so.0", "/opt/cray/pe/lib64/libpmi2.so.0:/usr/lib64/libpmi2.so.0", "/opt/cray/pals/1.4/lib/libpals.so.0:/usr/lib64/libpals.so.0", "/usr/lib64/libgfortran.so.5:/usr/lib64/libgfortran.so.5", "/opt/cray/pe/mpich/8.1.28/gtl/lib/libmpi_gtl_cuda.so:/usr/lib64/libmpi_gtl_cuda.so"]' + # PRRTE detects Slurm (SLURM_JOBID) and would take the allocation (1 task => 1 slot) + # from it and try to use srun, which is not available inside the container: ignore + # Slurm and run all ranks on the local node, without binding (the task's cpuset is + # managed by Slurm) + PRTE_MCA_ras: ^slurm + PRTE_MCA_plm: ^slurm + PRTE_MCA_hwloc_default_binding_policy: none + # without a Slurm allocation, the slots are the cores in the task's cpuset, which may be + # fewer than the 4 ranks: allow oversubscription + PRTE_MCA_rmaps_default_mapping_policy: ":oversubscribe" diff --git a/.github/workflows/python-package-tests-and-deploy.yml b/.github/workflows/python-package-tests-and-deploy.yml index 497f9758e..5ea518bb2 100644 --- a/.github/workflows/python-package-tests-and-deploy.yml +++ b/.github/workflows/python-package-tests-and-deploy.yml @@ -11,7 +11,10 @@ on: jobs: build: - runs-on: ubuntu-latest + # TODO: go back to ubuntu-latest once the gridtools-base images are rebuilt on a newer + # Ubuntu. On ubuntu-latest (26.04) setup-python uses the runner's preinstalled Pythons, + # built for 26.04, whose ssl module does not work inside the older container. + runs-on: ubuntu-24.04 container: ghcr.io/gridtools/gridtools-base:${{ matrix.compiler }} strategy: matrix: @@ -83,7 +86,10 @@ jobs: test-wheel: needs: build - runs-on: ubuntu-latest + # TODO: go back to ubuntu-latest once the gridtools-base images are rebuilt on a newer + # Ubuntu. On ubuntu-latest (26.04) setup-python uses the runner's preinstalled Python + # (3.11), built for 26.04, whose ssl module does not work inside the older container. + runs-on: ubuntu-24.04 container: ghcr.io/gridtools/gridtools-base:${{ matrix.compiler }} strategy: matrix: diff --git a/.gitignore b/.gitignore index 50d1f64c6..9c4e4eed2 100644 --- a/.gitignore +++ b/.gitignore @@ -25,7 +25,6 @@ __pycache__ /* # except -!ci !cmake !docs !docs_src @@ -38,6 +37,7 @@ __pycache__ !tools !tests !.clang-format +!.cscs-ci !.gitignore !CMakeLists.txt !Doxyfile.in diff --git a/ci/base.Dockerfile b/ci/base.Dockerfile deleted file mode 100644 index b3e015784..000000000 --- a/ci/base.Dockerfile +++ /dev/null @@ -1,55 +0,0 @@ -ARG UBUNTU_VERSION=24.04 -ARG CUDA_VERSION -FROM docker.io/nvidia/cuda:${CUDA_VERSION}-devel-ubuntu${UBUNTU_VERSION} -ENV LANG C.UTF-8 -ENV LC_ALL C.UTF-8 - -ARG DEBIAN_FRONTEND=noninteractive -RUN apt-get update -qq && \ - apt-get install -qq -y --no-install-recommends \ - gfortran \ - g++ \ - gcc \ - strace \ - build-essential \ - tar \ - wget \ - curl \ - cmake \ - ca-certificates \ - zlib1g-dev \ - libssl-dev \ - libbz2-dev \ - libsqlite3-dev \ - llvm \ - libncurses5-dev \ - libncursesw5-dev \ - xz-utils \ - tk-dev \ - libffi-dev \ - liblzma-dev \ - libreadline-dev \ - python3-dev \ - python3-pip \ - git \ - rustc \ - htop && \ - rm -rf /var/lib/apt/lists/* - -ARG MPICH_VERSION=3.3.2 -ARG MPICH_PATH=/usr/local -RUN wget -q https://www.mpich.org/static/downloads/${MPICH_VERSION}/mpich-${MPICH_VERSION}.tar.gz && \ - tar -xzf mpich-${MPICH_VERSION}.tar.gz && \ - cd mpich-${MPICH_VERSION} && \ - ./configure \ - --disable-fortran \ - --prefix=$MPICH_PATH && \ - make install -j32 && \ - rm -rf /root/mpich-${MPICH_VERSION}.tar.gz /root/mpich-${MPICH_VERSION} -RUN echo "${MPICH_PATH}/lib" >> /etc/ld.so.conf.d/cscs.conf && ldconfig - -ENV CXX=${MPICH_PATH}/bin/mpicxx -ENV CC=${MPICH_PATH}/bin/mpicc - -ENV CUDA_HOME /usr/local/cuda -ENV CUDA_ARCH=${CUDA_ARCH} diff --git a/tests/src/mpi_test_driver.cpp b/tests/src/mpi_test_driver.cpp index af5363b12..619be548a 100644 --- a/tests/src/mpi_test_driver.cpp +++ b/tests/src/mpi_test_driver.cpp @@ -16,24 +16,44 @@ #ifdef GT_HAS_CUDA +#include #include #include #include namespace { + // name of the environment variable providing the node-local rank, nullptr if none is set + char const *local_rank_var() { + // Open MPI, MPICH (hydra), MVAPICH2, Slurm (ranks started by srun). The MPI launchers' + // variables come first: when the ranks are started by mpiexec inside a single Slurm task, + // all of them inherit the same SLURM_LOCALID. + for (auto var : {"OMPI_COMM_WORLD_LOCAL_RANK", "MPI_LOCALRANKID", "MV2_COMM_WORLD_LOCAL_RANK", "SLURM_LOCALID"}) + if (std::getenv(var)) + return var; + return nullptr; + } + int get_local_rank() { - for (auto var : {"MV2_COMM_WORLD_LOCAL_RANK", "SLURM_LOCALID"}) - if (auto *str = std::getenv(var)) - return std::atoi(str); - return 0; + auto *var = local_rank_var(); + return var ? std::atoi(std::getenv(var)) : 0; } int dev_device_count() { - if (auto *str = std::getenv("NUM_GPU_DEVICES")) - return std::atoi(str); int res; + if (auto *str = std::getenv("NUM_GPU_DEVICES")) { + res = std::atoi(str); + if (res <= 0) { + std::fprintf(stderr, "[mpi_test_driver] invalid NUM_GPU_DEVICES=\"%s\"\n", str); + std::abort(); + } + return res; + } GT_CUDA_CHECK(cudaGetDeviceCount(&res)); + if (res <= 0) { + std::fprintf(stderr, "[mpi_test_driver] no GPU devices found\n"); + std::abort(); + } return res; } } // namespace @@ -48,6 +68,31 @@ int main(int argc, char **argv) { gridtools::gcl::init(argc, argv); +#ifdef GT_HAS_CUDA + { + // log the GPU used by each rank, to check that the ranks are spread over the GPUs + int rank, size, device, count; + MPI_Comm_rank(MPI_COMM_WORLD, &rank); + MPI_Comm_size(MPI_COMM_WORLD, &size); + GT_CUDA_CHECK(cudaGetDevice(&device)); + GT_CUDA_CHECK(cudaGetDeviceCount(&count)); + cudaDeviceProp prop; + GT_CUDA_CHECK(cudaGetDeviceProperties(&prop, device)); + auto *var = local_rank_var(); + std::fprintf(stderr, + "[mpi_test_driver] rank %d/%d: local rank %d (%s), GPU %d of %d, PCI %04x:%02x:%02x\n", + rank, + size, + get_local_rank(), + var ? var : "unset", + device, + count, + static_cast(prop.pciDomainID), + static_cast(prop.pciBusID), + static_cast(prop.pciDeviceID)); + } +#endif + // initialize google test environment testing::InitGoogleTest(&argc, argv);