<?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>So Jake Says &#187; workaround</title>
	<atom:link href="http://www.jakevoytko.com/blog/tag/workaround/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.jakevoytko.com/blog</link>
	<description>Ye Olde Computer Science Blogge</description>
	<lastBuildDate>Sun, 17 Jan 2010 15:16:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>A Code Workaround for a Busted Furnace</title>
		<link>http://www.jakevoytko.com/blog/2010/01/17/a-code-workaround-for-a-busted-furnace/</link>
		<comments>http://www.jakevoytko.com/blog/2010/01/17/a-code-workaround-for-a-busted-furnace/#comments</comments>
		<pubDate>Sun, 17 Jan 2010 15:16:00 +0000</pubDate>
		<dc:creator>Jake</dc:creator>
				<category><![CDATA[Misc]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[boost]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[furnace]]></category>
		<category><![CDATA[threading]]></category>
		<category><![CDATA[workaround]]></category>

		<guid isPermaLink="false">http://www.jakevoytko.com/blog/?p=406</guid>
		<description><![CDATA[warmup.cpp: [code] Late last December, I woke up and it was freezing in my apartment. I checked the thermostat, and it was already less than 60 degrees! I turned it from hot to cold to hot. And again. I turned it all the way up. The furnace never clicked on. COME ON! I checked, and [...]]]></description>
			<content:encoded><![CDATA[<p>warmup.cpp: [<a href="http://github.com/jakevoytko/warmup">code</a>]</p>
<p>Late last December, I woke up and it was <strong>freezing</strong> in my apartment. I checked the thermostat, and it was already less than 60 degrees! I turned it from hot to cold to hot. And again. I turned it all the way up. The furnace never clicked on. COME ON! I checked, and the pilot light was on, so I had no choice but to call maintenance to get it fixed.</p>
<p>Most days this wouldn&#8217;t have been a problem, since it would be fixed while I was at work, but I was in my end-of-year vacation-burning crunch. Maintenance always fix minor problems really fast, so I knew it would take all day to come and fix the heater. Sure enough, help didn&#8217;t arrive until 6PM.</p>
<p>I tried lying motionless on the floor under some blankets and played some Assassin&#8217;s Creed 2. This worked for an hour or so, but if I&#8217;m at home all day and not working on something on the computer, I start to feel like a waste. This was in the middle of trying to get <a href="http://www.vocabdojo.com/">a side project</a> of mine off the ground, and I wanted to get a beta version working before returning to work in January.</p>
<p>Could I use my laptop as a heater? It gets pretty warm when the CPU runs at full blast on one core. If I kept both going for hours, would that be enough?</p>
<p>8 minutes later, I had a C++ program that ran 2 CPUs at full blast. I hate picking C++ for a personal project, but I&#8217;ve used it for 10 years and I&#8217;m just not familiar enough with any other language&#8217;s CPU usage patterns to do the work that quickly. I probably didn&#8217;t have to worry, but I was cold!</p>
<p>The program, <code>warmup</code>, runs NUM_CPU copies of this thread:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">int</span> fn<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">bool</span> <span style="color: #000040;">*</span>go<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">double</span> a <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">static</span> mt19937 rng<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">static_cast</span><span style="color: #000040;">&amp;</span>lt<span style="color: #008080;">;</span><span style="color: #0000ff;">unsigned</span><span style="color: #000040;">&amp;</span>gt<span style="color: #008080;">;</span> <span style="color: #008000;">&#40;</span>std<span style="color: #008080;">::</span><span style="color: #0000dd;">time</span><span style="color: #008000;">&#40;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  normal_distribution<span style="color: #000040;">&amp;</span>lt<span style="color: #008080;">;</span><span style="color: #0000ff;">double</span><span style="color: #000040;">&amp;</span>gt<span style="color: #008080;">;</span> norm_dist<span style="color: #008000;">&#40;</span><span style="color:#800080;">1.0</span>, <span style="color:#800080;">1.0</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  variate_generator<span style="color: #000040;">&amp;</span>lt<span style="color: #008080;">;</span>mt19937<span style="color: #000040;">&amp;</span>amp<span style="color: #008080;">;</span>, normal_distribution<span style="color: #000040;">&amp;</span>lt<span style="color: #008080;">;</span><span style="color: #0000ff;">double</span><span style="color: #000040;">&amp;</span>gt<span style="color: #008080;">;</span> <span style="color: #000040;">&amp;</span>gt<span style="color: #008080;">;</span>
  normal_sampler<span style="color: #008000;">&#40;</span>rng, norm_dist<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
  <span style="color: #0000ff;">while</span><span style="color: #008000;">&#40;</span><span style="color: #000040;">*</span>go<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">double</span> val <span style="color: #000080;">=</span> normal_sampler<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    a <span style="color: #000040;">+</span><span style="color: #000080;">=</span> val<span style="color: #008080;">;</span>
  <span style="color: #008000;">&#125;</span>
  <span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>I already had Boost, and I&#8217;d never used their pRNGs before, so I decided if it was a copy/paste job, I would give them a test run. Sure enough, it plugged right in.</p>
<p>The main function is even simpler:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">bool</span> go <span style="color: #000080;">=</span> <span style="color: #0000ff;">true</span><span style="color: #008080;">;</span>
  boost<span style="color: #008080;">::</span><span style="color: #007788;">scoped_ptr</span><span style="color: #000040;">&amp;</span>lt<span style="color: #008080;">;</span>boost<span style="color: #008080;">::</span><span style="color: #007788;">thread</span><span style="color: #000040;">&amp;</span>gt<span style="color: #008080;">;</span> threads<span style="color: #008000;">&#91;</span>NUM_THREADS<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
&nbsp;
  <span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> i<span style="color: #000080;">=</span><span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> i<span style="color: #000040;">&amp;</span>lt<span style="color: #008080;">;</span>NUM_THREADS<span style="color: #008080;">;</span> <span style="color: #000040;">++</span>i<span style="color: #008000;">&#41;</span>
  threads<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span>.<span style="color: #007788;">reset</span><span style="color: #008000;">&#40;</span><span style="color: #0000dd;">new</span> boost<span style="color: #008080;">::</span><span style="color: #007788;">thread</span><span style="color: #008000;">&#40;</span>fn, <span style="color: #000040;">&amp;</span>amp<span style="color: #008080;">;</span>go<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
  std<span style="color: #008080;">::</span><span style="color: #007788;">string</span> asdf<span style="color: #008080;">;</span>
  <span style="color: #0000dd;">cout</span><span style="color: #000040;">&amp;</span>lt<span style="color: #008080;">;</span><span style="color: #000040;">&amp;</span>lt<span style="color: #008080;">;</span><span style="color: #FF0000;">&quot;enter any input and hit &amp;lt;Enter&amp;gt; to kill&quot;</span><span style="color: #000040;">&amp;</span>lt<span style="color: #008080;">;</span><span style="color: #000040;">&amp;</span>lt<span style="color: #008080;">;</span>endl<span style="color: #008080;">;</span>
  <span style="color: #0000dd;">cin</span><span style="color: #000040;">&amp;</span>gt<span style="color: #008080;">;</span><span style="color: #000040;">&amp;</span>gt<span style="color: #008080;">;</span>asdf<span style="color: #008080;">;</span>
&nbsp;
  go <span style="color: #000080;">=</span> <span style="color: #0000ff;">false</span><span style="color: #008080;">;</span>
&nbsp;
  <span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> i<span style="color: #000080;">=</span><span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> i<span style="color: #000040;">&amp;</span>lt<span style="color: #008080;">;</span>NUM_THREADS<span style="color: #008080;">;</span> <span style="color: #000040;">++</span>i<span style="color: #008000;">&#41;</span>
    threads<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #000040;">-</span><span style="color: #000040;">&amp;</span>gt<span style="color: #008080;">;</span>join<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
  <span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>Within 12 minutes of starting, I was nice and toasty!</p>
<p>I posted the code on Github in case I needed it somewhere else: [<a href="http://github.com/jakevoytko/warmup">code</a>]<br />
﻿</p>
<img src="http://www.jakevoytko.com/blog/?ak_action=api_record_view&id=406&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.jakevoytko.com/blog/2010/01/17/a-code-workaround-for-a-busted-furnace/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
