Advertisement
qvinhprolol

Untitled

Sep 26th, 2023
656
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.30 KB | None | 0 0
  1. #include "iom.h"
  2. #include <iostream>
  3. #include <omp.h>
  4.  
  5. const int dir[8][2] = {{1,1},{1,0},{1,-1},{0,-1},{0,1},{-1,-1},{-1,0},{-1,1}};
  6. inline int simulateCell(std::vector<std::vector<int>>& oldWorld, std::vector<std::vector<int>>& newWorld, int x, int y) {
  7.     int worldWidth = oldWorld.size();
  8.     int worldHeight = oldWorld[0].size();
  9.     int count[10] = {0};
  10.     // Total of cells surrounding
  11.     // #pragma omp sections
  12.     {for(auto d: dir) {
  13.         int nx = d[0] + x;
  14.         int ny = d[1] + y;
  15.  
  16.         if (nx < 0) {
  17.             nx += worldWidth;
  18.         } else if (nx >= worldWidth) {
  19.             nx -= worldWidth;
  20.         }
  21.         if (ny < 0) {
  22.             ny += worldHeight;
  23.         } else if (ny >= worldHeight) {
  24.             ny -= worldHeight;
  25.         }
  26.  
  27.         // #pragma omp critical
  28.         count[oldWorld[nx][ny]]++;
  29.     }}
  30.     int cur =  oldWorld[x][y];
  31.     if(cur == 0) {
  32.         if (count[0] == 8) return 0;
  33.         // new birth
  34.         for(int i = 9; i > 0; i--) if(count[i] ==3) {
  35.             newWorld[x][y] = i;
  36.             return 0;
  37.         }
  38.         return 0;
  39.     }
  40.    
  41.     // hostile
  42.     for(int i = 1; i <= 9; ++i) if(i != cur && count[i] != 0) {
  43.         newWorld[x][y] = 0;
  44.         return 1;
  45.     }
  46.  
  47.     // under/over population
  48.     if(count[cur] >= 4 || count[cur] < 2) {
  49.         newWorld[x][y] = 0;
  50.         return 0;
  51.     }
  52.    
  53.     // keep same
  54.     newWorld[x][y] = cur;
  55.     return 0;
  56. }
  57.  
  58. inline int invade(std::vector<std::vector<int>>& curWorld, std::vector<std::vector<int>>& invPlan, std::vector<std::vector<int>>& oldWorld) {
  59.     int sum = 0;
  60.     int i, j;
  61.     // #pragma omp parallel for shared(curWorld, invPlan, oldWorld, sum) private(i,j)
  62.     for(i = 0; i < (int)curWorld.size(); ++i)
  63.         for(j = 0; j < (int)curWorld[0].size(); ++j) {
  64.             if (invPlan[i][j] != 0) {
  65.                 if((curWorld[i][j] != 0 && oldWorld[i][j] != 0)) {
  66.                     // #pragma omp critical
  67.                         sum++;
  68.                 }        
  69.             }
  70.             curWorld[i][j] = invPlan[i][j];
  71.         }
  72.     return sum;
  73. }
  74.  
  75. int iom(int nThreads, int nGenerations, std::vector<std::vector<int>>& startWorld, int nRows, int nCols, int nInvasions, std::vector<int> invasionTimes, std::vector<std::vector<std::vector<int>>> invasionPlans) {
  76.    
  77.     // omp_set_num_threads(nThreads);
  78.  
  79.     int totalCount = 0;
  80.     int curInvasionIdx = 0;
  81.  
  82.  
  83.     for(int i = 1; i <= nGenerations; ++i ) {
  84.         std::vector<std::vector<int>> newWorld(nRows,std::vector<int>(nCols));
  85.         int x,y;
  86.         // #pragma omp parallel for private(x,y) shared(startWorld, newWorld)
  87.         for(x = 0; x < nRows; ++x) for(y = 0; y < nCols; ++y)  {
  88.             int res = simulateCell(startWorld,newWorld,x,y);
  89.             // #pragma omp critical
  90.             totalCount += res;
  91.         }
  92.  
  93.         if(nInvasions > 0 && invasionTimes[curInvasionIdx] == i){
  94.             totalCount += invade(newWorld,invasionPlans[curInvasionIdx], startWorld);
  95.             curInvasionIdx++;
  96.             nInvasions--;
  97.         }
  98.  
  99.         std::swap(newWorld,startWorld); // 1 way to swap
  100.         // #pragma omp parallel for private(x, y)
  101.         // for(int x = 0; x < nRows; ++x) for(int y = 0; y < nCols; ++y) startWorld[x][y] = newWorld[x][y];
  102.     }
  103.     return totalCount;
  104. }
  105.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement