Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "mpi.h"
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #include <pthread.h>
- typedef int bool;
- #define true 1
- #define false 0
- #define CHANGE_TIME 100
- #define SWIM_TIME 800
- #define REST_TIME 1500
- #define REQUEST_TAG 1
- #define RESPONSE_TAG 2
- #define RANK 0
- #define GENDER 1
- #define LOCKER 2
- #define STATE 3
- #define TIME 4
- //-----------------------------------------------------------------//
- enum action {CHANGE, SWIM, REST};
- enum state {WAITING, CHANGING, SWIMMING, RESTING};
- enum gender {MALE = 0, FEMALE = 1};
- pthread_t tid;
- bool starving;
- int lockers, lockerRooms, lockerRoomNo, people, state,
- gender, scalarTime, rank, provided;
- //-----------------------------------------------------------------//
- void enterSwimmingPool() { state = SWIMMING; }
- void leaveSwimmingPool() { state = CHANGING; }
- void leaveLocker() { state = RESTING; lockerRoomNo = 0; }
- void finalize() { MPI_Finalize(); }
- void doAction (int action) {
- int miliseconds;
- switch (action) {
- case CHANGE:
- miliseconds = 25 + rand() % CHANGE_TIME;
- break;
- case SWIM:
- miliseconds = 100 + rand() % SWIM_TIME;
- break;
- case REST:
- miliseconds = 300 + rand() % REST_TIME;
- break;
- }
- usleep(miliseconds * 1000);
- }
- void* answerToProcesses() {
- while (1) {
- MPI_Status status;
- int receiveMsg[1];
- int sendMsg[5];
- MPI_Recv(receiveMsg, 1, MPI_INT, MPI_ANY_SOURCE, REQUEST_TAG, MPI_COMM_WORLD, &status);
- sendMsg[RANK] = rank;
- sendMsg[GENDER] = gender;
- sendMsg[LOCKER] = lockerNo;
- sendMsg[STATE] = state;
- sendMsg[TIME] = scalarTime;
- MPI_Send(sendMsg, 5, MPI_INT, receiveMsg[0], RESPONSE_TAG, MPI_COMM_WORLD);
- }
- }
- void init (int argc, char **argv) {
- MPI_Init_thread(&argc, &argv, MPI_THREAD_MULTIPLE, &provided);
- MPI_Comm_rank(MPI_COMM_WORLD, &rank);
- MPI_Comm_size(MPI_COMM_WORLD, &people);
- // Process Data Initialization
- srand(time(NULL) + rank);
- lockerRooms = atoi(argv[1]);
- lockers = atoi(argv[2]);
- state = RESTING;
- lockerRoomNo = 0;
- scalarTime = 0;
- gender = (rand() % 2 == 1) ? FEMALE : MALE;
- starving = false;
- pthread_create(&tid, NULL, &answerToProcesses, NULL);
- }
- void sendLocalStateRequest () {
- printf("WYSYLAM\n");
- int i;
- for (i = 0; i < people; i++)
- MPI_Send(&rank, 1, MPI_INT, i, REQUEST_TAG, MPI_COMM_WORLD);
- }
- void checkAnotherLockerRoom() {
- if (starving == false) {
- lockerRoomNo++;
- if (lockerRoomNo > lockerRooms) {
- starving = true;
- lockerRoomNo = 1 + rand() % lockerRooms;
- }
- }
- tryToEnter();
- }
- void receiveLocalStateResponse () {
- MPI_Status status;
- int i, receivedData[people][5];
- for (i = 0; i < people; i++) {
- int receiveMsg[5];
- MPI_Recv(receiveMsg, 5, MPI_INT, MPI_ANY_SOURCE, RESPONSE_TAG, MPI_COMM_WORLD, &status);
- receivedData[i][0] = receiveMsg[0];
- receivedData[i][1] = receiveMsg[1];
- receivedData[i][2] = receiveMsg[2];
- receivedData[i][3] = receiveMsg[3];
- receivedData[i][4] = receiveMsg[4];
- }
- bool isLockerEmpty = true;
- for (i = 0; i < people; i++) {
- if (receivedData[i][GENDER] != gender
- && receivedData[i][LOCKER] == lockerRoomNo
- && (receivedData[i][STATE] == CHANGING
- || receivedData[i][STATE] == SWIMMING)) checkAnotherLockerRoom();
- if (receivedData[i][GENDER] == gender
- && receivedData[i][LOCKER] == lockerRoomNo
- && (receivedData[i][STATE] == CHANGING
- || receivedData[i][STATE] == SWIMMING)) isLockerEmpty = false;
- }
- if (isLockerEmpty()) {
- } else {
- }
- }
- void tryToEnter () {
- sendLocalStateRequest();
- receiveLocalStateResponse();
- }
- void takeLocker () {
- starving = false;
- lockerRoomNo = 1;
- state = WAITING;
- tryToEnter();
- }
- void mainLoop () {
- while (1) {
- takeLocker();
- doAction(CHANGE);
- enterSwimmingPool();
- doAction(SWIM);
- leaveSwimmingPool();
- doAction(CHANGE);
- leaveLocker();
- doAction(REST);
- }
- }
- int main (int argc, char **argv) {
- init(argc, argv);
- doAction(CHANGE);
- mainLoop();
- finalize();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement