Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Life.cpp: определяет точку входа для консольного приложения.
- //
- #include "stdafx.h"
- #include "IOStream"
- #include <omp.h>
- using namespace std;
- void generate(int** &a, const int &n, const int &m);
- void generate(int* &a, const int &n);
- void read(int** &a, const int &n, const int &m);
- void read(int* &a, const int &n);
- void println(int* const &a, const int &n);
- void println(int** const &a, const int &n, const int &m);
- void life(int** &a, int** &b, const int &n, const int &m);
- void p_life(int** &a, int** &b, const int &n, const int &m);
- int main(){
- int** a;
- int** b;
- int n = 10000;
- generate(a, n , n);
- b = new int*[n];
- for (int i = 0; i < n; i++) {
- a[i][0] = 0;
- a[0][i] = 0;
- a[n - 1][i] = 0;
- a[i][n - 1] = 0;
- b[i] = new int[n];
- for (int j = 0; j < n; j++) {
- b[i][j] = 0;
- }
- }
- cout << "start";
- //println(a,n ,n);
- double t = omp_get_wtime();
- life(a, b, n, n);
- cout << endl << omp_get_wtime() - t;
- cout << endl << "end";
- cout << "parallel start" << endl;
- t = omp_get_wtime();
- p_life(a, b, n, n);
- cout << omp_get_wtime() - t << endl;
- cout << "end";
- getchar();
- getchar();
- return 0;
- }
- //life
- bool checkCell(int &cell, const int &sum) {
- if (sum == 3) {
- return 1;
- }
- if (sum == 4) {
- return cell;
- }
- return 0;
- }
- void lifeCell(int** &a, int** &b, const int &i, const int &j) {
- int sum = 0;
- for (int p = -1; p <= 1; ++p) {
- for (int q = -1; q <= 1; ++q) {
- sum += a[i + p][j + q];
- }
- }
- b[i][j] = checkCell(a[i][j], sum);
- }
- void life(int** &a, int** &b, const int &n, const int &m) { // Life from 1 a to b
- for (int i = 1; i < n - 1; ++i) {
- for (int j = 1; j < m - 1; ++j) { //[i][j] cell
- lifeCell(a, b, i, j);
- }
- }
- }
- //p_life
- void p_life(int** &a, int** &b, const int &n, const int &m) {
- int** a1 = a + n / 4;
- int** a2 = a + 2 * n / 4 - 1;
- int** a3 = a + 3 * n / 4 - 1;
- int** b1 = b + n / 4;
- int** b2 = b + 2 * n / 4 - 1;
- int** b3 = b + 3 * n / 4 - 1;
- #pragma omp parallel sections
- {
- #pragma omp section
- {
- life(a, b, n / 4 + 1, n);
- }
- #pragma omp section
- {
- life(a1, b1, n / 4 + 1, n);
- }
- #pragma omp section
- {
- life(a2, b2, n / 4 + 1, n);
- }
- #pragma omp section
- {
- life(a3, b3, n / 4 + n % 4, n);
- }
- }
- }
- //Array functions
- void read(int** &a, const int &n, const int &m) {
- a = new int*[n];
- for (int i = 0; i < n; i++) {
- read(a[i], m);
- }
- }
- void read(int* &a, const int &n) {
- a = new int[n];
- for (int i = 0; i < n; i++) {
- std::cin >> a[i];
- }
- }
- void println(int* const &a, const int &n) {
- for (int i = 0; i < n; i++) {
- cout << a[i] << " ";
- }
- cout << endl;
- }
- void println(int** const &a, const int &n, const int &m) {
- for (int i = 0; i < n; i++) {
- println(a[i], m);
- }
- cout << endl;
- }
- void generate(int** &a, const int &n, const int &m) {
- a = new int*[n];
- for (int i = 0; i < n; i++) {
- generate(a[i], m);
- }
- }
- void generate(int* &a, const int &n) {
- a = new int[n];
- for (int i = 0; i < n; i++) {
- a[i] = rand() %2;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement