Skip to content

Instantly share code, notes, and snippets.

@suyash
Created March 20, 2016 18:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save suyash/adc163ece72150df648d to your computer and use it in GitHub Desktop.
Save suyash/adc163ece72150df648d to your computer and use it in GitHub Desktop.
OpenMPI
#include <iostream>
#include <mpi.h>
using namespace std;
void send (int data, int toProcess) {
MPI_Send(
&data,
1,
MPI_INT,
toProcess,
0,
MPI_COMM_WORLD);
}
void receive (int* data, int fromProcess) {
MPI_Recv(
data,
1,
MPI_INT,
fromProcess,
0,
MPI_COMM_WORLD,
MPI_STATUS_IGNORE);
}
int main (int argc, char *argv[]) {
MPI_Init(NULL, NULL);
int size = 0;
MPI_Comm_size(MPI_COMM_WORLD, &size);
// cout << size << endl;
int rank = 0;
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
cout << "Rank: " << rank << endl;
int number = 0;
if (!(rank % 2)) {
number = rank;
send(number, rank + 1);
cout << "Process " << rank << " sent " << number << endl;
} else {
receive(&number, rank - 1);
cout << "Process " << rank << " received " << number << endl;
}
MPI_Finalize();
}
all: main.out
mpirun -n 4 ./$<
main.out: main.o
mpic++ -o $@ $<
main.o: main.cc
mpic++ -c -o $@ $<
clean:
rm main.o
rm main.out
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment