<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Make on bramp.net</title>
    <link>https://blog.bramp.net/</link>
    <description>Recent content in Make on bramp.net</description>
    <generator>Hugo -- gohugo.io</generator>
    <language>en-GB</language>
    <lastBuildDate>Sun, 09 Sep 2012 00:00:00 +0000</lastBuildDate>
    <atom:link href="https://blog.bramp.net/tags/make/" rel="self" type="application/rss+xml" />
    
    <item>
      <title>Using Make to parallelise a task</title>
      <link>https://blog.bramp.net/post/2012/09/09/using-make-to-parallelise-a-task/</link>
      <pubDate>Sun, 09 Sep 2012 00:00:00 +0000</pubDate>
      
      <guid>https://blog.bramp.net/post/2012/09/09/using-make-to-parallelise-a-task/</guid>
      <description><p>I needed to run a CPU intensive script over a directory of files. Each file would be run independently, and I was using bash to achieve this:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl"><span class="k">for</span> <span class="nv">$i</span> in *.txt<span class="p">;</span> <span class="k">do</span> ./script <span class="nv">$i</span><span class="p">;</span> <span class="k">done</span>
</span></span></code></pre></div><p>This works fine, however, I have a quad core machine, and this task was CPU bound on one core. So I thought about parallelising this task so the script would run on four files at once. I didn’t want to get into the nitty gritty of changing the script to cope in this way, so instead, I “abused” Make to do this.</p>
<p>I created a file named “Makefile” with the following:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-makefile" data-lang="makefile"><span class="line"><span class="cl"><span class="nv">FILES</span><span class="o">=</span><span class="k">$(</span>shell ls *.txt<span class="k">)</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="c">#default target of everything
</span></span></span><span class="line"><span class="cl"><span class="nf">all</span><span class="o">:</span> <span class="k">$(</span><span class="nv">FILES</span><span class="k">)</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="nf">$(FILES)</span><span class="o">:</span>
</span></span><span class="line"><span class="cl">	./script <span class="nv">$@</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="nf">.PHONY</span><span class="o">:</span> <span class="n">all</span> <span class="k">$(</span><span class="nv">FILES</span><span class="k">)</span>
</span></span></code></pre></div><p>then you just run <code>make -j4</code>, and four instances of the script will start running, with the concurrency being handled by Make. You can also now type <code>make a.txt b.txt c.txt</code> and it’ll just run the script on those three files.</p>
<p>You can also extend this Makefile to handle dependencies, such as running a script before all the others. Make is pretty powerful, and should be considered for more than just compiling programs.</p></description>
    </item>
    
  </channel>
</rss>
