Created
June 11, 2020 18:41
-
-
Save hurbeana/3d9db75fccce80137d5f969d5cbc399f to your computer and use it in GitHub Desktop.
This file contains hidden or 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 <mpi.h> | |
#include <stdio.h> | |
int main(int argc, char *argv[]) { | |
int att = 1; // Change me | |
MPI_Init(&argc, &argv); | |
MPI_Comm comm = MPI_COMM_WORLD; | |
MPI_Win win; | |
int rank; | |
MPI_Comm_rank(comm, &rank); | |
int counter = 0; // counter resides with process 0 | |
if (rank == 0) { | |
printf("Running Attempt %d\n", att); | |
MPI_Win_create (&counter, sizeof(int), sizeof(int), MPI_INFO_NULL, comm, &win); | |
} else { | |
MPI_Win_create (NULL, 0, sizeof(int), MPI_INFO_NULL, comm, &win); | |
} | |
int inc = 1; | |
int val = rank, new; | |
// Attempt 1: | |
if(att == 1) { | |
//printf("RANK[%d]: outside\n", rank); | |
MPI_Win_lock(MPI_LOCK_EXCLUSIVE, 0, 0, win); | |
//printf("RANK[%d]: *hacker voice* im in\n", rank); | |
//printf("Rank[%d]: before_get_val <= %d\n", rank, val); | |
MPI_Get (&val, 1, MPI_INT, 0, 0, 1, MPI_INT, win); | |
//printf("Rank[%d]: after_get_val <= %d\n", rank, val); | |
new = val+inc; | |
//printf("Rank[%d]: new <= %d\n", rank, new); | |
MPI_Put (&new, 1, MPI_INT, 0, 0, 1, MPI_INT, win); | |
//printf("RANK[%d]: brb\n", rank); | |
MPI_Win_unlock (0, win); | |
//printf("Rank[%d]: after_unlock_val <= %d\n", rank, val); | |
//printf("RANK[%d]: left the chat\n", rank); | |
} | |
// Attempt 2: | |
else if(att == 2) { | |
//printf("RANK[%d]: outside get lock\n", rank); | |
MPI_Win_lock(MPI_LOCK_EXCLUSIVE, 0, 0, win); | |
//printf("RANK[%d]: inside get lock\n", rank); | |
MPI_Get (&val , 1, MPI_INT, 0, 0, 1, MPI_INT, win); | |
//printf("RANK[%d]: brb get lock\n", rank); | |
MPI_Win_unlock (0, win); | |
//printf("RANK[%d]: left get lock\n", rank); | |
//printf("Rank[%d]: val <= %d\n", rank, val); | |
new = val+inc; | |
//printf("RANK[%d]: outside put lock\n", rank); | |
MPI_Win_lock(MPI_LOCK_EXCLUSIVE, 0, 0, win); | |
//printf("RANK[%d]: inside put lock\n", rank); | |
MPI_Put (&new, 1, MPI_INT, 0, 0, 1, MPI_INT, win); | |
//printf("RANK[%d]: brb put lock\n", rank); | |
MPI_Win_unlock (0, win); | |
//printf("RANK[%d]: left put lock\n", rank); | |
} | |
MPI_Win_free(&win); | |
MPI_Finalize(); | |
if(rank == 0) { | |
printf("counter = %d\n", counter); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment