<?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; numerical methods</title>
	<atom:link href="http://coreyhoffstein.com/tag/numerical-methods/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>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>
	</channel>
</rss>
