Code Snippets for Parallel.For and Parallel.ForEach

Code Snippets for Parallel.For and Parallel.ForEach

Rate This
  • Comments 9

Introduced in Visual Studio 2005, Code Snippets allow you to quickly insert reusable blocks of code into your project.  For example, if you want to quickly write a for loop in C#, you can simply type "for" into your code file, and IntelliSense shows you the "for" code snippet:

image

Now you press the tab key twice, and Visual Studio automatically expands this for you into a for loop, allowing you to quickly replace things like the iteration variable's name:

image

One of the coolest things about Code Snippets, however, is that you can write your own.  Imagine being able to type "parall" and have a Code Snippet for Parallel.For pop up:

image

When you expand this, you'd get a Parallel.For loop:

image

"Magic," you say?  Not quite.  All you have to do is write a custom snippet that looks as follows:

<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
    <CodeSnippet Format="1.0.0">
        <Header>
            <Title>parallelfor</Title>
            <Shortcut>parallelfor</Shortcut>
            <Description>Code snippet for 'Parallel.For' loop</Description>
            <Author>Microsoft Corporation</Author>
            <SnippetTypes>
                <SnippetType>Expansion</SnippetType>
                <SnippetType>SurroundsWith</SnippetType>
            </SnippetTypes>
        </Header>
        <Snippet>
            <Declarations>
                <Literal>
                    <ID>index</ID>
                    <Default>i</Default>
                    <ToolTip>Index</ToolTip>
                </Literal>
        <Literal>
          <ID>from</ID>
          <Default>from</Default>
          <ToolTip>From</ToolTip>
        </Literal>
                <Literal>
                    <ID>to</ID>
                    <Default>to</Default>
                    <ToolTip>To</ToolTip>
                </Literal>
            </Declarations>
            <Code Language="csharp"><![CDATA[Parallel.For($from$, $to$, $index$ =>
            {
            $selected$ $end$
            });]]>
            </Code>
        </Snippet>
    </CodeSnippet>
</CodeSnippets>

Save that to a file parallelfor.snippet in a directory of your choosing.  In Visual Studio, go to Tools | Code Snippet Manager..., add the directory into which you stored the snippet, and you'll be off and running.

The parallelfor snippet file as well as another example for parallelforeach are attached to this blog post.  Are there any other useful snippets you'd be interested in having related to parallel programming in general or specifically to Parallel Extensions?

Enjoy!

Attachment: ParallelSnippets.zip
Leave a Comment
  • Please add 4 and 8 and type the answer here:
  • Post
  • Sweet!

  • Oh, I forgot. Maybe one or two for using the TPL.

  • PingBack from http://msdnrss.thecoderblogs.com/2007/12/10/code-snippets-for-parallelfor-and-parallelforeach/

  • Sadly, snippets were a feature of 2005 that I never got around to using (the default ones didn't format like I do, and I didn't bother to learn the format/deployment process). This might change my mind, though.

    Now, if I ever see "Concurrency Rules/Suggestions for FxCop/Code Analysis", you folks will make my year! (yeah, yeah, it's a pipe dream, but the abstractions .Net is starting to provide are opening things up to some very interesting analyses)

  • Thanks, Keith.  We're certainly interested in static analysis rules/tooling for concurrency.  If there are any particular patterns you'd be interested in seeing rules for, please do let us know.

  • I'll put more detailed ideas on the forums (or is Connect feedback better?), but things that come to mind off the top of my head are:

    - wrap loops in Parallel./PLINQ if internal uses local memory/calls static methods

    - difficult heuristic, but use "PLINQ if possible for enhancement-by-abstraction" if some lower-level PFX is being used when PLINQ would do

    - any kind of means for detecting unnecessary use of shared memory when localized & threaded would be better

    - just about any generally-accepted algorithms for determining when some programming construct can be threaded

    And to be honest, I've never actually written a custom FxCop rule (the one rule I've wanted for years going back to C++ requires FxCop integraton with the XML dox), so I'm not sure the extent to which this stuff is even possible.

  • Thanks for the suggestions, Keith.  A forum discussion on the topic would be a great thing to start.

  • PingBack from http://www.fooo.info/2008/02/07/code-snippet-pour-parallelfor-et-parallelforeach/

  • Thanks Stephen for providing the snippets.

Page 1 of 1 (9 items)