Advertisement
Guest User

Untitled

a guest
Apr 18th, 2014
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 18.05 KB | None | 0 0
  1.  
  2.     #include <stdlib.h>
  3.     #include <stdio.h>
  4.     #include <math.h>
  5.     #include <stdbool.h>
  6.     #include <unistd.h>
  7.     #include <time.h>
  8.     #include "mpi.h"
  9.          
  10.      
  11.     #define OCEAN_SIZE 10 //rozmiar siatki
  12.     #define HUNGRY_MAX 10 //maksymalny czas glodowania
  13.     #define MATURITY 10 //maksymalny czas zycia
  14.     #define LIFE_TIME 50 //czas zycia akwarium torusa
  15.      
  16.          
  17.     typedef struct {
  18.             int table[OCEAN_SIZE][OCEAN_SIZE]; //pole: 0-puste, 1-ryba, 2-rekin
  19.             int hungry[OCEAN_SIZE][OCEAN_SIZE]; //czas glodowania
  20.             int lifeTime[OCEAN_SIZE][OCEAN_SIZE];
  21.             int movePosiblity[OCEAN_SIZE][OCEAN_SIZE]; //czy rybka/rekin moze ruszyc - jak wykorzysta ruch to 0
  22.     } Area2D;
  23.      
  24.     int i,world_rank, world_size, seed;
  25.     int iterateCounter = 0;
  26.     MPI_Status status;
  27.     MPI_Request request;
  28.      
  29.     Area2D ocean;
  30.     Area2D leftPart, rightPart, upperPart, bottomPart; //po lewej i po prawej i gora i dol
  31.      
  32.     int leftProcess, rightProcess, upperProcess, bottomProcess; // sasiedzi
  33.      
  34.     MPI_Datatype typeArray[4], MPI_AREA;
  35.     MPI_Aint offsets[4], address[5];
  36.     int blockcounts[4];
  37.      
  38.     void printTab(int table[OCEAN_SIZE][OCEAN_SIZE]){
  39.             printf("Czesc oceanu dla  procesu %d w chwilczasowej t = %d:\n", world_rank,iterateCounter);
  40.             for(int i = 0 ; i < OCEAN_SIZE ; i++){
  41.                     for(int j = 0 ; j < OCEAN_SIZE ; j++)
  42.                             printf("%d ",table[j][i]);
  43.                     printf("\n");
  44.             }
  45.             printf("\n");
  46.     }
  47.      
  48.      
  49.     void genRand(int table[OCEAN_SIZE][OCEAN_SIZE]){    
  50.  
  51.             for(int i = 0 ; i < OCEAN_SIZE ; i++)
  52.                     for(int j = 0 ; j < OCEAN_SIZE ; j++){
  53.                             table[i][j] = rand()%3;
  54.                     }
  55.     }
  56.  
  57.     void begin(int lifeTime[OCEAN_SIZE][OCEAN_SIZE]){
  58.     for(int i = 0 ; i < OCEAN_SIZE ; i++)
  59.                     for(int j = 0 ; j < OCEAN_SIZE ; j++){
  60.                             lifeTime[i][j] = 0;
  61.                     }
  62.  
  63.     }
  64.    
  65.     void setSides(){
  66.  
  67.     if(world_rank%3 == 0)
  68.                     leftProcess = world_rank + 2;
  69.             else
  70.                     leftProcess = world_rank - 1;
  71.      
  72.      
  73.             if(world_rank%3 == 2)
  74.                     rightProcess = world_rank - 2;
  75.             else
  76.                     rightProcess = world_rank + 1;
  77.      
  78.            
  79.             if(world_rank < 3)
  80.                     upperProcess = world_rank + 2*3;
  81.             else
  82.                     upperProcess = world_rank - 3;
  83.      
  84.            
  85.             if(world_rank >= (3*2))
  86.                     bottomProcess = world_rank - 2*3;
  87.             else
  88.                     bottomProcess = world_rank + 3;
  89.     }
  90.      
  91.      
  92.      
  93.     void makeMove(int table[OCEAN_SIZE][OCEAN_SIZE], int newX, int newY, int previousX, int previousY, int movePosiblity[OCEAN_SIZE][OCEAN_SIZE], int lifeTime[OCEAN_SIZE][OCEAN_SIZE], int hungry[OCEAN_SIZE][OCEAN_SIZE], Area2D leftPart, Area2D rightPart, Area2D upperPart, Area2D bottomPart){
  94.            
  95.             int* newPos = &(table[newX][newY]);
  96.             int* newCanMove = &(movePosiblity[newX][newY]);
  97.             int* newLifeTime = &(lifeTime[newX][newY]);
  98.             int* newHungry = &(hungry[newX][newY]);
  99.             //graniceX
  100.  
  101.         if(newX<0){
  102.                     newPos = &(leftPart.table[OCEAN_SIZE-1][newY]);
  103.                     newCanMove = &(leftPart.movePosiblity[OCEAN_SIZE-1][newY]);
  104.                     newLifeTime = &(leftPart.lifeTime[OCEAN_SIZE-1][newY]);
  105.                     newHungry = &(leftPart.hungry[OCEAN_SIZE-1][newY]);
  106.             }
  107.  
  108.             if(newX>=OCEAN_SIZE){
  109.                     newPos = &(rightPart.table[0][newY]);
  110.                     newCanMove = &(rightPart.movePosiblity[0][newY]);
  111.                     newLifeTime = &(rightPart.lifeTime[0][newY]);
  112.                     newHungry = &(rightPart.hungry[0][newY]);
  113.             }
  114.            
  115.             //graniceY:
  116.             if(newY==OCEAN_SIZE){
  117.                     newPos = &(bottomPart.table[newX][0]);
  118.                     newCanMove = &(bottomPart.movePosiblity[newX][0]);
  119.                     newLifeTime = &(bottomPart.lifeTime[newX][0]);
  120.                     newHungry = &(bottomPart.hungry[newX][0]);
  121.             }
  122.             if(newY<0){
  123.                     newPos = &(upperPart.table[newX][OCEAN_SIZE-1]);
  124.                     newCanMove = &(upperPart.movePosiblity[newX][OCEAN_SIZE-1]);
  125.                     newLifeTime = &(upperPart.lifeTime[newX][OCEAN_SIZE-1]);
  126.                     newHungry = &(upperPart.hungry[newX][OCEAN_SIZE-1]);
  127.             }
  128.            
  129.             //jezeli to jest rekin:
  130.             if(table[previousX][previousY] == 2){
  131.                     //...i rusza na pole rybki
  132.                     if(*newPos == 1)
  133.                             hungry[previousX][previousY] = 0;
  134.             }      
  135.      
  136.             *newPos = table[previousX][previousY];
  137.             *newCanMove = 0;//NOWE MA BYC
  138.             table[previousX][previousY] = 0;
  139.             movePosiblity[previousX][previousY] = 0;
  140.      
  141.             *newLifeTime = lifeTime[previousX][previousY];//NOWE MA BYC
  142.             lifeTime[previousX][previousY] = 0;
  143.      
  144.             *newHungry = hungry[previousX][previousY];//NOWE MA BYC
  145.             hungry[previousX][previousY] = 0;
  146.      
  147.             //jezeli to jest stara rybka to rodzi nowa w starym miejscu:
  148.             if(*newPos == 1 && *newLifeTime >= MATURITY)
  149.                     table[previousX][previousY] = 1;
  150.             //jezeli to jest stary rekin to rodzi nowego rekina w starym miejscu:
  151.             if(*newPos == 2 && *newLifeTime >= MATURITY)
  152.                     table[previousX][previousY] = 2;
  153.            
  154.     }
  155.      
  156.     void fishMove(int table[OCEAN_SIZE][OCEAN_SIZE], int movePosiblity[OCEAN_SIZE][OCEAN_SIZE], int i, int j, int lifeTime[OCEAN_SIZE][OCEAN_SIZE], int hungry[OCEAN_SIZE][OCEAN_SIZE], Area2D leftPart, Area2D rightPart, Area2D upperPart, Area2D bottomPart){
  157.             if(movePosiblity[i][j]){
  158.                     int newX = (i+1);
  159.                     int newY =  (j);
  160.                     if(table[newX][newY]==0)
  161.                             makeMove(table, newX, newY, i, j, movePosiblity, lifeTime, hungry, leftPart, rightPart, upperPart, bottomPart);
  162.             }
  163.             if(movePosiblity[i][j]){
  164.                     int newX = (i);
  165.                     int newY =  (j+1);
  166.                     if(table[newX][newY]==0)
  167.                             makeMove(table, newX, newY, i, j, movePosiblity, lifeTime, hungry, leftPart, rightPart, upperPart, bottomPart);  
  168.             }
  169.             if(movePosiblity[i][j]){
  170.                     int newX = (i-1);
  171.                     int newY =  (j);
  172.                     if(table[newX][newY]==0)
  173.                             makeMove(table, newX, newY, i, j, movePosiblity, lifeTime, hungry, leftPart, rightPart, upperPart, bottomPart);  
  174.             }
  175.             if(movePosiblity[i][j]){
  176.                     int newX = (i);
  177.                     int newY =  (j-1);
  178.                     if(table[newX][newY]==0)
  179.                             makeMove(table, newX, newY, i, j, movePosiblity, lifeTime, hungry, leftPart, rightPart, upperPart, bottomPart);          
  180.             }
  181.     }
  182.      
  183.     void sharkMove(int table[OCEAN_SIZE][OCEAN_SIZE], int movePosiblity[OCEAN_SIZE][OCEAN_SIZE], int i, int j, int lifeTime[OCEAN_SIZE][OCEAN_SIZE], int hungry[OCEAN_SIZE][OCEAN_SIZE], Area2D planeR, Area2D planeL, Area2D planeU, Area2D planeB){
  184.             //jak jest gdzies ryba:
  185.             if(movePosiblity[i][j]){
  186.                     int newX = (i+1);
  187.                     int newY =  (j);
  188.                     if(table[newX][newY]==1)
  189.                             makeMove(table, newX, newY, i, j, movePosiblity, lifeTime, hungry, leftPart, rightPart, upperPart, bottomPart);
  190.             }
  191.             if(movePosiblity[i][j]){
  192.                     int newX = (i);
  193.                     int newY =  (j+1);
  194.                     if(table[newX][newY]==1)
  195.                             makeMove(table, newX, newY, i, j, movePosiblity, lifeTime, hungry, leftPart, rightPart, upperPart, bottomPart);
  196.             }
  197.             if(movePosiblity[i][j]){
  198.                     int newX = (i-1);
  199.                     int newY =  (j);
  200.                     if(table[newX][newY]==1)
  201.                             makeMove(table, newX, newY, i, j, movePosiblity, lifeTime, hungry, leftPart, rightPart, upperPart, bottomPart);  
  202.             }
  203.             if(movePosiblity[i][j]){
  204.                     int newX = (i);
  205.                     int newY =  (j-1);
  206.                     if(table[newX][newY]==1)
  207.                             makeMove(table, newX, newY, i, j, movePosiblity, lifeTime, hungry, leftPart, rightPart, upperPart, bottomPart);          
  208.             }
  209.             //jezeli pole puste:
  210.             if(movePosiblity[i][j]){
  211.                     int newX = (i+1);
  212.                     int newY =  (j);
  213.                     if(table[newX][newY]==0 || table[newX][newY]==1)
  214.                             makeMove(table, newX, newY, i, j, movePosiblity, lifeTime, hungry, leftPart, rightPart, upperPart, bottomPart);  
  215.             }
  216.             if(movePosiblity[i][j]){
  217.                     int newX = (i);
  218.                     int newY =  (j+1);
  219.                     if(table[newX][newY]==0 || table[newX][newY]==1)
  220.                             makeMove(table, newX, newY, i, j, movePosiblity, lifeTime, hungry, leftPart, rightPart, upperPart, bottomPart);  
  221.             }
  222.             if(movePosiblity[i][j]){
  223.                     int newX = (i-1);
  224.                     int newY =  (j);
  225.                     if(table[newX][newY]==0 || table[newX][newY]==1)
  226.                             makeMove(table, newX, newY, i, j, movePosiblity, lifeTime, hungry, leftPart, rightPart, upperPart, bottomPart);          
  227.             }
  228.             if(movePosiblity[i][j]){
  229.                     int newX = (i);
  230.                     int newY =  (j-1);
  231.                     if(table[newX][newY]==0 || table[newX][newY]==1)
  232.                             makeMove(table, newX, newY, i, j, movePosiblity, lifeTime, hungry, leftPart, rightPart, upperPart, bottomPart);  
  233.             }
  234.     }
  235.      
  236.     void iterate(int table[OCEAN_SIZE][OCEAN_SIZE], int movePosiblity[OCEAN_SIZE][OCEAN_SIZE], int lifeTime[OCEAN_SIZE][OCEAN_SIZE], int hungry[OCEAN_SIZE][OCEAN_SIZE], Area2D leftPart, Area2D rightPart, Area2D upperPart, Area2D bottomPart){
  237.            
  238.             Area2D plL;
  239.      
  240.             for(int i = 0 ; i < OCEAN_SIZE ; i++)
  241.                     for(int j = 0 ; j < OCEAN_SIZE ; j++){
  242.                             //dla rybki:
  243.                             if(table[i][j] == 1){
  244.                                     //zwiekszenie wieku rybki:
  245.                                     if(movePosiblity[i][j]==1)
  246.                                             lifeTime[i][j]++;
  247.                                     //proba ruchu: -probuje plynac z pradem (najlepiej w prawo)
  248.                                     fishMove(table, movePosiblity, i, j, lifeTime, hungry, leftPart, rightPart, upperPart, bottomPart);
  249.                             }
  250.      
  251.                             //dla rekina:
  252.                             if(table[i][j] == 2){
  253.      
  254.                                     //zwiekszenie wieku i glodu rekina:
  255.                                     if(movePosiblity[i][j]==1){
  256.                                             lifeTime[i][j]++;
  257.                                             hungry++;                              
  258.                                     }
  259.      
  260.                                     //umieranie rekina ze starosci:
  261.                                     if(hungry[i][j]>=HUNGRY_MAX)
  262.                                             table[i][j]=0;
  263.      
  264.                                     //proba ruchu: -probuje plynac z pradem (najlepiej w prawo) jezeli jeszcze zyje
  265.                                     if(table[i][j]==2)
  266.                                             sharkMove(table, movePosiblity, i, j, lifeTime, hungry, leftPart, rightPart, upperPart, bottomPart);
  267.                             }
  268.                     }
  269.            
  270.      
  271.             //update reszty brzegow:
  272.             //lewo:
  273.             MPI_Send(&leftPart, 1, MPI_AREA, rightProcess,
  274.                 0, MPI_COMM_WORLD);
  275.             //prawo:
  276.             MPI_Send(&rightPart, 1, MPI_AREA, leftProcess,
  277.                 0, MPI_COMM_WORLD);
  278.             //dol:
  279.             MPI_Send(&bottomPart, 1, MPI_AREA, upperProcess,
  280.                 0, MPI_COMM_WORLD);
  281.             //gora:
  282.             MPI_Send(&upperPart, 1, MPI_AREA, bottomProcess,
  283.                 0, MPI_COMM_WORLD);
  284.      
  285.             MPI_Recv(&plL, 1, MPI_AREA, leftProcess, 0,
  286.                        MPI_COMM_WORLD, &status);
  287.             for(int i = 0; i<OCEAN_SIZE; i++){
  288.                     ocean.table[0][i] = plL.table[0][i];
  289.                     ocean.movePosiblity[0][i] = plL.movePosiblity[0][i];
  290.                     ocean.lifeTime[0][i] = plL.lifeTime[0][i];
  291.                     ocean.hungry[0][i] = plL.hungry[0][i];        
  292.             }
  293.            
  294.      
  295.             MPI_Recv(&plL, 1, MPI_AREA, rightProcess, 0,
  296.                        MPI_COMM_WORLD, &status);
  297.             for(int i = 0; i<OCEAN_SIZE; i++){
  298.                     ocean.table[OCEAN_SIZE-1][i] = plL.table[OCEAN_SIZE-1][i];
  299.                     ocean.movePosiblity[OCEAN_SIZE-1][i] = plL.movePosiblity[OCEAN_SIZE-1][i];
  300.                     ocean.lifeTime[OCEAN_SIZE-1][i] = plL.lifeTime[OCEAN_SIZE-1][i];
  301.                     ocean.hungry[OCEAN_SIZE-1][i] = plL.hungry[OCEAN_SIZE-1][i];            
  302.             }
  303.            
  304.             MPI_Recv(&plL, 1, MPI_AREA, bottomProcess, 0,
  305.                        MPI_COMM_WORLD, &status);
  306.             for(int i = 0; i<OCEAN_SIZE; i++){
  307.                     ocean.table[i][OCEAN_SIZE-1] = plL.table[i][0];
  308.                     ocean.movePosiblity[i][OCEAN_SIZE-1] = plL.movePosiblity[i][OCEAN_SIZE-1];
  309.                     ocean.lifeTime[i][OCEAN_SIZE-1] = plL.lifeTime[i][OCEAN_SIZE-1];
  310.                     ocean.hungry[i][OCEAN_SIZE-1] = plL.hungry[i][OCEAN_SIZE-1];            
  311.             }
  312.            
  313.             MPI_Recv(&plL, 1, MPI_AREA, upperProcess, 0,
  314.                        MPI_COMM_WORLD, &status);
  315.             for(int i = 0; i<OCEAN_SIZE; i++){
  316.                     ocean.table[i][0] = plL.table[i][0];
  317.                     ocean.movePosiblity[i][0] = plL.movePosiblity[i][0];
  318.                     ocean.lifeTime[i][0] = plL.lifeTime[i][0];
  319.                     ocean.hungry[i][0] = plL.hungry[i][0];        
  320.             }
  321.            
  322.     }
  323.      
  324.     void tryToSynchro(int movePosiblity[OCEAN_SIZE][OCEAN_SIZE]){
  325.            
  326.             MPI_Send(&ocean, 1, MPI_AREA, rightProcess,
  327.                0, MPI_COMM_WORLD);
  328.             MPI_Send(&ocean, 1, MPI_AREA, leftProcess,
  329.                0, MPI_COMM_WORLD);
  330.             MPI_Send(&ocean, 1, MPI_AREA, upperProcess,
  331.                0, MPI_COMM_WORLD);
  332.             MPI_Send(&ocean, 1, MPI_AREA, bottomProcess,
  333.                0, MPI_COMM_WORLD);
  334.            
  335.             MPI_Recv(&leftPart, 1, MPI_AREA, leftProcess, 0,
  336.                MPI_COMM_WORLD, &status);
  337.             MPI_Recv(&rightPart, 1, MPI_AREA, rightProcess, 0,
  338.                MPI_COMM_WORLD, &status);
  339.             MPI_Recv(&bottomPart, 1, MPI_AREA, bottomProcess, 0,
  340.                MPI_COMM_WORLD, &status);
  341.             MPI_Recv(&upperPart, 1, MPI_AREA, upperProcess, 0,
  342.                MPI_COMM_WORLD, &status);
  343.      
  344.             //movePosiblity = true dla kazdej rybki
  345.             for(int i = 0 ; i < OCEAN_SIZE ; i++)
  346.                     for(int j = 0 ; j < OCEAN_SIZE ; j++)
  347.                             movePosiblity[i][j]=1;
  348.            
  349.     }
  350.          
  351.     int main(int argc, char** argv)
  352.     {
  353.             MPI_Init(&argc, &argv);
  354.             MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
  355.             MPI_Comm_size(MPI_COMM_WORLD, &world_size);
  356.      
  357.            
  358.      
  359.             //pobierania adresow:
  360.             MPI_Address(&ocean, &address[0]);
  361.             MPI_Address(&ocean.table, &address[1]);
  362.             MPI_Address(&ocean.hungry, &address[2]);
  363.             MPI_Address(&ocean.lifeTime, &address[3]);
  364.             MPI_Address(&ocean.movePosiblity, &address[4]);
  365.      
  366.             //liczenie offsetow blokow:
  367.             offsets[0]=address[1]-address[0];
  368.             offsets[1]=address[2]-address[0];      
  369.             offsets[2]=address[3]-address[0];
  370.             offsets[3]=address[4]-address[0];
  371.            
  372.      
  373.             //typy blokow:
  374.             typeArray[0]=MPI_INT;
  375.             typeArray[1]=MPI_INT;
  376.             typeArray[2]=MPI_INT;
  377.             typeArray[3]=MPI_INT;
  378.      
  379.             //liczebnosci blokow:
  380.             blockcounts[0]=OCEAN_SIZE*OCEAN_SIZE;
  381.             blockcounts[1]=OCEAN_SIZE*OCEAN_SIZE;
  382.             blockcounts[2]=OCEAN_SIZE*OCEAN_SIZE;
  383.             blockcounts[3]=OCEAN_SIZE*OCEAN_SIZE;
  384.      
  385.             //commit strukturki:
  386.             MPI_Type_struct(4, blockcounts, offsets, typeArray, &MPI_AREA);
  387.             MPI_Type_commit(&MPI_AREA);
  388.            
  389.             seed = time(NULL);
  390.         srand(seed);
  391.             //losowe wypelnienie:
  392.  
  393.         begin(ocean.lifeTime);
  394.    
  395.             genRand(ocean.table);
  396.      
  397.             leftProcess = world_rank - 1;
  398.             rightProcess = world_rank + 1;
  399.             upperProcess = world_rank - 3;
  400.             bottomProcess = world_rank + 3;
  401.            
  402.             setSides();
  403.      
  404.            
  405.             while(iterateCounter<LIFE_TIME){
  406.                     tryToSynchro(ocean.movePosiblity);
  407.                    
  408.                     if(world_rank == 0)
  409.                             printTab(ocean.table);
  410.                     MPI_Barrier(MPI_COMM_WORLD);
  411.                    
  412.                     iterate(ocean.table, ocean.movePosiblity, ocean.lifeTime, ocean.hungry, leftPart, rightPart, upperPart, bottomPart);
  413.                     iterateCounter++;
  414.                     MPI_Barrier(MPI_COMM_WORLD);
  415.             }
  416.      
  417.      
  418.             printf("koniec ");
  419.             MPI_Finalize();
  420.             return 0;
  421.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement