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.
18 Evolution.cpp: implementation file for evolution classes; evolution
19 classes do looped evolution of patterns in a defined
23 #include "Evolution.h"
25 #include "../../../common/utility/get_default_num_threads.h"
30 #define GRAIN_SIZE 4000
32 #define TIME_SLICE 330
39 Evolution::UpdateMatrix() - moves the calculated destination data
40 to the source data block. No destination zeroing is required since it will
41 be completely overwritten during the next calculation cycle.
43 void Evolution::UpdateMatrix()
45 memcpy(m_matrix->data, m_dest, m_size);
52 //! SequentialEvolution::Run - begins looped evolution
54 void SequentialEvolution::Run()
57 void SequentialEvolution::Run(double execution_time, int nthread)
59 printf("Starting game (Sequential evolution)\n");
64 tbb::tick_count t0 = tbb::tick_count::now();
69 tbb::tick_count t = tbb::tick_count::now();
71 tbb::tick_count t1 = tbb::tick_count::now();
73 double work_time = (t1-t0).seconds();
75 if ( work_time * 1000 < TIME_SLICE )
77 m_serial_time += work_time;
78 m_board->draw(m_nIteration);
80 m_serial_time += work_time;
83 //! Let the parallel algorithm work uncontended almost the same time
84 //! as the serial one. See ParallelEvolution::Run() as well.
86 m_evt_start_parallel->Set();
87 m_evt_start_serial->WaitOne();
88 t0 = tbb::tick_count::now();
90 t0 = tbb::tick_count::now();
91 if(m_serial_time > execution_time)
93 printf("iterations count = %d time = %g\n", m_nIteration, m_serial_time);
100 //! SequentialEvolution::Step() - override of step method
101 void SequentialEvolution::Step()
106 UpdateState(m_matrix, m_matrix->data, 0, m_matrix->height);
108 UpdateState(m_matrix, m_dest, 0, (m_matrix->width * m_matrix->height)-1);
118 //! SequentialEvolution::Run - begins looped evolution
120 void ParallelEvolution::Run()
123 void ParallelEvolution::Run(double execution_time, int nthread)
125 if(nthread == utility::get_default_num_threads())
126 printf("Starting game (Parallel evolution for automatic number of thread(s))\n");
128 printf("Starting game (Parallel evolution for %d thread(s))\n", nthread);
135 //! start task scheduler as necessary
136 if (m_pGlobControl == NULL)
138 m_pGlobControl = new tbb::global_control(tbb::global_control::max_allowed_parallelism, utility::get_default_num_threads());
140 m_evt_start_parallel->WaitOne();
142 tbb::global_control* pGlobControl = new tbb::global_control(tbb::global_control::max_allowed_parallelism, nthread);
145 double work_time = m_serial_time;
146 tbb::tick_count t0 = tbb::tick_count::now();
152 tbb::tick_count t = tbb::tick_count::now();
154 tbb::tick_count t1 = tbb::tick_count::now();
156 double real_work_time = (t1-t0).seconds();
158 if ( real_work_time < work_time )
160 m_parallel_time += real_work_time;
161 m_board->draw(m_nIteration);
163 m_parallel_time += real_work_time;
166 //! Let the serial algorithm work the same time as the parallel one.
168 m_evt_start_serial->Set();
169 m_evt_start_parallel->WaitOne();
171 work_time = m_serial_time - m_parallel_time;
172 t0 = tbb::tick_count::now();
174 t0 = tbb::tick_count::now();
175 if(m_parallel_time > execution_time)
177 printf("iterations count = %d time = %g\n", m_nIteration, m_parallel_time);
178 delete pGlobControl; pGlobControl = NULL;
188 class tbb_parallel_task
190 TBB requires a class for parallel loop implementations. The actual
191 loop "chunks" are performed using the () operator of the class.
192 The blocked_range contains the range to calculate. Please see the
193 TBB documentation for more information.
196 public class tbb_parallel_task
198 class tbb_parallel_task
202 static void set_values (Matrix* source, char* dest)
209 void operator()( const tbb::blocked_range<size_t>& r ) const
211 int begin = (int)r.begin(); //! capture lower range number for this chunk
212 int end = (int)r.end(); //! capture upper range number for this chunk
213 UpdateState(m_source, m_dest, begin, end);
216 tbb_parallel_task () {}
219 static Matrix* m_source;
223 Matrix* tbb_parallel_task::m_source;
224 char* tbb_parallel_task::m_dest;
226 //! ParallelEvolution::Step() - override of Step method
227 void ParallelEvolution::Step()
229 size_t begin = 0; //! beginning cell position
231 size_t end = m_matrix->height; //! ending cell position
233 size_t end = m_size-1; //! ending cell position
236 //! set matrix pointers
237 tbb_parallel_task::set_values(m_matrix, m_dest);
239 //! do calculation loop
240 parallel_for (tbb::blocked_range<size_t> (begin, end, GRAIN_SIZE), tbb_parallel_task());