{"id":560,"date":"2018-12-07T23:21:46","date_gmt":"2018-12-07T23:21:46","guid":{"rendered":"http:\/\/teaching.sociology.ul.ie\/bhalpin\/wordpress\/?p=560"},"modified":"2019-02-16T00:27:08","modified_gmt":"2019-02-16T00:27:08","slug":"simple-parallelism-in-stata","status":"publish","type":"post","link":"https:\/\/teaching.sociology.ul.ie\/bhalpin\/wordpress\/?p=560","title":{"rendered":"Simple parallelism in Stata"},"content":{"rendered":"<div id=\"outline-container-orgaa9e91a\" class=\"outline-2\">\n<h2 id=\"orgaa9e91a\">Speeding up embarrassingly parallel work in Stata<\/h2>\n<div id=\"text-orgaa9e91a\" class=\"outline-text-2\">\n<p>Stata-MP enables parallel processing, but this really kicks in only in estimation commands that are programmed to exploit it. A lot of the time we may have very simply repetitive tasks for Stata to do, that could be packaged for parallel operation very easily. This sort of work is often called embarrassingly parallel, and isn&#8217;t automatically catered for by Stata-MP.<\/p>\n<p>The simplest situation is where the same work needs to be done multiple times, with some different details each time but no dependence whatsoever on the other parts. For instance we may have a loop where the work in the body takes a long time but doesn&#8217;t depend on the results of the other runs.<\/p>\n<p>I came across this situation the other day: I have data created by running a simulation with different values of two key parameters, and I wanted to create graphs for each combination of the parameters (PNGs rather than Stata graphs, but that&#8217;s just a detail). So I wrote code like the following:<br \/>\n<!--more--><\/p>\n<p>&nbsp;<\/p>\n<div class=\"org-src-container\">\n<pre class=\"src src-Stata\">forvalues i = 0\/20 {\r\n  forvalues j = 1\/8 {\r\n[... complicated commands making the <span style=\"color: #f5666d;\">graph<\/span>, depending in `<span style=\"color: #0084c8; font-weight: bold;\">i<\/span>' and `<span style=\"color: #0084c8; font-weight: bold;\">j<\/span>' ...]\r\n  }\r\n}\r\n<\/pre>\n<\/div>\n<p>OK, so that specified 21 times 8 = 168 graphs. But each one took about 25 seconds to generate, so it takes over an hour. On the other hand, it uses only one core, and my computer has eight. Eight? What if I ran this code eight times, varying the 1 in the second line:<\/p>\n<div class=\"org-src-container\">\n<pre class=\"src src-Stata\"><span style=\"color: #a52a2a; font-weight: bold;\">use<\/span> simdata\r\n<span style=\"color: #f5666d;\">local<\/span><span style=\"color: #0084c8; font-weight: bold;\"> j<\/span> 1\r\nforvalues i = 0\/20 {\r\n[... complicated commands making the <span style=\"color: #f5666d;\">graph<\/span>, depending in `<span style=\"color: #0084c8; font-weight: bold;\">i<\/span>' and `<span style=\"color: #0084c8; font-weight: bold;\">j<\/span>' ...]\r\n}\r\n<\/pre>\n<\/div>\n<p>In fact, if the code looks like this and resides in <code>smallfile.do<\/code>:\u2026<\/p>\n<div class=\"org-src-container\">\n<pre class=\"src src-Stata\"><span style=\"color: #a52a2a; font-weight: bold;\">use<\/span> simdata\r\n<span style=\"color: #f5666d;\">local<\/span><span style=\"color: #0084c8; font-weight: bold;\"> j<\/span> `1'\r\nforvalues i = 0\/20 {\r\n[...]\r\n}\r\n<\/pre>\n<\/div>\n<p>\u2026I can run it from within Stata as follows (varying the 1 from 1 to 8):<\/p>\n<div class=\"org-src-container\">\n<pre class=\"src src-Stata\">!stata -b <span style=\"color: #f5666d;\">do<\/span> smallfile.do 1 &amp;\r\n<\/pre>\n<\/div>\n<p>Under Unix, the &#8220;&amp;&#8221; at the end makes this work in the background, as long as the &#8220;!stata -b do \u2026&#8221; command is issued in an interactive Stata session.<\/p>\n<p>There is a slight problem: I can&#8217;t run files with the same name simultaneously in the same folder, without Stata fighting with itself about writing to the log files. One strategy I&#8217;ve used before is to make each command work in its own subfolder: that&#8217;s good but requires a bit more prep. What I did yesterday was to copy the <code>smallfile.do<\/code> to temporary files:<\/p>\n<div class=\"org-src-container\">\n<pre class=\"src src-Stata\">forvalues x = 1\/8 {\r\n  <span style=\"color: #f5666d;\">tempname<\/span><span style=\"color: #2f8b58; font-weight: bold;\"> aux`<\/span><span style=\"color: #0084c8; font-weight: bold;\">x<\/span><span style=\"color: #2f8b58; font-weight: bold;\">'<\/span>\r\n  !cp -p smallfile.do `<span style=\"color: #0084c8; font-weight: bold;\">aux`x<\/span>''.do\r\n  !stata -b <span style=\"color: #f5666d;\">do<\/span> `<span style=\"color: #0084c8; font-weight: bold;\">aux`x<\/span>''.do `<span style=\"color: #0084c8; font-weight: bold;\">x<\/span>' &amp;\r\n}\r\n<\/pre>\n<\/div>\n<p>This creates files named like <code>__000001.do<\/code>, and Stata logs to files like <code>__000001.log<\/code>. Since the outputs (PNG-files) are named depending on the <code>`x'<\/code> value, they don&#8217;t clash.<\/p>\n<p>Finally, I realised that the &#8220;!&#8221; or <code>shell<\/code> command in Stata will run in the background (asychronously, in parallel) only if it is invoked in an interactive Stata session. If the main file is run in batch mode, the sub-tasks are executed sequentially. So I tried the <code>winexec<\/code> command, which according to the documentation is intended explicitly for calling graphical programs like a web browser, while in interactive sessions. But it works:<\/p>\n<div class=\"org-src-container\">\n<pre class=\"src src-Stata\">forvalues x = 1\/8 {\r\n  <span style=\"color: #f5666d;\">tempname<\/span><span style=\"color: #2f8b58; font-weight: bold;\"> aux`<\/span><span style=\"color: #0084c8; font-weight: bold;\">x<\/span><span style=\"color: #2f8b58; font-weight: bold;\">'<\/span>\r\n  !cp -p smallfile.do `<span style=\"color: #0084c8; font-weight: bold;\">aux`x<\/span>''.do\r\n  winexec stata -b <span style=\"color: #f5666d;\">do<\/span> `<span style=\"color: #0084c8; font-weight: bold;\">aux`x<\/span>''.do `<span style=\"color: #0084c8; font-weight: bold;\">x<\/span>'\r\n}\r\n<\/pre>\n<\/div>\n<p><code>winexec<\/code>, without an <code>&amp;<\/code>, will run the subtasks in parallel, even from batch mode!<\/p>\n<\/div>\n<\/div>\n<p><!--more--><\/p>\n<p><!--more--><\/p>\n<div id=\"outline-container-org7890420\" class=\"outline-2\">\n<h2 id=\"org7890420\">Summary<\/h2>\n<div id=\"text-org7890420\" class=\"outline-text-2\">\n<p>This strategy depends on the subtasks being independent: while they all use the same <code>.dta<\/code> file, they don&#8217;t depend on each other, and they produce output that is named independently and doesn&#8217;t clash. The <code>smallfile.do<\/code> takes a command line parameter, so it operates differently for each run. It uses this parameter to name the outputs (e.g., <code>gr_`x'_`1'.png<\/code>). Copying the <code>smallfile.do<\/code> to a temporary file (or functionally equivalently, to a run-specific folder) means that the parallel Stata tasks don&#8217;t clash with each other. The temporary files should be handled more cleanly (check they don&#8217;t exist first, delete them after use, etc), but are easy to remove afterwards.<\/p>\n<p>This example creates lots of graphs. We could equally generate new data files, and use a simple append to pull them together afterwards:<\/p>\n<div class=\"org-src-container\">\n<pre class=\"src src-Stata\"><span style=\"color: #a52a2a; font-weight: bold;\">clear<\/span>\r\nforvalues x = 1\/8 {\r\n  <span style=\"color: #a52a2a; font-weight: bold;\">append<\/span> <span style=\"color: #f5666d;\">using<\/span> smalldata`<span style=\"color: #0084c8; font-weight: bold;\">x<\/span>'\r\n}\r\n<\/pre>\n<\/div>\n<p>It&#8217;s a good speedup: I get my PNG files generated at 16\/min rather than 2.5\/min, so it gets done in about 10 minutes rather than about an hour. Easy to code and saves time. And it makes my CPU monitor look like this:<\/p>\n<div class=\"figure\">\n<p><img decoding=\"async\" src=\"http:\/\/teaching.sociology.ul.ie\/bhalpin\/cpu100.png\" alt=\"Over-active CPUs\" width=\"100%\" \/><\/p>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Speeding up embarrassingly parallel work in Stata Stata-MP enables parallel processing, but this really kicks in only in estimation commands that are programmed to exploit it. A lot of the time we may have very simply repetitive tasks for Stata to do, that could be packaged for parallel operation very easily. This sort of work &hellip; <a href=\"https:\/\/teaching.sociology.ul.ie\/bhalpin\/wordpress\/?p=560\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Simple parallelism in Stata<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/teaching.sociology.ul.ie\/bhalpin\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/560"}],"collection":[{"href":"https:\/\/teaching.sociology.ul.ie\/bhalpin\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/teaching.sociology.ul.ie\/bhalpin\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/teaching.sociology.ul.ie\/bhalpin\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/teaching.sociology.ul.ie\/bhalpin\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=560"}],"version-history":[{"count":7,"href":"https:\/\/teaching.sociology.ul.ie\/bhalpin\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/560\/revisions"}],"predecessor-version":[{"id":569,"href":"https:\/\/teaching.sociology.ul.ie\/bhalpin\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/560\/revisions\/569"}],"wp:attachment":[{"href":"https:\/\/teaching.sociology.ul.ie\/bhalpin\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=560"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/teaching.sociology.ul.ie\/bhalpin\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=560"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/teaching.sociology.ul.ie\/bhalpin\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=560"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}