![]() |
Shadowrun: Awakened 29 September 2011 - Build 871
|
00001 /* 00002 Copyright 2005-2010 Intel Corporation. All Rights Reserved. 00003 00004 This file is part of Threading Building Blocks. 00005 00006 Threading Building Blocks is free software; you can redistribute it 00007 and/or modify it under the terms of the GNU General Public License 00008 version 2 as published by the Free Software Foundation. 00009 00010 Threading Building Blocks is distributed in the hope that it will be 00011 useful, but WITHOUT ANY WARRANTY; without even the implied warranty 00012 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 GNU General Public License for more details. 00014 00015 You should have received a copy of the GNU General Public License 00016 along with Threading Building Blocks; if not, write to the Free Software 00017 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 00018 00019 As a special exception, you may use this file as part of a free software 00020 library without restriction. Specifically, if other files instantiate 00021 templates or use macros or inline functions from this file, or you compile 00022 this file and link it with other files to produce an executable, this 00023 file does not by itself cause the resulting executable to be covered by 00024 the GNU General Public License. This exception does not however 00025 invalidate any other reasons why the executable file might be covered by 00026 the GNU General Public License. 00027 */ 00028 00029 /* 00030 Evolution.cpp: implementation file for evolution classes; evolution 00031 classes do looped evolution of patterns in a defined 00032 2 dimensional space 00033 */ 00034 00035 #include "Evolution.h" 00036 #include "Board.h" 00037 00038 #ifdef USE_SSE 00039 #define GRAIN_SIZE 14 00040 #else 00041 #define GRAIN_SIZE 4000 00042 #endif 00043 #define TIME_SLICE 330 00044 00045 /* 00046 Evolution 00047 */ 00048 00054 void Evolution::UpdateMatrix() 00055 { 00056 memcpy(m_matrix->data, m_dest, m_size); 00057 } 00058 00059 /* 00060 SequentialEvolution 00061 */ 00062 00064 #ifndef _CONSOLE 00065 void SequentialEvolution::Run() 00066 { 00067 #else 00068 void SequentialEvolution::Run(double execution_time, int nthread) 00069 { 00070 printf("Starting game (Sequential evolution)\n"); 00071 #endif 00072 00073 m_nIteration = 0; 00074 m_serial_time = 0; 00075 tbb::tick_count t0 = tbb::tick_count::now(); 00076 while (!m_done) 00077 { 00078 if( !is_paused ) 00079 { 00080 tbb::tick_count t = tbb::tick_count::now(); 00081 Step(); 00082 tbb::tick_count t1 = tbb::tick_count::now(); 00083 ++m_nIteration; 00084 double work_time = (t1-t0).seconds(); 00085 #ifndef _CONSOLE 00086 if ( work_time * 1000 < TIME_SLICE ) 00087 continue; 00088 m_serial_time += work_time; 00089 m_board->draw(m_nIteration); 00090 #else 00091 m_serial_time += work_time; 00092 #endif 00093 } 00096 #ifndef _CONSOLE 00097 m_evt_start_parallel->Set(); 00098 m_evt_start_serial->WaitOne(); 00099 t0 = tbb::tick_count::now(); 00100 #else 00101 t0 = tbb::tick_count::now(); 00102 if(m_serial_time > execution_time) 00103 { 00104 printf("iterations count = %d time = %g\n", m_nIteration, m_serial_time); 00105 break; 00106 } 00107 #endif 00108 } 00109 } 00110 00112 void SequentialEvolution::Step() 00113 { 00114 if( !is_paused ) 00115 { 00116 #ifdef USE_SSE 00117 UpdateState(m_matrix, m_matrix->data, 0, m_matrix->height); 00118 #else 00119 UpdateState(m_matrix, m_dest, 0, (m_matrix->width * m_matrix->height)-1); 00120 UpdateMatrix(); 00121 #endif 00122 } 00123 } 00124 00125 /* 00126 ParallelEvolution 00127 */ 00128 00130 #ifndef _CONSOLE 00131 void ParallelEvolution::Run() 00132 { 00133 #else 00134 void ParallelEvolution::Run(double execution_time, int nthread) 00135 { 00136 if(nthread == tbb::task_scheduler_init::automatic) 00137 printf("Starting game (Parallel evolution for automatic number of thread(s))\n"); 00138 else 00139 printf("Starting game (Parallel evolution for %d thread(s))\n", nthread); 00140 #endif 00141 00142 m_nIteration = 0; 00143 m_parallel_time = 0; 00144 00145 #ifndef _CONSOLE 00146 00147 if (m_pInit == NULL) 00148 { 00149 m_pInit = new tbb::task_scheduler_init(); 00150 } 00151 m_evt_start_parallel->WaitOne(); 00152 #else 00153 tbb::task_scheduler_init init(nthread); 00154 #endif 00155 00156 double work_time = m_serial_time; 00157 tbb::tick_count t0 = tbb::tick_count::now(); 00158 00159 while (!m_done) 00160 { 00161 if( !is_paused ) 00162 { 00163 tbb::tick_count t = tbb::tick_count::now(); 00164 Step(); 00165 tbb::tick_count t1 = tbb::tick_count::now(); 00166 ++m_nIteration; 00167 double real_work_time = (t1-t0).seconds(); 00168 #ifndef _CONSOLE 00169 if ( real_work_time < work_time ) 00170 continue; 00171 m_parallel_time += real_work_time; 00172 m_board->draw(m_nIteration); 00173 #else 00174 m_parallel_time += real_work_time; 00175 #endif 00176 } 00178 #ifndef _CONSOLE 00179 m_evt_start_serial->Set(); 00180 m_evt_start_parallel->WaitOne(); 00181 00182 work_time = m_serial_time - m_parallel_time; 00183 t0 = tbb::tick_count::now(); 00184 #else 00185 t0 = tbb::tick_count::now(); 00186 if(m_parallel_time > execution_time) 00187 { 00188 printf("iterations count = %d time = %g\n", m_nIteration, m_parallel_time); 00189 init.terminate(); 00190 break; 00191 } 00192 #endif 00193 } 00194 } 00195 00204 #ifndef _CONSOLE 00205 public class tbb_parallel_task 00206 #else 00207 class tbb_parallel_task 00208 #endif 00209 { 00210 public: 00211 static void set_values (Matrix* source, char* dest) 00212 { 00213 m_source = source; 00214 m_dest = dest; 00215 return; 00216 } 00217 00218 void operator()( const tbb::blocked_range<size_t>& r ) const 00219 { 00220 int begin = (int)r.begin(); 00221 int end = (int)r.end(); 00222 UpdateState(m_source, m_dest, begin, end); 00223 } 00224 00225 tbb_parallel_task () {} 00226 00227 private: 00228 static Matrix* m_source; 00229 static char* m_dest; 00230 }; 00231 00232 Matrix* tbb_parallel_task::m_source; 00233 char* tbb_parallel_task::m_dest; 00234 00236 void ParallelEvolution::Step() 00237 { 00238 size_t begin = 0; 00239 #ifdef USE_SSE 00240 size_t end = m_matrix->height; 00241 #else 00242 size_t end = m_size-1; 00243 #endif 00244 00246 tbb_parallel_task::set_values(m_matrix, m_dest); 00247 00249 parallel_for (tbb::blocked_range<size_t> (begin, end, GRAIN_SIZE), tbb_parallel_task()); 00250 UpdateMatrix(); 00251 }
Copyright © 2007-2010 by The Shadowrun: Awakened Team. This work is licensed under the GNU Lesser General Public License 3.