r.html 38.3 KB
Newer Older
Lukáš Krupčík's avatar
Lukáš Krupčík committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml" lang="cs">

    
    
    
    
    


<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

    
        <base href="" /><!--[if lt IE 7]></base><![endif]-->
    

    
        

  
    <link rel="stylesheet" type="text/css" href="https://docs.it4i.cz/portal_css/Sunburst%20Theme/reset-cachekey-e265a1e68e2103b05918ef661c168cdb.css" media="screen" />
    <link rel="stylesheet" type="text/css" href="https://docs.it4i.cz/portal_css/Sunburst%20Theme/base-cachekey-f42567a9b3d7b2318db6477cef6d2d1b.css" />
        <!--[if lt IE 8]>    
    
    <link rel="stylesheet" type="text/css" href="https://docs.it4i.cz/portal_css/Sunburst%20Theme/IEFixes-cachekey-ec9b60024fd235ff2c883b4b54db91cb.css" media="screen" />
        <![endif]-->
    
    <style type="text/css" media="screen">@import url(https://docs.it4i.cz/portal_css/Sunburst%20Theme/resourcecioppino.twothumbs.stylesheetstwothumbs-cachekey-a07097acfdb29ca21e76e2372e673858.css);</style>
    <link rel="stylesheet" type="text/css" href="https://docs.it4i.cz/portal_css/Sunburst%20Theme/resourceit4i.portal.commonjquery.dataTables.min-cachekey-5939d0a22955a2f783ae949901e3df21.css" />

  
  
    <script type="text/javascript" src="https://docs.it4i.cz/portal_javascripts/Sunburst%20Theme/resourceplone.app.jquery-cachekey-c2d21aafabd41e32e7d82e9e4e57f6d1.js"></script>
    <script type="text/javascript" src="https://docs.it4i.cz/portal_javascripts/Sunburst%20Theme/collective.js.jqueryui.custom.min-cachekey-d6da9ac77d181fb49c1976e00c066a74.js"></script>
    <script type="text/javascript" src="https://docs.it4i.cz/portal_javascripts/Sunburst%20Theme/resourcedropdown-menu-cachekey-b132c7c9cab5eb4d3c541e47271da509.js"></script>
    <script type="text/javascript" src="https://docs.it4i.cz/portal_javascripts/Sunburst%20Theme/++resource++it4i.portal.common/exporting.js"></script>
    <script type="text/javascript" src="https://docs.it4i.cz/portal_javascripts/Sunburst%20Theme/resourceit4i.portal.commonjquery.dataTables.min-cachekey-58659b8d76594989f3faa967a94e5541.js"></script>


<title>R &mdash; IT4I Docs</title>
        
    <link rel="canonical" href="r.html" />

    <link rel="shortcut icon" type="image/x-icon" href="https://docs.it4i.cz/favicon.ico" />
    <link rel="apple-touch-icon" href="https://docs.it4i.cz/touch_icon.png" />


<script type="text/javascript">
        jQuery(function($){
            if (typeof($.datepicker) != "undefined"){
              $.datepicker.setDefaults(
                jQuery.extend($.datepicker.regional['cs'],
                {dateFormat: 'dd.mm.yy'}));
            }
        });
        </script>




    <link rel="search" href="https://docs.it4i.cz/@@search" title="Prohledejte portál" />



        
        
        
        
        

        <meta name="viewport" content="width=device-width, initial-scale=0.6666, maximum-scale=1.0, minimum-scale=0.6666" />
        <meta name="generator" content="Plone - http://plone.org" />
    
</head>

<body dir="ltr" class="template-document_view portaltype-document site-docs section-anselm-cluster-documentation subsection-software subsection-software-numerical-languages subsection-software-numerical-languages-r icons-on userrole-anonymous">

<div id="visual-portal-wrapper">

        <div id="portal-top" class="row">
<div class="cell width-full position-0">
            <div id="portal-header">
    <p class="hiddenStructure">
  <a accesskey="2" href="r.html#content">Přejít na obsah</a> |

  <a accesskey="6" href="r.html#portal-globalnav">Přejít na navigaci</a>
</p>

<div id="portal-personaltools-wrapper">

<p class="hiddenStructure">Osobní nástroje</p>





</div>



<div id="portal-searchbox">

    <form id="searchGadget_form" action="https://docs.it4i.cz/@@search">

        <div class="LSBox">
        <label class="hiddenStructure" for="searchGadget">Vyhledat</label>

        <input name="SearchableText" type="text" size="18" title="Prohledat portál" placeholder="Prohledat portál" accesskey="4" class="searchField" id="searchGadget" />

        <input class="searchButton" type="submit" value="Vyhledat" />

        <div class="searchSection">
            <input id="searchbox_currentfolder_only" class="noborder" type="checkbox" name="path" value="/docs/anselm-cluster-documentation/software/numerical-languages" />
            <label for="searchbox_currentfolder_only" style="cursor: pointer">pouze v aktuální sekci</label>
        </div>

        <div class="LSResult" id="LSResult"><div class="LSShadow" id="LSShadow"></div></div>
        </div>
    </form>

    <div id="portal-advanced-search" class="hiddenStructure">
        <a href="https://docs.it4i.cz/@@search" accesskey="5">Pokročilé vyhledávání...</a>
    </div>

</div>

<a id="portal-logo" title="IT4I Docs" accesskey="1" href="../../../index.html">
    <img src="https://docs.it4i.cz/logo.png" alt="IT4I Docs" title="IT4I Docs" height="73" width="565" /></a>

</div>

</div>
        </div>
    <div id="portal-columns" class="row">

        <div id="portal-column-content" class="cell width-3:4 position-1:4">

            <div id="viewlet-above-content"><div id="portal-breadcrumbs">

    <span id="breadcrumbs-you-are-here">Nacházíte se zde:</span>
    <span id="breadcrumbs-home">
        <a href="../../../index.html">Úvod</a>
        <span class="breadcrumbSeparator">
            /
            
        </span>
    </span>
    <span dir="ltr" id="breadcrumbs-1">
        
            <a href="../../../anselm.html">Anselm Cluster Documentation</a>
            <span class="breadcrumbSeparator">
                /
                
            </span>
            
         
    </span>
    <span dir="ltr" id="breadcrumbs-2">
        
            <a href="../../software.1.html">Software</a>
            <span class="breadcrumbSeparator">
                /
                
            </span>
            
         
    </span>
    <span dir="ltr" id="breadcrumbs-3">
        
            <a href="../numerical-languages.1.html">Numerical languages</a>
            <span class="breadcrumbSeparator">
                /
                
            </span>
            
         
    </span>
    <span dir="ltr" id="breadcrumbs-4">
        
            
            
            <span id="breadcrumbs-current">R</span>
         
    </span>

</div>
</div>

            
                <div class="">

                    

                    

    <dl class="portalMessage info" id="kssPortalMessage" style="display:none">
        <dt>Info</dt>
        <dd></dd>
    </dl>



                    
                        <div id="content">

                            

                            

                             <div id="viewlet-above-content-title"></div>
                             
                                 
                                 
            
                <h1 id="parent-fieldname-title" class="documentFirstHeading">
                    R
                </h1>
            
            
        
                             
                             <div id="viewlet-below-content-title">
</div>

                             
                                 
                                 
            
                
            
            
        
                             

                             <div id="viewlet-above-content-body">
<dl id="document-toc" class="portlet toc" style="display: none">
  <dt class="portletHeader">Obsah</dt>
  <dd class="portletItem">
  </dd>
</dl>
</div>
                             <div id="content-core">
                                 
    
        
        <div id="parent-fieldname-text-203c1335acdd4aa9897b00d5fc6c7a41" class="">
            <h2 class=" " id="parent-fieldname-title">Introduction</h2>
<p class=" ">The R is a language and environment for statistical computing and graphics.  R provides a wide variety of statistical (linear and nonlinear modelling, classical statistical tests, time-series analysis, classification, clustering, ...) and graphical techniques, and is highly extensible.</p>
<div id="content-core">
<div id="parent-fieldname-text-4a9d341ec7e54eecbc6b9c0d8f470ebc">
<p>One of R's strengths is the ease with which well-designed publication-quality plots can be produced, including mathematical symbols and formulae where needed. Great care has been taken over the defaults for the minor design choices in graphics, but the user retains full control.</p>
<p>Another convenience is the ease with which the C code or third party libraries may be integrated within R.</p>
<p>Extensive support for parallel computing is available within R.</p>
<p>Read more on <a class="external-link" href="http://www.r-project.org/" target="_self" title="">http://www.r-project.org/</a>, <a class="external-link" href="http://cran.r-project.org/doc/manuals/r-release/R-lang.html" target="_self" title="">http://cran.r-project.org/doc/manuals/r-release/R-lang.html</a></p>
<h2 class=" ">Modules</h2>
<p><strong></strong>The R version 3.0.1 is available on Anselm, along with GUI interface Rstudio</p>
<table class="listing">
<tbody>
<tr><th style="text-align: left; ">Application</th><th style="text-align: left; ">Version</th><th style="text-align: left; ">module</th></tr>
<tr class="even">
<td><strong>R</strong></td>
<td>R 3.0.1</td>
<td>R</td>
</tr>
<tr class="odd">
<td><strong>Rstudio</strong></td>
<td>Rstudio 0.97</td>
<td>Rstudio</td>
</tr>
</tbody>
</table>
<pre class=" ">$ module load R</pre>
<h2 class=" ">Execution</h2>
<p><a name="interactive-execution"></a>The R on Anselm is linked to highly optimized MKL mathematical library. This provides threaded parallelization to many R kernels, notably the linear algebra subroutines. The R runs these heavy calculation kernels without any penalty. By default, the R would parallelize to 16 threads. You may control the threads by setting the OMP_NUM_THREADS environment variable.</p>
<h3 class=" ">Interactive execution</h3>
<p>To run R interactively, using Rstudio GUI, log in with ssh -X parameter for X11 forwarding. Run rstudio:</p>
<pre class=" ">$ module load Rstudio<br />$ rstudio</pre>
<h3 class=" ">Batch execution</h3>
<p>To run R in batch mode, write an R script, then write a bash jobscript and execute via the qsub command. By default, R will use 16 threads when running MKL kernels.</p>
<p>Example jobscript:</p>
<pre class=" ">#!/bin/bash<br /><br /># change to local scratch directory<br />cd /lscratch/$PBS_JOBID || exit<br /><br /># copy input file to scratch <br />cp $PBS_O_WORKDIR/rscript.R .<br /><br /># load R module<br />module load R<br /><br /># execute the calculation<br />R CMD BATCH rscript.R routput.out<br /><br /># copy output file to home<br />cp routput.out $PBS_O_WORKDIR/.<br /><br />#exit<br />exit</pre>
<p>This script may be submitted directly to the PBS workload manager via the qsub command.  The inputs are in rscript.R file, outputs in routput.out file. See the single node jobscript example in the <a href="../../resource-allocation-and-job-execution/job-submission-and-execution.html" title="" target="_self" class="internal-link">Job execution section</a>.</p>
<h2 class=" ">Parallel R</h2>
<p>Parallel execution of R may be achieved in many ways. One approach is the implied parallelization due to linked libraries or specially enabled functions, as <a class="anchor-link" href="r.html#interactive-execution" target="_self" title="">described above</a>. In the following sections, we focus on explicit parallelization, where  parallel constructs are directly stated within the R script.</p>
<h2 class=" "><a name="package-parallel"></a>Package parallel</h2>
<p class=" ">The package parallel provides support for parallel computation, including by forking (taken from package multicore), by sockets (taken from package snow) and random-number generation.</p>
<p class=" ">The package is activated this way:</p>
<pre class=" ">$ R<br />&gt; library(parallel)</pre>
<p class=" ">More information and examples may be obtained directly by reading the documentation available in R</p>
<pre class=" ">&gt; ?parallel<br />&gt; library(help = "parallel")<br />&gt; <span class="internal-link">vignette("parallel")</span></pre>
<p class=" ">Download the package <a href="package-parallel-vignette" title="" target="_self" class="internal-link">parallell</a> vignette.</p>
<p class=" ">The forking is the most simple to use. Forking family of functions provide parallelized, drop in replacement for the serial apply() family of functions.</p>
<p class="callout">Forking via package parallel provides functionality similar to OpenMP construct<br />#omp parallel for <br /><br />Only cores of single node can be utilized this way!</p>
<p class=" ">Forking example:</p>
<pre class=" ">library(parallel)<br /><br />#integrand function<br />f &lt;- function(i,h) {<br />x &lt;- h*(i-0.5)<br />return (4/(1 + x*x))<br />}<br /><br />#initialize<br />size &lt;- detectCores()<br /><br />while (TRUE)<br />{<br />  #read number of intervals<br />  cat("Enter the number of intervals: (0 quits) ")<br />  fp&lt;-file("stdin"); n&lt;-scan(fp,nmax=1); close(fp)<br /><br />  if(n&lt;=0) break<br /><br />  #run the calculation<br />  n &lt;- max(n,size)<br />  h &lt;-   1.0/n<br /><br />  i &lt;- seq(1,n);<br />  pi3 &lt;- h*sum(simplify2array(mclapply(i,f,h,mc.cores=size)));<br /><br />  #print results<br />  cat(sprintf("Value of PI %16.14f, diff= %16.14f\n",pi3,pi3-pi))<br />}<br /><br /></pre>
<p>The above example is the classic parallel example for calculating the number π. Note the <strong>detectCores()</strong> and <strong>mclapply()</strong> functions.<br />Execute the example as:</p>
<pre class=" ">$ R --slave --no-save --no-restore -f pi3p.R</pre>
<p>Every evaluation of the integrad function runs in parallel on different process.</p>
<h2 class=" ">Package Rmpi</h2>
<p class="callout">package Rmpi provides an interface (wrapper) to MPI APIs.</p>
<p>It also provides interactive R slave environment. On Anselm, Rmpi provides interface to the <a href="../mpi-1/Running_OpenMPI.html" title="" target="_self" class="internal-link">OpenMPI</a>.</p>
<p>Read more on Rmpi at <a class="external-link" href="http://cran.r-project.org/web/packages/Rmpi/" target="_self" title="">http://cran.r-project.org/web/packages/Rmpi/</a>, reference manual is available at <a class="external-link" href="http://cran.r-project.org/web/packages/Rmpi/Rmpi.pdf" target="_self" title="">http://cran.r-project.org/web/packages/Rmpi/Rmpi.pdf</a></p>
<p>When using package Rmpi, both openmpi and R modules must be loaded</p>
<pre class=" ">$ module load openmpi<br />$ module load R</pre>
<p>Rmpi may be used in three basic ways. The static approach is identical to executing any other MPI programm. In addition, there is Rslaves dynamic MPI approach and the mpi.apply approach. In the following section, we will use the number π integration example, to illustrate all these concepts.</p>
<h3 class=" "><a name="static-rmpi"></a>static Rmpi</h3>
<p>Static Rmpi programs are executed via mpiexec, as any other MPI programs. Number of processes is static - given at the launch time.</p>
<p>Static Rmpi example:</p>
<pre class=" ">library(Rmpi)<br /><br />#integrand function<br />f &lt;- function(i,h) {<br />x &lt;- h*(i-0.5)<br />return (4/(1 + x*x))<br />}<br /><br />#initialize<br />invisible(mpi.comm.dup(0,1))<br />rank &lt;- mpi.comm.rank()<br />size &lt;- mpi.comm.size()<br />n&lt;-0<br /><br />while (TRUE)<br />{<br />  #read number of intervals<br />  if (rank==0) {<br />   cat("Enter the number of intervals: (0 quits) ")<br />   fp&lt;-file("stdin"); n&lt;-scan(fp,nmax=1); close(fp)<br />  }<br /><br />  #broadcat the intervals<br />  n &lt;- mpi.bcast(as.integer(n),type=1)<br /><br />  if(n&lt;=0) break<br /><br />  #run the calculation<br />  n &lt;- max(n,size)<br />  h &lt;-   1.0/n<br /><br />  i &lt;- seq(rank+1,n,size);<br />  mypi &lt;- h*sum(sapply(i,f,h));<br /><br />  pi3 &lt;- mpi.reduce(mypi)<br /><br />  #print results<br />  if (rank==0) cat(sprintf("Value of PI %16.14f, diff= %16.14f\n",pi3,pi3-pi))<br />}<br /><br />mpi.quit()</pre>
<p>The above is the static MPI example for calculating the number π. Note the <strong>library(Rmpi) </strong>and<strong> mpi.comm.dup()</strong> function calls.<br />Execute the example as:</p>
<pre class=" ">$ mpiexec R --slave --no-save --no-restore -f pi3.R</pre>
<h3 class=" ">dynamic Rmpi</h3>
<p>Dynamic Rmpi programs are executed by calling the R directly. openmpi module must be still loaded. The R slave processes will be spawned by a function call within the Rmpi program.</p>
<p>Dynamic Rmpi example:</p>
<pre class=" ">#integrand function<br />f &lt;- function(i,h) {<br />x &lt;- h*(i-0.5)<br />return (4/(1 + x*x))<br />}<br /><br />#the worker function<br />workerpi &lt;- function()<br />{<br />#initialize<br />rank &lt;- mpi.comm.rank()<br />size &lt;- mpi.comm.size()<br />n&lt;-0<br /><br />while (TRUE)<br />{<br />  #read number of intervals<br />  if (rank==0) {<br />   cat("Enter the number of intervals: (0 quits) ")<br />   fp&lt;-file("stdin"); n&lt;-scan(fp,nmax=1); close(fp)<br />  }<br /><br />  #broadcat the intervals<br />  n &lt;- mpi.bcast(as.integer(n),type=1)<br /><br />  if(n&lt;=0) break<br /><br />  #run the calculation<br />  n &lt;- max(n,size)<br />  h &lt;-   1.0/n<br /><br />  i &lt;- seq(rank+1,n,size);<br />  mypi &lt;- h*sum(sapply(i,f,h));<br /><br />  pi3 &lt;- mpi.reduce(mypi)<br /><br />  #print results<br />  if (rank==0) cat(sprintf("Value of PI %16.14f, diff= %16.14f\n",pi3,pi3-pi))<br />}<br />}<br /><br />#main<br />library(Rmpi)<br /><br />cat("Enter the number of slaves: ")<br />fp&lt;-file("stdin"); ns&lt;-scan(fp,nmax=1); close(fp)<br /><br />mpi.spawn.Rslaves(nslaves=ns)<br />mpi.bcast.Robj2slave(f)<br />mpi.bcast.Robj2slave(workerpi)<br /><br />mpi.bcast.cmd(workerpi())<br />workerpi()<br /><br /><br />mpi.quit()</pre>
<p>The above example is the dynamic MPI example for calculating the number π. Both master and slave processes carry out the calculation. Note the <strong>mpi.spawn.Rslaves(), mpi.bcast.Robj2slave() </strong>and the<strong> mpi.bcast.cmd()</strong> function calls.<br />Execute the example as:</p>
<pre class=" ">$ R --slave --no-save --no-restore -f pi3Rslaves.R</pre>
<h3 class=" ">mpi.apply Rmpi</h3>
<p>mpi.apply is a specific way of executing Dynamic Rmpi programs.</p>
<p class="callout">mpi.apply() family of functions provide MPI parallelized, drop in replacement for the serial apply() family of functions.</p>
<p>Execution is identical to other dynamic Rmpi programs.</p>
<p>mpi.apply Rmpi example:</p>
<pre class=" ">#integrand function<br />f &lt;- function(i,h) {<br />x &lt;- h*(i-0.5)<br />return (4/(1 + x*x))<br />}<br /><br />#the worker function<br />workerpi &lt;- function(rank,size,n)<br />{<br />  #run the calculation<br />  n &lt;- max(n,size)<br />  h &lt;- 1.0/n<br /><br />  i &lt;- seq(rank,n,size);<br />  mypi &lt;- h*sum(sapply(i,f,h));<br /><br />  return(mypi)<br />}<br /><br />#main<br />library(Rmpi)<br /><br />cat("Enter the number of slaves: ")<br />fp&lt;-file("stdin"); ns&lt;-scan(fp,nmax=1); close(fp)<br /><br />mpi.spawn.Rslaves(nslaves=ns)<br />mpi.bcast.Robj2slave(f)<br />mpi.bcast.Robj2slave(workerpi)<br /><br />while (TRUE)<br />{<br />  #read number of intervals<br />  cat("Enter the number of intervals: (0 quits) ")<br />  fp&lt;-file("stdin"); n&lt;-scan(fp,nmax=1); close(fp)<br />  if(n&lt;=0) break<br /><br />  #run workerpi<br />  i=seq(1,2*ns)<br />  pi3=sum(mpi.parSapply(i,workerpi,2*ns,n))<br /><br />  #print results<br />  cat(sprintf("Value of PI %16.14f, diff= %16.14f\n",pi3,pi3-pi))<br />}<br /><br />mpi.quit()</pre>
<p>The above is the mpi.apply MPI example for calculating the number π. Only the slave processes carry out the calculation. Note the <strong>mpi.parSapply(), </strong> function call. The package <span class="anchor-link">parallel <a class="anchor-link" href="r.html#package-parallel" target="_self" title="">example</a></span><a class="anchor-link" href="r.html#package-parallel" target="_self" title=""> above</a> may be trivially adapted (for much better performance) to this structure using the mclapply() in place of mpi.parSapply().</p>
<p>Execute the example as:</p>
<pre class=" ">$ R --slave --no-save --no-restore -f pi3parSapply.R</pre>
<h2 class=" ">Combining parallel and Rmpi</h2>
<p>Currently, the two packages can not be combined for hybrid calculations.</p>
<h2 class=" ">Parallel execution</h2>
<p>The R parallel jobs are executed via the PBS queue system exactly as any other parallel jobs. User must create an appropriate jobscript and submit via the <strong>qsub</strong></p>
<p>Example jobscript for <a class="anchor-link" href="r.html#static-rmpi" target="_self" title="">static Rmpi</a> parallel R execution, running 1 process per core:</p>
<pre class=" ">#!/bin/bash<br />#PBS -q qprod<br />#PBS -N Rjob<br />#PBS -l select=100:ncpus=16:mpiprocs=16:ompthreads=1<br /><br /># change to  scratch directory<span class="pln"><br />SCRDIR</span><span class="pun">=/</span><span class="pln">scratch</span><span class="pun">/</span><span class="pln">$USER</span><span class="pun">/</span><span class="pln">myjob</span><br /><span class="pln">cd $SCRDIR </span><span class="pun">||</span><span class="pln"> exit</span><br /><br /># copy input file to scratch <br />cp $PBS_O_WORKDIR/rscript.R .<br /><br /># load R and openmpi module<br />module load R<br />module load openmpi<br /><br /># execute the calculation<br />mpiexec -bycore -bind-to-core R --slave --no-save --no-restore -f rscript.R<br /><br /># copy output file to home<br />cp routput.out $PBS_O_WORKDIR/.<br /><br />#exit<br />exit</pre>
For more information about jobscripts and MPI execution refer to the <a href="../../resource-allocation-and-job-execution/job-submission-and-execution.html" title="" target="_self" class="internal-link">Job submission</a> and general <a href="../mpi-1.html" title="" target="_self" class="internal-link">MPI</a> sections.</div>
</div>
            
        </div>
    

    <!-- <div tal:content="structure here/@@rate-if-you-dare"/> -->

    <div id="plone-discussions">
        <hr />
        <b><a id="plone-discussions-href" href="r.html#plone-discussions">Komentáře</a></b>
    </div>

    <!-- <div id="contact-form" tal:condition="context/isDiscussable">
        <hr />
        <b><a id="contact-form-href" href="#contact-form" i18n:translate="">Contact form</a></b>
        <tal:contactform tal:content="structure python:getattr(context, 'contact-info')()"></tal:contactform>
    </div> -->

    

                             </div>
                             <div id="viewlet-below-content-body">



    <div class="visualClear"><!-- --></div>

    <div class="documentActions">
        

        

    </div>

</div>

                            
                        </div>
                    

                    
                </div>
            

            <div id="viewlet-below-content">






    <div class="reply">
        <form action="https://docs.it4i.cz/login_form?came_from=https%3A//docs.it4i.cz/anselm-cluster-documentation/software/numerical-languages/r/document_view">
            <input class="standalone loginbutton" type="submit" value="Pro přidávání komentářů je nutné se přihlásit." />
        </form>
    </div>

    

    

    


</div>
        </div>

        
        <div id="portal-column-one" class="cell width-1:4 position-0">
            
                
<div class="portletWrapper" data-portlethash="706c6f6e652e6c656674636f6c756d6e0a636f6e746578740a2f646f63730a6e617669676174696f6e" id="portletwrapper-706c6f6e652e6c656674636f6c756d6e0a636f6e746578740a2f646f63730a6e617669676174696f6e">
<dl class="portlet portletNavigationTree">

    <dt class="portletHeader hiddenStructure">
        <span class="portletTopLeft"></span>
        <a href="../../../sitemap.html" class="tile">Navigace</a>
        <span class="portletTopRight"></span>
    </dt>

    <dd class="portletItem lastItem">
        <ul class="navTree navTreeLevel0">
            
            



<li class="navTreeItem visualNoMarker navTreeFolderish section-whats-new">

    


        <a href="../../../whats-new.html" title="" class="state-published navTreeFolderish contenttype-folder">
            
            <span>What's New</span>
        </a>

        

    
</li>


<li class="navTreeItem visualNoMarker navTreeFolderish section-get-started-with-it4innovations">

    


        <a href="../../../index.html" title="" class="state-published navTreeFolderish contenttype-folder">
            
            <span>Get Started with IT4Innovations</span>
        </a>

        

    
</li>


<li class="navTreeItem visualNoMarker navTreeFolderish section-salomon">

    


        <a href="../../../salomon.html" title="" class="state-published navTreeFolderish contenttype-folder">
            
            <span>Salomon Cluster Documentation</span>
        </a>

        

    
</li>


<li class="navTreeItem visualNoMarker navTreeItemInPath navTreeFolderish section-anselm-cluster-documentation">

    


        <a href="../../../anselm.html" title="" class="state-published navTreeItemInPath navTreeFolderish contenttype-folder">
            
            <span>Anselm Cluster Documentation</span>
        </a>

        
            <ul class="navTree navTreeLevel1">
                



<li class="navTreeItem visualNoMarker section-hardware-overview">

    


        <a href="../../hardware-overview.html" title="" class="state-published contenttype-document">
            
            <span>Hardware Overview</span>
        </a>

        

    
</li>


<li class="navTreeItem visualNoMarker navTreeFolderish section-accessing-the-cluster">

    


        <a href="../../accessing-the-cluster.html" title="" class="state-published navTreeFolderish contenttype-folder">
            
            <span>Accessing the Cluster</span>
        </a>

        

    
</li>


<li class="navTreeItem visualNoMarker section-environment-and-modules">

    


        <a href="../../environment-and-modules.html" title="" class="state-published contenttype-document">
            
            <span>Environment and Modules</span>
        </a>

        

    
</li>


<li class="navTreeItem visualNoMarker navTreeFolderish section-resource-allocation-and-job-execution">

    


        <a href="../../resource-allocation-and-job-execution.html" title="" class="state-published navTreeFolderish contenttype-folder">
            
            <span>Resource Allocation and Job Execution</span>
        </a>

        

    
</li>


<li class="navTreeItem visualNoMarker section-compute-nodes">

    


        <a href="../../compute-nodes.html" title="" class="state-published contenttype-document">
            
            <span>Compute Nodes</span>
        </a>

        

    
</li>


<li class="navTreeItem visualNoMarker navTreeFolderish section-storage-1">

    


        <a href="../../storage-1.html" title="" class="state-published navTreeFolderish contenttype-folder">
            
            <span>Storage</span>
        </a>

        

    
</li>


<li class="navTreeItem visualNoMarker section-network">

    


        <a href="../../network.html" title="" class="state-published contenttype-document">
            
            <span>Network</span>
        </a>

        

    
</li>


<li class="navTreeItem visualNoMarker navTreeItemInPath navTreeFolderish section-software">

    


        <a href="../../software.1.html" title="In this section we provide overview of installed software and its usage.


" class="state-published navTreeItemInPath navTreeFolderish contenttype-folder">
            
            <span>Software</span>
        </a>

        
            <ul class="navTree navTreeLevel2">
                



<li class="navTreeItem visualNoMarker navTreeFolderish section-ansys">

    


        <a href="../ansys.html" title="An engineering simulation software " class="state-published navTreeFolderish contenttype-folder">
            
            <span>ANSYS</span>
        </a>

        

    
</li>


<li class="navTreeItem visualNoMarker navTreeFolderish section-comsol">

    


        <a href="../comsol.html" title=" A finite element analysis, solver and Simulation software" class="state-published navTreeFolderish contenttype-folder">
            
            <span>COMSOL</span>
        </a>

        

    
</li>


<li class="navTreeItem visualNoMarker navTreeFolderish section-debuggers">

    


        <a href="../debuggers.1.html" title="A collection of development tools" class="state-published navTreeFolderish contenttype-folder">
            
            <span>Debuggers and profilers</span>
        </a>

        

    
</li>


<li class="navTreeItem visualNoMarker navTreeFolderish section-chemistry">

    


        <a href="../chemistry.html" title="Tools for computational chemistry." class="state-published navTreeFolderish contenttype-folder">
            
            <span>Chemistry and Materials science</span>
        </a>

        

    
</li>


<li class="navTreeItem visualNoMarker navTreeFolderish section-intel-suite">

    


        <a href="../intel-suite.html" title="The Intel Parallel Studio XE" class="state-published navTreeFolderish contenttype-folder">
            
            <span>Intel Parallel studio</span>
        </a>

        

    
</li>


<li class="navTreeItem visualNoMarker navTreeFolderish section-mpi-1">

    


        <a href="../mpi-1.html" title="Message Passing Interface libraries on ANSELM" class="state-published navTreeFolderish contenttype-folder">
            
            <span>MPI</span>
        </a>

        

    
</li>


<li class="navTreeItem visualNoMarker navTreeFolderish section-numerical-libraries">

    


        <a href="../numerical-libraries.html" title="Libraries for numerical computations" class="state-published navTreeFolderish contenttype-folder">
            
            <span>Numerical Libraries</span>
        </a>

        

    
</li>


<li class="navTreeItem visualNoMarker navTreeItemInPath navTreeFolderish section-numerical-languages">

    


        <a href="../numerical-languages.1.html" title="Interpreted languages for numerical computations" class="state-published navTreeItemInPath navTreeFolderish contenttype-folder">
            
            <span>Numerical languages</span>
        </a>

        
            <ul class="navTree navTreeLevel3">
                



<li class="navTreeItem visualNoMarker section-copy_of_matlab">

    


        <a href="copy_of_matlab.html" title="" class="state-published contenttype-document">
            
            <span>Matlab</span>
        </a>

        

    
</li>


<li class="navTreeItem visualNoMarker section-matlab">

    


        <a href="matlab.html" title="" class="state-published contenttype-document">
            
            <span>Matlab 2013-2014</span>
        </a>

        

    
</li>


<li class="navTreeItem visualNoMarker section-octave">

    


        <a href="octave.html" title="" class="state-published contenttype-document">
            
            <span>Octave</span>
        </a>

        

    
</li>


<li class="navTreeItem visualNoMarker navTreeCurrentNode section-r">

    


        <a href="r.html" title="" class="state-published navTreeCurrentItem navTreeCurrentNode contenttype-document">
            
            <span>R</span>
        </a>

        

    
</li>




            </ul>
        

    
</li>


<li class="navTreeItem visualNoMarker navTreeFolderish section-virtualization">

    


        <a href="../virtualization.html" title="" class="state-published navTreeFolderish contenttype-folder">
            
            <span>Virtualization</span>
        </a>

        

    
</li>


<li class="navTreeItem visualNoMarker section-compilers">

    


        <a href="../compilers.html" title="Available compilers, including GNU, INTEL and UPC compilers" class="state-published contenttype-document">
            
            <span>Compilers</span>
        </a>

        

    
</li>


<li class="navTreeItem visualNoMarker section-intel-xeon-phi">

    


        <a href="../intel-xeon-phi.html" title="A guide to Intel Xeon Phi  usage" class="state-published contenttype-document">
            
            <span>Intel Xeon Phi</span>
        </a>

        

    
</li>


<li class="navTreeItem visualNoMarker section-isv_licenses">

    


        <a href="../isv_licenses.html" title="A guide to managing Independent Software Vendor licences" class="state-published contenttype-document">
            
            <span>ISV Licenses</span>
        </a>

        

    
</li>


<li class="navTreeItem visualNoMarker section-java">

    


        <a href="../java.html" title="Java on ANSELM" class="state-published contenttype-document">
            
            <span>Java</span>
        </a>

        

    
</li>


<li class="navTreeItem visualNoMarker section-nvidia-cuda">

    


        <a href="../nvidia-cuda.html" title="A guide to nVidia CUDA programming and GPU usage" class="state-published contenttype-document">
            
            <span>nVidia CUDA</span>
        </a>

        

    
</li>


<li class="navTreeItem visualNoMarker navTreeFolderish section-omics-master-1">

    


        <a href="../omics-master-1.html" title="" class="state-published navTreeFolderish contenttype-folder">
            
            <span>OMICS Master</span>
        </a>

        

    
</li>


<li class="navTreeItem visualNoMarker section-openfoam">

    


        <a href="../openfoam.html" title="A free, open source CFD software package" class="state-published contenttype-document">
            
            <span>OpenFOAM</span>
        </a>

        

    
</li>


<li class="navTreeItem visualNoMarker section-operating-system">

    


        <a href="../operating-system.html" title="The operating system, deployed on ANSELM" class="state-published contenttype-document">
            
            <span>Operating System</span>
        </a>

        

    
</li>


<li class="navTreeItem visualNoMarker section-paraview">

    


        <a href="../paraview.html" title="An open-source, multi-platform data analysis and visualization application " class="state-published contenttype-document">
            
            <span>ParaView</span>
        </a>

        

    
</li>


<li class="navTreeItem visualNoMarker section-gpi2">

    


        <a href="../gpi2.html" title="A library that implements the GASPI specification" class="state-published contenttype-document">
            
            <span>GPI-2</span>
        </a>

        

    
</li>




            </ul>
        

    
</li>


<li class="navTreeItem visualNoMarker section-remote-visualization">

    


        <a href="../../remote-visualization.html" title="" class="state-published contenttype-document">
            
            <span>Remote visualization service</span>
        </a>

        

    
</li>


<li class="navTreeItem visualNoMarker section-prace">

    


        <a href="../../prace.html" title="" class="state-published contenttype-document">
            
            <span>PRACE User Support</span>
        </a>

        

    
</li>




            </ul>
        

    
</li>


<li class="navTreeItem visualNoMarker section-links">

    


        <a href="../../../links.html" title="" class="state-published contenttype-document">
            
            <span>Links</span>
        </a>

        

    
</li>


<li class="navTreeItem visualNoMarker section-changelog">

    


        <a href="../../../changelog.html" title="" class="state-published contenttype-collection">
            
            <span>Changelog</span>
        </a>

        

    
</li>


<li class="navTreeItem visualNoMarker navTreeFolderish section-pbspro-documentation">

    


        <a href="../../../pbspro-documentation.html" title="PBSPro Quick Start Guide, User's Guide, Reference Guide, Programmers Guide" class="state-published navTreeFolderish contenttype-folder">
            
            <span>PBSPro Documentation</span>
        </a>

        

    
</li>




        </ul>
        <span class="portletBottomLeft"></span>
        <span class="portletBottomRight"></span>
    </dd>
</dl>

</div>




            
        </div>

        
        
    </div>

    <div id="portal-footer-wrapper" class="row">
        <div class="cell width-full position-0">
            <!-- Piwik -->
<script type="text/javascript">
var _paq = _paq || [];
_paq.push(['trackPageView']);
_paq.push(['enableLinkTracking']);
(function() {
var u="//extranet.it4i.cz/piwik/";
_paq.push(['setTrackerUrl', u+'piwik.php']);
_paq.push(['setSiteId', 5]);
var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
g.type='text/javascript'; g.async=true; g.defer=true; g.src=u+'piwik.js'; s.parentNode.insertBefore(g,s);
})();
</script>
<noscript><p><img src="https://extranet.it4i.cz/piwik/piwik.php?idsite=5" style="border:0;" alt="" /></p></noscript>
<!-- End Piwik Code -->
<!-- nagios_check_http_expect_string -->

<div id="viewlet-cookiepolicy" style="display:none">
    <h1 id="tlspu_cookiepolicy_title">This Site Uses Cookies</h1>
    <p id="tlspu_cookiepolicy_message">We may use cookies to record some preference settings and to analyse how you use our web site. We may also use external analysis systems which may set additional cookies to perform their analysis. These cookies are integral to our web site. You can delete or disable these cookies in your web browser if you wish but then our site may not work correctly.</p>
    <form id="tlspu_cookiepolicy_form">
    <input type="checkbox" id="tlspu_cookiepolicy_agreed" value="confirmed" />
    <label for="tlspu_cookiepolicy_agreed">I have read and understood this message.</label>
    <button disabled="disabled" id="tlspu_cookiepolicy_button" value="hide">Hide this message</button>
    </form>
</div>

<script type="text/javascript">
/*
*/
</script>

<div id="portal-footer">
          <p>
               <a href="http://www.it4i.cz/?lang=en">IT4Innovations</a>
               <a href="../../../anselm.html">Dokumentace pro Anselm</a>
               <a href="https://events.it4i.cz">Events</a>
               <a href="https://extranet.it4i.cz">Extranet</a>
               <a href="http://prace.it4i.cz/">HPC & PRACE</a>
               <a href="http://study.it4i.cz/">HPC Study</a>
               <a href="../../../salomon.html">Dokumentace pro Salomon</a>
               <a href="http://industry.it4i.cz/en/">Superpočítání pro průmysl</a>
               <a href="mailto:support [at] it4i.cz">support [at] it4i.cz</a>
          </p>
          <!-- nagios_check_http_expect_string -->
</div>

        </div>
    </div>

    <!-- TODO -->
    <!-- nagios_check_http_expect_string -->
    <!-- TODO -->

</div>
</body>
</html>