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