Archive for the ‘ C/C++/C# Programming ’ Category

Blocking Collection and the Producer-Consumer Problem

This time I want to discuss features that belong to the new System.Collections.Concurrent namespace in the.NET Framework 4. When you design parallel applications, you often need thread-safe data storage as well as some mechanism of sending messages between tasks. Once again, this post will touch on just the basics and the most common problems a beginner might encounter, but I’ll provide links for further reading.

This is the fourth post in the parallel programming series. Here’s a list of all the posts:

To keep things short, I’ll start with the code that I have at the end of the Task Schedulers and Synchronization Context post. This is a small parallel WPF application with a responsive UI that has one Start button and displays the results of long-running operations in a text box.

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    public static double SumRootN(int root)
    {
        double result = 0;
        for (int i = 1; i < 10000000; i++)
        {
            result += Math.Exp(Math.Log(i) / root);
        }
        return result;
    }

    private void start_Click(object sender, RoutedEventArgs e)
    {
        textBlock1.Text = "";
        label1.Content = "Milliseconds: ";

        var watch = Stopwatch.StartNew();
        List<Task> tasks = new List<Task>();
        var ui = TaskScheduler.FromCurrentSynchronizationContext();
        for (int i = 2; i < 20; i++)
        {
            int j = i;
            var compute = Task.Factory.StartNew(() =>
            {
                return SumRootN(j);
            });
            tasks.Add(compute);

            var display = compute.ContinueWith(resultTask =>
                            textBlock1.Text += "root " + j.ToString() + " " +
                                               compute.Result.ToString() +
                                               Environment.NewLine,
                                ui);
        }

        Task.Factory.ContinueWhenAll(tasks.ToArray(),
            result =>
            {
                var time = watch.ElapsedMilliseconds;
                label1.Content += time.ToString();
            }, CancellationToken.None, TaskContinuationOptions.None, ui);
    }
}

But imagine that I’m designing a larger application and I need to store the results of the long-running parallel operations somewhere. (I don’t do this in the current version at all.)

Until .NET Framework 4, this task was challenging for C# developers: the collections in the System.Collections and System.Collections.Generic namespaces do not guarantee thread safety, and developers needed to design the locking and synchronization mechanisms themselves. But now generic thread-safe collections are a part of the .NET Framework. So, let me introduce the new namespace: System.Collections.Concurrent.

I’m going to use the BlockingCollection<T> class. This class can help you implement the well-known producer-consumer pattern, where items are produced and consumed by different operations at different rates. I will update my application to imitate the producer-consumer scenario, so that the compute task will become a producer, and the display task will become a consumer.

var results = new BlockingCollection<double>();

I’ll also update the compute task, so that instead of returning the result, it will add it to the results collection.

var compute = Task.Factory.StartNew(() =>
{
    results.Add(SumRootN(j));
});

When all the results are ready, I’ll set the collection to the “completed” state, so the consumer will know that this collection won’t be updated anymore. For this purpose, I’ll call the CompleteAdding method, which will set the IsCompleted property of the results collection to true. The good place to perform this operation is the task that calculates the total time: it waits for all other tasks to finish, which is exactly what I need.

Task.Factory.ContinueWhenAll(tasks.ToArray(),
    result =>
    {
        results.CompleteAdding();
        var time = watch.ElapsedMilliseconds;
        label1.Content += time.ToString();
    }, CancellationToken.None, TaskContinuationOptions.None, ui);

As you can see, the producer part is easy: all tasks can safely write to the results collection, and all locking and synchronization issues are managed by the .NET Framework and TPL.

Now let’s move to the consumer side. I’ll do a small refactoring: I’ll convert the display task into a consume task that will run the display method:

var consume = Task.Factory.StartNew(() => display(results));

I want to start the consume task before I start any of the compute tasks so that the consumer can wait for the producer and I can see the real-time results. That’s why I put the above line right before the main for loop in the button’s event handler.

This is what the naïve first version of the display method might look like. (Don’t forget to convert the ui task scheduler into a field. It’s a local variable in the original code.)

public void display(BlockingCollection<double> results)
{
    double item;
    while (!results.IsCompleted)
    {
        while (!results.TryTake(out item));
        double currentItem = item;
        Task.Factory.StartNew(new Action(() =>
                  textBlock1.Text += currentItem.ToString() + Environment.NewLine),
             CancellationToken.None, TaskCreationOptions.None, ui);
    }
}

This method checks for new elements in the collection, until it’s notified that the collection is completed, which means that it has finished adding new items. If it gets a new element from the collection, it immediately removes the item and prints the value into the UI threads. Did you notice that I copied item to currentItem? It’s all about closure again: you’ll get a list of zero’s otherwise.

This version works and you won’t get any exceptions. But if you run it on a dual-core computer like I did, you’ll discover that it takes twice as long as the version that doesn’t use the collection. In fact, it runs as if the application weren’t parallelized at all! This is just one of the problems that you might run into, so don’t forget to always measure the performance of your parallel applications: it’s easy to cancel out the benefits of parallelization.

Of course, the problem is in this line:

while (!results.TryTake(out item));

An empty loop is rarely a good idea. This was an attempt to implement some kind of messaging between the threads – the consumer is constantly checking the collection and starts working only if it can retrieve a value from it. And it does this over and over again, so one of my processors is fully occupied with this work and can’t compute the values anymore.

One simple trick is to make this loop to consume less processing power. It can be as easy as this (however, this is not a recommended way, but rather an illustration of the principle):

while (!results.TryTake(out item)) Thread.Sleep(200);

Now after each attempt the task simply waits for 200 milliseconds before trying again. And during those 200 milliseconds the processor can compute the results this task is actually waiting for. You can compile and run the code to make sure that the performance indeed improved.

However, it might be tricky to find the perfect wait time. Ideally, I need some kind of message from the collection notifying me that the value was added.

In a blocking collection, you can do this by using a foreach loop. The BlockingCollection class has the GetConsumingEnumerable method that can be used to enumerate through the blocking collection and consume its elements until the collection is completed. It might look like this:

public void display(BlockingCollection<double> results)
{
    foreach (var item in results.GetConsumingEnumerable())
    {
        double currentItem = item;
        Task.Factory.StartNew(new Action(() =>
             textBlock1.Text += currentItem.ToString() + Environment.NewLine),
        CancellationToken.None, TaskCreationOptions.None, ui);
    }
}

Now the display method checks whether there is an item in the results collection and, if there is, consumes the item. When the collection is completed and empty, execution exits the loop. All the locking, synchronization, and messaging between the tasks are managed by the TPL.

The resulting application will probably still be a little slower than the version that didn’t use the collection at all, but of course writing to and reading from thread-safe data storage added some overhead.

If you got lost in all the changes, here’s the full code:

public partial class MainWindow : Window
{
    TaskScheduler ui = TaskScheduler.FromCurrentSynchronizationContext();

    public MainWindow()
    {
        InitializeComponent();
    }

    public static double SumRootN(int root)
    {
        double result = 0;
        for (int i = 1; i < 10000000; i++)
        {
            result += Math.Exp(Math.Log(i) / root);
        }
        return result;
    }

    private void start_Click(object sender, RoutedEventArgs e)
    {
        textBlock1.Text = "";
        label1.Content = "Milliseconds: ";

        var results = new BlockingCollection<double>();
        var watch = Stopwatch.StartNew();
        List<Task> tasks = new List<Task>();

        var consume = Task.Factory.StartNew(() => display(results));

        for (int i = 2; i < 20; i++)
        {
            int j = i;
            var compute = Task.Factory.StartNew(() =>
            {
                results.Add(SumRootN(j));
            });
            tasks.Add(compute);
        }

        Task.Factory.ContinueWhenAll(tasks.ToArray(),
            result =>
            {
                results.CompleteAdding();
                var time = watch.ElapsedMilliseconds;
                label1.Content += time.ToString();
            }, CancellationToken.None, TaskContinuationOptions.None, ui);
    }

    public void display(BlockingCollection<double> results)
    {
        foreach (var item in results.GetConsumingEnumerable())
        {
            double currentItem = item;
            Task.Factory.StartNew(new Action(() =>
                 textBlock1.Text += currentItem.ToString() + Environment.NewLine),
            CancellationToken.None, TaskCreationOptions.None, ui);
        }
    }
}

For now, this is the last post in my parallel programming series. I hope that I’ve provided enough information and examples and bumped into and recovered from enough problems to enable even beginners to continue on their own. (At least I asked fewer questions while writing this post than I did for the first one!)

If you want to learn more about blocking collections, check out BlockingCollection Overview in the MSDN Library and BlockingCollection Extensions on the Parallel Programming with .NET blog.

I could not cover all the features provided by the TPL. If you want to see what else is available, here are some links:

  • Data Structures for Parallel Programming. This MSDN topic lists .NET 4 classes that are useful for parallel programming, such as thread-safe collections, synchronization primitives, and lazy initialization classes.
  • Introduction to PLINQ. Parallel Language Integrated Query, or PLINQ, enables quick and easy parallelization of LINQ queries.

Two more links that I used a lot in this series are Parallel Programming in the .NET Framework on MSDN and the Parallel Programming with .NET team blog.

P.S.

Thanks to Dmitry Lomov, Michael Blome, and Danny Shih for reviewing this and providing helpful comments, to Mick Alberts for editing.

Syndicated via RSS From: http://blogs.msdn.com/b/csharpfaq/

15 Good Computer Programming Habits


1. Before sitting down for coding, you must have formal or a paper-napkin design of the solution to be coded. Never start coding without any design unless the code is trivial one.

2. Good code documentation is as important as good knowledge of a programming language. Write brief logic for each major block of your code as comments in source code file itself. Its good to mention creation and modification dates of your program along-with why modification was required.

3. Maintaining versions of your program is another important task. Some present-day programming tools already have a built-in version management. Whenever you make any change to your program, they save its copy as.bak file.

My approach is to maintain 3 versions of a program. Say, I have a file program.c which is used by other project team members also. I copy this file as program.c.old as backup and make another copy as program.c.wrk where I do modifications. When modifications are successfully compiled, replace program.c with.wrk file.

You can also append a date or some explanation phrase to your program versions like program260505.c or programReadFnWrking.c.

4. If your project contains multiple source files then maintain a README file stating purpose of each source files, data files, intermediate and log files (if any). You may also mention the compilation and execution steps.

5. Ever wondered why your IF statement is not working as it should do. May be your are using single equal i.e. “=” instead of “==” in the condition check. A good approach is to write condition in reverse order. So, your condition should read something like this:

if ( 10==i)…. So, if you put single equal sign by mistake then it will be detected at compilation time only as an error.

6. While using loops and conditional statements, always first put closing braces corresponding opening braces and then write the inner statements i.e.

1) for(int i=0;i<10;i++)

2) {

4) printf(“i=%dn”,i);

3) }

The numbers at the starting of each line indicate sequence of writing loop code.

7. Avoid using magic numbers. For example, instead of writing

circleArea = 3.14 * pow(radius,2);

use following code:

#define PI 3.14

circleArea = PI * pow(radius,2);

8. Use meaningful variable and function names. For e.g. instead of using ‘r’ use ‘radius’ to represent radius of a circle. Similarly, function name ‘calculateArea’ is better than any cryptic short name. In a hurry, we may use short variable names but the time saved leads to double wastage of time later when you guess for what that short variable name stands for.

9. Using print statements for later debugging is a good habit. But, removing them when final code is ready is, sometimes, a risky task. So, make a function that displays debugging information passed to it. When your final version is ready, simply comment the internals of this function. So, this requires changes only at one place.

10. Once you are done with coding, start optimizing your code. Some of the variables you declared earlier may not be of use at this stage. Similarly, statements which are not loop dependent can be moved out of loop block. Sound knowledge of compiler can also help in optimizing the code further.

11. With good knowledge of your operating system and hardware, you can improve performance of your program in terms of resource requirements etc.

12. Always indent your code for clarity and easy readability.

13. You will also like the idea of organizing project files in into various folders like SOURCE, HEADERS, MAKE, EXES etc.

14. Study the code written by others. This will bring to you new programming techniques and what approach they have followed for the task for which you have also coded.

15. Last but not least important, take backup of your source-code files so that your effort don’t go waste if hard-disk crashes or a similar mishappening occurs.

Al-katib has obtained M.Tech. (Comp Sc & Engg) degree from Indian Institute of Technology, Delhi (INDIA). His areas of interests are distributed computing, computer graphics and Internet Technologies. Currently, he is involved in Software Project Planning, Development and Management. His other interests include writing for magazines and contributing utility softwares on Magazine’s CDs. He also have flair for teaching computer science with new teaching methodologies. His web-page URL is http://www.computer-science-notes.blogspot.com

Article Source: http://EzineArticles.com/?expert=Al_Kaatib

  • Digg
  • Mixx
  • Faves
  • Propeller
  • StumbleUpon
  • Tumblr
  • Yahoo! Buzz
  • del.icio.us
  • Facebook
  • Reddit
  • RSS
  • Technorati

C# 4.0 PowerPoint Presentations

Alexandru Ghiondea from the C# team created an excellent deck of slides that he used at Microsoft Days 2010 in Bulgaria. The C# team reused the deck several times for User Groups' presentations and talks. If you are planning a talk at your User Group, feel free to download. And even if you are not, it’s still might be useful to take a look – it’s a nice overview of the new C# 4.0 and Visual Studio 2010 features.

There are three presentations included: Introduction to C# 4.0, Increased Productivity with Visual C# IDE, and Dynamic Deep Dive.

Syndicated via RSS From: http://blogs.msdn.com/b/csharpfaq/

Computer Programming Languages: Why There Can Never Be Just One!

The mentality that there can be only one programming language is as illogical as believing there should be only one flavor of ice cream or only one style of automobile! There’s a reason they make both sports cars and family cars. Different programming languages occupy different niches in the programming world like different kinds of cars occupy different niches in the car world.

The C++ language is the child of the C language, which is known for small compile size and blazing execution speeds. Which explains why it was used for scene analysis and route planning in the Mars Rover autonomous driving system. Computer scientists have unexpectedly had more difficulty making computers understand visual information at the speed and richness that mere humans can (go figure, something to do with a third of the human brain being used to process what we see)! Which means that the visual interpretation function of the Rovers software would eat huge CPU cycles. It therefore needed that which is lean and mean. Enter C++, the son of C. To get back to my automotive metaphor, C++ would be like the high performance sports car of the automotive market – low weight, high power, maybe a 2 seater only.

But Java is never just a 2 seater only! Java is like the bus, which can transport many people happily at once. Java shines when a project is going to require many developers – It makes it easy to compartmentalize developers off from each other, to manage changes coming from many directions simultaneously, to make substantial future functional changes, and to document, document, document! But, like the bus, the performance of Java around the tight curves and up the hills is not to be compared to a sports car. Technically, while Java is also the son of C because of the language syntax, C++ is a more pure son because of the continued focus on speed. In order to compartmentalize different developers, Java had to introduce more code layers for the program to pop through, thereby eating considerable extra CPU cycles. Therefore Java will never be the speed demon of computer languages, but it has its place as the bus has its place.

To continue my automobile metaphor, the Visual Basic language would be an automobile that does everything possible for the driver so that he doesn’t have to do it himself. Of course there would be cruise control, cruise control is standard with VB. Navigation is done with a gps + computer system that blinks your dot on a street grid just like Dick Tracey. That way the VB user always knows where he’s going and never gets lost and loses time because of being lost. All of which saves programmer time and programmer time is expensive so the development cost of the programs functionality is greatly lessened. However, because of all of those VB layers that are necessary to do all of that work for the programmer, the program is going to have a fat memory size and a slow running speed (compared to C++). But that doesn’t matter if you are a business network with mostly underutilized CPU cycles and RAM memory. Which is probably true of most business networks. In that case the programmer hours you save by using VB will be offset by no significant loss at all. The slower speed will not be noticeable by the end user and the extra memory used will be taken out of that which was excess capacity to start with. We might say “VB is good for business, but bad for Mars”.

Because php was the first language built specifically for the web, it is hands down the best language for building web applications. It takes much less code to create equivalent applications using php than Java and the code will run far faster . PHP comes with a huge range of functions related to programming in a web environment. It plays well with a lot of databases, and it supports all the major web security standards. PHP has built in facilities so that it plays well with HTML, integrates web FORMS and URL variable passing, allows real-time creation of graphics to be displayed in web pages, and far, far too many other features related to web development to list. So PHP is the best language for programming on the web. Unless of course if your web project is going to require a large team of programmers. In which case you might be tempted to trade the efficiencies of php for Java’s ability to lock down any loose programming canon that might be on your team.

The point is that there will never be only one programming language in the computer programming language world. Different computer languages appeal to different niches like different cars appeal to their different niche markets. There will never be only one style car, there will never be only one color car, some cars will always be more automated than other cars, and etc. With the greatly increased richness of the home computer and the business computer environment over the last 10 years, the number of niches available for different computer languages has increased not decreased. There are languages devoted to multimedia display (smil), languages devoted to browser animation (flash), and I haven’t even mentioned CNC, the programming language devoted to controlling machine tools. How about if I compared CNC to C++ and asked you which was the better language? Talk about comparing apples to oranges! Which is better, an apple or an orange, or does it depend on what mood you are in or on whether you want to create a windows interface or to create a metal part?

Article Source: http://EzineArticles.com/?expert=David_Malia

  • Digg
  • Mixx
  • Faves
  • Propeller
  • StumbleUpon
  • Tumblr
  • Yahoo! Buzz
  • del.icio.us
  • Facebook
  • Reddit
  • RSS
  • Technorati

Five Tips for Effective Object-oriented Programming in C++

Spurgeon’s Law says that 90% of everything is crap. This certainly holds true in the world of software development, and especially when it comes to object-orientation and C++ programming. This is largely due to the pervasiveness and complexity of C++; it’s one of the most commonly used object-oriented languages, and yet few people know how to use it effectively. Couple this with the fact that few programmers truly grasp the object-oriented programming philosophy and you have an instant recipe for sloppy code. Volumes can be–and have been–written on how to develop proper object-oriented software designs using C++. Obviously, there won’t be space in this article for a truly thorough discussion; however, I would like to present a few quick, simple and effective tips for developing robust C++ software.

1. Work on the class designs first before deciding on the precise sequence of operations. Many programmers fancy themselves as doing object-oriented programming, but in reality, they use structured programming with some external object-oriented trappings. Ideally, one should first select a set of software objects that provide a logical abstraction of the software, then work on the sequence of operations. (This is by no means an absolute rule though, as the process of developing this sequence often reveals ways in which the object design can be refined.)

2. Think in terms of design patterns. Design patterns allow someone to draw on years of problem-solving experience within the computer science community. Instead of reinventing the wheel, why not just pick one off the shelf?

3. Use ‘const’ objects and ‘const’ functions whenever possible. If you know that an object’s data is never supposed to be modified, then declare the object to be ‘const’. This will prevent you–or worse, a naïve colleague–from accidentally modifying its state later on. Of course, one should also declare the appropriate member functions to be ‘const’ as well, to properly enforce this rule.

4. Avoid using public ‘get’ and ’set’ member functions. Getters and setters are not inherently bad; however, they are often a sign of poor software abstraction. ‘Get’ and ’set’ functions implicitly force the user to think in terms of the object’s internal data (i.e. whatever these functions are getting or setting), and such details should ideally be hidden from the object’s user. Getters and setters have their place, but please use them sparingly.

5. Avoid double indirection. C programmers are forced to work with pointers, and so they are used to having pointers to pointers throughout their code. This can often lead to confusing code, due to the all-too-familiar problems of NULL references and pointer arithmetic. In contrast, C++ programmers are under no such constraints, due to the built-in support for variable references in C++; that is, one can always use references to pointers instead, which makes the code much easier to understand. (Hint: If a C++ programmer uses double indirection, that’s a clear sign that he’s still thinking like an inexperienced C hack!)

About the author:

V. B. Velasco Jr., Ph.D. has worked as an electrical and software engineer for more than a decade. He currently works for a biotech firm that provides cryopreserved PBMCs, ELISPOT analyzers [http://www.elispot.cn] and ELISPOT expertise [http://www.elispot.co.jp].

  • Digg
  • Mixx
  • Faves
  • Propeller
  • StumbleUpon
  • Tumblr
  • Yahoo! Buzz
  • del.icio.us
  • Facebook
  • Reddit
  • RSS
  • Technorati

Parallel Programming: Task Cancellation

In this post, which is the third one in my parallel programming introduction series, I want to show how you can cancel parallel operations when working with the Task Parallel Library (TPL). I’m going to modify the program that I started in the previous posts. By the way, here’s the full list of posts in this series:

At the end of the last post, I had a small parallel application with responsive UI that could be easily used in both WPF and Windows Forms UI programming models. I’m going to stick with the WPF version and add a Cancel button to the application.

This is the code I’m going to work with:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }
    public static double SumRootN(int root)
    {
        double result = 0;
        for (int i = 1; i < 10000000; i++)
        {
            result += Math.Exp(Math.Log(i) / root);
        }
        return result;
    }
    private void start_Click(object sender, RoutedEventArgs e)
    {
        textBlock1.Text = "";
        label1.Content = "Milliseconds: ";

        var watch = Stopwatch.StartNew();
        List<Task> tasks = new List<Task>();
        var ui = TaskScheduler.FromCurrentSynchronizationContext();
        for (int i = 2; i < 20; i++)
        {
            int j = i;
            var compute = Task.Factory.StartNew(() =>
            {
                return SumRootN(j);
            });
            tasks.Add(compute);

            var display = compute.ContinueWith(resultTask =>
                              textBlock1.Text += "root " + j.ToString() + " " +
                                                  compute.Result.ToString() +
                                                  Environment.NewLine,
                              ui);
        }

        Task.Factory.ContinueWhenAll(tasks.ToArray(),
            result =>
            {
                var time = watch.ElapsedMilliseconds;
                label1.Content += time.ToString();
            }, CancellationToken.None, TaskContinuationOptions.None, ui);
    }

    private void cancel_Click(object sender, RoutedEventArgs e)
    {

    }

Now let’s refer to the Task Cancellation topic on MSDN. It tells me that to cancel a task I need to pass a special cancellation token to the task factory. By the way, the cancellation model is one of the new features in .NET Framework 4, so if you haven’t heard about it yet, take a look at Cancellation on MSDN and the .NET 4 Cancellation Framework post from Mike Liddell.

I have several tasks created in a loop. I’m going to use just one cancellation token so I can cancel them all at once.

I’ll add the following field to the MainWindow class:

CancellationTokenSource tokenSource = new CancellationTokenSource();

The tasks I want to cancel are the ones that compute the results (and I don’t want to cancel the tasks that display the results). So here’s the next change. The code

var compute = Task.Factory.StartNew(() =>
{
    return SumRootN(j);
});

becomes

var compute = Task.Factory.StartNew(() =>
{
    return SumRootN(j);
}, tokenSource.Token);

Finally, I’ll add code to the event handler for the Cancel button. I’ll simply call the Cancel method for the cancellation token and all tasks created with this token are notified about the cancellation. I also want to print “Cancel” into the text block.

private void cancel_Click(object sender, RoutedEventArgs e)
{
    tokenSource.Cancel();
    textBlock1.Text += "Cancel" + Environment.NewLine;
}

Let’s press F5 to compile and run the code. Now I can click Start, then click Cancel, and get an exception. (Aren’t you getting used to it?) AggregateException is the exception that tasks throw if something goes wrong. True to its name, it aggregates all the task failures into a single exception.

In my case, the tasks that calculate the results were canceled successfully, but the ones that display the results to the UI failed. This is reasonable: there were no results to display.

Let’s take a short pause here. Basically, to cancel a task, it’s enough to just pass a cancellation token to the task and then call the Cancel method on the token, as I did above. But in the real world, even in a small application like this one, you have to deal with the consequences of task cancellation.

In my example, there are several options for handling this exception. First is the standard try-catch block. The exception is thrown by the delegate within the ContinueWith method, so I need to add the try-catch block right into the lambda expression. I may get more than one exception aggregated, so I need to iterate through the collection of inner exceptions to see the exceptions’ messages.

var display = compute.ContinueWith(resultTask =>
{
    try
    {
        textBlock1.Text += "root " + j.ToString() + " " +
                            compute.Result.ToString() +
                            Environment.NewLine;
    }
    catch (AggregateException ae)
    {
        foreach (var inner in ae.InnerExceptions)
            textBlock1.Text += "root " + j.ToString() + " "
                + inner.Message + Environment.NewLine;
    }
}, ui);

This works fine. (You can compile and check, if you want to.) But once again, TPL provides a more elegant way of dealing with this type of issue: I can analyze what happened with the compute task and use its status to decide whether I want to start the display task.

var displayResults = compute.ContinueWith(resultTask =>
                     textBlock1.Text += "root " + j.ToString() + " " +
                                            compute.Result.ToString() +
                                            Environment.NewLine,
                         CancellationToken.None,
                         TaskContinuationOptions.OnlyOnRanToCompletion,
                         ui);

Now I’m passing two more parameters to the ContinueWith method. The first is the cancellation token. I don’t want the display task to be dependent on the cancellation token, so I’m passing CancellationToken.None. But I want this task to run only if the compute task returns some result. For this purpose I need to choose one of the TaskContinuationOptions. In my case the best solution is simply not to run the display task if the compute task was canceled. I can use the NotOnCanceled option that does just that.

But tasks may fail for some other reason, not just because of cancellation. I don’t want to display results of any failed task, so I’m choosing the OnlyOnRanToCompletion option. I’ll explain the “ran to completion” concept more a little bit later in this post.

However, the previous version with the try-catch block could also inform me that the tasks were indeed canceled. (Each task either printed the result or reported a cancellation by printing the exception message.) How to do the same in this version? Well, I can create a new task that will run only if the task is canceled. I’m going to convert the display task into two different tasks:

var displayResults = compute.ContinueWith(resultTask =>
                     textBlock1.Text += "root " + j.ToString() + " " +
                                            compute.Result.ToString() +
                                            Environment.NewLine,
                         CancellationToken.None,
                         TaskContinuationOptions.OnlyOnRanToCompletion,
                         ui);

var displayCancelledTasks = compute.ContinueWith(resultTask =>
                               textBlock1.Text += "root " + j.ToString() +
                                                  " canceled" +
                                                  Environment.NewLine,
                               CancellationToken.None,
                               TaskContinuationOptions.OnlyOnCanceled, ui);

Now I get the same results as I did with the try-catch block, and I don’t have to deal with exceptions at all. Let me emphasize that this is a more natural way of working with TPL and tasks, and I’d recommend that you to use this approach whenever possible.

By the way, if you click the Cancel button and then click the Start button in my application, all you will see is a list of canceled tasks and no results at all. The cancellation token got into the canceled state, and there is no way to turn it back to “not canceled.” You have to create a new token each time. But in my case it’s easy. I simply add the following line at the beginning of the event handler for the Start button:

tokenSource = new CancellationTokenSource();

Now let’s take a look at the output of my little program. (I clicked Cancel right after I saw the results of the 5th root.)

tasks

You can see that after I canceled the operation some roots were calculated nonetheless. This is a good illustration of the task cancellation concept in .NET. After I called the Cancel method for the task cancellation token, tasks that were already running switched into the “run to completion” mode, so I got the results even after I canceled the tasks. The tasks that were still waiting in the queue were indeed canceled.

What if I don’t want to waste resources and want to stop all computations immediately after I click Cancel? In this case, I have to periodically check for the status of the cancellation token somewhere within the method that performs the long-running operation. Since I declared the cancellation token as a field, I can simply use it within the method.

public double SumRootN(int root)
{
    double result = 0;
    for (int i = 1; i < 10000000; i++)
    {
        tokenSource.Token.ThrowIfCancellationRequested();
        result += Math.Exp(Math.Log(i) / root);
    }
    return result;
}

I made two changes: I removed the static keyword from the method declaration to enable field access, and I added a line that checks for the status of the cancellation token. The ThrowIfCancellationRequested method indicates so-called “cooperative cancellation,” which means that the task throws an exception to show that it accepted the cancellation request and will stop working.

In this case, the thrown exception is handled by the TPL, which transitions the task to the canceled state. You cannot and should not handle this exception in your code. However, Visual Studio checks for all unhandled exceptions and shows them when in debug mode. So, if you now press F5, you’re going to see this exception. Basically, you need to ignore it: You can simply press F5 several times to continue or run the program by using Ctrl+F5 to avoid debug mode.

Another possibility is to switch off the checking of these “unhandled by the user code” exceptions in Visual Studio: Go to Tools -> Options -> Debugging -> General, and clear the Just My Code check box. This makes Visual Studio “swallow” this exception. However, this may cause side effects in your debugging routine, so check the MSDN documentation to make sure it’s the right option for you.

Well, that’s all what I wanted to show you this time. Here is the final code of my still surprisingly small program.

public partial class MainWindow : Window
{
    CancellationTokenSource tokenSource = new CancellationTokenSource();
    public MainWindow()
    {
        InitializeComponent();
    }
    public double SumRootN(int root)
    {
        double result = 0;
        for (int i = 1; i < 10000000; i++)
        {
            tokenSource.Token.ThrowIfCancellationRequested();
            result += Math.Exp(Math.Log(i) / root);
        }
        return result;
    }

    private void start_Click(object sender, RoutedEventArgs e)
    {
        tokenSource = new CancellationTokenSource();
        
        textBlock1.Text = "";
        label1.Content = "Milliseconds: ";

        var watch = Stopwatch.StartNew();
        List<Task> tasks = new List<Task>();
        var ui = TaskScheduler.FromCurrentSynchronizationContext();
        for (int i = 2; i < 20; i++)
        {
            int j = i;
            var compute = Task.Factory.StartNew(() =>
            {
                return SumRootN(j);
            }, tokenSource.Token);

            tasks.Add(compute);

            var displayResults = compute.ContinueWith(resultTask =>
                                 textBlock1.Text += "root " + j.ToString() + " " +
                                                        compute.Result.ToString() +
                                                        Environment.NewLine,
                                     CancellationToken.None,
                                     TaskContinuationOptions.OnlyOnRanToCompletion,
                                     ui);

            var displayCancelledTasks = compute.ContinueWith(resultTask =>
                                           textBlock1.Text += "root " + j.ToString() +
                                                              " canceled" +
                                                              Environment.NewLine,
                                           CancellationToken.None,
                                           TaskContinuationOptions.OnlyOnCanceled, ui);
        }

        Task.Factory.ContinueWhenAll(tasks.ToArray(),
            result =>
            {
                var time = watch.ElapsedMilliseconds;
                label1.Content += time.ToString();
            }, CancellationToken.None, TaskContinuationOptions.None, ui);
    }

    private void cancel_Click(object sender, RoutedEventArgs e)
    {
        tokenSource.Cancel();
        textBlock1.Text += "Cancel" + Environment.NewLine;
    }

}

As usual, here are some links for further reading if you want to know more about task cancellation:

 

P.S.

Thanks to Dmitry Lomov, Michael Blome, and Danny Shih for reviewing this and providing helpful comments, to Mick Alberts for editing.

Syndicated via RSS From: http://blogs.msdn.com/b/csharpfaq/

Using Select Method on a DataTable in C# Dot Net

In previous article I populated a DataTable called ‘leagueTable’ with data read in from a comma delimited text file and performed operations on this data to get it into the format I wanted.

The league table has the name of the team and number of wins and draws home and away ready to calculate the relative strengths of each result. For each home team I will re evaluate the rating of its opponent based on all its other away results excluding the current fixture.

For example the fixture Team A v Team B, would require re evaluating all away matches for Team B excluding the current one against Team A. To do this I need to run a query on the DataTable that holds all the result data called ‘importResultsTable’.

One feature of the DataTable is that it can perform SQL queries on the data it holds in memory by using its ‘Select’ method. The result from this select method will return a number of DataRows. In the example we want to re-evaluate the away opponent ‘Team B’ based on all its away results except against Team A.

In SQL query analyser we would select this data by using the following query

Select * from importResultsTable where home’TeamA’ and away=’Team B’

It is very similarly for the select method in DataTable only the ’select’ part of the query is already done. I’m using a string variable called ‘AnExpression’ that I’m passing as a parameter into the select method of the DataTable. The query could be passed in directly if wanted.

string AnExpression = “awayTeam=” ‘ “+awayTeam+” ‘ and homeTeam’ “+TeamA+” ‘ “;

DataRow[] awayTeamFixtures = importResultsTable.Select(AnExpression);

From the resulting array of DataRows (awayTeamFixtures) we can re-evaluate the away team value. An example of returned data is as follows

Team C v Team B 1-0

Team D v Team B 2-0

Team E v Team B 1-1

By giving points for a win and draw we can give a value to Team B away performance. Having two losses and a draw Team B away from home is not such a tough opponent so any home win against them may not be worth as much as other home wins.

Article Source: http://EzineArticles.com/?expert=Antonio_Viatle

  • Digg
  • Mixx
  • Faves
  • Propeller
  • StumbleUpon
  • Tumblr
  • Yahoo! Buzz
  • del.icio.us
  • Facebook
  • Reddit
  • RSS
  • Technorati

C Programming Tutorial – Simple Array Program

Good refresher video on building arrays in C. I got stuck and it helped because I was looping one past my array max by using an <= sign instead of <.

  • Digg
  • Mixx
  • Faves
  • Propeller
  • StumbleUpon
  • Tumblr
  • Yahoo! Buzz
  • del.icio.us
  • Facebook
  • Reddit
  • RSS
  • Technorati

Parallel Programming: Task Schedulers and Synchronization Context

Thanks to everyone who provided feedback on my previous post Parallel Programming in .NET Framework 4: Getting Started. As promised, I am continuing the series. This time, let’s go a little bit deeper and talk about task schedulers, synchronization context, tasks that return values, and some other cool features of the Task Parallel Library (TPL).

By the way, here’s the full list of posts in this series:

This time I’m not going to add any new functionality to my sample application, but rather I will show how you can communicate with the UI thread a little easier and make your application less dependent on a particular UI framework.

Here is the final code from my last post:

using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Documents;
using System.Threading.Tasks;
using System.Diagnostics;

namespace ParallelApplication
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        public static double SumRootN(int root)
        {
            double result = 0;
            for (int i = 1; i < 10000000; i++)
            {
                result += Math.Exp(Math.Log(i) / root);
            }
            return result;
        }
        private void start_Click(object sender, RoutedEventArgs e)
        {
            textBlock1.Text = "";
            label1.Content = "Milliseconds: ";

            var watch = Stopwatch.StartNew();
            List<Task> tasks = new List<Task>();
            for (int i = 2; i < 20; i++)
            {
                int j = i;
                var t = Task.Factory.StartNew(() =>
                {
                    var result = SumRootN(j);
                    this.Dispatcher.BeginInvoke(new Action(() =>
                         textBlock1.Text += "root " + j.ToString() + " " +
                                             result.ToString() +
                                             Environment.NewLine)
                    , null);
                });
                tasks.Add(t);
            }
            Task.Factory.ContinueWhenAll(tasks.ToArray(),
                  result =>
                  {
                      var time = watch.ElapsedMilliseconds;
                      this.Dispatcher.BeginInvoke(new Action(() =>
                          label1.Content += time.ToString()));
                  });
        }
    }
}

Yes, it does everything that I wanted, but it uses the WPF API extensively. Although it is perfectly fine to use WPF as shown above, with the TPL you can make your code much less dependent on the particular UI framework. Right now, if you copy and paste the button event handler and the SumRootN method to a Windows Forms application with almost identical UI, you would need to do a lot of work, because Windows Forms doesn’t have the Dispatcher object and uses different API for managing interactions with the UI thread.

Let’s go back to the previous post and remember why I added the Dispatcher object in the first place. Well, I needed it to communicate with the UI thread, because all the computation results were in background threads created by the TPL. However, the TPL provides a different way of handling interactions between threads. It has task schedulers: very useful objects that are responsible for queuing and executing tasks.

My application already uses a default task scheduler, because this is how the TPL works with the ThreadPool. The TPL has other schedulers in addition to the default one and also allows you to create custom schedulers. One of the schedulers that TPL provides is based on the current synchronization context, and it can be used to ensure that my task executes on the UI thread. For example, let’s take a look at this code:

Task.Factory.ContinueWhenAll(tasks.ToArray(),
      result =>
      {
          var time = watch.ElapsedMilliseconds;
          this.Dispatcher.BeginInvoke(new Action(() =>
              label1.Content += time.ToString()));
      });

The time computation is very simple and fast, so it doesn’t require a background thread. The next line is about displaying the result, which is pure UI work.

I need to somehow get a reference to the UI thread, so I can run a task on it. In this case, it’s quite easy. The code above is from the button event handler, so before I start the task I am in fact operating on the UI thread. I just need to remember the current context and then pass it to the TaskFactory.ContinueWhenAll method.

Here is how I can do this (plus I need to add System.Threading to the list of namespaces):

var ui = TaskScheduler.FromCurrentSynchronizationContext();
Task.Factory.ContinueWhenAll(tasks.ToArray(),
    result =>
    {
        var time = watch.ElapsedMilliseconds;
        label1.Content += time.ToString();
    }, CancellationToken.None, TaskContinuationOptions.None, ui);

The TaskScheduler.FromCurrentSynchronizationContext method returns a task scheduler for the current context, in this case, the UI thread. The ContinueWhenAll method has an overload that accepts the task scheduler parameter. This overload requires some other parameters as well, but since I don’t need them, I use corresponding None properties. I got rid of the inner delegate, so it’s much easier to see what the task is doing and there is no Dispatcher any more.

Now let’s take a look at a more complicated case:

for (int i = 2; i < 20; i++)
{
    int j = i;
    var t = Task.Factory.StartNew(() =>
    {
        var result = SumRootN(j);
        this.Dispatcher.BeginInvoke(new Action(() =>
             textBlock1.Text += "root " + j.ToString() + " " +
                                 result.ToString() +
                                 Environment.NewLine)
        , null);
    });
    tasks.Add(t);
}

This one requires more thorough refactoring. I can’t run all the tasks on the UI thread, because they perform long-running operations and this will make my UI freeze. Furthermore, it will cancel all parallelization benefits, because there is only one UI thread.

What I can do is to split each task into two: one will compute the results and another one will display information to the UI thread. I have already used the ContinueWhenAll method that waits for an array of tasks to finish. It’s not surprising that TPL also allows you to wait for a certain task to finish and then to perform some operation. The method that does the job is Task.ContinueWith.

var ui = TaskScheduler.FromCurrentSynchronizationContext();
for (int i = 2; i < 20; i++)
{
    int j = i;
    var compute = Task.Factory.StartNew(() =>
    {
        var result = SumRootN(j);
    });
    tasks.Add(compute);

    var display = compute.ContinueWith(resultTask =>
                     textBlock1.Text += "root " + j.ToString() + " " +
                                         result.ToString() +
                                         Environment.NewLine,
                     ui);
}

OK, the code above doesn’t compile for an obvious reason. The result variable is local for the task compute and the task display doesn’t know anything about it. How can I pass the result from one task to another one? Here is one more trick from the TPL: tasks can return values. To make the task compute return the value, all I need is to change “var result =” to “return” in the compute task.

var compute = Task.Factory.StartNew(() =>
{
    return SumRootN(j);
});

This makes the compiler change the type of the compute object from Task to Task<TResult>. In my case, the type of compute is now Task<double>. Objects of the Task<TResult> type save the return value in the Result property.

var display = compute.ContinueWith(resultTask =>
                  textBlock1.Text += "root " + j.ToString() + " " +
                                      compute.Result.ToString() +
                                      Environment.NewLine,
                  ui);

That’s it. Here is my final code for the event handler:

private void start_Click(object sender, RoutedEventArgs e)
{
    textBlock1.Text = "";
    label1.Content = "Milliseconds: ";

    var watch = Stopwatch.StartNew();
    List<Task> tasks = new List<Task>();
    var ui = TaskScheduler.FromCurrentSynchronizationContext();
    for (int i = 2; i < 20; i++)
    {
        int j = i;
        var compute = Task.Factory.StartNew(() =>
        {
            return SumRootN(j);
        });
        tasks.Add(compute);

        var display = compute.ContinueWith(resultTask =>
                          textBlock1.Text += "root " + j.ToString() + " " +
                                              compute.Result.ToString() +
                                              Environment.NewLine,
                          ui);

    }

    Task.Factory.ContinueWhenAll(tasks.ToArray(),
        result =>
        {
            var time = watch.ElapsedMilliseconds;
            label1.Content += time.ToString();
        }, CancellationToken.None, TaskContinuationOptions.None, ui);

}

If you copy the code from this event handler plus the SumRootN method to the Windows Forms application, you will need to change the code only slightly, mostly because the UI elements are a little bit different. (Windows Forms applications do not have TextBlock control and their labels do not have the Content property.) Just for fun, I did it myself and highlighted the changes I had to make in the event handler.

private void start_Click(object sender, EventArgs e)
{
    label2.Text = "";
    label1.Text = "Milliseconds: ";

    var watch = Stopwatch.StartNew();
    List<Task> tasks = new List<Task>();
    var ui = TaskScheduler.FromCurrentSynchronizationContext();
    for (int i = 2; i < 20; i++)
    {
        int j = i;
        var compute = Task.Factory.StartNew(() =>
        {
            return SumRootN(j);
        });
        tasks.Add(compute);

        var display = compute.ContinueWith(resultTask =>
               label2.Text += "root " + j.ToString() + " " +
                               compute.Result.ToString() +
                               Environment.NewLine,
                ui);
    }

    Task.Factory.ContinueWhenAll(tasks.ToArray(),
         result =>
         {
             var time = watch.ElapsedMilliseconds;
             label1.Text += time.ToString();
         }, CancellationToken.None, TaskContinuationOptions.None, ui);
}

Now I have a parallel Windows Forms application, with responsive UI. The migration was really easy. So, my advice is to stick to the TPL way of managing UI thread instead of the UI framework API. It makes your code much easier to migrate and allows you to write essentially the same code, no matter what UI framework you use.

I’m going to talk about task cancellation next time. For now, if you want to know more about the features used in this post, here are some interesting links:

P.S.

Thanks to Dmitry Lomov, Michael Blome, and Danny Shih for reviewing this and providing helpful comments, to Robin Reynolds-Haertle for editing.

Syndicated via RSS From: http://blogs.msdn.com/b/csharpfaq/

C++ and format_iterator

Enhancing C++'s flexibility and expressiveness

Syndicated via RSS From: http://www.drdobbs.com