Using MPI from Go: a parallel sum with gompi and Open MPI

MPI (Message Passing Interface) is the standard API for running one program as many cooperating processes, often across the nodes of a cluster. Each process has a rank (0 to N-1), and processes exchange data with point-to-point calls (Send, Recv) and collective calls (Bcast, Reduce). Go has no MPI support in the standard library, so you use a wrapper around a C implementation.
Within a single machine, goroutines and channels are usually the simpler choice. MPI earns its keep when the work has to span several machines, or when you need to fit into an existing HPC job scheduler that launches programs with mpirun.
Prerequisites
Section titled “Prerequisites”- Linux or macOS. gompi’s README states it does not work on Windows.
- Open MPI with its headers and the
mpirunlauncher (for exampleapt install libopenmpi-dev openmpi-binon Debian/Ubuntu, orbrew install open-mpi). - A C compiler, since gompi uses cgo.
- Go 1.27 or later. gompi v0.3.0’s
go.modrequires it, because the API uses generic methods such ascomm.Recv[float64](...).
The code below was written against gompi v0.3.0.
Setting up
Section titled “Setting up”mkdir mpi-sum && cd mpi-sumgo mod init example.com/mpi-sumgo get github.com/sbromberger/gompi@v0.3.0The import path’s last element is gompi, but the package name is mpi, so import it with an explicit name to keep things readable.
A parallel sum
Section titled “A parallel sum”The program builds the numbers 1 to 4×N on rank 0, broadcasts them to every rank, has each rank sum its own chunk, and reduces the partial sums back to rank 0.
package main
import ( "fmt" "log"
mpi "github.com/sbromberger/gompi")
func main() { m, err := mpi.Start() if err != nil { log.Fatal(err) } defer m.Stop()
rank := m.WorldRank() size := m.WorldSize() comm := m.NewCommunicator(nil) // nil means all processes (MPI_COMM_WORLD)
// Every rank needs a buffer of the same length for Bcast. const perRank = 4 data := make([]int64, size*perRank) if rank == 0 { for i := range data { data[i] = int64(i + 1) } } comm.Bcast(data, 0)
// Each rank sums its own contiguous chunk. var local int64 for _, v := range data[rank*perRank : (rank+1)*perRank] { local += v }
// Combine the partial sums on rank 0. dest must be allocated on every rank. total := make([]int64, 1) if err := comm.Reduce(total, []int64{local}, mpi.OpSum, 0); err != nil { log.Fatal(err) }
fmt.Printf("rank %d of %d: partial sum %d\n", rank, size, local) if rank == 0 { fmt.Printf("total: %d\n", total[0]) }}A few things to note:
mpi.StartcallsMPI_Init, andm.StopcallsMPI_Finalize. No MPI calls are allowed afterStop.- Collective calls such as
BcastandReducemust be called by every rank in the communicator, with the same root and buffer lengths, or the program hangs. - gompi’s element types are fixed-size numbers (
int8,byte,int16,uint16,int32,uint32,int64,uint64,float32,float64,complex64,complex128). Plainintis not accepted, which is why the example usesint64. - Broadcasting the whole array is the simplest way to share it. MPI also has a scatter operation that sends each rank only its chunk, but gompi v0.3.0 does not wrap
MPI_Scatter, so for large inputs you would useSend/Recvper rank or have each rank load its own slice of the data.
Running it
Section titled “Running it”Build the binary, then launch it with mpirun:
go build -o mpi-sum .mpirun -n 4 ./mpi-sumWith four processes the data is 1 to 16, and rank 0 prints total: 136. The per-rank lines can appear in any order. If your machine has fewer cores than the -n value, Open MPI refuses to start unless you add --oversubscribe.
Further reading
Section titled “Further reading”- gompi on GitHub, including a migration guide from v0.2
- Open MPI documentation
© 2026 David Li · Home · Blog · RSS ·Classic site · Built with Astro, Starlight and three.js · Logos:Font Awesome Free (CC BY 4.0)