Distributed memory
2024-09-26
Send
and Recv
are point-to-point primitivesMPI_Bcast(buffer, count, datatype,
root, comm);
MPI_Reduce(sendbuf, recvbuf, count, datatype,
op, root, comm);
MPI_MAX
, MPI_SUM
, …\(\}\)#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>
int main(int argc, char** argv) {
int nproc, myid, ntrials = atoi(argv[1]);
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &nproc);
MPI_Comm_rank(MPI_COMM_WORLD, &my_id);
MPI_Bcast(&ntrials, 1, MPI_INT,
0, MPI_COMM_WORLD);
run_mc(myid, nproc, ntrials);
MPI_Finalize();
return 0;
}
Let sum[0]
= \(\sum_i X_i\) and sum[1]
= \(\sum_i X_i^2\).
void run_mc(int myid, int nproc, int ntrials) {
double sums[2] = {0,0};
double my_sums[2] = {0,0};
/* ... run ntrials local experiments ... */
MPI_Reduce(my_sums, sums, 2, MPI_DOUBLE,
MPI_SUM, 0, MPI_COMM_WORLD);
if (myid == 0) {
int N = nproc*ntrials;
double EX = sums[0]/N;
double EX2 = sums[1]/N;
printf("Mean: %g; err: %g\n",
EX, sqrt((EX*EX-EX2)/N));
}
}
Not much more to say. Not needed that often.
Allreduce
), Reduce_scatter
, …Init
/Finalize
Get_comm_rank
, Get_comm_size
Send
/Recv
variants and Wait
Allreduce
, Allgather
, Bcast
Let’s look at how these work in practice:
https://github.com/cs5220-f24/demos/