<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Xavier Llorà &#187; profiling</title>
	<atom:link href="http://www.xavierllora.net/tag/profiling/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.xavierllora.net</link>
	<description>A notebook on data-intensive computing, genetics-based machine learning &#38; more.</description>
	<lastBuildDate>Sun, 08 Jan 2012 19:39:15 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Squeezing for cycles</title>
		<link>http://www.xavierllora.net/2009/04/01/squeezing-for-cycles/</link>
		<comments>http://www.xavierllora.net/2009/04/01/squeezing-for-cycles/#comments</comments>
		<pubDate>Thu, 02 Apr 2009 01:55:24 +0000</pubDate>
		<dc:creator>Xavier</dc:creator>
				<category><![CDATA[Notes]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[genetic algorithms]]></category>
		<category><![CDATA[multithreading]]></category>
		<category><![CDATA[optimization]]></category>
		<category><![CDATA[profiling]]></category>

		<guid isPermaLink="false">http://www.xavierllora.net/?p=491</guid>
		<description><![CDATA[Sometimes thinking a bit helps to rush decisions that may lead to weird places. Today I was going over a simple genetic algorithm for numeric optimization written in C. The code is nothing special, tournament selection without replacement, SBX crossover operator, and polynomial mutation. To the point, I was running a simple OneMax-like problem (in [...]
Related posts:<ol>
<li><a href='http://www.xavierllora.net/2005/11/27/evaluation-consistency-in-igas-user-contradictions-as-cycles-in-partial-ordering-graphs/' rel='bookmark' title='Evaluation consistency in iGAs: User contradictions as cycles in partial-ordering graphs'>Evaluation consistency in iGAs: User contradictions as cycles in partial-ordering graphs</a></li>
<li><a href='http://www.xavierllora.net/2008/11/13/fast-mutation-implementation-for-genetic-algorithms-in-python/' rel='bookmark' title='Fast mutation implementation for genetic algorithms in Python'>Fast mutation implementation for genetic algorithms in Python</a></li>
<li><a href='http://www.xavierllora.net/2006/07/07/analyzing-active-interactive-genetic-algorithms-using-visual-analytics/' rel='bookmark' title='Analyzing active interactive genetic algorithms using visual analytics'>Analyzing active interactive genetic algorithms using visual analytics</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>Sometimes thinking a bit helps to rush decisions that may lead to weird places. Today I was going over a simple genetic algorithm for numeric optimization written in C. The code is nothing special, tournament selection without replacement, SBX crossover operator, and polynomial mutation. To the point, I was running a simple OneMax-like problem (in this case, minimize the value of the sum of all the genes), and I was quite surprised the guy was taking so long for. </p>
<pre>
$time ./GATest
----------------------- GA parameters ------------------------------------
Seed: 69
Lower/upper bounds: [1.000000,0.000000]
Genes: 18
Population size: 2000
Iterations: 30
Tournament size: 6
Crossover: pc=0.900000, gwp=0.500000, etaC=10.000000
Mutation: pm=0.100000, etaM=20.000000
----------------------- Evolution Statistics -----------------------------
4.663210	8.974190	13.158102
3.351912	7.405489	11.619360
2.285005	5.375426	9.531691
1.326318	3.711156	7.178203
0.767981	2.432192	4.790854
0.392001	1.543097	3.223604
0.279961	0.977706	2.308249
0.173406	0.600002	1.335702
0.092746	0.359343	0.877080
0.044705	0.216218	0.533978
0.029053	0.130256	0.315306
0.022827	0.078331	0.172908
0.013641	0.047317	0.105886
0.007370	0.028098	0.066994
0.004320	0.016787	0.038499
0.002807	0.010254	0.025155
0.001604	0.006238	0.014528
0.001007	0.003799	0.008883
0.000708	0.002212	0.005627
0.000343	0.001305	0.003263
0.000211	0.000781	0.002025
0.000131	0.000468	0.001155
0.000085	0.000280	0.000774
0.000054	0.000168	0.000392
0.000031	0.000100	0.000243
0.000017	0.000061	0.000144
0.000010	0.000037	0.000083
0.000006	0.000022	0.000054
0.000003	0.000013	0.000035
0.000002	0.000008	0.000020
0.000002	0.000005	0.000011
----------------------- Final outcome ------------------------------------
Min:	(0.000002)	0.000000	0.000000	0.000000	0.000000.000000	0.000000	0.000000	0.000000	0.000000	0.000000.000000	0.000000	0.000000	0.000000	0.000000	0.000000.000000	0.000000
Max:	(0.000011)	0.000000	0.000001	0.000000	0.000000.000001	0.000000	0.000000	0.000001	0.000000	0.000000.000000	0.000003	0.000000	0.000000	0.000000	0.000000.000002	0.000001	

real	0m6.748s
user	0m6.228s
sys	0m0.088s
</pre>
<p>Yup, after turning on all the possible compiler optimizations I could think of, 6.7 seconds was the best I could do on a first generation MacBook Pro. I was wondering if I should spend the time writing a simple multithreaded evaluation. As I said, before making something simple complicated, I decided to put grab <a href="http://developer.apple.com/tools/performance/optimizingwithsystemtrace.html">Shark</a> (Mac&#8217;s free profiler) and get a better picture of what was going. Oh boy! Intuition looking at the wrong place! The outcome: 45% of time spend on tournament selection and 31% generating random number. Mmh, digging a bit further almost all time of tournament selection was spent shuffling an array to guarantee tournaments without replacements.</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">/** Runs tournament selection without replacement to create a new population
 * 
 * popDest: The destination population
 * popInit: The original population
 * fita: The fitness of the initial populaiton
 * iSize: Tournament size
 * iIndividuals: The population size
 * iGenes: The number of genes of an individual
 */</span>
<span style="color: #993333;">void</span> ga_tournament_selection <span style="color: #009900;">&#40;</span> Population popDest<span style="color: #339933;">,</span> Population popInit<span style="color: #339933;">,</span> Fitness <span style="color: #339933;">*</span> fita<span style="color: #339933;">,</span> <span style="color: #993333;">int</span> iSize<span style="color: #339933;">,</span> <span style="color: #993333;">int</span> iIndividuals <span style="color: #339933;">,</span> <span style="color: #993333;">int</span> iGenes <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #993333;">int</span> piaShuffle<span style="color: #009900;">&#91;</span>iSize<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #993333;">int</span> i <span style="color: #339933;">=</span> <span style="color: #339933;">-</span><span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
	<span style="color: #993333;">int</span> j <span style="color: #339933;">=</span> <span style="color: #339933;">-</span><span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #993333;">int</span> iIndTmp <span style="color: #339933;">=</span> <span style="color: #339933;">-</span><span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
	<span style="color: #993333;">int</span> iIndWin <span style="color: #339933;">=</span> <span style="color: #339933;">-</span><span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
&nbsp;
	Fitness fitWin <span style="color: #339933;">=</span> DBL_MAX<span style="color: #339933;">;</span>
	Fitness fitTmp <span style="color: #339933;">=</span> DBL_MAX<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span> i<span style="color: #339933;">=</span><span style="color: #0000dd;">0</span> <span style="color: #339933;">;</span> i<span style="color: #339933;">&lt;</span>iIndividuals <span style="color: #339933;">;</span> i<span style="color: #339933;">++</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #808080; font-style: italic;">/* Initialization for the current tournament */</span>
		fitWin  <span style="color: #339933;">=</span> DBL_MAX<span style="color: #339933;">;</span>
		iIndWin <span style="color: #339933;">=</span> <span style="color: #339933;">-</span><span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
		genrand_shuffle <span style="color: #009900;">&#40;</span>piaShuffle<span style="color: #339933;">,</span>iIndividuals<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span> j<span style="color: #339933;">=</span><span style="color: #0000dd;">0</span> <span style="color: #339933;">;</span> j<span style="color: #339933;">&lt;</span>iSize <span style="color: #339933;">;</span> j<span style="color: #339933;">++</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #666666; font-style: italic;">// A new randomly drawn individual</span>
			iIndTmp <span style="color: #339933;">=</span> piaShuffle<span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
			fitTmp  <span style="color: #339933;">=</span> fita<span style="color: #009900;">&#91;</span>piaShuffle<span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
			<span style="color: #666666; font-style: italic;">// If it is the first is the winner</span>
			<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> iIndWin<span style="color: #339933;">==-</span><span style="color: #0000dd;">1</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				iIndWin  <span style="color: #339933;">=</span> iIndTmp<span style="color: #339933;">;</span>
				fitWin <span style="color: #339933;">=</span> fitTmp<span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
			<span style="color: #666666; font-style: italic;">// If not, chack the fitness (Minimize)</span>
			<span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> fitWin<span style="color: #339933;">&gt;</span>fitTmp <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				iIndWin  <span style="color: #339933;">=</span> iIndTmp<span style="color: #339933;">;</span>
				fitWin <span style="color: #339933;">=</span> fitTmp<span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
		population_copy_individual<span style="color: #009900;">&#40;</span>popDest<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>popInit<span style="color: #009900;">&#91;</span>iIndWin<span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>iGenes<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>		
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>It was <code>genrand_shuffle</code> (see below) the one that took most of the time. Also if you take a close inspection you will see that it is also the one to blame for calling calling too many times <code>genrand_int31</code>.</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">void</span> genrand_shuffle <span style="color: #009900;">&#40;</span> <span style="color: #993333;">int</span> <span style="color: #339933;">*</span> pia<span style="color: #339933;">,</span> <span style="color: #993333;">int</span> iSize <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #993333;">int</span> i<span style="color: #339933;">,</span> iOther<span style="color: #339933;">;</span>
	<span style="color: #993333;">register</span> <span style="color: #993333;">int</span> iTmp<span style="color: #339933;">;</span>
	<span style="color: #993333;">int</span> iRndMax <span style="color: #339933;">=</span> iSize<span style="color: #339933;">-</span><span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// Initialize</span>
	<span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span> i<span style="color: #339933;">=</span><span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> i<span style="color: #339933;">&lt;</span>iSize<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span> <span style="color: #009900;">&#41;</span> pia<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> i<span style="color: #339933;">;</span>
	<span style="color: #666666; font-style: italic;">// shuffle</span>
	<span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span> i<span style="color: #339933;">=</span><span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> i<span style="color: #339933;">&lt;</span>iRndMax<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	    iOther <span style="color: #339933;">=</span> genrand_int31<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">%</span>iSize<span style="color: #339933;">;</span>
	    iTmp <span style="color: #339933;">=</span> pia<span style="color: #009900;">&#91;</span>iOther<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
	    pia<span style="color: #009900;">&#91;</span>iOther<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> pia<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
	    pia<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> iTmp<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This inherited implementation of tournament selection works well for small populations, but as you increase the population size, each tournament requires shuffling a number proportional to the population size. If you make the numbers, that leads to a quadratic implementation of tournament selection without replacement. Mmh, really needed? Definitely not. The only thing you need to guarantee to provide a tournament selection without replacement is that you provide different individuals for the tournaments (avoiding repetition). If that selection can be done quickly, you can take the complexity of the implementation down to linear. So there I went, and modified the shuffling function as follows.</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">void</span> genrand_shuffle_fast <span style="color: #009900;">&#40;</span> <span style="color: #993333;">int</span> <span style="color: #339933;">*</span> pia<span style="color: #339933;">,</span> <span style="color: #993333;">int</span> iSize<span style="color: #339933;">,</span> <span style="color: #993333;">int</span> iSlots <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #993333;">int</span> i <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
	<span style="color: #993333;">int</span> j <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
&nbsp;
	pia<span style="color: #009900;">&#91;</span>i<span style="color: #339933;">++</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> genrand_int31<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">%</span>iSize<span style="color: #339933;">;</span>
	<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span> i<span style="color: #339933;">&lt;</span>iSlots <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		pia<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> genrand_int31<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">%</span>iSize<span style="color: #339933;">;</span>
		<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span> j<span style="color: #339933;">=</span>i<span style="color: #339933;">-</span><span style="color: #0000dd;">1</span> <span style="color: #339933;">;</span> j<span style="color: #339933;">&gt;=</span><span style="color: #0000dd;">0</span> <span style="color: #339933;">&amp;&amp;</span> j<span style="color: #339933;">&lt;</span>i <span style="color: #339933;">;</span> j<span style="color: #339933;">++</span> <span style="color: #009900;">&#41;</span>
			<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> pia<span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span><span style="color: #339933;">==</span>pia<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#41;</span> 
				<span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> j<span style="color: #339933;">==</span>i <span style="color: #009900;">&#41;</span> i<span style="color: #339933;">++</span> <span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Tournaments sizes are usually much much smaller than population sizes (e.g. s=6 for the pop_size=2,000 individuals population used above). Thus, if random numbers are generated, the chances of repeating it are quite small. Also if you also make sure it is not there (and if it is, you generate a new out), basically your are set. (This implementation will only work efficiently if s&lt;&lt;pop_size, otherwise the cost of checking and generated new numbers will be even worst than the original version).</p>
<p>So there I went. I modified the original inherited version of the tournament selection without replacement, and rerun the simple time measures. </p>
<pre>
$ time ./GATest
----------------------- GA parameters ------------------------------------
Seed: 69
Lower/upper bounds: [1.000000,0.000000]
Genes: 18
Population size: 2000
Iterations: 30
Tournament size: 6
Crossover: pc=0.900000, gwp=0.500000, etaC=10.000000
Mutation: pm=0.100000, etaM=20.000000
----------------------- Evolution Statistics -----------------------------
4.663210	8.974190	13.158102
3.350933	7.401935	11.503243
1.964580	5.461794	9.246779
1.297656	3.819533	7.364562
0.810695	2.512797	5.142622
0.478789	1.603199	3.652348
0.305106	0.999304	2.138109
0.191904	0.602315	1.336870
0.108593	0.361237	0.869652
0.060862	0.219145	0.502403
0.040076	0.136125	0.307478
0.028629	0.084893	0.191327
0.016301	0.052274	0.115169
0.009433	0.032699	0.071849
0.003934	0.020275	0.047970
0.002762	0.012328	0.031204
0.001405	0.007259	0.019575
0.001043	0.004280	0.010909
0.000790	0.002550	0.005799
0.000404	0.001530	0.003566
0.000287	0.000950	0.002406
0.000198	0.000600	0.001390
0.000127	0.000386	0.000818
0.000068	0.000245	0.000599
0.000045	0.000153	0.000377
0.000026	0.000093	0.000206
0.000020	0.000058	0.000125
0.000011	0.000035	0.000095
0.000007	0.000022	0.000049
0.000004	0.000014	0.000029
0.000002	0.000009	0.000018
----------------------- Final outcome ------------------------------------
Min:	(0.000002)	0.000000	0.000000	0.000000	0.000000.000000	0.000000	0.000000	0.000000	0.000000	0.000000.000000	0.000000	0.000000	0.000000	0.000000	0.000000.000000	0.000000
Max:	(0.000018)	0.000001	0.000000	0.000000	0.000000.000000	0.000000	0.000001	0.000001	0.000000	0.000000.000004	0.000000	0.000001	0.000001	0.000002	0.000000.000001	0.000001	

real	0m0.258s
user	0m0.246s
sys	0m0.006s
</pre>
<p>Yup. That simple change yielded a speedup of 26.15. Mmh, as I said, a little bit of thinking helped to avoid going down some crazy path in a rushing code fever for the wrong reasons. Yup, the threading will be very useful if the cost of the evaluation is expensive (as most of the real world optimization are), but for this silly OneMax, no need to make it more complicated than it need <img src='http://www.xavierllora.net/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>Related posts:<ol>
<li><a href='http://www.xavierllora.net/2005/11/27/evaluation-consistency-in-igas-user-contradictions-as-cycles-in-partial-ordering-graphs/' rel='bookmark' title='Evaluation consistency in iGAs: User contradictions as cycles in partial-ordering graphs'>Evaluation consistency in iGAs: User contradictions as cycles in partial-ordering graphs</a></li>
<li><a href='http://www.xavierllora.net/2008/11/13/fast-mutation-implementation-for-genetic-algorithms-in-python/' rel='bookmark' title='Fast mutation implementation for genetic algorithms in Python'>Fast mutation implementation for genetic algorithms in Python</a></li>
<li><a href='http://www.xavierllora.net/2006/07/07/analyzing-active-interactive-genetic-algorithms-using-visual-analytics/' rel='bookmark' title='Analyzing active interactive genetic algorithms using visual analytics'>Analyzing active interactive genetic algorithms using visual analytics</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.xavierllora.net/2009/04/01/squeezing-for-cycles/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

