Created
March 20, 2016 18:51
-
-
Save suyash/adc163ece72150df648d to your computer and use it in GitHub Desktop.
OpenMPI
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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