1 /* 2 Copyright (c) 2005-2022 Intel Corporation 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 /* 18 Game_of_life.cpp : 19 main project file. 20 */ 21 22 #include <ctime> 23 24 #include <iostream> 25 #include <sstream> 26 27 #include "common/utility/get_default_num_threads.hpp" 28 29 #include "Board.hpp" 30 #include "Evolution.hpp" 31 32 #define BOARD_SQUARE_SIZE 2 33 34 int low; //! lower range limit of threads 35 int high; //! high range limit of threads 36 double execution_time; //! time for game of life iterations 37 38 Board::Board(int width, int height, int squareSize, LabelPtr counter) 39 : m_width(width), 40 m_height(height), 41 m_squareSize(squareSize), 42 m_counter(counter) { 43 m_matrix = new Matrix(); 44 m_matrix->width = width; 45 m_matrix->height = height; 46 m_matrix->data = new char[width * height]; 47 memset(m_matrix->data, 0, width * height); 48 } 49 50 Board::~Board() { 51 delete[] m_matrix->data; 52 delete m_matrix; 53 } 54 55 void Board::seed(int s) { 56 srand(s); 57 for (int j = 0; j < m_height; j++) { 58 for (int i = 0; i < m_width; i++) { 59 int x = rand() / (int)(((unsigned)RAND_MAX + 1) / 100); 60 m_matrix->data[i + j * m_width] = x > 75 ? 1 : 0; // 25% occupied 61 } 62 } 63 } 64 65 void Board::seed(const BoardPtr src) { 66 memcpy(m_matrix->data, src->m_matrix->data, m_height * m_width); 67 } 68 69 //! Print usage of this program 70 void PrintUsage() { 71 printf("Usage: game_of_life [M[:N] -t execution_time]\n" 72 "M and N are a range of numbers of threads to be used.\n" 73 "execution_time is a time (in sec) for execution game_of_life iterations\n"); 74 printf("Default values:\n" 75 "M:\t\tautomatic\n" 76 "N:\t\tM\n" 77 "execution_time:\t10\n"); 78 } 79 80 //! Parse command line 81 bool ParseCommandLine(int argc, char* argv[]) { 82 char* s = argv[1]; 83 char* end; 84 //! command line without parameters 85 if (argc == 1) { 86 low = utility::get_default_num_threads(); 87 high = low; 88 execution_time = 5; 89 return true; 90 } 91 //! command line with parameters 92 if (argc != 4) { 93 PrintUsage(); 94 return false; 95 } 96 if (std::string("-t") != argv[argc - 2]) 97 //! process M[:N] parameter 98 high = strtol(s, &end, 0); 99 low = strtol(s, &end, 0); 100 switch (*end) { 101 case ':': high = strtol(end + 1, nullptr, 0); break; 102 case '\0': break; 103 default: PrintUsage(); return false; 104 } 105 if (high < low) { 106 std::cout << "Set correct range. Current range: " << low << ":" << high << "\n"; 107 PrintUsage(); 108 return false; 109 } 110 //! process execution_time parameter 111 execution_time = strtol(argv[argc - 1], &end, 0); 112 return true; 113 } 114 115 int main(int argc, char* argv[]) { 116 if (!ParseCommandLine(argc, argv)) 117 return -1; 118 SequentialEvolution* m_seq; 119 ParallelEvolution* m_par; 120 Board* m_board1; 121 Board* m_board2; 122 int* count = nullptr; 123 124 int boardWidth = 300; 125 int boardHeight = 300; 126 127 m_board1 = new Board(boardWidth, boardHeight, BOARD_SQUARE_SIZE, count); 128 m_board2 = new Board(boardWidth, boardHeight, BOARD_SQUARE_SIZE, count); 129 130 time_t now = time(nullptr); 131 printf("Generate Game of life board\n"); 132 m_board1->seed((int)now); 133 m_board2->seed(m_board1); 134 135 m_seq = new SequentialEvolution(m_board1->m_matrix, m_board1); 136 m_seq->Run(execution_time, 1); 137 delete m_seq; 138 139 m_par = new ParallelEvolution(m_board2->m_matrix, m_board2); 140 for (int p = low; p <= high; ++p) { 141 m_par->Run(execution_time, p); 142 } 143 delete m_par; 144 145 delete m_board1; 146 delete m_board2; 147 return 0; 148 } 149