Skip to content

MPI Hello World in Practice ​

Abstract: No theory — just run it. Twenty lines of hello.c take you through compile → single-node multi-rank → multi-node, with the output decoded and the fabric flags explained.

1. Source: hello.c ​

c
#include <mpi.h>
#include <stdio.h>

int main(int argc, char* argv[])
{
    MPI_Init(NULL, NULL);

    int world_size;
    MPI_Comm_size(MPI_COMM_WORLD, &world_size);

    int world_rank;
    MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);

    char processor_name[MPI_MAX_PROCESSOR_NAME];
    int name_len;
    MPI_Get_processor_name(processor_name, &name_len);

    printf("Hello World from processor %s, rank %d"
           " out of %d processor\n",
           processor_name, world_rank, world_size);

    MPI_Finalize();
}

These 5 calls are the skeleton of every MPI program: MPI_Init (init), MPI_Comm_size (total ranks), MPI_Comm_rank (my rank), MPI_Get_processor_name (host), MPI_Finalize (cleanup).

2. Compile ​

bash
source /opt/intel2021/oneapi/setvars.sh intel64 --force
mpicc -o hello hello.c

Silent success produces hello.

3. Single-Node Multi-Rank ​

bash
mpirun -np 5 ./hello
text
Hello World from processor node-01, rank 0 out of 5 processor
Hello World from processor node-01, rank 1 out of 5 processor
Hello World from processor node-01, rank 2 out of 5 processor
Hello World from processor node-01, rank 3 out of 5 processor
Hello World from processor node-01, rank 4 out of 5 processor

rank is the process id (0..N-1), processor_name is the host. All ranks on node-01 means intra-node MPI works.

4. Multi-Node ​

4.1 hostfile ​

bash
# one node per line
node-01
node-02
node-03

4.2 Run (Ethernet) ​

bash
mpirun -genv I_MPI_FABRICS shm:ofi -genv FI_PROVIDER tcp -machinefile hostfile -np 6 ./hello
text
Hello World from processor node-01, rank 0 out of 6 processor
Hello World from processor node-01, rank 1 out of 6 processor
Hello World from processor node-02, rank 2 out of 6 processor
Hello World from processor node-02, rank 3 out of 6 processor
Hello World from processor node-03, rank 4 out of 6 processor
Hello World from processor node-03, rank 5 out of 6 processor

6 ranks across 3 nodes (2 each). Different processor_names mean cross-node communication works.

4.3 Flags explained ​

FlagPurpose
-np Ntotal ranks
-machinefile / -fnode list file
-hostfilealias (Intel MPI)
-perhost nmax ranks per node
-genv I_MPI_FABRICS shm:ofishared memory + libfabric transport
-genv FI_PROVIDER tcpEthernet fabric (use mlx on InfiniBand)

Fabric quick select

  • Ethernet: -genv I_MPI_FABRICS shm:ofi -genv FI_PROVIDER tcp
  • InfiniBand: -genv I_MPI_FABRICS shm:ofi -genv FI_PROVIDER mlx
  • Put them in your run script to avoid retyping

5. Troubleshooting ​

SymptomCauseFix
mpirun: command not foundoneAPI not loadedsource setvars.sh
Unable to reach the hostno SSH trust / bad nametest ssh node-02; use IPs
Hangsranks > cores or wrong fabriccheck -np; switch FI_PROVIDER

6. Summary ​

Hello world passing means:

  • ✅ MPI compile chain works
  • ✅ intra-node multi-rank works
  • ✅ cross-node works

Next, actually parallelize computation: MPI Programming in Practice: from Hello to Reduce.

AI-HPC Expert · Contact: openaihpc@gmail.com