Advertisement
kernel_memory_dump

SPPURV vezba 7

Mar 17th, 2015
377
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.37 KB | None | 0 0
  1. #include "GameOfLife.h"
  2.  
  3. // lel snovak trademark
  4. // Friendship is Magic <3
  5.  
  6. #include <iostream>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <time.h>
  10.  
  11. using namespace std;
  12.  
  13. GameOfLife::GameOfLife(unsigned int height, unsigned int width, InitModel model)
  14. {
  15.     m_height = height;
  16.     m_width = width;
  17.  
  18.     m_buffer = new unsigned char [m_width * m_height];
  19.     m_nextIter = new unsigned char [m_width * m_height];
  20.  
  21.     // fill buffer
  22.     switch (model)
  23.     {
  24.     case PULSAR:
  25.         {
  26.             if (width > 15 && height > 15)
  27.             {
  28.                 memset(m_buffer, 0x0, m_width * m_height * sizeof(unsigned char));
  29.                 m_buffer[2 * m_width + 4] = 1;
  30.                 m_buffer[2 * m_width + 5] = 1;
  31.                 m_buffer[2 * m_width + 6] = 1;
  32.  
  33.                 m_buffer[2 * m_width + 10] = 1;
  34.                 m_buffer[2 * m_width + 11] = 1;
  35.                 m_buffer[2 * m_width + 12] = 1;
  36.  
  37.                 m_buffer[4 * m_width + 2] = 1;
  38.                 m_buffer[5 * m_width + 2] = 1;
  39.                 m_buffer[6 * m_width + 2] = 1;
  40.  
  41.                 m_buffer[4 * m_width + 7] = 1;
  42.                 m_buffer[5 * m_width + 7] = 1;
  43.                 m_buffer[6 * m_width + 7] = 1;
  44.  
  45.                 m_buffer[4 * m_width + 9] = 1;
  46.                 m_buffer[5 * m_width + 9] = 1;
  47.                 m_buffer[6 * m_width + 9] = 1;
  48.  
  49.                 m_buffer[4 * m_width + 14] = 1;
  50.                 m_buffer[5 * m_width + 14] = 1;
  51.                 m_buffer[6 * m_width + 14] = 1;
  52.  
  53.                 m_buffer[7 * m_width + 4] = 1;
  54.                 m_buffer[7 * m_width + 5] = 1;
  55.                 m_buffer[7 * m_width + 6] = 1;
  56.  
  57.                 m_buffer[7 * m_width + 10] = 1;
  58.                 m_buffer[7 * m_width + 11] = 1;
  59.                 m_buffer[7 * m_width + 12] = 1;
  60.  
  61.                 m_buffer[9 * m_width + 4] = 1;
  62.                 m_buffer[9 * m_width + 5] = 1;
  63.                 m_buffer[9 * m_width + 6] = 1;
  64.  
  65.                 m_buffer[9 * m_width + 10] = 1;
  66.                 m_buffer[9 * m_width + 11] = 1;
  67.                 m_buffer[9 * m_width + 12] = 1;
  68.  
  69.                 m_buffer[10 * m_width + 2] = 1;
  70.                 m_buffer[11 * m_width + 2] = 1;
  71.                 m_buffer[12 * m_width + 2] = 1;
  72.  
  73.                 m_buffer[10 * m_width + 7] = 1;
  74.                 m_buffer[11 * m_width + 7] = 1;
  75.                 m_buffer[12 * m_width + 7] = 1;
  76.  
  77.                 m_buffer[10 * m_width + 9] = 1;
  78.                 m_buffer[11 * m_width + 9] = 1;
  79.                 m_buffer[12 * m_width + 9] = 1;
  80.  
  81.                 m_buffer[10 * m_width + 14] = 1;
  82.                 m_buffer[11 * m_width + 14] = 1;
  83.                 m_buffer[12 * m_width + 14] = 1;
  84.  
  85.                 m_buffer[14 * m_width + 4] = 1;
  86.                 m_buffer[14 * m_width + 5] = 1;
  87.                 m_buffer[14 * m_width + 6] = 1;
  88.  
  89.                 m_buffer[14 * m_width + 10] = 1;
  90.                 m_buffer[14 * m_width + 11] = 1;
  91.                 m_buffer[14 * m_width + 12] = 1;
  92.  
  93.                 break;
  94.             }
  95.             else
  96.             {
  97.                 cout << "Board too small, using RANDOM." << endl;
  98.             }
  99.         }
  100.  
  101.     case RANDOM:
  102.         {
  103.             srand(time(NULL));
  104.             for (unsigned int i = 0; i < m_height; i++)
  105.             {
  106.                 for (unsigned int j = 0; j < m_width; j++)
  107.                 {
  108.                     m_buffer[i * m_width + j] = rand() % 2;
  109.                 }
  110.             }
  111.             break;
  112.         }
  113.  
  114.     default:
  115.         break;
  116.     }
  117. }
  118.  
  119.  
  120. void GameOfLife::printIteration()
  121. {
  122.     for (int row = 0; row < m_height; row++)
  123.     {
  124.         for (int col = 0; col < m_width; col++)
  125.         {
  126.             char c;
  127.  
  128.             if (m_buffer[row * m_width + col] == 1)
  129.                 c = 0xb2;
  130.             else
  131.                 c = 0x20;
  132.  
  133.             cout << c << " ";
  134.         }
  135.         cout << "\n";
  136.     }
  137. }
  138.  
  139.  
  140. /*int GameOfLife::getNeighbourSum(unsigned int row, unsigned int col)
  141. {
  142.     int sum = 0;
  143.  
  144.     // TODO: PLACE CODE HERE
  145.     unsigned int i=row;
  146.     unsigned int j=col;
  147.  
  148.     if(i==0 && j==0){
  149.         sum=m_buffer[1*m_width+1]+
  150.             m_buffer[0*m_width+1]+
  151.             m_buffer[1*m_width+0]+sum;
  152.     }else if(i==0 && j==m_width-1){
  153.         sum=m_buffer[0*m_width+(m_width-2)]+
  154.             m_buffer[1*m_width+(m_width-2)]+
  155.             m_buffer[1*m_width+(m_width-1)]+sum;
  156.     }else if(i==m_height-1 && j==0){
  157.         sum=m_buffer[(m_height-2)*m_width+0]+
  158.             m_buffer[(m_height-2)*m_width+1]+
  159.             m_buffer[(m_height-1)*m_width+1];
  160.     }else if(i=m_height-1 && j==m_width-1){
  161.         sum=m_buffer[(m_height-2)*m_width+(m_width-2)]+
  162.             m_buffer[(m_height-2)*m_width+(m_width-1)]+
  163.             m_buffer[(m_height-1)*m_width+(m_width-2)];
  164.     }else if(i==0 && j>0 && j<m_width-1){
  165.         sum=m_buffer[0*(m_width)+(j-1)]+
  166.             m_buffer[0*(m_width)+j+1]+
  167.             m_buffer[1*m_width+(j-1)]+
  168.             m_buffer[1*m_width+(j+1)]+
  169.             m_buffer[1*m_width+j];
  170.     }else if(i>0 && i<m_height-1 && j==0){
  171.         sum=m_buffer[(i-1)*m_width+j+1]+
  172.             m_buffer[(i-1)*m_width+j]+
  173.             m_buffer[i*m_width+j+1]+
  174.             m_buffer[(i+1)*m_width+j+1]+
  175.             m_buffer[(i+1)*m_width+j];
  176.     }else if(i>0&&i<m_height && j<m_width-1){
  177.         sum=m_buffer[(i-1)*m_width+j-1]+
  178.             m_buffer[(i-1)*m_width+j]+
  179.             m_buffer[(i-1)*m_width+j+1]+
  180.             m_buffer[i*m_width+j+1]+
  181.             m_buffer[i*m_width+j-1];
  182.     }else if(i==m_height-1 && j>0 && j<m_width-1){
  183.                 sum=        m_buffer[(i-1)*m_width+(j-1)]+
  184.                             m_buffer[(i-1)*m_width+j]+
  185.                             m_buffer[(i-1)*m_width+(j+1)]+
  186.                             m_buffer[i*m_width+(j-1)]+
  187.                             m_buffer[i*m_width+(j+1)];
  188.     }else{
  189.         sum=m_buffer[(i+1)*m_width+j+1]+
  190.             m_buffer[(i+1)*m_width+j]+
  191.             m_buffer[(i+1)*m_width+j-1]+
  192.             m_buffer[i*m_width+j+1]+
  193.             m_buffer[i*m_width+j-1]+
  194.             m_buffer[(i-1)*m_width+j+1]+
  195.             m_buffer[(i-1)*m_width+j]+
  196.             m_buffer[(i-1)*m_width+j-1];
  197.     }
  198.  
  199.     return sum;
  200. }
  201. */
  202.  
  203. int GameOfLife::getNeighbourSum(unsigned int row, unsigned int col)
  204. {
  205.         int sum = 0;
  206.        
  207. // TODO: PLACE CODE HERE
  208.  
  209.        
  210.             if(row>=0){
  211.             if(m_buffer[row*m_width+col*m_width]==1)
  212.                         sum++;
  213.                 if(col>=0){
  214.                     if(m_buffer[row*m_width+col*m_width]==1)
  215.                         sum++;
  216.                
  217.             }
  218.         }
  219.        
  220. //gore levo
  221. /*if(row==0 && col==0){
  222.         if(m_buffer[0*m_width + 1] == 1) sum++;
  223.         if(m_buffer[1*m_width + 0] == 1) sum++;
  224.         if(m_buffer[1*m_width + 1] == 1) sum++;
  225. }else
  226. //gore desno
  227. if(row == 0 && col == m_width-1){
  228.         if(m_buffer[0*m_width + m_width-2] == 1) sum++;
  229.     if(m_buffer[1*m_width + m_width-2] == 1) sum++;
  230.         if(m_buffer[1*m_width + m_width-1] == 1) sum++;
  231. }else
  232. //dole levo
  233. if(row == m_height-1 && col == 0){
  234.         if(m_buffer[(m_height-2)*m_width + 0] == 1) sum++;
  235.         if(m_buffer[(m_height-1)*m_width + 1] == 1) sum++;
  236.         if(m_buffer[(m_height-2)*m_width + 1] == 1) sum++;
  237. }else
  238. //dole desno
  239. if(row == m_height-1 && col == m_width-1){
  240.         if(m_buffer[(m_height-2)*m_width + m_width-1] == 1) sum++;
  241.         if(m_buffer[(m_height-1)*m_width + m_width-2] == 1) sum++;
  242.         if(m_buffer[(m_height-2)*m_width + m_width-2] == 1) sum++;
  243. }else
  244. //gore
  245. if(row == 0){
  246.         if(m_buffer[0*m_width + col-1] == 1) sum++;
  247.         if(m_buffer[0*m_width + col+1] == 1) sum++;
  248.         if(m_buffer[1*m_width + col-1] == 1) sum++;
  249.         if(m_buffer[1*m_width + col+1] == 1) sum++;
  250.         if(m_buffer[1*m_width + col] == 1) sum++;
  251. }else
  252. //dole
  253. if(row == m_height-1){
  254.         if(m_buffer[(m_height-1)*m_width + col-1] == 1) sum++;
  255.         if(m_buffer[(m_height-1)*m_width + col+1] == 1) sum++;
  256.         if(m_buffer[(m_height-2)*m_width + col-1] == 1) sum++;
  257.         if(m_buffer[(m_height-2)*m_width + col+1] == 1) sum++;
  258.         if(m_buffer[(m_height-2)*m_width + col] == 1) sum++;
  259. }else
  260. //levo
  261. if(col == 0){
  262.         if(m_buffer[(row-1)*m_width + 0] == 1) sum++;
  263.         if(m_buffer[(row+1)*m_width + 0] == 1) sum++;
  264.         if(m_buffer[(row-1)*m_width + 1] == 1) sum++;
  265.         if(m_buffer[(row+1)*m_width + 1] == 1) sum++;
  266.         if(m_buffer[(row)*m_width + 1] == 1) sum++;
  267. }else
  268. //desno
  269. if(col == m_width-1){
  270.         if(m_buffer[(row-1)*m_width + m_height-1] == 1) sum++;
  271.         if(m_buffer[(row+1)*m_width + m_height-1] == 1) sum++;
  272.         if(m_buffer[(row-1)*m_width + m_height-2] == 1) sum++;
  273.         if(m_buffer[(row+1)*m_width + m_height-2] == 1) sum++;
  274.         if(m_buffer[(row)*m_width + m_height-2] == 1) sum++;
  275. }else{
  276. //sredina
  277.         if(m_buffer[(row-1)*m_width + col-1] == 1) sum++;
  278.         if(m_buffer[(row+1)*m_width + col-1] == 1) sum++;
  279.         if(m_buffer[(row)*m_width + col-1] == 1) sum++;
  280.         if(m_buffer[(row)*m_width + col+1] == 1) sum++;
  281.         if(m_buffer[(row-1)*m_width + col+1] == 1) sum++;
  282.         if(m_buffer[(row+1)*m_width + col+1] == 1) sum++;
  283.         if(m_buffer[(row-1)*m_width + col] == 1) sum++;
  284.         if(m_buffer[(row+1)*m_width + col] == 1) sum++;
  285. //} */
  286.         return sum;
  287.  
  288.        
  289. }
  290.  
  291. void GameOfLife::ajme(){
  292.     for(int row=0;row<m_height;row++){
  293.         nextIterSerial(row);
  294.     }
  295. }
  296.  
  297.  
  298. void GameOfLife::nextIterSerial(int row)
  299. {
  300.     for (int row = 0; row < m_height; row++)
  301.     {
  302.         for (col-1; col+1; col++)
  303.         {
  304.             // TODO: IMPLEMENT getNeighbourSum
  305.             int neighbours = getNeighbourSum(row, col);
  306.             if (m_buffer[row * m_width + col] == 1)
  307.             {
  308.                 if ((neighbours == 2) || (neighbours == 3))
  309.                 {
  310.                     m_nextIter[row * m_width + col] = 1;
  311.                 }
  312.                 else
  313.                 {
  314.                     m_nextIter[row * m_width + col] = 0;
  315.                 }
  316.             }
  317.             else
  318.             {
  319.                 if (neighbours == 3)
  320.                 {
  321.                     m_nextIter[row * m_width + col] = 1;
  322.                 }
  323.                 else
  324.                 {
  325.                     m_nextIter[row * m_width + col] = 0;
  326.                 }
  327.             }
  328.         }
  329.     }
  330.  
  331.     // switch buffers
  332.     unsigned char* temp = m_buffer;
  333.     m_buffer = m_nextIter;
  334.     m_nextIter = temp;
  335. }
  336.  
  337. void GameOfLife::doWork(unsigned int row){
  338.     for(int col=0; col<m_width; col++){
  339.         int neighbours=0;
  340.             neighbours=getNeighbourSum(row, col);
  341.         if(m_buffer[row*m_width+col]==1){
  342.             if(neighbours==2 || neighbours==3){
  343.                 m_nextIter[row*m_width+col]=1;
  344.             }else
  345.                 m_nextIter[row*m_width+col]=0;
  346.         } else{
  347.             if(neighbours==3)
  348.                 m_nextIter[row * m_width + col] = 1;
  349.             else
  350.                 m_nextIter[row * m_width + col] = 0;
  351.         }
  352.     }
  353.  
  354. }
  355.  
  356. void GameOfLife::nextIterParallel()
  357. {
  358.     for (unsigned int row=0; row<m_height;row++){
  359.         cilk_spawn doWork(row);
  360.     // TODO: PLACE CODE HERE
  361.     }
  362.     cilk_sync;
  363.  
  364.     unsigned char* temp = m_buffer;
  365.     m_buffer = m_nextIter;
  366.     m_nextIter = temp;
  367. }
  368.  
  369.  
  370. GameOfLife::~GameOfLife()
  371. {
  372.     delete [] m_buffer;
  373.     delete [] m_nextIter;
  374. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement