<?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; thrust</title>
	<atom:link href="http://coreyhoffstein.com/tag/thrust/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>Asian Call Option Pricing, Part Deux</title>
		<link>http://coreyhoffstein.com/2009/10/01/asian-call-option-pricing-part-deux/</link>
		<comments>http://coreyhoffstein.com/2009/10/01/asian-call-option-pricing-part-deux/#comments</comments>
		<pubDate>Thu, 01 Oct 2009 18:04:31 +0000</pubDate>
		<dc:creator>Corey</dc:creator>
				<category><![CDATA[gpgpu]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[gpu]]></category>
		<category><![CDATA[thrust]]></category>

		<guid isPermaLink="false">http://coreyhoffstein.com/?p=202</guid>
		<description><![CDATA[Well, it seems that while my code &#8216;worked,&#8217; it didn&#8217;t really &#8216;work.&#8217; Such are the subtleties of using libraries you don&#8217;t wholly understand. Check the comments on my Asian Call Option Pricing post. It is a quick fix to get rid of the threadIndex issue (just use a counting_iterator and set the thread index value [...]]]></description>
			<content:encoded><![CDATA[<p>Well, it seems that while my code &#8216;worked,&#8217; it didn&#8217;t really &#8216;work.&#8217;  Such are the subtleties of using libraries you don&#8217;t wholly understand.  Check the comments on my Asian Call Option Pricing post.  It is a quick fix to get rid of the threadIndex issue (just use a counting_iterator and set the thread index value to the iterator value modulus MT_RNG_COUNT &#8212; not perfect, but it works), but it isn&#8217;t quite as easy to refactor away the __shared__ state in the functor, which makes the Mersenne Twister hard to use.  But not all is lost, if you check the experimental branch of Thrust, you will find a linear congruential generator that you can use.  </p>
<p>You can check some example code on a monte-carlo implementation with Thrust <a href="http://code.google.com/p/thrust/source/browse/trunk/examples/monte_carlo.cu">here</a>.  Much better than my code!</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/10/01/asian-call-option-pricing-part-deux/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Asian Call Option Pricing with Trust</title>
		<link>http://coreyhoffstein.com/2009/09/25/asian-call-option-pricing-with-trust/</link>
		<comments>http://coreyhoffstein.com/2009/09/25/asian-call-option-pricing-with-trust/#comments</comments>
		<pubDate>Fri, 25 Sep 2009 15:59:25 +0000</pubDate>
		<dc:creator>Corey</dc:creator>
				<category><![CDATA[gpgpu]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[CUDA]]></category>
		<category><![CDATA[gpu]]></category>
		<category><![CDATA[options]]></category>
		<category><![CDATA[pricing]]></category>
		<category><![CDATA[thrust]]></category>

		<guid isPermaLink="false">http://coreyhoffstein.com/?p=192</guid>
		<description><![CDATA[Asian Call Options are valued based on their average price over a time-period. This program estimates an Asian Call Option that expires 250 periods from now, based on a stock currently at $50, with a $50 strike, and a volatility of 0.4. It runs 500,000 simulations, and averages their average price to get the price [...]]]></description>
			<content:encoded><![CDATA[<p>Asian Call Options are valued based on their average price over a time-period.  This program estimates an Asian Call Option that expires 250 periods from now, based on a stock currently at $50, with a $50 strike, and a volatility of 0.4.  It runs 500,000 simulations, and averages their average price to get the price of the option.</p>
<p>Not too realistic, but fun to play with.</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/transform_reduce.h&gt;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Mersenne Twister code reproduced &amp; modified from: </span>
<span style="color: #666666; font-style: italic;">//										http://svn.jcornwall.me.uk/applications/MersenneTwisterGPU/</span>
<span style="color: #666666; font-style: italic;">//										http://www.jcornwall.me.uk/2009/04/mersenne-twisters-in-cuda/</span>
<span style="color: #339933;">#include &lt;cassert&gt;</span>
<span style="color: #339933;">#include &lt;cstdio&gt;</span>
<span style="color: #339933;">#include &lt;vector&gt;</span>
&nbsp;
<span style="color: #339933;">#define CUDA_SAFE_CALL(call)                                        \
  do {                                                              \
    cudaError err = call;                                           \
    if( cudaSuccess != err) {                                       \
      fprintf(stderr, &quot;Cuda error in file '%s' in line %i : %s.\n&quot;, \
        __FILE__, __LINE__, cudaGetErrorString( err) );             \
      exit(EXIT_FAILURE);                                           \
    }                                                               \
  } while(0)</span>
&nbsp;
<span style="color: #339933;">#define MT_MM     9</span>
<span style="color: #339933;">#define MT_NN     19</span>
<span style="color: #339933;">#define MT_WMASK  0xFFFFFFFFU</span>
<span style="color: #339933;">#define MT_UMASK  0xFFFFFFFEU</span>
<span style="color: #339933;">#define MT_LMASK  0x1U</span>
<span style="color: #339933;">#define MT_RNG_COUNT 32768</span>
<span style="color: #339933;">#define MT_SHIFT0 12</span>
<span style="color: #339933;">#define MT_SHIFTB 7</span>
<span style="color: #339933;">#define MT_SHIFTC 15</span>
<span style="color: #339933;">#define MT_SHIFT1 18</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Record format for MersenneTwister.dat, created by spawnTwisters.c</span>
<span style="color: #993333;">struct</span> mt_struct_stripped <span style="color: #009900;">&#123;</span>
	<span style="color: #993333;">unsigned</span> <span style="color: #993333;">int</span> matrix_a<span style="color: #339933;">;</span>
	<span style="color: #993333;">unsigned</span> <span style="color: #993333;">int</span> mask_b<span style="color: #339933;">;</span>
	<span style="color: #993333;">unsigned</span> <span style="color: #993333;">int</span> mask_c<span style="color: #339933;">;</span>
	<span style="color: #993333;">unsigned</span> <span style="color: #993333;">int</span> seed<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Per-thread state object for a single twister.</span>
<span style="color: #993333;">struct</span> MersenneTwisterState <span style="color: #009900;">&#123;</span>
	<span style="color: #993333;">unsigned</span> <span style="color: #993333;">int</span> mt<span style="color: #009900;">&#91;</span>MT_NN<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
	<span style="color: #993333;">int</span> iState<span style="color: #339933;">;</span>
	<span style="color: #993333;">unsigned</span> <span style="color: #993333;">int</span> mti1<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Preloaded, offline-generated seed data structure.</span>
__device__ <span style="color: #993333;">static</span> mt_struct_stripped MT<span style="color: #009900;">&#91;</span>MT_RNG_COUNT<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
__device__ <span style="color: #993333;">unsigned</span> <span style="color: #993333;">int</span> MersenneTwisterGenerate<span style="color: #009900;">&#40;</span>MersenneTwisterState <span style="color: #339933;">&amp;</span>state<span style="color: #339933;">,</span> <span style="color: #993333;">unsigned</span> <span style="color: #993333;">int</span> threadID<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #993333;">int</span> iState1 <span style="color: #339933;">=</span> state.<span style="color: #202020;">iState</span> <span style="color: #339933;">+</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
	<span style="color: #993333;">int</span> iStateM <span style="color: #339933;">=</span> state.<span style="color: #202020;">iState</span> <span style="color: #339933;">+</span> MT_MM<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>iState1 <span style="color: #339933;">&gt;=</span> MT_NN<span style="color: #009900;">&#41;</span> iState1 <span style="color: #339933;">-=</span> MT_NN<span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>iStateM <span style="color: #339933;">&gt;=</span> MT_NN<span style="color: #009900;">&#41;</span> iStateM <span style="color: #339933;">-=</span> MT_NN<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #993333;">unsigned</span> <span style="color: #993333;">int</span> mti <span style="color: #339933;">=</span> state.<span style="color: #202020;">mti1</span><span style="color: #339933;">;</span>
	state.<span style="color: #202020;">mti1</span> <span style="color: #339933;">=</span> state.<span style="color: #202020;">mt</span><span style="color: #009900;">&#91;</span>iState1<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
	<span style="color: #993333;">unsigned</span> <span style="color: #993333;">int</span> mtiM <span style="color: #339933;">=</span> state.<span style="color: #202020;">mt</span><span style="color: #009900;">&#91;</span>iStateM<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #993333;">unsigned</span> <span style="color: #993333;">int</span> x <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>mti <span style="color: #339933;">&amp;</span> MT_UMASK<span style="color: #009900;">&#41;</span> <span style="color: #339933;">|</span> <span style="color: #009900;">&#40;</span>state.<span style="color: #202020;">mti1</span> <span style="color: #339933;">&amp;</span> MT_LMASK<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	x <span style="color: #339933;">=</span> mtiM <span style="color: #339933;">^</span> <span style="color: #009900;">&#40;</span>x <span style="color: #339933;">&gt;&gt;</span> <span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">^</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>x <span style="color: #339933;">&amp;</span> <span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">?</span> MT<span style="color: #009900;">&#91;</span>threadID<span style="color: #009900;">&#93;</span>.<span style="color: #202020;">matrix_a</span> <span style="color: #339933;">:</span> <span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	state.<span style="color: #202020;">mt</span><span style="color: #009900;">&#91;</span>state.<span style="color: #202020;">iState</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> x<span style="color: #339933;">;</span>
	state.<span style="color: #202020;">iState</span> <span style="color: #339933;">=</span> iState1<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// Tempering transformation.</span>
	x <span style="color: #339933;">^=</span> <span style="color: #009900;">&#40;</span>x <span style="color: #339933;">&gt;&gt;</span> MT_SHIFT0<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	x <span style="color: #339933;">^=</span> <span style="color: #009900;">&#40;</span>x <span style="color: #339933;">&lt;&lt;</span> MT_SHIFTB<span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;</span> MT<span style="color: #009900;">&#91;</span>threadID<span style="color: #009900;">&#93;</span>.<span style="color: #202020;">mask_b</span><span style="color: #339933;">;</span>
	x <span style="color: #339933;">^=</span> <span style="color: #009900;">&#40;</span>x <span style="color: #339933;">&lt;&lt;</span> MT_SHIFTC<span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;</span> MT<span style="color: #009900;">&#91;</span>threadID<span style="color: #009900;">&#93;</span>.<span style="color: #202020;">mask_c</span><span style="color: #339933;">;</span>
	x <span style="color: #339933;">^=</span> <span style="color: #009900;">&#40;</span>x <span style="color: #339933;">&gt;&gt;</span> MT_SHIFT1<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #b1b100;">return</span> x<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #339933;">#define TWISTER_WARM_UP 0</span>
&nbsp;
__device__ <span style="color: #993333;">void</span> MersenneTwisterInitialize<span style="color: #009900;">&#40;</span>MersenneTwisterState <span style="color: #339933;">&amp;</span>state<span style="color: #339933;">,</span> <span style="color: #993333;">unsigned</span> <span style="color: #993333;">int</span> threadID<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	state.<span style="color: #202020;">mt</span><span style="color: #009900;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> MT<span style="color: #009900;">&#91;</span>threadID<span style="color: #009900;">&#93;</span>.<span style="color: #202020;">seed</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> i <span style="color: #339933;">=</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> MT_NN<span style="color: #339933;">;</span> <span style="color: #339933;">++</span> i<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		state.<span style="color: #202020;">mt</span><span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>1812433253U <span style="color: #339933;">*</span> <span style="color: #009900;">&#40;</span>state.<span style="color: #202020;">mt</span><span style="color: #009900;">&#91;</span>i <span style="color: #339933;">-</span> <span style="color: #0000dd;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">^</span> <span style="color: #009900;">&#40;</span>state.<span style="color: #202020;">mt</span><span style="color: #009900;">&#91;</span>i <span style="color: #339933;">-</span> <span style="color: #0000dd;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">&gt;&gt;</span> <span style="color: #0000dd;">30</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> i<span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;</span> MT_WMASK<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	state.<span style="color: #202020;">iState</span> <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
	state.<span style="color: #202020;">mti1</span> <span style="color: #339933;">=</span> state.<span style="color: #202020;">mt</span><span style="color: #009900;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// warm up the twister</span>
        <span style="color: #339933;">#if TWISTER_WARM_UP</span>
	<span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">int</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> <span style="color: #0000dd;">10000</span><span style="color: #339933;">;</span> <span style="color: #339933;">++</span> i<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		MersenneTwisterGenerate<span style="color: #009900;">&#40;</span>state<span style="color: #339933;">,</span> threadID<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
        <span style="color: #339933;">#endif</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// </span>
__global__ <span style="color: #993333;">void</span> TestMersenneTwister<span style="color: #009900;">&#40;</span><span style="color: #993333;">float</span> <span style="color: #339933;">*</span>outArr<span style="color: #339933;">,</span> <span style="color: #993333;">int</span> nNumbers<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #993333;">unsigned</span> <span style="color: #993333;">int</span> tid <span style="color: #339933;">=</span> __mul24<span style="color: #009900;">&#40;</span>blockIdx.<span style="color: #202020;">x</span><span style="color: #339933;">,</span> blockDim.<span style="color: #202020;">x</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> threadIdx.<span style="color: #202020;">x</span><span style="color: #339933;">;</span>
&nbsp;
	MersenneTwisterState mtState<span style="color: #339933;">;</span>
	MersenneTwisterInitialize<span style="color: #009900;">&#40;</span>mtState<span style="color: #339933;">,</span> tid<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> i <span style="color: #339933;">=</span> tid<span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> nNumbers<span style="color: #339933;">;</span> i <span style="color: #339933;">+=</span> __mul24<span style="color: #009900;">&#40;</span>blockDim.<span style="color: #202020;">x</span><span style="color: #339933;">,</span> gridDim.<span style="color: #202020;">x</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">// Make a floating-point number between 0...1 from integer 0...UINT_MAX.</span>
		outArr<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #993333;">float</span><span style="color: #009900;">&#40;</span>MersenneTwisterGenerate<span style="color: #009900;">&#40;</span>mtState<span style="color: #339933;">,</span> tid<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">/</span> <span style="color:#800080;">4294967295.0f</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Initialize seeds for the Mersenne Twister</span>
<span style="color: #993333;">void</span> InitializeSeeds<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	mt_struct_stripped <span style="color: #339933;">*</span>mtStripped <span style="color: #339933;">=</span> new mt_struct_stripped<span style="color: #009900;">&#91;</span>MT_RNG_COUNT<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
	FILE <span style="color: #339933;">*</span>datFile <span style="color: #339933;">=</span> fopen<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;MersenneTwister.dat&quot;</span><span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;rb&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	assert<span style="color: #009900;">&#40;</span>datFile<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	assert<span style="color: #009900;">&#40;</span>fread<span style="color: #009900;">&#40;</span>mtStripped<span style="color: #339933;">,</span> <span style="color: #993333;">sizeof</span><span style="color: #009900;">&#40;</span>mt_struct_stripped<span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> MT_RNG_COUNT<span style="color: #339933;">,</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">,</span> datFile<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	fclose<span style="color: #009900;">&#40;</span>datFile<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// Seed the structure with low-quality random numbers. Twisters will need &quot;warming up&quot;</span>
	<span style="color: #666666; font-style: italic;">// before the RNG quality improves.</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>
	<span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">int</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> MT_RNG_COUNT<span style="color: #339933;">;</span> <span style="color: #339933;">++</span> i<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		mtStripped<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #202020;">seed</span> <span style="color: #339933;">=</span> rand<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// Upload the initial configurations to the GPU.</span>
	CUDA_SAFE_CALL<span style="color: #009900;">&#40;</span>cudaMemcpyToSymbol<span style="color: #009900;">&#40;</span>MT<span style="color: #339933;">,</span> mtStripped<span style="color: #339933;">,</span> <span style="color: #993333;">sizeof</span><span style="color: #009900;">&#40;</span>mt_struct_stripped<span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> MT_RNG_COUNT<span style="color: #339933;">,</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">,</span> cudaMemcpyHostToDevice<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	delete<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> mtStripped<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Generates random numbers on the GPU</span>
<span style="color: #993333;">void</span> GenerateRandomNumbers<span style="color: #009900;">&#40;</span><span style="color: #993333;">float</span> <span style="color: #339933;">*</span>randomNumbers<span style="color: #339933;">,</span> <span style="color: #993333;">int</span> nRandomNumbers<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #666666; font-style: italic;">// Read offline-generated initial configuration file.</span>
	<span style="color: #993333;">float</span> <span style="color: #339933;">*</span>randomNumbersDev<span style="color: #339933;">;</span>
	CUDA_SAFE_CALL<span style="color: #009900;">&#40;</span>cudaMalloc<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">void</span> <span style="color: #339933;">**</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">&amp;</span>randomNumbersDev<span style="color: #339933;">,</span> <span style="color: #993333;">sizeof</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">float</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> nRandomNumbers<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	dim3 threads<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">512</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	dim3 grid<span style="color: #009900;">&#40;</span>MT_RNG_COUNT <span style="color: #339933;">/</span> <span style="color: #0000dd;">512</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	TestMersenneTwister<span style="color: #339933;">&lt;&lt;&lt;</span>grid<span style="color: #339933;">,</span> threads<span style="color: #339933;">&gt;&gt;&gt;</span><span style="color: #009900;">&#40;</span>randomNumbersDev<span style="color: #339933;">,</span> nRandomNumbers<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	CUDA_SAFE_CALL<span style="color: #009900;">&#40;</span>cudaMemcpy<span style="color: #009900;">&#40;</span>randomNumbers<span style="color: #339933;">,</span> randomNumbersDev<span style="color: #339933;">,</span> <span style="color: #993333;">sizeof</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">float</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> nRandomNumbers<span style="color: #339933;">,</span> cudaMemcpyDeviceToHost<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	CUDA_SAFE_CALL<span style="color: #009900;">&#40;</span>cudaFree<span style="color: #009900;">&#40;</span>randomNumbersDev<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #666666; font-style: italic;">// End Mersenne Twister Code</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//Box Muller transform</span>
<span style="color: #339933;">#define PI 3.14159265358979f</span>
__device__ 
<span style="color: #993333;">void</span> BoxMuller<span style="color: #009900;">&#40;</span><span style="color: #993333;">float</span> <span style="color: #339933;">&amp;</span>u1<span style="color: #339933;">,</span> <span style="color: #993333;">float</span> <span style="color: #339933;">&amp;</span>u2<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #993333;">float</span>   r <span style="color: #339933;">=</span> sqrtf<span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span><span style="color:#800080;">2.0f</span> <span style="color: #339933;">*</span> logf<span style="color: #009900;">&#40;</span>u1<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #993333;">float</span> phi <span style="color: #339933;">=</span> <span style="color: #0000dd;">2</span> <span style="color: #339933;">*</span> PI <span style="color: #339933;">*</span> u2<span style="color: #339933;">;</span>
&nbsp;
    u1 <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>r <span style="color: #339933;">*</span> __cosf<span style="color: #009900;">&#40;</span>phi<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	u2 <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>r <span style="color: #339933;">*</span> __sinf<span style="color: #009900;">&#40;</span>phi<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> AsianOptionPricer <span style="color: #009900;">&#123;</span>
	<span style="color: #993333;">int</span> term<span style="color: #339933;">;</span>
	<span style="color: #993333;">float</span> price0<span style="color: #339933;">;</span>
	<span style="color: #993333;">float</span> strike<span style="color: #339933;">;</span>
	<span style="color: #993333;">float</span> volatility<span style="color: #339933;">;</span>
	<span style="color: #993333;">int</span> numPeriods<span style="color: #339933;">;</span>
	<span style="color: #993333;">float</span> deltaT<span style="color: #339933;">;</span>
	<span style="color: #993333;">float</span> invariant<span style="color: #339933;">;</span>
&nbsp;
	AsianOptionPricer<span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> t<span style="color: #339933;">,</span> <span style="color: #993333;">float</span> s<span style="color: #339933;">,</span> <span style="color: #993333;">float</span> v<span style="color: #339933;">,</span> <span style="color: #993333;">float</span> rfr<span style="color: #339933;">,</span> <span style="color: #993333;">int</span> p<span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> 
		term<span style="color: #009900;">&#40;</span>t<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
		strike<span style="color: #009900;">&#40;</span>s<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> 
		volatility<span style="color: #009900;">&#40;</span>v<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
		numPeriods<span style="color: #009900;">&#40;</span>p<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			deltaT <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">float</span><span style="color: #009900;">&#41;</span>term <span style="color: #339933;">/</span> numPeriods<span style="color: #339933;">;</span>
			invariant <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>rfr <span style="color: #339933;">-</span> volatility <span style="color: #339933;">*</span> volatility <span style="color: #339933;">/</span> <span style="color: #0000dd;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> deltaT<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	__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><span style="color: #993333;">float</span> price0<span style="color: #009900;">&#41;</span> <span style="color: #993333;">const</span> <span style="color: #009900;">&#123;</span>
		__shared__ MersenneTwisterState mtState<span style="color: #339933;">;</span>
		<span style="color: #993333;">unsigned</span> <span style="color: #993333;">int</span> thrOffset <span style="color: #339933;">=</span> blockIdx.<span style="color: #202020;">x</span> <span style="color: #339933;">*</span> blockDim.<span style="color: #202020;">x</span> <span style="color: #339933;">+</span> threadIdx.<span style="color: #202020;">x</span><span style="color: #339933;">;</span>
&nbsp;
		MersenneTwisterInitialize<span style="color: #009900;">&#40;</span>mtState<span style="color: #339933;">,</span> thrOffset<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #993333;">float</span> sum <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
		<span style="color: #993333;">float</span> price <span style="color: #339933;">=</span> price0<span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// since we move the price two steps a loop, we only go from 0 to numPeriods/2</span>
		<span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">int</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> <span style="color: #009900;">&#40;</span>numPeriods<span style="color: #009900;">&#41;</span> <span style="color: #339933;">/</span> <span style="color: #0000dd;">2</span><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: #993333;">float</span> u1 <span style="color: #339933;">=</span> <span style="color: #993333;">float</span><span style="color: #009900;">&#40;</span>MersenneTwisterGenerate<span style="color: #009900;">&#40;</span>mtState<span style="color: #339933;">,</span> thrOffset<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">/</span> <span style="color:#800080;">4294967295.0f</span><span style="color: #339933;">;</span>
			<span style="color: #993333;">float</span> u2 <span style="color: #339933;">=</span> <span style="color: #993333;">float</span><span style="color: #009900;">&#40;</span>MersenneTwisterGenerate<span style="color: #009900;">&#40;</span>mtState<span style="color: #339933;">,</span> thrOffset<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">/</span> <span style="color:#800080;">4294967295.0f</span><span style="color: #339933;">;</span>
&nbsp;
			BoxMuller<span style="color: #009900;">&#40;</span>u1<span style="color: #339933;">,</span> u2<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//transform uniform into two independent standard normals</span>
&nbsp;
			<span style="color: #666666; font-style: italic;">// move the price a step</span>
			price <span style="color: #339933;">=</span> price <span style="color: #339933;">*</span> __expf<span style="color: #009900;">&#40;</span>invariant <span style="color: #339933;">+</span> volatility <span style="color: #339933;">*</span> u1 <span style="color: #339933;">*</span> sqrt<span style="color: #009900;">&#40;</span>deltaT<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			sum <span style="color: #339933;">=</span> sum <span style="color: #339933;">+</span> price<span style="color: #339933;">;</span>
&nbsp;
			<span style="color: #666666; font-style: italic;">// move the price a second step</span>
			price <span style="color: #339933;">=</span> price <span style="color: #339933;">*</span> __expf<span style="color: #009900;">&#40;</span>invariant <span style="color: #339933;">+</span> volatility <span style="color: #339933;">*</span> u2 <span style="color: #339933;">*</span> sqrt<span style="color: #009900;">&#40;</span>deltaT<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			sum <span style="color: #339933;">=</span> sum <span style="color: #339933;">+</span> price<span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #993333;">float</span> save <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>sum <span style="color: #339933;">/</span> numPeriods<span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> strike<span style="color: #339933;">;</span>
		<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>save <span style="color: #339933;">&lt;</span> <span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #b1b100;">return</span> <span style="color:#800080;">0.0</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #b1b100;">return</span> save<span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</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>
	<span style="color: #993333;">float</span> S0 <span style="color: #339933;">=</span> <span style="color: #0000dd;">50</span><span style="color: #339933;">;</span>
	<span style="color: #993333;">int</span> M <span style="color: #339933;">=</span> <span style="color: #0000dd;">500000</span><span style="color: #339933;">;</span>
&nbsp;
	InitializeSeeds<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	thrust<span style="color: #339933;">::</span><span style="color: #202020;">device_vector</span><span style="color: #339933;">&lt;</span>float<span style="color: #339933;">&gt;</span> payoff<span style="color: #009900;">&#40;</span>M<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
	thrust<span style="color: #339933;">::</span><span style="color: #202020;">fill</span><span style="color: #009900;">&#40;</span>payoff.<span style="color: #202020;">begin</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> payoff.<span style="color: #202020;">end</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> S0<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #993333;">float</span> r <span style="color: #339933;">=</span> <span style="color:#800080;">0.1</span><span style="color: #339933;">;</span>
	<span style="color: #993333;">float</span> T <span style="color: #339933;">=</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
	<span style="color: #993333;">float</span> sigma <span style="color: #339933;">=</span> <span style="color:#800080;">0.4</span><span style="color: #339933;">;</span>
&nbsp;
	AsianOptionPricer pricer<span style="color: #009900;">&#40;</span>T<span style="color: #339933;">,</span> <span style="color: #0000dd;">50</span><span style="color: #339933;">,</span> sigma<span style="color: #339933;">,</span> r<span style="color: #339933;">,</span> <span style="color: #0000dd;">250</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #993333;">float</span> sum <span style="color: #339933;">=</span> thrust<span style="color: #339933;">::</span><span style="color: #202020;">transform_reduce</span><span style="color: #009900;">&#40;</span>payoff.<span style="color: #202020;">begin</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> payoff.<span style="color: #202020;">end</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> pricer<span style="color: #339933;">,</span> <span style="color:#800080;">0.0f</span><span style="color: #339933;">,</span> thrust<span style="color: #339933;">::</span><span style="color: #202020;">plus</span><span style="color: #339933;">&lt;</span>float<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;The price is: %f<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#40;</span>sum <span style="color: #339933;">/</span> M<span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> exp<span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span>r <span style="color: #339933;">*</span> T<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</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>Results:<br />
<code><br />
macbook:asian option thrust choffstein$ ./price_asian_option_c++<br />
Simulation with 500000 trials and 250 increments per trial took 14689312 milliseconds.<br />
The price is: 5.59305<br />
macbook:asian option thrust choffstein$ ./price_asian_option<br />
Simulation with 500000 trials and 250 increments per trial took 991164 milliseconds.<br />
The price is: 5.59295</code></p>
<p>Impressive speedup, no?  About 7% of the time of my comparable C++ program (note: not exact replica).  One should also note that the speedup is drastically reduced as the number of simulations decreases &#8212; most likely due to data copying speeds from to and from the GPU.</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/25/asian-call-option-pricing-with-trust/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Ruby and CUDA and Thrust, Oh My!</title>
		<link>http://coreyhoffstein.com/2009/06/15/ruby-and-cuda-and-thrust-oh-my/</link>
		<comments>http://coreyhoffstein.com/2009/06/15/ruby-and-cuda-and-thrust-oh-my/#comments</comments>
		<pubDate>Mon, 15 Jun 2009 22:57:11 +0000</pubDate>
		<dc:creator>Corey</dc:creator>
				<category><![CDATA[gpgpu]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[CUDA]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[RubyInline]]></category>
		<category><![CDATA[thrust]]></category>

		<guid isPermaLink="false">http://coreyhoffstein.com/?p=111</guid>
		<description><![CDATA[Thought I would have a bit of fun to see if I could get CUDA and Thrust working with Ruby. Since no CUDA bindings exist, and I didn&#8217;t want to give up the power of Thrust, I decided to emply RubyInline to create wrappers. Got it a nice little demo working in under 30 minutes. [...]]]></description>
			<content:encoded><![CDATA[<p>Thought I would have a bit of fun to see if I could get CUDA and Thrust working with Ruby.  Since no CUDA bindings exist, and I didn&#8217;t want to give up the power of Thrust, I decided to emply RubyInline to create wrappers.  Got it a nice little demo working in under 30 minutes.</p>
<p><strong>vectors.cu</strong></p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><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;iostream&gt;</span>
&nbsp;
<span style="color: #339933;">#include &quot;vector_test.h&quot;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">extern</span> <span style="color: #ff0000;">&quot;C&quot;</span> <span style="color: #993333;">int</span> vector_test<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   thrust<span style="color: #339933;">::</span><span style="color: #202020;">host_vector</span> H<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">4</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   H<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000dd;">14</span><span style="color: #339933;">;</span>
   H<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000dd;">20</span><span style="color: #339933;">;</span>
   H<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">2</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000dd;">38</span><span style="color: #339933;">;</span>
   H<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">3</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000dd;">46</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;H has size &quot;</span><span style="color: #339933;">&lt;&lt;</span> H.<span style="color: #202020;">size</span><span style="color: #009900;">&#40;</span><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;">for</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">int</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> H.<span style="color: #202020;">size</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
      std<span style="color: #339933;">::</span><span style="color: #000066;">cout</span> <span style="color: #339933;">&lt;&lt;</span> <span style="color: #ff0000;">&quot;H[&quot;</span> <span style="color: #339933;">&lt;&lt;</span> i <span style="color: #339933;">&lt;&lt;</span> <span style="color: #ff0000;">&quot;] = &quot;</span> <span style="color: #339933;">&lt;&lt;</span> H<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</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;
   H.<span style="color: #202020;">resize</span><span style="color: #009900;">&#40;</span><span style="color: #0000dd;">2</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;H now has size &quot;</span> <span style="color: #339933;">&lt;&lt;</span> H.<span style="color: #202020;">size</span><span style="color: #009900;">&#40;</span><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;
   thrust<span style="color: #339933;">::</span><span style="color: #202020;">device_vector</span> D <span style="color: #339933;">=</span> H<span style="color: #339933;">;</span>
&nbsp;
   D<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000dd;">99</span><span style="color: #339933;">;</span>
   D<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000dd;">88</span><span style="color: #339933;">;</span>
&nbsp;
   <span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">int</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> D.<span style="color: #202020;">size</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
      std<span style="color: #339933;">::</span><span style="color: #000066;">cout</span> <span style="color: #339933;">&lt;&lt;</span> <span style="color: #ff0000;">&quot;D[&quot;</span> <span style="color: #339933;">&lt;&lt;</span> i <span style="color: #339933;">&lt;&lt;</span> <span style="color: #ff0000;">&quot;] = &quot;</span> <span style="color: #339933;">&lt;&lt;</span> D<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</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><strong>vector_test.h</strong></p>
<pre lang="c++">extern "C" int vector_test();</pre>
<p>Compile a shared library: <em>nvcc &#8211;shared -o libvectors.so vectors.cu -I/usr/local/cuda/include -L/usr/local/cuda/lib -lcudart</em></p>
<p>cuda_inline.rb</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'rubygems'</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'inline'</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> MyWrapper
   inline<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:C</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>builder<span style="color:#006600; font-weight:bold;">|</span>
      builder.<span style="color:#9966CC; font-weight:bold;">include</span> <span style="color:#996600;">'&quot;vector_test.h&quot;'</span>
      builder.<span style="color:#9900CC;">add_compile_flags</span> <span style="color:#996600;">'-x c++'</span>, <span style="color:#996600;">'-lstdc++'</span>, <span style="color:#996600;">'-L/usr/local/cuda/lib'</span>, <span style="color:#996600;">'-L.'</span>, <span style="color:#996600;">'-lvectors'</span>, <span style="color:#996600;">'-I.'</span>
      builder.<span style="color:#9900CC;">c</span> <span style="color:#996600;">'
         void wrapper() {
            vector_test();
         }'</span>
   <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
t = MyWrapper.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
t.<span style="color:#9900CC;">wrapper</span></pre></div></div>

<p>Note here that I have to specify for RubyInline to look in the current directory for headers and libraries!  </p>
<p>So, as long as libvectors.so and vector_test.h is in the directory you are running cuda_inline.rb from, everything is gravy!  Maybe not the most efficient tool-chain, but it works!</p>
<p><em>$ ruby cuda_inline.rb<br />
ld warning: in ./libvectors.so, file is not of required architecture<br />
H has size 4<br />
H[0] = 14<br />
H[1] = 20<br />
H[2] = 38<br />
H[3] = 46<br />
H now has size 2<br />
D[0] = 99<br />
D[1] = 88<br />
</em></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/06/15/ruby-and-cuda-and-thrust-oh-my/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Thrust</title>
		<link>http://coreyhoffstein.com/2009/06/15/thrust/</link>
		<comments>http://coreyhoffstein.com/2009/06/15/thrust/#comments</comments>
		<pubDate>Mon, 15 Jun 2009 11:22:09 +0000</pubDate>
		<dc:creator>Corey</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[gpgpu]]></category>
		<category><![CDATA[thrust]]></category>

		<guid isPermaLink="false">http://coreyhoffstein.com/?p=109</guid>
		<description><![CDATA[Here is a cool tool I found while browsing GPGPU. I finally got a computer that had a video card that could be used with CUDA, so I have been dying to try it out. Thrust makes this transition look painless&#8230; Thrust is a CUDA library of parallel algorithms with an interface resembling the C++ [...]]]></description>
			<content:encoded><![CDATA[<p>Here is a cool tool I found while browsing GPGPU.  I finally got a computer that had a video card that could be used with CUDA, so I have been dying to try it out.  Thrust makes this transition look painless&#8230;</p>
<blockquote><p>Thrust is a CUDA library of parallel algorithms with an interface resembling the C++ Standard Template Library (STL). Thrust provides a flexible high-level interface for GPU programming that greatly enhances developer productivity. Develop high-performance applications rapidly with Thrust!</p></blockquote>
<p>Check it out <a href="http://code.google.com/p/thrust/">here</a>.</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/06/15/thrust/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
