<?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>Ashish Agarwal &#187; C</title>
	<atom:link href="http://ashishagarwal.org/tag/c/feed/" rel="self" type="application/rss+xml" />
	<link>http://ashishagarwal.org</link>
	<description></description>
	<lastBuildDate>Mon, 11 Mar 2019 21:28:46 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.1.42</generator>
	<item>
		<title>Small example of pthreads</title>
		<link>http://ashishagarwal.org/2011/02/13/multicore-programming-with-pthreads/</link>
		<comments>http://ashishagarwal.org/2011/02/13/multicore-programming-with-pthreads/#comments</comments>
		<pubDate>Sun, 13 Feb 2011 22:54:06 +0000</pubDate>
		<dc:creator><![CDATA[ashish]]></dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[C]]></category>

		<guid isPermaLink="false">http://ashishagarwal.org/?p=138</guid>
		<description><![CDATA[Here&#8217;s a simple C program that demonstrates the use of the pthreads library. The following code can be downloaded here, so you can test it yourself. Simply compile with the command: gcc -lpthread pthreads_test.c -o pthreads_test First, let&#8217;s import some &#8230; <a href="http://ashishagarwal.org/2011/02/13/multicore-programming-with-pthreads/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>Here&#8217;s a simple C program that demonstrates the use of the <strong>pthreads</strong> library. The following code can be downloaded <a href="http://ashishagarwal.org/wp-content/uploads/2011/02/pthreads_test.c">here</a>, so you can test it yourself. Simply compile with the command:</p>
<p><code>gcc -lpthread pthreads_test.c -o pthreads_test</code></p>
<p>First, let&#8217;s import some necessary headers, mainly <em>pthread.h</em> which provides the POSIX threads implementation.</p>
<div class="highlight" style="background: #f8f8f8;">
<pre style="line-height: 125%;"><span style="color: #bc7a00;">#include &lt;pthread.h&gt;</span>
<span style="color: #bc7a00;">#include &lt;stdio.h&gt;</span>
<span style="color: #bc7a00;">#include &lt;stdlib.h&gt;</span>
<span style="color: #bc7a00;">#include &lt;math.h&gt;</span></pre>
</div>
<p>Now, define a task that takes some non-negligible amount of time to complete. We pass in an id simply to identify each call to the task in output messages.</p>
<div class="highlight" style="background: #f8f8f8;">
<pre style="line-height: 125%;"><span style="color: #b00040;">void</span> <span style="color: #666666;">*</span><span style="color: #0000ff;">task</span>(<span style="color: #b00040;">int</span> id) {
  printf(<span style="color: #ba2121;">"Task %d started</span><span style="color: #bb6622; font-weight: bold;">\n</span><span style="color: #ba2121;">"</span>, id);
  <span style="color: #b00040;">int</span> i;
  <span style="color: #b00040;">double</span> result <span style="color: #666666;">=</span> <span style="color: #666666;">0.0</span>;
  <span style="color: #008000; font-weight: bold;">for</span> (i <span style="color: #666666;">=</span> <span style="color: #666666;">0</span>; i <span style="color: #666666;">&lt;</span> <span style="color: #666666;">1000000</span>; i<span style="color: #666666;">++</span>) {
    result <span style="color: #666666;">=</span> result <span style="color: #666666;">+</span> sin(i) <span style="color: #666666;">*</span> tan(i);
  }
  printf(<span style="color: #ba2121;">"Task %d completed with result %e</span><span style="color: #bb6622; font-weight: bold;">\n</span><span style="color: #ba2121;">"</span>, id, result);
}</pre>
</div>
<p>We can run the above task a desired number of times in serial by calling the following function:</p>
<div class="highlight" style="background: #f8f8f8;">
<pre style="line-height: 125%;"><span style="color: #b00040;">void</span> <span style="color: #666666;">*</span><span style="color: #0000ff;">serial</span>(<span style="color: #b00040;">int</span> num_tasks) {
  <span style="color: #b00040;">int</span> i;
  <span style="color: #008000; font-weight: bold;">for</span> (i <span style="color: #666666;">=</span> <span style="color: #666666;">0</span>; i <span style="color: #666666;">&lt;</span> num_tasks; i<span style="color: #666666;">++</span>) {
    task(i);
  }
}</pre>
</div>
<p>Now, let&#8217;s define <em>task</em> in a manner suitable for being called in its own thread. All this requires is to use <em>pthread_exit</em> to let the parent process know this thread has completed. Actually this doesn&#8217;t affect the simple program we&#8217;re writing, but it&#8217;s the correct thing to do if you want to later join threads. Also, we have to make sure the function signature matches the requirements of <em>pthread_create</em>.</p>
<div class="highlight" style="background: #f8f8f8;">
<pre style="line-height: 125%;"><span style="color: #b00040;">void</span> <span style="color: #666666;">*</span><span style="color: #0000ff;">threaded_task</span>(<span style="color: #b00040;">void</span> <span style="color: #666666;">*</span>t) {
  <span style="color: #b00040;">long</span> id <span style="color: #666666;">=</span> (<span style="color: #b00040;">long</span>) t;
  printf(<span style="color: #ba2121;">"Thread %ld started</span><span style="color: #bb6622; font-weight: bold;">\n</span><span style="color: #ba2121;">"</span>, id);
  task(id);
  printf(<span style="color: #ba2121;">"Thread %ld done</span><span style="color: #bb6622; font-weight: bold;">\n</span><span style="color: #ba2121;">"</span>, id);
  pthread_exit(<span style="color: #666666;">0</span>);
}</pre>
</div>
<p>Finally, the interesting code generates a new thread for each call to <em>threaded_task</em>.</p>
<div class="highlight" style="background: #f8f8f8;">
<pre style="line-height: 125%;"><span style="color: #b00040;">void</span> <span style="color: #666666;">*</span><span style="color: #0000ff;">parallel</span>(<span style="color: #b00040;">int</span> num_tasks)
{
  <span style="color: #b00040;">int</span> num_threads <span style="color: #666666;">=</span> num_tasks;
  pthread_t <span style="color: #008000; font-weight: bold;">thread</span>[num_threads];
  <span style="color: #b00040;">int</span> rc;
  <span style="color: #b00040;">long</span> t;
  <span style="color: #008000; font-weight: bold;">for</span> (t <span style="color: #666666;">=</span> <span style="color: #666666;">0</span>; t <span style="color: #666666;">&lt;</span> num_threads; t<span style="color: #666666;">++</span>) {
    printf(<span style="color: #ba2121;">"Creating thread %ld</span><span style="color: #bb6622; font-weight: bold;">\n</span><span style="color: #ba2121;">"</span>, t);
    rc <span style="color: #666666;">=</span> pthread_create(<span style="color: #666666;">&amp;</span><span style="color: #008000; font-weight: bold;">thread</span>[t], <span style="color: #008000;">NULL</span>, threaded_task, (<span style="color: #b00040;">void</span> <span style="color: #666666;">*</span>)t);
    <span style="color: #008000; font-weight: bold;">if</span> (rc) {
      printf(<span style="color: #ba2121;">"ERROR: return code from pthread_create() is %d</span><span style="color: #bb6622; font-weight: bold;">\n</span><span style="color: #ba2121;">"</span>, rc);
      exit(<span style="color: #666666;">-1</span>);
    }
  }
}</pre>
</div>
<p>The main function runs a specified number of tasks either in serial or parallel.</p>
<div class="highlight" style="background: #f8f8f8;">
<pre style="line-height: 125%;"><span style="color: #b00040;">void</span> <span style="color: #666666;">*</span><span style="color: #0000ff;">print_usage</span>(<span style="color: #b00040;">int</span> argc, <span style="color: #b00040;">char</span> <span style="color: #666666;">*</span>argv[]) {
  printf(<span style="color: #ba2121;">"Usage: %s serial|parallel num_tasks</span><span style="color: #bb6622; font-weight: bold;">\n</span><span style="color: #ba2121;">"</span>, argv[<span style="color: #666666;">0</span>]);
  exit(<span style="color: #666666;">1</span>);
}

<span style="color: #b00040;">int</span> <span style="color: #0000ff;">main</span>(<span style="color: #b00040;">int</span> argc, <span style="color: #b00040;">char</span> <span style="color: #666666;">*</span>argv[]) {
  <span style="color: #008000; font-weight: bold;">if</span> (argc <span style="color: #666666;">!=</span> <span style="color: #666666;">3</span>) {print_usage(argc, argv);}

  <span style="color: #b00040;">int</span> num_tasks <span style="color: #666666;">=</span> atoi(argv[<span style="color: #666666;">2</span>]);

  <span style="color: #008000; font-weight: bold;">if</span> (<span style="color: #666666;">!</span>strcmp(argv[<span style="color: #666666;">1</span>], <span style="color: #ba2121;">"serial"</span>)) {
    serial(num_tasks);
  } <span style="color: #008000; font-weight: bold;">else</span> <span style="color: #008000; font-weight: bold;">if</span> (<span style="color: #666666;">!</span>strcmp(argv[<span style="color: #666666;">1</span>], <span style="color: #ba2121;">"parallel"</span>)) {
    parallel(num_tasks);
  }
  <span style="color: #008000; font-weight: bold;">else</span> {
    print_usage(argc, argv);
  }

  printf(<span style="color: #ba2121;">"Main completed</span><span style="color: #bb6622; font-weight: bold;">\n</span><span style="color: #ba2121;">"</span>);
  pthread_exit(<span style="color: #008000;">NULL</span>);
}</pre>
</div>
<p>That&#8217;s it! Now, compile the program using the command given at the beginning of this post. Here are some results on a MacBook Air.<br />
<code><br />
$ time ./pthreads_test serial 4<br />
Task 0 started<br />
Task 0 completed with result -3.153838e+06<br />
Task 1 started<br />
Task 1 completed with result -3.153838e+06<br />
Task 2 started<br />
Task 2 completed with result -3.153838e+06<br />
Task 3 started<br />
Task 3 completed with result -3.153838e+06<br />
Main completed</code></p>
<p>real 0m0.673s<br />
user 0m0.665s<br />
sys 0m0.003s</p>
<p><code><br />
$ time ./pthreads_test parallel 4<br />
Creating thread 0<br />
Creating thread 1<br />
Creating thread 2<br />
Creating thread 3<br />
Main completed<br />
Thread 1 started<br />
Task 1 started<br />
Thread 2 started<br />
Task 2 started<br />
Thread 3 started<br />
Task 3 started<br />
Thread 0 started<br />
Task 0 started<br />
Task 3 completed with result -3.153838e+06<br />
Thread 3 done<br />
Task 2 completed with result -3.153838e+06<br />
Task 1 completed with result -3.153838e+06<br />
Thread 2 done<br />
Thread 1 done<br />
Task 0 completed with result -3.153838e+06<br />
Thread 0 done</code></p>
<p>real 0m0.378s<br />
user 0m0.667s<br />
sys 0m0.007s</p>
<p>Note that threads do not necessarily start or end in order. And the point of it all; there is a factor 2x speedup for the multi-threaded run! Here&#8217;s a plot of the <em>real</em> time against number of tasks for 2, 4, 8, 16, 32, and 64 tasks.</p>
<p><img style="display: block; margin-left: auto; margin-right: auto;" alt="" src="http://chart.apis.google.com/chart?chxl=1:|2|4|8|16|32|64&amp;chxr=0,0,11|1,-125,100&amp;chxt=y,x&amp;chbh=a,4,16&amp;chs=600x225&amp;cht=bvg&amp;chco=A2C180,3D7930&amp;chds=0,11,0,11&amp;chd=t:0.36,0.692,1.344,2.736,5.423,10.825|0.206,0.418,0.754,1.528,2.999,5.879&amp;chdl=serial|parallel&amp;chma=0,0,10" width="600" height="225" /></p>
<p>You can learn more by following the excellent tutorial provided <a href="https://computing.llnl.gov/tutorials/pthreads/">here</a>, from which the above example was derived.</p>
]]></content:encoded>
			<wfw:commentRss>http://ashishagarwal.org/2011/02/13/multicore-programming-with-pthreads/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>RSEQtools: A modular framework to analyze RNA-Seq data using compact, anonymized data summaries</title>
		<link>http://ashishagarwal.org/2010/12/07/rseqtools/</link>
		<comments>http://ashishagarwal.org/2010/12/07/rseqtools/#comments</comments>
		<pubDate>Tue, 07 Dec 2010 16:06:47 +0000</pubDate>
		<dc:creator><![CDATA[ashish]]></dc:creator>
				<category><![CDATA[Publications]]></category>
		<category><![CDATA[Bioinformatics]]></category>
		<category><![CDATA[C]]></category>

		<guid isPermaLink="false">http://ashishagarwal.org/?p=129</guid>
		<description><![CDATA[Abstract Summary: The advent of next-generation sequencing for functional genomics has given rise to quantities of sequence information that are often so large that they are difficult to handle. Moreover, sequence reads from a specific individual can contain sufficient information &#8230; <a href="http://ashishagarwal.org/2010/12/07/rseqtools/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p><strong>Abstract</strong></p>
<blockquote><p><strong> Summary:</strong> The advent of next-generation sequencing for functional genomics has given rise to quantities of sequence information that are often so large that they are difficult to handle. Moreover, sequence reads from a specific individual can contain sufficient information to potentially identify and genetically characterize that person, raising privacy concerns. In order to address these issues we have developed the Mapped Read Format (MRF), a compact data summary format for both short and long read alignments that enables the anonymization of confidential sequence information, while allowing one to still carry out many functional genomics studies. We have developed a suite of tools that use this format for the analysis of RNA-Seq experiments. RSEQtools consists of a set of modules that perform common tasks such as calculating gene expression values, generating signal tracks of mapped reads, and segmenting that signal into actively transcribed regions. Moreover, these tools can readily be used to build customizable RNA-Seq workflows. In addition to the anonymization afforded by this format it also facilitates the decoupling of the alignment of reads from downstream analyses.</p>
<p>Availability and implementation: RSEQtools is implemented in C and the source code is available at <a href="http://rseqtools.gersteinlab.org/">http://rseqtools.gersteinlab.org/</a></p></blockquote>
<p><a class="html" href="http://dx.doi.org/10.1093/bioinformatics/btq643">Download free from publisher</a></p>
<p><strong>Citation</strong><br />
Lukas Habegger, Andrea Sboner, Tara A. Gianoulis, Joel Rozowsky, Ashish Agarwal, Michael Snyder, Mark Gerstein (2011). RSEQtools: A modular framework to analyze RNA-Seq data using compact, anonymized data summaries, <em>Bioinformatics</em> <strong>27</strong>(2):281-283.</p>
]]></content:encoded>
			<wfw:commentRss>http://ashishagarwal.org/2010/12/07/rseqtools/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
