Advertisement
patryk

MPI Therms Backup

May 22nd, 2016
398
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.02 KB | None | 0 0
  1. #include "mpi.h"
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <time.h>
  5. #include <pthread.h>
  6.  
  7. typedef int bool;
  8.  
  9. #define true 1
  10. #define false 0
  11.  
  12. #define CHANGE_TIME     100
  13. #define SWIM_TIME       800
  14. #define REST_TIME       1500
  15. #define REQUEST_TAG     1
  16. #define RESPONSE_TAG    2
  17. #define RANK            0
  18. #define GENDER          1
  19. #define LOCKER          2
  20. #define STATE           3
  21. #define TIME            4
  22.  
  23.  
  24. //-----------------------------------------------------------------//
  25.  
  26. enum action {CHANGE, SWIM, REST};
  27. enum state  {WAITING, CHANGING, SWIMMING, RESTING};
  28. enum gender {MALE = 0, FEMALE = 1};
  29.  
  30. pthread_t tid;
  31. bool starving;
  32. int lockers, lockerRooms, lockerRoomNo, people, state,
  33.     gender, scalarTime, rank, provided;
  34.  
  35. //-----------------------------------------------------------------//
  36.  
  37. void enterSwimmingPool() { state = SWIMMING; }
  38. void leaveSwimmingPool() { state = CHANGING; }
  39. void leaveLocker() { state = RESTING; lockerRoomNo = 0; }
  40. void finalize() { MPI_Finalize(); }
  41.  
  42.  
  43. void doAction (int action) {
  44.     int miliseconds;
  45.     switch (action) {
  46.         case CHANGE:
  47.             miliseconds = 25 + rand() % CHANGE_TIME;
  48.             break;
  49.         case SWIM:
  50.             miliseconds = 100 + rand() % SWIM_TIME;
  51.             break;
  52.         case REST:
  53.             miliseconds = 300 + rand() % REST_TIME;
  54.             break;
  55.     }
  56.     usleep(miliseconds * 1000);
  57. }
  58.  
  59.  
  60. void* answerToProcesses() {
  61.     while (1) {
  62.         MPI_Status status;
  63.         int receiveMsg[1];
  64.         int sendMsg[5];
  65.        
  66.         MPI_Recv(receiveMsg, 1, MPI_INT, MPI_ANY_SOURCE, REQUEST_TAG, MPI_COMM_WORLD, &status);
  67.        
  68.         sendMsg[RANK] = rank;
  69.         sendMsg[GENDER] = gender;
  70.         sendMsg[LOCKER] = lockerNo;
  71.         sendMsg[STATE] = state;
  72.         sendMsg[TIME] = scalarTime;
  73.  
  74.         MPI_Send(sendMsg, 5, MPI_INT, receiveMsg[0], RESPONSE_TAG, MPI_COMM_WORLD);
  75.     }
  76. }
  77.  
  78. void init (int argc, char **argv) {
  79.     MPI_Init_thread(&argc, &argv, MPI_THREAD_MULTIPLE, &provided);
  80.     MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  81.     MPI_Comm_size(MPI_COMM_WORLD, &people);
  82.  
  83.     // Process Data Initialization
  84.     srand(time(NULL) + rank);  
  85.     lockerRooms = atoi(argv[1]);
  86.     lockers = atoi(argv[2]);
  87.     state = RESTING;
  88.     lockerRoomNo = 0;
  89.     scalarTime = 0;
  90.     gender = (rand() % 2 == 1) ? FEMALE : MALE;
  91.     starving = false;
  92.  
  93.     pthread_create(&tid, NULL, &answerToProcesses, NULL);
  94. }
  95.  
  96.  
  97. void sendLocalStateRequest () {
  98.     printf("WYSYLAM\n");
  99.     int i;
  100.     for (i = 0; i < people; i++)
  101.         MPI_Send(&rank, 1, MPI_INT, i, REQUEST_TAG, MPI_COMM_WORLD);
  102. }
  103.  
  104.  
  105. void checkAnotherLockerRoom() {
  106.     if (starving == false) {
  107.         lockerRoomNo++;
  108.         if (lockerRoomNo > lockerRooms) {
  109.             starving = true;
  110.             lockerRoomNo = 1 + rand() % lockerRooms;
  111.         }
  112.     }
  113.     tryToEnter();
  114. }
  115.  
  116.  
  117. void receiveLocalStateResponse () {
  118.     MPI_Status status;
  119.     int i, receivedData[people][5];
  120.  
  121.     for (i = 0; i < people; i++) {
  122.         int receiveMsg[5];
  123.        
  124.         MPI_Recv(receiveMsg, 5, MPI_INT, MPI_ANY_SOURCE, RESPONSE_TAG, MPI_COMM_WORLD, &status);   
  125.        
  126.         receivedData[i][0] = receiveMsg[0];    
  127.         receivedData[i][1] = receiveMsg[1];
  128.         receivedData[i][2] = receiveMsg[2];
  129.         receivedData[i][3] = receiveMsg[3];
  130.         receivedData[i][4] = receiveMsg[4];
  131.     }
  132.  
  133.     bool isLockerEmpty = true;
  134.  
  135.     for (i = 0; i < people; i++) {
  136.         if (receivedData[i][GENDER] != gender
  137.         &&  receivedData[i][LOCKER] == lockerRoomNo
  138.         && (receivedData[i][STATE] == CHANGING
  139.         ||  receivedData[i][STATE] == SWIMMING)) checkAnotherLockerRoom();
  140.  
  141.        
  142.         if (receivedData[i][GENDER] == gender
  143.         &&  receivedData[i][LOCKER] == lockerRoomNo
  144.         && (receivedData[i][STATE] == CHANGING
  145.         ||  receivedData[i][STATE] == SWIMMING)) isLockerEmpty = false;
  146.     }
  147.  
  148.     if (isLockerEmpty()) {
  149.  
  150.     } else {
  151.  
  152.     }          
  153. }
  154.  
  155.  
  156. void tryToEnter () {
  157.     sendLocalStateRequest();
  158.     receiveLocalStateResponse();
  159. }
  160.  
  161.  
  162. void takeLocker () {
  163.     starving = false;
  164.     lockerRoomNo = 1;
  165.     state = WAITING;
  166.  
  167.     tryToEnter();
  168. }
  169.  
  170.  
  171. void mainLoop () {
  172.     while (1) {
  173.         takeLocker();
  174.         doAction(CHANGE);
  175.         enterSwimmingPool();
  176.         doAction(SWIM);
  177.         leaveSwimmingPool();
  178.         doAction(CHANGE);
  179.         leaveLocker();
  180.         doAction(REST);
  181.     }
  182. }
  183.  
  184.  
  185. int main (int argc, char **argv) { 
  186.     init(argc, argv);
  187.     doAction(CHANGE);
  188.     mainLoop();
  189.     finalize();
  190. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement