Advertisement
Guest User

NukaLife

a guest
Nov 26th, 2015
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.05 KB | None | 0 0
  1. // Life.cpp: определяет точку входа для консольного приложения.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "IOStream"
  6. #include <omp.h>
  7.  
  8. using namespace std;
  9.  
  10. void generate(int** &a, const int &n, const int &m);
  11. void generate(int* &a, const int &n);
  12. void read(int** &a, const int &n, const int &m);
  13. void read(int* &a, const int &n);
  14. void println(int* const &a, const int &n);
  15. void println(int** const &a, const int &n, const int &m);
  16. void life(int** &a, int** &b, const int &n, const int &m);
  17. void p_life(int** &a, int** &b, const int &n, const int &m);
  18.  
  19.  
  20. int main(){
  21.     int** a;
  22.     int** b;
  23.     int n = 10000;
  24.     generate(a, n , n);
  25.     b = new int*[n];
  26.     for (int i = 0; i < n; i++) {
  27.         a[i][0] = 0;
  28.         a[0][i] = 0;
  29.         a[n - 1][i] = 0;
  30.         a[i][n - 1] = 0;
  31.         b[i] = new int[n];
  32.         for (int j = 0; j < n; j++) {
  33.             b[i][j] = 0;
  34.         }
  35.     }
  36.     cout << "start";
  37.     //println(a,n ,n);
  38.     double t = omp_get_wtime();
  39.     life(a, b, n, n);
  40.     cout << endl << omp_get_wtime() - t;
  41.     cout << endl << "end";
  42.  
  43.     cout << "parallel start" << endl;
  44.     t = omp_get_wtime();
  45.     p_life(a, b, n, n);
  46.     cout << omp_get_wtime() - t << endl;
  47.     cout << "end";
  48.  
  49.     getchar();
  50.     getchar();
  51.     return 0;
  52. }
  53.  
  54. //life
  55. bool checkCell(int &cell, const int &sum) {
  56.     if (sum == 3) {
  57.         return 1;
  58.     }
  59.     if (sum == 4) {
  60.         return cell;
  61.     }
  62.     return 0;
  63. }
  64.  
  65. void lifeCell(int** &a, int** &b, const int &i, const int &j) {
  66.     int sum = 0;
  67.     for (int p = -1; p <= 1; ++p) {
  68.         for (int q = -1; q <= 1; ++q) {
  69.             sum += a[i + p][j + q];
  70.         }
  71.     }
  72.     b[i][j] = checkCell(a[i][j], sum);
  73. }
  74.  
  75. void life(int** &a, int** &b, const int &n, const int &m) { // Life from 1 a to b
  76.     for (int i = 1; i < n - 1; ++i) {
  77.         for (int j = 1; j < m - 1; ++j) { //[i][j] cell
  78.             lifeCell(a, b, i, j);
  79.         }
  80.     }
  81. }
  82.  
  83. //p_life
  84. void p_life(int** &a, int** &b, const int &n, const int &m) {
  85.     int** a1 = a + n / 4;
  86.     int** a2 = a + 2 * n / 4 - 1;
  87.     int** a3 = a + 3 * n / 4 - 1;
  88.     int** b1 = b + n / 4;
  89.     int** b2 = b + 2 * n / 4 - 1;
  90.     int** b3 = b + 3 * n / 4 - 1;
  91. #pragma omp parallel sections
  92.     {
  93. #pragma omp section
  94.     {
  95.     life(a, b, n / 4 + 1, n);
  96.     }
  97. #pragma omp section
  98.     {
  99.     life(a1, b1, n / 4 + 1, n);
  100.     }
  101. #pragma omp section
  102.     {
  103.     life(a2, b2, n / 4 + 1, n);
  104.     }
  105. #pragma omp section
  106.     {
  107.     life(a3, b3, n / 4 + n % 4, n);
  108.     }
  109. }
  110. }
  111.  
  112. //Array functions
  113. void read(int** &a, const int &n, const int &m) {
  114.     a = new int*[n];
  115.     for (int i = 0; i < n; i++) {
  116.         read(a[i], m);
  117.     }
  118. }
  119.  
  120. void read(int* &a, const int &n) {
  121.     a = new int[n];
  122.     for (int i = 0; i < n; i++) {
  123.         std::cin >> a[i];
  124.     }
  125. }
  126.  
  127. void println(int* const &a, const int &n) {
  128.     for (int i = 0; i < n; i++) {
  129.         cout << a[i] << " ";
  130.     }
  131.     cout << endl;
  132. }
  133.  
  134. void println(int** const &a, const int &n, const int &m) {
  135.     for (int i = 0; i < n; i++) {
  136.         println(a[i], m);
  137.     }
  138.     cout << endl;
  139. }
  140.  
  141. void generate(int** &a, const int &n, const int &m) {
  142.     a = new int*[n];
  143.     for (int i = 0; i < n; i++) {
  144.         generate(a[i], m);
  145.     }
  146. }
  147.  
  148. void generate(int* &a, const int &n) {
  149.     a = new int[n];
  150.     for (int i = 0; i < n; i++) {
  151.         a[i] = rand() %2;
  152.     }
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement