<?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>rajorshi.net &#187; Programming</title>
	<atom:link href="http://rajorshi.net/blog/category/technology/programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://rajorshi.net/blog</link>
	<description>My musings on life, movies and technology</description>
	<lastBuildDate>Sat, 05 Jun 2010 19:15:12 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Programming for multicore: An introduction to OpenMP using GCC-4.4</title>
		<link>http://rajorshi.net/blog/2009/05/programming-for-multicore-introduction-openmp-gcc/</link>
		<comments>http://rajorshi.net/blog/2009/05/programming-for-multicore-introduction-openmp-gcc/#comments</comments>
		<pubDate>Sun, 24 May 2009 13:13:08 +0000</pubDate>
		<dc:creator>rajorshi</dc:creator>
				<category><![CDATA[Hardware]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[AMD]]></category>
		<category><![CDATA[boost]]></category>
		<category><![CDATA[gcc]]></category>
		<category><![CDATA[Intel]]></category>
		<category><![CDATA[Istanbul]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Multi-Core]]></category>
		<category><![CDATA[Nehalem]]></category>
		<category><![CDATA[OpenMP]]></category>
		<category><![CDATA[parallel]]></category>
		<category><![CDATA[Ubuntu]]></category>

		<guid isPermaLink="false">http://rajorshi.net/blog/?p=52</guid>
		<description><![CDATA[About a couple of months back, I happened to attend a short seminar on multi-core programming by Intel here at Hyderabad. What I liked immensely about it was that it was not yet another blatant advertising campaign on some hardware or software product by some industry giant. It was about the paradigm shift that the [...]]]></description>
			<content:encoded><![CDATA[<p>About a couple of months back, I happened to attend a short seminar on <a href="http://en.wikipedia.org/wiki/Multi-core_(computing)">multi-core</a> programming by Intel here at Hyderabad. What I liked immensely about it was that it was not yet another blatant advertising campaign on some hardware or software product by some industry giant.</p>
<p>It was about the paradigm shift that the chip industry is undergoing &#8211; the trend towards more cores, rather than higher gigahertz horsepower. However, if you are an average Joe developer like me, you probably program your applications without leveraging the power of two or more cores simultaneously. By default, we don&#8217;t &#8216;think parallel&#8217; for various reasons. For one, threading is not an easy concept. The seminar looked at some of Intel&#8217;s software offerings that help developers (especially Visual Studio developers) to create, debug and optimize threaded/multicore applications. (However, this post will not focus on those tools &#8211; you may want to visit <a href="http://www.intel.com/go/parallel">www.intel.com/go/parallel</a> for more information).</p>
<p>On a related thread (pun intended!), <a href="http://gcc.gnu.org/gcc-4.4/">gcc-4.4.0</a> was released recently. This added support for version 3.0 of the <a href="http://openmp.org/">OpenMP</a> specification. OpenMP is something I had heard of before, but never actually tried. It is an API for C, C++ and Fortran programmers that enables you to &#8216;parallel program&#8217; easily. Jargonspeak calls it &#8216;platform independent shared memory multiprocessing&#8217;. In effect, it&#8217;s threads without the associated headache of thread management. By the way, gcc has supported OpenMP way back from version 4.2. So, you don&#8217;t need the latest bleeding edge version for this. However, should you want to, on Windows you can always download the excellent <a href="http://www.tdragon.net/recentgcc/">TDM MingW</a> builds for gcc-4.4.0 (<a href="http://downloads.sourceforge.net/tdm-gcc/tdm-mingw-1.905.0-4.4.0-2.exe">latest direct link</a>). If you&#8217;re a Linux geek, you probably know how to get gcc-4.4 for your distro anyway. Also, Microsoft Visual C++ Express does not include/support OpenMP &#8211; hence my experiments are limited to gcc on both Win and Lin.</p>
<p>All right then, let&#8217;s see how OpenMP aids a classic case of parallelization: matrix multiplication. Agreed &#8211; this is a rather simple programming problem, and real world problems are usually harder to parallelize than this. However, this should serve as a good starting point to explore further.</p>
<p>So, here&#8217;s the basic matrix multiplication loop that we want to parallelize, assuming arr1 and arr2 are inputs, and arr3 is the output array:</p>
<pre name="code" class="cpp">

for(i=0; i&lt;n; ++i) {
  for(j=0; j&lt;n; ++j) {
    temp = 0;
    for(k=0; k&lt;n; ++k) {
      temp += arr1[i][k] * arr2[k][j];
    }
    arr3[i][j] = temp;
  }
}
</pre>
<p>OpenMP is mostly a set of compiler directives (pragmas) and library routines. In this case, it&#39;s enough for us to add on single statement before our loop.</p>
<pre name="code" class="cpp">

#pragma omp parallel for private(i, j, k, temp)
for(i=0; i&lt;n; ++i) {
  for(j=0; j&lt;n; ++j) {
    temp = 0;
    for(k=0; k&lt;n; ++k) {
      temp += arr1[i][k] * arr2[k][j];
    }
    arr3[i][j] = temp;
  }
}
</pre>
<p>That&#8217;s it! This pragma tells the OpenMP subsystem to do it&#8217;s little magic behind the scenes and parallelize the &#8216;for loop&#8217; following it.</p>
<p>Here&#8217;s the <a href="http://rajorshi.net/blog/wp-content/uploads/2009/05/matmul.c">complete program</a>, which contains additional code to initialize the arrays arr1 and arr2 pseudo-randomly, and to calculate the timings taken by the normal and the parallelized versions. You can compile the program with gcc-4.4 by the simple command:</p>
<pre>
gcc -fopenmp matmul.c
</pre>
<p>And on Windows, you might need to edit the PATH variable to include the GNU libgomp runtime (libgomp-1.dll). (<a href="http://gcc.gnu.org/onlinedocs/libgomp/">Libgomp</a> is GNU&#8217;s implementation of OpenMP). Here&#8217;s how I did it, for example:</p>
<pre>
set PATH=D:\MinGW\lib\gcc\mingw32\bin;%PATH%
</pre>
<p>So, the end result? Here are 4 sets of execution outputs. Two from Windows (TDM gcc-4.4.0):</p>
<pre>
Enter dimension ('N' for 'NxN' matrix) (100-2000): 1000
Populating array with random values...
Completed array init.
Crunching without OMP... took 23.032000 seconds.
Crunching with OMP... took 13.000000 seconds.

Enter dimension ('N' for 'NxN' matrix) (100-2000): 2000
Populating array with random values...
Completed array init.
Crunching without OMP... took 216.140000 seconds.
Crunching with OMP... took 118.641000 seconds.
</pre>
<p>And two from Linux (Ubuntu 9.04, gcc-4.3.3):</p>
<pre>
Enter dimension ('N' for 'NxN' matrix) (100-2000): 1000
Populating array with random values...
Completed array init.
Crunching without OMP... took 21.623144 seconds.
Crunching with OMP... took 13.686926 seconds.

Enter dimension ('N' for 'NxN' matrix) (100-2000): 2000
Populating array with random values...
Completed array init.
Crunching without OMP... took 189.184673 seconds.
Crunching with OMP... took 104.220751 seconds.
</pre>
<p>That&#8217;s almost doubling the speed, while adding one statement to your program! Actually two statements, if you include the include directive for &lt;omp.h&gt;. I&#8217;m sure you&#8217;d agree that for this case, OpenMP provides a really easy way of utilizing the idle core of most desktop machines out there. The good part is, even on a single core machine, the code works the way it should (the pragmas essentially NOP out, since there&#8217;d be no benefit in parallelizing on one core).</p>
<p>A look at the CPU utilization proves to be interesting too. (By the way, my home <a href="http://rajorshi.net/blog/2008/08/upgrading-my-rig/">system</a> runs an AMD Althon64 X2 4600 dual core, at a clock speed of 2.4GHz). In the first case, here&#8217;s a snap of the system information (using the excellent <a href="http://technet.microsoft.com/en-us/sysinternals/bb896653.aspx">Process Explorer</a>). Notice how the CPU usage remains peaked at around 50%, and the second CPU is mostly idle. Please click on the images below for the full view.</p>
<p><a href="http://rajorshi.net/blog/wp-content/uploads/2009/05/1.jpg"><img class="center frame" src="http://rajorshi.net/blog/wp-content/uploads/2009/05/1_tm.jpg" alt="1" width="450" height="438" /></a></p>
<p>And here&#8217;s the usage when the OpenMP crunching is in action:</p>
<p><a href="http://rajorshi.net/blog/wp-content/uploads/2009/05/2.jpg"><img class="center frame" src="http://rajorshi.net/blog/wp-content/uploads/2009/05/2_tm.jpg" alt="2" width="450" height="438" /></a></p>
<p>That&#8217;s more like it. Both horses in action, CPU peaked at 100%. Similar stuff can be seen on Linux, using Ubuntu&#8217;s (rather, GNOME&#8217;s) inbuilt System Monitor:</p>
<p><a href="http://rajorshi.net/blog/wp-content/uploads/2009/05/ubuntu-sm.jpg"><img class="center frame" src="http://rajorshi.net/blog/wp-content/uploads/2009/05/ubuntu_tm.jpg" alt="ubuntu sm" width="450" height="133" /></a></p>
<p>The portion where the red and orange worms collide at the top is the duration of the OpenMP version of the matrix multiplication program.</p>
<p>As already stated, matrix multiplication is an ideal case &#8211; and such 2x speedup on dual core machines are possible with only such ideal problems. However, there often are, if you look closely enough, parts of your program that can be parallelized. Further, we have not even scratched the surface of what&#8217;s possible using OpenMP 3.0. It goes way beyond parallelizing simple for loops. (<a href="http://www.openmp.org/mp-documents/spec30.pdf">Here&#8217;s</a> the link to the spec in PDF).</p>
<p>And for sure, OpenMP is not the only way to go parallel portably. If you work in C++, you would have heard of the <a href="http://www.boost.org/">Boost</a> C++ libraries. Give <a href="http://www.boost.org/doc/libs/1_39_0/doc/html/thread.html">boost::threads</a> a go!</p>
<p>With Intel gearing up for the release of its eight core <a href="http://en.wikipedia.org/wiki/Nehalem_(microarchitecture)">Nehalem</a> EX processors, and with AMD&#8217;s six core <a href="http://www.youtube.com/watch?v=XDdvZkBo4JE">Istanbul</a> processor already finding its way into mainstream desktop boards, there remains only one thing to say: if there is a time to think in parallel, this is it!</p>
<p>~Raj</p>
<script type="text/javascript">
  addthis_url    = 'http%3A%2F%2Frajorshi.net%2Fblog%2F2009%2F05%2Fprogramming-for-multicore-introduction-openmp-gcc%2F';
  addthis_title  = 'Programming+for+multicore%3A+An+introduction+to+OpenMP+using+GCC-4.4';
  addthis_pub    = '';
</script><script type="text/javascript" src="http://s7.addthis.com/js/addthis_widget.php?v=12" ></script>
]]></content:encoded>
			<wfw:commentRss>http://rajorshi.net/blog/2009/05/programming-for-multicore-introduction-openmp-gcc/feed/</wfw:commentRss>
		<slash:comments>28</slash:comments>
		</item>
		<item>
		<title>Using Qt 4.4 opensource with Microsoft Visual C++ Express 2008</title>
		<link>http://rajorshi.net/blog/2009/01/using-qt-with-msvc-express-2008/</link>
		<comments>http://rajorshi.net/blog/2009/01/using-qt-with-msvc-express-2008/#comments</comments>
		<pubDate>Wed, 14 Jan 2009 15:12:58 +0000</pubDate>
		<dc:creator>rajorshi</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[Qt]]></category>
		<category><![CDATA[Visual Studio]]></category>
		<category><![CDATA[Windows]]></category>

		<guid isPermaLink="false">http://rajorshi.net/blog/?p=41</guid>
		<description><![CDATA[Qt from Trolltech is widely acknowledged as one of the best cross-platform GUI toolkits available. However, installing the Qt open source edition on Windows is not as effortless as &#8220;sudo apt-get install qt&#8221; on Ubuntu or other Linux flavors. It&#8217;s not that hard either, and this post shows you how to develop using the freely [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.qtsoftware.com/products">Qt</a> from Trolltech is widely acknowledged as one of the best cross-platform GUI toolkits available. However, installing the Qt open source edition on Windows is not as effortless as &#8220;sudo apt-get install qt&#8221; on Ubuntu or other Linux flavors. It&#8217;s not that hard either, and this post shows you how to develop using the freely available <strong>Microsoft Visual C++ 2008 </strong>Express as our IDE.</p>
<p>1. I&#8217;m assuming you have <a href="http://www.microsoft.com/express/vc/">MSVC 2008 Express</a> already installed. If not, download the offline install ISO from <a href="http://www.microsoft.com/express/download">here</a>, mount it (using <a href="http://forum.daemon-tools.cc/download.php">Daemon Tools</a> for example), and launch the installer from the virtual drive.  Next, download the Windows open source version of Qt from <a href="http://www.qtsoftware.com/downloads/opensource/appdev/windows-cpp">here</a>.</p>
<p>2. Now, you can either extract the Qt source package to a folder where you want it to be installed, or you might want to extract it to a temporary location, and install only the final files to your install directory. Doing the latter of course makes more sense. Except that it is NOT recommended for Windows. I have faced quite a few problems (which I will detail further down the line). Bottom line is &#8211; if you have no problems sparing about 1G for Qt, then choose the former approach.</p>
<p>Open up the &#8220;Visual Studio 2008 Command Prompt&#8221; (available in the &#8220;Tools&#8221; sub-menu in your Visual C++ start menu entry). For the former approach, issue the following command:</p>
<pre>configure</pre>
<p>If you want a separate install directory (let&#8217;s say in D:\Qt-4.4.3), use the &#8216;prefix&#8217; flag in this manner:</p>
<pre>configure -prefix "D:\Qt-4.4.3"</pre>
<p>3. Depending on your system, this takes a quite a while. Oh, and if you face an error like this, fear not:</p>
<pre>copy qmake.exe P:\qt-win-opensource-src-4.4.3\bin\qmake.exe
        1 file(s) copied.
Creating makefiles in src...
Generating Visual Studio project files...
Could not find mkspecs for your QMAKESPEC(win32-msvc2008)
after trying:
        D:\Qt-4.4.3\mkspecs
Error processing project file:
P:/qt-win-opensource-src-4.4.3/projects.pro
Qmake failed, return code 3</pre>
<p>This is the first of a few problems that crop up when you use a custom install location (i.e. the latter approach). Just copy the &#8220;mkspecs&#8221; folder from your source directory tree over to your install directory and re-run the configure program.</p>
<p>4. Once &#8216;configure&#8217; completes, run &#8216;nmake&#8217;. This takes a really long time. If you chose to have a separate install location, run &#8216;nmake install&#8217; once this completes.</p>
<p>5. Another problem of a separate install directory is that the Makefile forgets to copy the <a href="http://msdn.microsoft.com/en-us/library/aa375365(VS.85).aspx">MANIFEST </a>files. So, if at this stage you try to start &#8220;designer.exe&#8221; from your install/bin folder, you may get an error saying that the application failed to start because MSVCP90.dll was not found.</p>
<p><a href="http://rajorshi.net/blog/wp-content/uploads/2009/01/qt_error.jpg"></a></p>
<p>To fix this, copy over all the &#8220;.manifest&#8221; files from your source &#8220;bin&#8221; and &#8220;lib&#8221; directories over to the install folder&#8217;s &#8220;bin&#8221; and &#8220;lib&#8221; directories. At this point, you should be able to run Qt-Designer, Qt-Assistant etc from your bin directory.</p>
<p>6. Let us set up a couple of environment variables that make life easier for us. To edit environment variables, you need to right click &#8220;My Computer &gt; Properties &gt; Advanced &gt; Environment Variables&#8221;. Add a new variable <strong>QTDIR </strong>pointing to your Qt install directory, and edit your <strong>PATH</strong> to include Qt&#8217;s &#8220;bin&#8221; directory as follows:</p>
<p><img class="center frame" src="http://rajorshi.net/blog/wp-content/uploads/2009/01/qt_var1.jpg" alt="Setting QTDIR" /></p>
<p><img class="center frame" src="http://rajorshi.net/blog/wp-content/uploads/2009/01/qt_var2.jpg" alt="Adding to PATH" /></p>
<p> </p>
<p>7. Now let&#8217;s try to get Qt&#8217;s &#8220;Hello World&#8221; tutorial program running from the command line. Fire up the Visual Studio Command Prompt, and create a file &#8220;Hello.cpp&#8221; containing the following code in a new directory called &#8220;hello&#8221;:</p>
<pre name="code" class="cpp">

#include &quot;QApplication&quot;
#include &quot;QPushButton&quot;

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    QPushButton hello(&quot;Hello world&quot;);
    hello.resize(100, 30);

    hello.show();
    return app.exec();
}
</pre>
<p>Now, type the following commands in this new folder:</p>
<pre>qmake -project
qmake hello.pro
nmake</pre>
<p>This should create an executable &#8220;hello.exe&#8221;, which you should be able to execute to see your first GUI program using Qt-4.4 and MSVC 2008.</p>
<p>7. I would suggest working from the command prompt, but should you wish to use the Visual Studio Express IDE, here&#8217;s what you should do.</p>
<p>Fire it up, and go to &#8220;Tools &gt; Options &gt; Projects and Solutions &gt; VC++ Directories&#8221;. Add &#8220;$(QTDIR)\include&#8221; to the &#8220;Include files&#8221;, and &#8220;$(QTDIR)\lib&#8221; to the &#8220;Library files&#8221; drop-down lists respectively.</p>
<p>8. Create a new project (&#8220;File &gt; New &gt; Project &gt; General &gt; Makefile Project&#8221;) named &#8220;HelloQt&#8221;.</p>
<p>Go to &#8220;Project &gt; Properties &gt; Configuration Properties &gt; Nmake&#8221; and enter the following in the build command line &#8220;qmake -project &amp;&amp; qmake &amp;&amp; nmake release-all&#8221;. Also enter &#8220;release\HelloQt.exe&#8221; in the &#8220;Output&#8221; field. (You may enter corresponding debug versions here as well).</p>
<p>Right click &#8220;Source Files&#8221; in the &#8220;Solution Explorer&#8221; and create a new file &#8220;HelloQt.cpp&#8221;. Copy paste the above program into it.</p>
<p>Run your program using &#8220;Ctrl+F5&#8243;. You should see this:</p>
<p><a href="http://rajorshi.net/blog/wp-content/uploads/2009/01/qt_sample.jpg"><br />
<img class="center frame" src="http://rajorshi.net/blog/wp-content/uploads/2009/01/qt_clip.jpg" alt="Sample Qt 4.4 program running inside Microsoft Visual C++ Express 2008" /><br />
</a></p>
<p>So there you have it. A crash HOWTO on developing Qt-4.4 programs using Visual Studio 2008 express. Feel free do comment on any problems you may have faced.</p>
<p>
~Raj</p>
<script type="text/javascript">
  addthis_url    = 'http%3A%2F%2Frajorshi.net%2Fblog%2F2009%2F01%2Fusing-qt-with-msvc-express-2008%2F';
  addthis_title  = 'Using+Qt+4.4+opensource+with+Microsoft+Visual+C%2B%2B+Express+2008';
  addthis_pub    = '';
</script><script type="text/javascript" src="http://s7.addthis.com/js/addthis_widget.php?v=12" ></script>
]]></content:encoded>
			<wfw:commentRss>http://rajorshi.net/blog/2009/01/using-qt-with-msvc-express-2008/feed/</wfw:commentRss>
		<slash:comments>53</slash:comments>
		</item>
	</channel>
</rss>

