Parallel Programming in Native Code

Parallel programming using C++ AMP, PPL and Agents libraries.

Visual Studio 2010 CTP available: Including the Concurrency Runtime, Parallel Pattern Library and Asynchronous Agents Library!

Visual Studio 2010 CTP available: Including the Concurrency Runtime, Parallel Pattern Library and Asynchronous Agents Library!

  • Comments 9

In his blog post on Monday, Soma mentioned some of the great things happening at PDC 2008 and also announced the Visual Studio 2010 CTP.  We’re very excited to announce that this CTP includes the Concurrency Runtime, the Parallel Pattern and Asynchronous Agents Libraries for C++ developers that we’ve mentioned here and on channel9 previously. 

 

He also announced that the Visual Studio 2010 and .NET 4.0 include the Task Parallel Library, PLINQ  and includes parallel profiling and debugging experiences that support both native and managed code.

 

The CTP is available as a VPC image and is available here:  

https://connect.microsoft.com/VisualStudio/content/content.aspx?ContentID=9790

Getting started with the PPL and Agents Library: the walkthrough

Once you have the CTP running, getting started is relatively straightforward.  Launch the Visual Studio 2010 CTP and click the link to the walkthroughs.  There is one provided which shows  a very simple example of using the Parallel Pattern Library and the Agents Library.   

 

Here’s a snippet of that walkthrough, where a parallel_for is used to concurrently check a series of numbers for primality and then sends the prime numbers as a message to an agent. The agent in this example is responsible for inserting them into a vector.

 

What’s important to take away from this example is how the agents functionality (send, receive and the unbounded_buffer) are used in conjunction with the parallel_for to insert data into a std::vector, which isn’t typically threadsafe.  We accomplish this by separating the shared state and in this case the computationally expensive work (IsPrime is slow J) from the less expensive insert operation.  It’s also interesting to note, that It may be also useful to just leave those messages in the unbounded_buffer since it supports enqueue and dequeue operations, but in this case we’re assuming there is a need for the random access iteration that std::vector provides.

 

Here’s the virtual ‘run’ method in the derived ConsumerAgent class below which waits for messages being sent to a message block and then inserts them into a std::vector v:

    virtual bool run(){

        int curValue;

        while(true){

            curValue = receive(buf_);

            //use -1 to signal exit

            if (curValue == -1)

                break;

            v_.push_back(curValue);

        };

        return true;

    };

 

And here’s the code which iterates over the range of numbers and sends messages for the agent to process. Note that I’m using a C++ lambda for the body of the loop.

 

    std::vector<int> v;

    unbounded_buffer<int> intBuf;

 

    ConsumerAgent myAgent(v,intBuf);

    myAgent.start();

 

    parallel_for(1,count,1,[&](int i){

        if (IsPrime(i))

            send(intBuf,i);

    });

    //use -1 to signal exit

    send(intBuf,-1);

    myAgent.wait();

 

    printf("Found %d prime numbers in parallel\n",v.size());

 

The full example is provided in the walkthrough.

What’s in the CTP: Parallel Pattern Library, Asynchronous Agents Library, Concurrency Runtime

The classes and APIs in the Parallel Pattern Library and Asynchronous Agents Library are in the header files ppl.h and agents.h respectively and all of the APIs are currently in the Concurrency namespace.   The Concurrency Runtime itself and the load-balancing scheduler are included as binary code and part of msvcr100.dll in the CTP.  Here’s a brief list of the header files and major APIs in each one.

 

ppl.h: includes parallel_for, parallel_for_each, parallel_invoke, task_group and task_handle

 

agents.h: includes unbounded_buffer<T>, overwrite_buffer<T>, and the send and receive helper methods. call<T> and transform<Input,Output> and the abstract agent class are also included.

 

concrt.h: includes APIs for interacting with the scheduler.  Note that that it isn’t necessary to interact with the scheduler when you use the APIs and classes in ppl.h or agents.h an implicit scheduler will be created on demand.

 

I’ll try to take some time over the next couple of days to provide a few examples using each of these APIs on this blog and if you’re at the PDC this week, stop by the Parallel Computing Platform Teams’ booth.  We’d love to hear from you.

 

-Rick

Blog - Comment List MSDN TechNet
  • Loading...
Leave a Comment
  • Please add 2 and 4 and type the answer here:
  • Post