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}
Related videos on Youtube
Author by
Jamie
Updated on October 02, 2020Comments
-
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 over 12 yearsDoesn't stating it is a parallel loop nullify the pseudo aspect of
algorithmic
? Do you perhaps needlistings
or some variant for code inclusion? -
Jamie over 12 yearsHow about the pseudocodes of parallel algorithms? These are not the implementations (listings) but the concepts.
-
Pieter over 12 yearsI 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}}
.
-