This commit is contained in:
2020-04-12 18:55:29 -06:00
parent 26480bcc76
commit 4081c9783d
56 changed files with 551 additions and 835 deletions

View File

@@ -1,26 +1,28 @@
#include<iostream>
#include<mpi.h>
#include<stdlib.h>
#include<time.h>
#include<cstdlib>
#include<ctime>
#include<queue>
#define MCW MPI_COMM_WORLD
#define GENERIC 0
#define SPECIAL 1
#define JOB 1
#define ALLSTOP (-1)
#define TOKEN 2
using namespace std;
int main(int argc, char **argv) {
int rank, size;
int jobFlag, tokenFlag;
int jobFlag = 0;
int tokenFlag = 0;
int tasksCreated = 0;
int tasksCompleted = 0;
int taskLimit;
int tokenValue = SPECIAL;
int myToken = SPECIAL;
int newTask;
int newTask = 0;
bool hasToken = false;
queue<int> myTasks;
MPI_Status status;
@@ -30,40 +32,41 @@ int main(int argc, char **argv) {
MPI_Comm_rank(MCW, &rank);
MPI_Comm_size(MCW, &size);
srand(time(NULL) + rank);
taskLimit = rand() % 1025 + 1024; //Set the limit to some number between 1024 and 2048
srand(static_cast<unsigned int>(time(nullptr) + rank));
taskLimit = rand() % 1025 + 1024;
if (rank == 0) {
if (!rank) {
hasToken = true;
int job = rand() % 3 + 1;
int job = rand() % size;
tasksCreated++;
MPI_Send(&job, 1, MPI_INT, rand() % 4, JOB, MCW); //Send out the initial job
MPI_Send(&job, 1, MPI_INT, rand() % size, JOB, MCW);
}
while (true) {
MPI_Iprobe(MPI_ANY_SOURCE, JOB, MCW, &jobFlag, &status); //Check to see if a job is available
if (jobFlag) {
MPI_Iprobe(MPI_ANY_SOURCE, JOB, MCW, &jobFlag, &status);
if (jobFlag == 1) {
MPI_Irecv(&newTask, 1, MPI_INT, MPI_ANY_SOURCE, JOB, MCW, &request);
myTasks.push(newTask);
}
if (newTask == -1) { //If the stop signal is received, send my info to process 0 and break out of the loop
if (newTask == ALLSTOP) {
MPI_Send(&tasksCreated, 1, MPI_INT, 0, size - 1, MCW);
MPI_Send(&tasksCompleted, 1, MPI_INT, 0, size, MCW);
break;
}
if (myTasks.size() > 16) { //If my task queue is greater than size 16, send off 2 tasks
if (myTasks.size() > 16) {
for (int i = 0; i < 2; i++) {
int task = myTasks.front();
myTasks.pop();
int dest = rand() % 4;
int dest = rank;
while (dest == rank) { dest = rand() % size; }
if (dest < rank) { myToken = GENERIC; }
MPI_Send(&task, 1, MPI_INT, dest, JOB, MCW);
}
}
if (!myTasks.empty()) {
int num = myTasks.front();
myTasks.pop();
int num;
num = myTasks.front();
myTasks.pop();
num ^ 3 - num ^ 4;
tasksCompleted++;
}
@@ -73,18 +76,19 @@ int main(int argc, char **argv) {
tasksCreated++;
}
}
if (myTasks.empty()) { //Check the token information
if (myTasks.empty()) {
if (hasToken) {
string tmp = "";
string tmp;
if (tokenValue == GENERIC)
tmp = "Generic";
else
tmp = "Special";
cout << "My Rank: " << rank << " My token: " << tmp << endl;
cout << "My Rank: " << rank << " My token: " << tmp << endl;
int dest = rank + 1 < size ? rank + 1 : 0;
if (rank == 0) { tokenValue = SPECIAL; }
if (!rank)
tokenValue = SPECIAL;
MPI_Send(&tokenValue, 1, MPI_INT, dest, TOKEN, MCW);
hasToken = false;
myToken = SPECIAL;
@@ -95,37 +99,36 @@ int main(int argc, char **argv) {
else
src = size - 1;
MPI_Iprobe(src, TOKEN, MCW, &tokenFlag, &status);
if (tokenFlag) {
if (tokenFlag == 1) {
MPI_Irecv(&tokenValue, 1, MPI_INT, src, TOKEN, MCW, &request);
hasToken = true;
if (rank == 0 && tokenValue == SPECIAL) {
int Stop = -1;
if (!rank && tokenValue) {
int stop = ALLSTOP;
for (int i = 1; i < size; i++) {
MPI_Send(&Stop, 1, MPI_INT, i, JOB, MCW);
MPI_Send(&stop, 1, MPI_INT, i, JOB, MCW);
}
break;
}
if (myToken == GENERIC) {
tokenValue = GENERIC;
}
tokenValue = (myToken == GENERIC) ? GENERIC : SPECIAL;
}
}
}
}
//Print info after run.
if (rank == 0) {
int created;
int completed;
cout << "Rank 0 created " << tasksCreated << " tasks and completed " << tasksCompleted << " tasks" << endl;
for (int i = 0; i < size; i++) {
MPI_Recv(&created, 1, MPI_INT, i, size - 1, MCW, MPI_STATUS_IGNORE);
if (!rank) {
cout << "Rank 0--Total tasks created: " << tasksCreated << " Total Tasks completed: " << tasksCompleted << endl;
for (int i = 1; i < size; i++) {
int created;
int completed;
MPI_Recv(&completed, 1, MPI_INT, i, size, MCW, MPI_STATUS_IGNORE);
cout << "Rank " << i << " created " << created << " tasks and completed " << completed << " tasks" << endl;
MPI_Recv(&created, 1, MPI_INT, i, size - 1, MCW, MPI_STATUS_IGNORE);
cout << "Rank "<<i<<"--Total tasks created: " << created << " Total Tasks completed: " << completed << endl;
tasksCreated += created;
tasksCompleted += completed;
}
cout << "Tasks Created: " << tasksCreated << endl;
cout << "Tasks Completed: " << tasksCompleted << endl;
cout << "Total Tasks Created: " << tasksCreated << endl;
cout << "Total Tasks Completed: " << tasksCompleted << endl;
}