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.cSilent success produces hello.
3. Single-Node Multi-Rank
bash
mpirun -np 5 ./hellotext
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
rankis the process id (0..N-1),processor_nameis the host. All ranks onnode-01means intra-node MPI works.
4. Multi-Node
4.1 hostfile
bash
# one node per line
node-01
node-02
node-034.2 Run (Ethernet)
bash
mpirun -genv I_MPI_FABRICS shm:ofi -genv FI_PROVIDER tcp -machinefile hostfile -np 6 ./hellotext
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 processor6 ranks across 3 nodes (2 each). Different
processor_names mean cross-node communication works.
4.3 Flags explained
| Flag | Purpose |
|---|---|
-np N | total ranks |
-machinefile / -f | node list file |
-hostfile | alias (Intel MPI) |
-perhost n | max ranks per node |
-genv I_MPI_FABRICS shm:ofi | shared memory + libfabric transport |
-genv FI_PROVIDER tcp | Ethernet 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
| Symptom | Cause | Fix |
|---|---|---|
mpirun: command not found | oneAPI not loaded | source setvars.sh |
Unable to reach the host | no SSH trust / bad name | test ssh node-02; use IPs |
| Hangs | ranks > cores or wrong fabric | check -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.