Advertise filters param in discovery doc.
[arvados.git] / doc / user / tutorials / tutorial-parallel.html.textile.liquid
1 ---
2 layout: default
3 navsection: userguide
4 navmenu: Tutorials
5 title: "Parallel Crunch tasks"
6
7 ...
8
9 h1. Parallel Crunch tasks
10
11 In the tutorial "writing a crunch script,":tutorial-firstscript.html our script used a "for" loop to compute the md5 hashes for each file in sequence.  This approach, while simple, is not able to take advantage of the compute cluster with multiple nodes and cores to speed up computation by running tasks in parallel.  This tutorial will demonstrate how to create parallel Crunch tasks.
12
13 Start by entering the @crunch_scripts@ directory of your git repository:
14
15 <notextile>
16 <pre><code>~$ <span class="userinput">cd <b>you</b>/crunch_scripts</span>
17 </code></pre>
18 </notextile>
19
20 Next, using @nano@ or your favorite Unix text editor, create a new file called @parallel-hash.py@ in the @crunch_scripts@ directory.
21
22 notextile. <pre>~/<b>you</b>/crunch_scripts$ <code class="userinput">nano parallel-hash.py</code></pre>
23
24 Add the following code to compute the md5 hash of each file in a 
25
26 <pre><code class="userinput">{% include 'parallel_hash_script_py' %}</code></pre>
27
28 Make the file executable:
29
30 notextile. <pre><code>~/<b>you</b>/crunch_scripts$ <span class="userinput">chmod +x parallel-hash.py</span></code></pre>
31
32 Next, add the file to @git@ staging, commit and push:
33
34 <notextile>
35 <pre><code>~/<b>you</b>/crunch_scripts$ <span class="userinput">git add parallel-hash.py</span>
36 ~/<b>you</b>/crunch_scripts$ <span class="userinput">git commit -m"parallel hash"</span>
37 ~/<b>you</b>/crunch_scripts$ <span class="userinput">git push origin master</span>
38 </code></pre>
39 </notextile>
40
41 You should now be able to run your new script using Crunch, with "script" referring to our new "parallel-hash.py" script.  We will use a different input from our previous examples.  We will use @887cd41e9c613463eab2f0d885c6dd96+83@ which consists of three files, "alice.txt", "bob.txt" and "carol.txt" (the example collection used previously in "fetching data from Arvados using Keep":tutorial-keep.html).
42
43 <notextile>
44 <pre><code>~/<b>you</b>/crunch_scripts$ <span class="userinput">cat &gt;~/the_job &lt;&lt;EOF
45 {
46  "script": "parallel-hash.py",
47  "script_version": "you:master",
48  "script_parameters":
49  {
50   "input": "887cd41e9c613463eab2f0d885c6dd96+83"
51  }
52 }
53 EOF</span>
54 ~/<b>you</b>/crunch_scripts$ <span class="userinput">arv job create --job "$(cat ~/the_job)"</span>
55 {
56  ...
57  "uuid":"qr1hi-xxxxx-xxxxxxxxxxxxxxx"
58  ...
59 }
60 ~/<b>you</b>/crunch_scripts$ <span class="userinput">arv job get --uuid qr1hi-xxxxx-xxxxxxxxxxxxxxx</span>
61 {
62  ...
63  "output":"e2ccd204bca37c77c0ba59fc470cd0f7+162",
64  ...
65 }
66 </code></pre>
67 </notextile>
68
69 Because the job ran in parallel, each instance of parallel-hash creates a separate @md5sum.txt@ as output.  Arvados automatically collates theses files into a single collection, which is the output of the job:
70
71 <notextile>
72 <pre><code>~/<b>you</b>/crunch_scripts$ <span class="userinput">arv keep get e2ccd204bca37c77c0ba59fc470cd0f7+162</span>
73 md5sum.txt
74 md5sum.txt
75 md5sum.txt
76 ~/<b>you</b>/crunch_scripts$ <span class="userinput">arv keep get e2ccd204bca37c77c0ba59fc470cd0f7+162/md5sum.txt</span>
77 0f1d6bcf55c34bed7f92a805d2d89bbf alice.txt
78 504938460ef369cd275e4ef58994cffe bob.txt
79 8f3b36aff310e06f3c5b9e95678ff77a carol.txt
80 </code></pre>
81 </notextile>
82
83 h2. The one job per file pattern
84
85 This example demonstrates how to schedule a new task per file.  Because this is a common pattern, the Crunch Python API contains a convenience function to "queue a task for each input file":{{site.baseurl}}/sdk/python/crunch-utility-libraries.html#one_task_per_input which reduces the amount of boilerplate code required to handle parallel jobs.
86
87 Next, "Constructing a Crunch pipeline":tutorial-new-pipeline.html
88