<?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>Flirting With Models &#187; C++</title>
	<atom:link href="http://coreyhoffstein.com/tag/c/feed/" rel="self" type="application/rss+xml" />
	<link>http://coreyhoffstein.com</link>
	<description>musings on numerical computing and financial modeling</description>
	<lastBuildDate>Mon, 30 Aug 2010 21:53:34 +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>[C++] Learn something new every day</title>
		<link>http://coreyhoffstein.com/2010/01/18/c-learn-something-new-every-day/</link>
		<comments>http://coreyhoffstein.com/2010/01/18/c-learn-something-new-every-day/#comments</comments>
		<pubDate>Mon, 18 Jan 2010 16:01:53 +0000</pubDate>
		<dc:creator>Corey</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[C++]]></category>

		<guid isPermaLink="false">http://coreyhoffstein.com/2010/01/18/c-learn-something-new-every-day/</guid>
		<description><![CDATA[If you want to inherit from a class that does not have a virtual destructor, you will be in trouble if you inherit from it publicly, because deleting through a base-class pointer will lead to nasty behavior. But having a private or protected inheritance means that you won&#8217;t be able to delete through a base [...]]]></description>
			<content:encoded><![CDATA[<p>If you want to inherit from a class that does not have a virtual destructor, you will be in trouble if you inherit from it publicly, because deleting through a base-class pointer will lead to nasty behavior.  But having a private or protected inheritance means that you won&#8217;t be able to delete through a base class &#8212; because you won&#8217;t be ABLE to obtain a pointer to the base class.</p>
<p>So, no virtual destructor doesn&#8217;t mean you can&#8217;t inherit &#8212; it just means that it should be private or protected inheritance.</p>
<p>Of course, composition is probably a better choice, but whatever.</p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save"><img src="http://coreyhoffstein.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://coreyhoffstein.com/2010/01/18/c-learn-something-new-every-day/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Monte-Carlo Integration using Thrust</title>
		<link>http://coreyhoffstein.com/2009/09/18/monte-carlo-integration-using-thrust/</link>
		<comments>http://coreyhoffstein.com/2009/09/18/monte-carlo-integration-using-thrust/#comments</comments>
		<pubDate>Fri, 18 Sep 2009 20:26:45 +0000</pubDate>
		<dc:creator>Corey</dc:creator>
				<category><![CDATA[gpgpu]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[monte-carlo]]></category>
		<category><![CDATA[numerical methods]]></category>

		<guid isPermaLink="false">http://coreyhoffstein.com/?p=186</guid>
		<description><![CDATA[Bored. Wrote a numerical integrator using Thrust for fun. Solves, numerically, the integral from 0 to 1 for (x^2)*cos(x). Analytically, we can solve and get: 2*cos(1) &#8211; sin(1) = 0.239133627 So, the code is as follows: #include &#60;vector_functions.h&#62; &#160; #include &#60;thrust/host_vector.h&#62; #include &#60;thrust/device_vector.h&#62; &#160; #include &#60;thrust/generate.h&#62; #include &#60;thrust/transform.h&#62; #include &#60;thrust/count.h&#62; &#160; #include &#60;cstdlib&#62; #include &#60;math.h&#62; [...]]]></description>
			<content:encoded><![CDATA[<p>Bored.  Wrote a numerical integrator using Thrust for fun.</p>
<p>Solves, numerically, the integral from 0 to 1 for (x^2)*cos(x).</p>
<p><a href="http://coreyhoffstein.com/wp-content/uploads/2009/09/Screen-shot-2009-09-18-at-4.23.39-PM.png"><img class="alignnone size-full wp-image-187" title="x*x*cos(x)" src="http://coreyhoffstein.com/wp-content/uploads/2009/09/Screen-shot-2009-09-18-at-4.23.39-PM.png" alt="x*x*cos(x)" width="473" height="416" /></a></p>
<p>Analytically, we can solve and get: 2*cos(1) &#8211; sin(1) = 0.239133627</p>
<p>So, the code is as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#include &lt;vector_functions.h&gt;</span>
&nbsp;
<span style="color: #339933;">#include &lt;thrust/host_vector.h&gt;</span>
<span style="color: #339933;">#include &lt;thrust/device_vector.h&gt;</span>
&nbsp;
<span style="color: #339933;">#include &lt;thrust/generate.h&gt;</span>
<span style="color: #339933;">#include &lt;thrust/transform.h&gt;</span>
<span style="color: #339933;">#include &lt;thrust/count.h&gt;</span>
&nbsp;
<span style="color: #339933;">#include &lt;cstdlib&gt;</span>
<span style="color: #339933;">#include &lt;math.h&gt;</span>
&nbsp;
float2 bottomLeft<span style="color: #339933;">;</span>
float2 topRight<span style="color: #339933;">;</span>
&nbsp;
__device__
<span style="color: #993333;">float</span> f<span style="color: #009900;">&#40;</span><span style="color: #993333;">float</span> x<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span>x<span style="color: #339933;">*</span>x<span style="color: #339933;">*</span>__cosf<span style="color: #009900;">&#40;</span>x<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #993333;">struct</span> above_below_function <span style="color: #009900;">&#123;</span>
	__device__ 
	<span style="color: #993333;">float</span> operator<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>float2 p<span style="color: #009900;">&#41;</span> <span style="color: #993333;">const</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span>p.<span style="color: #202020;">y</span> <span style="color: #339933;">&lt;</span> f<span style="color: #009900;">&#40;</span>p.<span style="color: #202020;">x</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">?</span> <span style="color: #0000dd;">1</span> <span style="color: #339933;">:</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
float2 make_random_float2<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">return</span> make_float2<span style="color: #009900;">&#40;</span> <span style="color: #009900;">&#40;</span>rand<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">/</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">float</span><span style="color: #009900;">&#41;</span>RAND_MAX<span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">float</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>topRight.<span style="color: #202020;">x</span> <span style="color: #339933;">-</span> bottomLeft.<span style="color: #202020;">x</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">float</span><span style="color: #009900;">&#41;</span>bottomLeft.<span style="color: #202020;">x</span><span style="color: #339933;">,</span>
						<span style="color: #009900;">&#40;</span>rand<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">/</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">float</span><span style="color: #009900;">&#41;</span>RAND_MAX<span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">float</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>topRight.<span style="color: #202020;">y</span> <span style="color: #339933;">-</span> bottomLeft.<span style="color: #202020;">y</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">float</span><span style="color: #009900;">&#41;</span>bottomLeft.<span style="color: #202020;">y</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #993333;">int</span> main<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	srand<span style="color: #009900;">&#40;</span>time<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	bottomLeft.<span style="color: #202020;">x</span> <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
	bottomLeft.<span style="color: #202020;">y</span> <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
&nbsp;
	topRight.<span style="color: #202020;">x</span> <span style="color: #339933;">=</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
	topRight.<span style="color: #202020;">y</span> <span style="color: #339933;">=</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #993333;">int</span> numPoints <span style="color: #339933;">=</span> <span style="color: #0000dd;">10000000</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// find the area of our bounding square</span>
	<span style="color: #993333;">float</span> area <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>topRight.<span style="color: #202020;">x</span> <span style="color: #339933;">-</span> bottomLeft.<span style="color: #202020;">x</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color: #009900;">&#40;</span>topRight.<span style="color: #202020;">y</span> <span style="color: #339933;">-</span> bottomLeft.<span style="color: #202020;">y</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// generate our points</span>
	thrust<span style="color: #339933;">::</span><span style="color: #202020;">host_vector</span><span style="color: #339933;">&lt;</span>float2<span style="color: #339933;">&gt;</span> h_locations<span style="color: #009900;">&#40;</span>numPoints<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>		
	thrust<span style="color: #339933;">::</span><span style="color: #202020;">generate</span><span style="color: #009900;">&#40;</span>h_locations.<span style="color: #202020;">begin</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> h_locations.<span style="color: #202020;">end</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> make_random_float2<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// create two device vectors -- one for our locations, and another to create a count</span>
	thrust<span style="color: #339933;">::</span><span style="color: #202020;">device_vector</span><span style="color: #339933;">&lt;</span>float2<span style="color: #339933;">&gt;</span> d_locations <span style="color: #339933;">=</span> h_locations<span style="color: #339933;">;</span>
	thrust<span style="color: #339933;">::</span><span style="color: #202020;">device_vector</span><span style="color: #339933;">&lt;</span>unsigned int<span style="color: #339933;">&gt;</span> d_boolean<span style="color: #009900;">&#40;</span>numPoints<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// for each element, if the y is below f(x), then put a 1 in the array.  otherwise, put a 0</span>
	thrust<span style="color: #339933;">::</span><span style="color: #202020;">transform</span><span style="color: #009900;">&#40;</span>d_locations.<span style="color: #202020;">begin</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> d_locations.<span style="color: #202020;">end</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> d_boolean.<span style="color: #202020;">begin</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> above_below_function<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #993333;">int</span> total <span style="color: #339933;">=</span> thrust<span style="color: #339933;">::</span><span style="color: #202020;">count</span><span style="color: #009900;">&#40;</span>d_boolean.<span style="color: #202020;">begin</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> d_boolean.<span style="color: #202020;">end</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	std<span style="color: #339933;">::</span><span style="color: #000066;">cout</span> <span style="color: #339933;">&lt;&lt;</span> <span style="color: #ff0000;">&quot;Area under curve: &quot;</span> <span style="color: #339933;">&lt;&lt;</span> area <span style="color: #339933;">*</span> <span style="color: #009900;">&#40;</span>total <span style="color: #339933;">/</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">float</span><span style="color: #009900;">&#41;</span>numPoints<span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;&lt;</span> std<span style="color: #339933;">::</span><span style="color: #202020;">endl</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #b1b100;">return</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The solutions?  Here are a few sample runs&#8230;<br />
Area under curve: 0.239177<br />
Area under curve: 0.238916<br />
Area under curve: 0.239181<br />
Area under curve: 0.239129</p>
<p>Pretty accurate, and pretty damn fast too.</p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save"><img src="http://coreyhoffstein.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://coreyhoffstein.com/2009/09/18/monte-carlo-integration-using-thrust/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Some random libraries</title>
		<link>http://coreyhoffstein.com/2009/07/21/some-random-libraries/</link>
		<comments>http://coreyhoffstein.com/2009/07/21/some-random-libraries/#comments</comments>
		<pubDate>Tue, 21 Jul 2009 16:50:38 +0000</pubDate>
		<dc:creator>Corey</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[trading]]></category>
		<category><![CDATA[Amazon]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[Cloud Computing]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://coreyhoffstein.com/?p=135</guid>
		<description><![CDATA[Random programming libraries that I have discovered in trying to solve some problems lately&#8230; C++ Acedia: acedia is designed to be an easy to use C++ library. It provides an ERLANG like actor implementation with message passing and pattern matching. It&#8217;s main goal is to support development of distributed software &#8211; also across a network. [...]]]></description>
			<content:encoded><![CDATA[<p>Random programming libraries that I have discovered in trying to solve some problems lately&#8230;</p>
<p><strong>C++</strong><br />
<a href="http://sourceforge.net/projects/acedia/">Acedia</a>: acedia is designed to be an easy to use C++ library. It provides an ERLANG like actor implementation with message passing and pattern matching. It&#8217;s main goal is to support development of distributed software &#8211; also across a network.<br />
<a href="http://pocoproject.org/">POCO</a>: The POCO C++ Libraries (POCO stands for POrtable COmponents) are open source C++ class libraries that simplify and accelerate the development of network-centric, portable applications in C++.</p>
<p><strong>Ruby</strong><br />
<a href="http://www.sinatrarb.com/">Sinatra</a>: Embeddable website DSL<br />
<a href="http://github.com/ezmobius/nanite/tree/master/README">Nanite</a>: Nanite is a new way of thinking about building cloud ready web applications. Having a scalable message queueing back-end with all the discovery and dynamic load based dispatch that Nanite has is a very scalable way to construct web application back-ends.<br />
<a href="http://journeta.rubyforge.org/">Journeta</a>: Journeta is a dirt simple library for peer discovery and message passing between Ruby applications on a LAN.</p>
<p>That&#8217;s all for now.  I&#8217;ve been working on some interesting stuff, developing a client registration and authentication system.  I started out in C++ using boost::ASIO to handle server connections, but then I just got annoyed, switched to Ruby, and have been running along ever since.  I am using Revactor (with a few personal tweaks in the code) to handle my components, SQLite3 for a simple database (though, I might have to switch to MySQL for production), and Sinatra for a web-interface (running on Thin with Shotgun).  All in all, I must say that simply plugging in working components instead of having to develop the resources makes production so much easier&#8230;  As always, hardware is cheaper than programmer hours.</p>
<p>Speaking of which, why did I not notice Amazon&#8217;s Auto Scaling and Load Balancing features before?  If you don&#8217;t have the hardware resources to build your own cloud (Eucalyptus with Xen), Amazon really does seem like a very cheap alternative.  I mean, think of all the money you save not having to hire I.T. guys to figure out why your harddrive failed in the middle of the night.  Amazon does it all for you!</p>
<p>Hopefully, some sort of Cloud Computing standard will come out soon.  The only current problem with Amazon&#8217;s model is that it doesn&#8217;t give the clients any leverage.  Start using Amazon and you can&#8217;t make the threat to leave and go to Google (well, easily at least).  Worse yet, if Amazon goes out of business, you are in a world of hurt.  Eucalyptus adopted the Amazon scheme, so at least there is a bit of leverage saying that you will just buy your own hardware &#8212; but it would be much better if we could get some pricing competition going on.</p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save"><img src="http://coreyhoffstein.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://coreyhoffstein.com/2009/07/21/some-random-libraries/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
