2 Copyright (c) 2005-2019 Intel Corporation
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
8 http://www.apache.org/licenses/LICENSE-2.0
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.
23 #include "../../../common/utility/get_default_num_threads.h"
25 #define WIN32_LEAN_AND_MEAN
33 #include "Evolution.h"
35 #define BOARD_SQUARE_SIZE 2
37 int low; //! lower range limit of threads
38 int high; //! high range limit of threads
39 double execution_time; //! time for game of life iterations
42 Board::Board(int width, int height, int squareSize, LabelPtr counter)
43 : m_width(width), m_height(height), m_squareSize(squareSize), m_counter(counter)
46 InitializeComponent();
47 DoubleBuffered = true;
49 this->Width = m_squareSize*width;
50 this->Height = m_squareSize*height;
52 m_matrix = new Matrix();
53 m_matrix->width = width;
54 m_matrix->height = height;
55 m_matrix->data = new char[width*height];
56 memset(m_matrix->data, 0, width*height);
58 m_occupiedBrush = gcnew SolidBrush(Color::Black);
59 m_freeBrush = gcnew SolidBrush(Color::LightGray);
61 m_graphics = CreateGraphics();
62 m_bmp = gcnew Bitmap(Width, Height);
63 m_mem_dc = Graphics::FromImage(m_bmp);
75 delete[] m_matrix->data;
79 void Board::seed(int s)
82 for (int j=0; j<m_height; j++)
84 for (int i=0; i<m_width; i++)
86 int x = rand()/(int)(((unsigned)RAND_MAX + 1) / 100);
87 m_matrix->data[i+j*m_width] = x>75? 1: 0; // 25% occupied
95 void Board::seed( const BoardPtr src )
97 memcpy(m_matrix->data, src->m_matrix->data, m_height*m_width);
104 void Board::draw(Graphics^ g)
106 m_mem_dc->FillRectangle(m_freeBrush, Drawing::Rectangle(0, 0, m_width*m_squareSize, m_height*m_squareSize));
107 for (int j=0; j<m_height; j++)
109 for (int i=0; i<m_width; i++)
111 if ( m_matrix->data[i+j*m_width] )
113 m_mem_dc->FillRectangle(m_occupiedBrush, Drawing::Rectangle(i*m_squareSize, j*m_squareSize, m_squareSize, m_squareSize));
117 g->DrawImage(m_bmp, 0, 0);
120 void Board::OnPaint(PaintEventArgs^ e)
126 int main(array<System::String ^> ^args)
128 // Enabling Windows XP visual effects before any controls are created
129 Application::EnableVisualStyles();
130 Application::SetCompatibleTextRenderingDefault(false);
132 // Create the main window and run it
133 Application::Run(gcnew Form1());
138 //! Print usage of this program
141 printf("Usage: gol [M[:N] -t execution_time]\nM and N are a range of numbers of threads to be used.\nexecution_time is a time (in sec) for execution game_of_life iterations\n");
142 printf("Default values:\nM:\t\tautomatic\nN:\t\tM\nexecution_time:\t10\n");
145 //! Parse command line
146 bool ParseCommandLine(int argc, char * argv [])
150 //! command line without parameters
153 low = utility::get_default_num_threads();
158 //! command line with parameters
164 if(std::string("-t") != argv[argc-2])
165 //! process M[:N] parameter
166 high = strtol(s,&end,0);
167 low = strtol(s,&end,0);
171 high = strtol(end+1,0,0);
181 std::cout << "Set correct range. Current range: " << low << ":" << high << std::endl;
186 //! process execution_time parameter
187 execution_time = strtol(argv[argc-1],&end,0);
191 int main( int argc, char* argv[] )
193 if(!ParseCommandLine( argc, argv ))
195 SequentialEvolution* m_seq;
196 ParallelEvolution* m_par;
201 int boardWidth = 300;
202 int boardHeight = 300;
204 m_board1 = new Board(boardWidth, boardHeight, BOARD_SQUARE_SIZE, count);
205 m_board2 = new Board(boardWidth, boardHeight, BOARD_SQUARE_SIZE, count);
207 time_t now = time(NULL);
208 printf("Generate Game of life board\n");
209 m_board1->seed((int)now);
210 m_board2->seed(m_board1);
212 m_seq = new SequentialEvolution(m_board1->m_matrix, m_board1);
213 m_seq->Run(execution_time, 1);
216 m_par = new ParallelEvolution(m_board2->m_matrix, m_board2);
217 for( int p = low; p <= high; ++p )
219 m_par->Run(execution_time, p);