Skip to content

Commit

Permalink
Add pure MPI sample that run as non-root
Browse files Browse the repository at this point in the history
  • Loading branch information
alculquicondor committed Jul 26, 2021
1 parent f692d41 commit 15a6e78
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 0 deletions.
20 changes: 20 additions & 0 deletions examples/pi/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
FROM debian:buster

RUN apt update && apt install -y --no-install-recommends \
build-essential \
libopenmpi-dev \
openmpi-bin \
openssh-server \
openssh-client \
&& rm -rf /var/lib/apt/lists/*
# Add priviledge separation directoy to run sshd as root.
RUN mkdir -p /var/run/sshd
# Add capability to run sshd as non-root.
RUN setcap CAP_NET_BIND_SERVICE=+eip /usr/sbin/sshd

RUN useradd -m mpiuser
WORKDIR /home/mpiuser
COPY --chown=mpiuser sshd_config .sshd_config
RUN sed -i 's/[ #]\(.*StrictHostKeyChecking \).*/ \1no/g' /etc/ssh/ssh_config
COPY pi.cc /src/pi.cc
RUN mpic++ /src/pi.cc -o /home/mpiuser/pi
24 changes: 24 additions & 0 deletions examples/pi/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Pure MPI example

This example shows to run a pure MPI application.

The program prints some basic information about the workers.
Then, it calculates an approximate value for pi.

## How to build Image

```bash
docker build -t mpi-pi .
```

## Create MPIJob

Modify `pi.yaml` to set up the image name from your own registry.

Then, run:

```
kubectl create -f pi.yaml
```

The YAML shows how to run the binaries as a non-root user.
51 changes: 51 additions & 0 deletions examples/pi/pi.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright 2021 The Kubeflow Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "mpi.h"
#include <random>
#include <cstdio>

int main(int argc, char *argv[]) {
int rank, workers, proc_name_size;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &workers);
if (rank == 0) {
printf("Workers: %d\n", workers);
}
char hostname[MPI_MAX_PROCESSOR_NAME];
MPI_Get_processor_name(hostname, &proc_name_size);
printf("Rank %d on host %s\n", rank, hostname);

std::minstd_rand generator(rank);
std::uniform_real_distribution<double> distribution(-1.0, 1.0);
double x, y;
long long worker_count = 0;
int worker_tests = 100000000;
for (int i = 0; i < worker_tests; i++) {
x = distribution(generator);
y = distribution(generator);
if (x * x + y * y <= 1.0) {
worker_count++;
}
}
long long total_count = 0;
MPI_Reduce(&worker_count, &total_count, 1, MPI_LONG_LONG, MPI_SUM, 0, MPI_COMM_WORLD);
if (rank == 0) {
double pi = 4 * (double)total_count / (double)(worker_tests) / (double)(workers);
printf("pi is approximately %.16lf\n", pi);
}
MPI_Finalize();
return 0;
}
50 changes: 50 additions & 0 deletions examples/pi/pi.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
apiVersion: kubeflow.org/v2beta1
kind: MPIJob
metadata:
name: pi
spec:
slotsPerWorker: 1
cleanPodPolicy: Running
sshAuthMountPath: /home/mpiuser/.ssh
mpiReplicaSpecs:
Launcher:
replicas: 1
template:
spec:
containers:
- image: docker.io/kubeflow/mpi-pi
name: mpi-launcher
securityContext:
runAsUser: 1000
command:
- mpirun
args:
- -np
- "2"
- /home/mpiuser/pi
resources:
limits:
cpu: 1
memory: 2Gi
Worker:
replicas: 2
template:
spec:
containers:
- image: docker.io/kubeflow/mpi-pi
name: mpi-worker
securityContext:
runAsUser: 1000
capabilities:
add:
- NET_BIND_SERVICE
command:
- /usr/sbin/sshd
args:
- -De
- -f
- /home/mpiuser/.sshd_config
resources:
limits:
cpu: 2
memory: 4Gi
2 changes: 2 additions & 0 deletions examples/pi/sshd_config
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
PidFile /home/mpiuser/sshd.pid
HostKey /home/mpiuser/.ssh/id_rsa

0 comments on commit 15a6e78

Please sign in to comment.