Skip to content

Instantly share code, notes, and snippets.

@kparrish
Last active March 8, 2020 23:05
Show Gist options
  • Save kparrish/9d9fb2f44d1adc6c76878fd7935b03c7 to your computer and use it in GitHub Desktop.
Save kparrish/9d9fb2f44d1adc6c76878fd7935b03c7 to your computer and use it in GitHub Desktop.
A simple mpi example to perform distributed calculations on an array.
#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>
int main(int argc, char** argv){
// MPI
int PE, nPE;
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &nPE);
MPI_Comm_rank(MPI_COMM_WORLD, &PE);
if(PE==0){
MPI_Status status;
int node;
for(int i=1; i<nPE; i++){
MPI_Recv(&node, 1, MPI_INT, i, 10, MPI_COMM_WORLD, &status);
}
}else{
MPI_Send(&PE, 1, MPI_INT, 0, 10, MPI_COMM_WORLD);
}
MPI_Barrier(MPI_COMM_WORLD);
/*
* Only one process, i.e. process 0, will execute this command.
*/
if(PE == 0){
printf("Using %d processes.\n", nPE);
}
/*
* All processes will exectute this command, although each have a different PE
* number.
*/
printf("Process %d reporting.\n", PE);
// Parameters
int numElements = 10;
int *elements;
elements = (int*) calloc(numElements, sizeof(double));
// Calculation
for(int i=0+PE; i<numElements; i=i+nPE){
elements[i] = i;
}
// Output
MPI_Barrier(MPI_COMM_WORLD);
for(int i=0; i<nPE; i++){
if(PE == i){
printf("Process %d copy of elements pre-broadcast\n", PE);
for(int j=0; j<numElements; j++){
printf("%d ", elements[j]);
}
printf("\n");
}
}
// Broadcast
MPI_Barrier(MPI_COMM_WORLD);
for(int i=0; i<numElements; i++){
MPI_Bcast(&elements[i], 1, MPI_INT, i%nPE, MPI_COMM_WORLD);
}
MPI_Barrier(MPI_COMM_WORLD);
for(int i=0; i<nPE; i++){
if(PE == i){
printf("Process %d copy of elements post-broadcast\n", PE);
for(int j=0; j<numElements; j++){
printf("%d ", elements[j]);
}
printf("\n");
}
}
// Closing
free(elements);
MPI_Barrier(MPI_COMM_WORLD);
MPI_Finalize();
return 0;
}
CC=mpicc
CFLAGS=-std=c11 -pedantic -Wall -Wextra -O3 -lm -g
OBJ = mpiexample.o
DEPS =
%.o: %.c $(DEPS)
$(CC) -c -o $@ $< $(CFLAGS)
mpiexample: mpiexample.o
$(CC) -o $@ $^ $(CFLAGS)
clean:
rm -rf *.o mpiexample
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment