How do I add parfor in algorithmic environment?

5,028

You can use the algorithmicx package to define a new block that behaves as the standard \For loop; a little example (using algpseudocode):

\documentclass{article}
\usepackage{algpseudocode}

\begin{document}

% declaration of the new block
\algblock{ParFor}{EndParFor}
% customising the new block
\algnewcommand\algorithmicparfor{\textbf{parfor}}
\algnewcommand\algorithmicpardo{\textbf{do}}
\algnewcommand\algorithmicendparfor{\textbf{end\ parfor}}
\algrenewtext{ParFor}[1]{\algorithmicparfor\ #1\ \algorithmicpardo}
\algrenewtext{EndParFor}{\algorithmicendparfor}

\begin{algorithmic}[1]
\For{$i\gets 1, n$}
  \State $sum\gets sum+i$
\EndFor
\ParFor{$j\gets 1, m$}
  \State $prod\gets prod*j$
  \ParFor{$k\gets 1, p$}
    \State $fact\gets fact*k$
  \EndParFor
\EndParFor
\end{algorithmic}

\end{document}

Share:
5,028

Related videos on Youtube

Jamie
Author by

Jamie

Updated on October 02, 2020

Comments

  • Jamie
    Jamie about 3 years

    I want to have a parfor loop in my algorithm listing (it should work exactly as standard for loop works). How to either change the displayed name of for loop or add my own loop which will keep the indents?

    • Pieter
      Pieter over 12 years
      Doesn't stating it is a parallel loop nullify the pseudo aspect of algorithmic? Do you perhaps need listings or some variant for code inclusion?
    • Jamie
      Jamie over 12 years
      How about the pseudocodes of parallel algorithms? These are not the implementations (listings) but the concepts.
    • Pieter
      Pieter over 12 years
      I consider parfor to be a Matlab feature, with a certain set of conditions on side-effects etc. An algorithm doesn't become a parallel algorithm just because some loop is executed in parallel. A (bad) solution: edit algorithmic.sty, adding ` \newcommand{\PARFOR}[2][default]{\ALC@it\algorithmicparfor\ ##2\ \algorithmicdo% \ALC@com{##1}\begin{ALC@for}}` and \newcommand{\algorithmicparfor}{\textbf{parfor}}.