Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <mpi.h>
- #include <thread>
- #include <chrono>
- #include <stdlib.h>
- #include <iostream>
- #include <time.h>
- using namespace std;
- int main(int argc, char** argv) {
- MPI_Init(NULL, NULL);
- int rank;
- MPI_Comm_rank(MPI_COMM_WORLD, &rank);
- srand (time(NULL)+rank);
- int wt = rand() % 5 + 1;
- cout <<"rank: "<< rank<< " sleep: "<< wt<<endl;
- //here is the magic...
- int * flag;
- MPI_Win window;
- int master;
- MPI_Win_allocate(sizeof(int), sizeof(int),MPI_INFO_NULL, MPI_COMM_WORLD, &flag, &window );
- *flag = -1;
- this_thread::sleep_for(chrono::seconds(wt));
- MPI_Win_lock (MPI_LOCK_EXCLUSIVE, 0, 0, window);
- if(rank==0)
- {
- master = *flag;
- }
- else
- {
- MPI_Get(&master, 1, MPI_INT, 0, 0, 1, MPI_INT, window);
- }
- if(master == -1)
- {
- master = rank;
- MPI_Put(&rank,1,MPI_INT,0,0,1,MPI_INT,window);
- }
- MPI_Win_unlock (0, window);
- if(master == rank)
- {
- cout<< "rank: "<<rank<<". I am the master"<<endl;
- }
- else
- {
- cout<< "rank: "<<rank<<". I am the slave of "<<master<<endl;
- }
- MPI_Win_free( &window );
- MPI_Finalize();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement