Managed World

An Odyssey Through The Land Of Geekdom

  Home  |   Contact  |   Syndication    |   Login
  469 Posts | 0 Stories | 292 Comments | 1309 Trackbacks

News

Twitter












Archives

Post Categories

Blog Roll

Tuesday, June 09, 2009 #

One of the big areas of improvements coming to Visual Studio 2010 and .NET Framework 4 is in the area parallel computing. There are a LOT of new features and improvements to existing features here with this latest release of our developer tools.

Many people smarter and more capable than I have already written about most of this stuff, so there’s not much for me to add. What I did want to do though is to let you know all the places where you can find this wealth of information (and there is a lot of it). So, without further ado, here is a list of content for you if you are interested in Parallel Computing with Visual Studio 2010 and .NET Framework 4 Beta 1.

 

MSDN

What’s New…

Diving Deeper

Channel 9 Videos

More Oldie-But-Goodie Channel 9 Videos

Misc…

Enjoy! There’s a lot here to digest. We definitely live in exciting times :).

Do you know of some links that you like that I missed here? Feel free to let me know by adding a comment here.


Friday, April 17, 2009 #

As I’ve been writing more F# code and learning more about the F# language, I have found myself intrigued by how some surprisingly simple language features can combine with each other to enable us to write very expressive code. While I am still learning F# (and realize just how much more I have to learn), I’m hoping that sharing some of these thoughts will encourage others to dig into what I think is a very powerful language that a lot of us developers can learn from.

In this post, we’re going to talk about Infix Operators. Before we dive into F#’s implementation of infix operators though, let’s discuss what an “Infix” Operator actually is.

So what is an “Infix” operator? An infix operator is an operator that is expressed using a mathematical notation called infix notation. When it comes to these types of mathematical notations, there are three primary kinds: Prefix Notation, Infix Notation, and Postfix Notation.

Take for example the addition operator. The addition operator takes two operands that are then added together. Using infix notation, the operator is written between the two operands it operates upon. Prefix notation would have the operator before the operands, and postfix notation has the operator after the operands. So given the addition operator, the different notations would like the following:

  • Prefix notation: + 3 4 (definitely familiar to LISP-heads –> “(+ 3 4)”)
  • Infix notation: 3 + 4 (familiar to most of us developers)
  • Postfix notation: 3 4 + (an interest way used by Forth for one, a stack-as-first-class-citizen language –> “3 4 ADD”)

One of the things I love about F# is that infix operators are normal functions that you can easily define yourself. Let’s explore this concept a bit. Open up F# Interactive and execute the following command:

> (+);;
val it : (int -> int -> int) = <fun:clo@42>

Note: Surrounding the infix operator with “()” allows us to get at the underlying function that implements the infix operator, rather than the operator itself.

Heck, we can even call it like a normal function:

> (+) 3 4;;
val it : int = 7

You can see here that the addition operator is simply a function that accepts two integers and returns an integer (well, this isn’t technically true, but you can think about it this way for now; we’ll get into the difference in a different post). I said before we can easily define our own infix operator. How do we do that? Well, just for giggles, let’s go ahead and define our own add operator:

> let (|+) x y = x + y;;
val (|+) : int -> int -> int

After we define this function, we can use our new infix operator just like any of the other built-in infix operator:

> 3 |+ 4;;
val it : int = 7

Yes, it’s that simple.

Well, that’s a very quick look at a seemingly simple language feature that a lot of us developers tend to take for granted. In future posts, we’ll see how combining infix operators with other features in F# allows us to write more expressive F# code.


Tuesday, February 24, 2009 #

Well, I said I was going to be moving on to CountdownEvent, but I was wrong. there is one more aspect of using Barrier that I just had to share (thanks go to Stephen Toub on the Parallel Computing Platform team for bringing this up).

In my first Barrier post, I had mentioned:

Barrier is a great new synchronization primitive to use when there is a known amount of work to do that is being done by different workers that all have common synchronization points.

To make a long story short, this isn’t strictly true. Barrier can be used just fine when there is an unknown amount of work. The distinction between Barrier and other synchronization primitives isn’t about “known amount versus unknown amount of work”, it’s more about “usable once versus usable many times.” To explain this in more detail, let’s look back at the example from the last post (a Road Trip to Seattle).

Multiple Usages

First, it is perfectly allowed to use a Barrier more than once. Using the Road Trip metaphor, instead of going straight to Seattle after leaving the Gas Station, let’s have everybody stop for lunch on there way. So now there are two “sync points” that we have. First, we’ll wait for everybody to meet up at the gas station, then we’ll also wait for everybody to meet up for lunch after they’ve left the gas station.

Revising our DriveToSeattle method from the last post, we will simply call SignalAndWait() a second time to provide a second sync point where everybody meets for lunch:

static void DriveToSeattle(string name, TimeSpan timeDelay)
{
    // Perform some work
    Console.WriteLine("[{0}] Leaving House", name);
    Thread.Sleep(timeDelay);
    Console.WriteLine("[{0}] Arrived at Gas Station", name);

    // Need to wait for others
    sync.SignalAndWait();

    // Perform some work
    Console.WriteLine("[{0}] Leaving for Seattle", name);
    Thread.Sleep(timeDelay);
    Console.WriteLine("[{0}] Meet for lunch", name);

    // Need to wait for others
    sync.SignalAndWait();

    Console.WriteLine("[{0}] Leaving for Seattle", name);
}

In real-word usage of the Barrier primitive, this doesn’t have to be limited to two uses of the Barrier. We can use the Barrier any number of times we need to in order to provide these common synchronization points for our parallel code.

Unknown Number of Workers

I mentioned that it wasn’t strictly true that Barrier is great for a just a known amount of work. That’s because Barrier provides the functionality you need to even use it when the number of workers performing our work is unknown.

Back to our Road Trip. Dennis’s sister Dee is planning to come along on the Road Trip as well. However, she forgets to set her alarm and wakes up late. And unfortunately, Charlie, Mac, and Dennis have already left for Seattle. She still wants to go though so she plans to meet up with them for lunch.

What we need is for a way for Dee to flag “Hey guys! I’m coming!” And it just so happens that the Barrier class provides an AddParticipant method that we can use to make sure the extra “worker” (Dee in this case) is included the next time everybody waits for each other to “complete their work.”

In this case, Dee would simply “add herself” to the Barrier when her thread is spun up. To do this, let’s create a new DriveStraightToLunch method that will be used instead of the existing DriveToSeattle method we created before.

static void DriveStraightToLunch(string name, TimeSpan timeDelay)
{
    // "I'm coming along guys"
    sync.AddParticipant();

    // Meet for lunch
    Console.WriteLine("[{0}] Leaving for Seattle", name);
    Thread.Sleep(timeDelay);
    Console.WriteLine("[{0}] Meeting for lunch", name);

    // Need to wait for others
    sync.SignalAndWait();

    Console.WriteLine("[{0}] Leaving for Seattle", name);
}

And then we just spin up this new piece of work (added to our code in Main before all threads are joined):

... creation and start of Dennis thread

Thread.Sleep(TimeSpan.FromSeconds(6));
var dee = new Thread(() => DriveStraightToLunch("Dee", TimeSpan.FromSeconds(5)));
dee.Start();

dee.Join();
... join other threads

It’s that simple.

In Closing

I hope in the last two posts you’ve seen how useful the new Barrier synchronization primitive can be when writing Parallel Code. API-wise there really isn’t much to the new Barrier class. Of course, as is usual with writing parallel code, it’s learning to use it correctly or use it in the right situations that’s the tricky part :).

I would say "tune in next time when we chat about CountdownEvent” but considering this recent change of plans, I’m not going to tempt fate :). Later!


Monday, February 09, 2009 #

Over the coming months, I would like to take you on a whirlwind tour of a bunch of the new data structures and APIs being added to the Base Class Libraries (BCL) for .NET Framework 4.0. This will take us everywhere from new multithreading-oriented data structures like Barrier and CountdownEvent, to more basic structures like Tuple.

I’ll update this post with correct links as new posts are created…

Enjoy!


In this first stage of our Tour de BCL, we will be passing through the new Barrier class.

So what is a Barrier? Let’s take a look at the boring technical description for a Barrier:

A Barrier is a synchronization primitive that enforces the stopping of execution between a number of threads or processes at a given point and prevents further execution until all threads or processors have reached the given point.

I don’t know about you, but sometimes technical descriptions like the above just sound like “blah, blah, blah” to me. What does a Barrier really mean to me, as a developer. Let’s break it down a different way by looking at a specific real-life scenario.

Think of a Road Trip

Instead of looking at the technical description above, think of the concept of a road trip.

There are three friends, Mac, Charlie, and Dennis. They live in a small town south of Seattle but want to take a road trip up to Seattle. So Mac calls up Charlie and Dennis and says “Hey guys, let’s take a road trip to Seattle. Meet up at the local gas station and then we’ll all leave from there.” So now they all now they need to go to the local gas station, wait for all of them to show up, and then they’ll leave for Seattle together and follow each other there.

Barrier

Relating this scenario to some code, let’s say that Charlie, Mac, and Dennis are all individual threads that have one method of execution, the DriveToSeattle() method. Within that method, they are all going “to drive” to the same gas station and then continue to drive to Seattle.

If we don’t use the Barrier to synchronize the work (aka wait for each other to arrive at the gas station), each one of them will leave for Seattle the second they arrive at the gas station:

static void Main(string[] args)
{
    var charlie = new Thread(() => {
        DriveToSeattle("Charlie", TimeSpan.FromSeconds(1))
    }); 
    charlie.Start();

    var mac = new Thread(() => {
        DriveToSeattle("Mac", TimeSpan.FromSeconds(2))
    }); 
    mac.Start();

    var dennis = new Thread(() => {
        DriveToSeattle("Dennis", TimeSpan.FromSeconds(3))
    }); 
    dennis.Start();

    charlie.Join();
    mac.Join();
    dennis.Join();

    Console.ReadKey();
}

static void DriveToSeattle(string name, TimeSpan timeToGasStation)
{
    // Drive to gas station
    Console.WriteLine("[{0}] Leaving House", name);
    Thread.Sleep(timeToGasStation);
    Console.WriteLine("[{0}] Arrived at Gas Station", name);

    // Need to sync here

    // Perform some more work
    Console.WriteLine("[{0}] Leaving for Seattle", name);
}

BeforeBarrier

Well, that’s hardly a road trip. If I were in Mac’s or Charlie’s shoes when Dennis left for Seattle without me, I know I would be a little upset. We obviously don’t want that to happen. So we can use the new Barrier to make sure they all “wait up” for each other at the gas station.

Using the new Barrier class

So how do you use the new Barrier class? It’s actually quite simple.The cool thing is that there are really only three things you need to learn to use the Barrier for this simple type of scenario: a single constructor and two method calls (both of which take zero parameters). Yes, it’s that simple to use.

Let’s look at what our new code using Barrier looks like (the three bold lines are the new ones):

static Barrier sync;

static void Main(string[] args)
{
    sync = new Barrier(participantCount:3);

    var charlie = new Thread(() => {
        DriveToSeattle("Charlie", TimeSpan.FromSeconds(1))
    }); 
    charlie.Start();

    var mac = new Thread(() => {
        DriveToSeattle("Mac", TimeSpan.FromSeconds(2))
    }); 
    mac.Start();

    var dennis = new Thread(() => {
        DriveToSeattle("Dennis", TimeSpan.FromSeconds(3))
    }); 
    dennis.Start();

    charlie.Join();
    mac.Join();
    dennis.Join();

    Console.ReadKey();
}

static void DriveToSeattle(string name, TimeSpan timeToGasStation)
{
    // Drive to gas station
    Console.WriteLine("[{0}] Leaving House", name);
    Thread.Sleep(timeToGasStation);
    Console.WriteLine("[{0}] Arrived at Gas Station", name);

    // Need to sync here
    sync.SignalAndWait();

    // Perform some more work
    Console.WriteLine("[{0}] Leaving for Seattle", name);
}

AfterBarrier

You can see now that nobody leaves for Seattle until everybody has arrived at the gas station. Essentially, with every call to SignalAndWait(), the number of signals received by the barrier is incremented. Once the number of signals received reaches the number of participants the Barrier was constructed with, all threads are then allowed to continue execution. And that’s all it takes to leverage the new Barrier class being introduced into the BCL with .NET Framework 4.0.

But Jason... What is up with that line of code: “sync = new Barrier(participantCount:3);”? I mean, what the heck is “participantCount:3”? How is that valid syntax?

Well, that’s easy. This is a new feature in C# 4.0 called Named Parameters where you can actually use a parameter’s name to specify what a value applies to. This becomes very handy in two places: 1) when combined with Optional Parameters, and 2) when used to clarify “magic numbers” (or “magic strings”, “magic booleans”, etc.) without having to introducing a temporary variable.

In this case, I could have left out participantCount and written just “sync = new Barrier(3);”. But I personally don’t like a magic number like “3” just floating around like this and Named Parameters give me an easy way to provide more context about the value “3”.

In Closing

So let’s look back at that boring technical description again:

A Barrier is a synchronization primitive that enforces the stopping of execution between a number of threads or processes at a given point and prevents further execution until all threads or processors have reached the given point.

Think about it this way:

  • “stopping of execution between a number of threads...” = waiting for each other to arrive
  • “… at a given point” = the gas station
  • “prevents further execution until all threads… have reached the given point” = can’t leave for Seattle until everybody shows up

See? Nice and simple!

Barrier is a great new synchronization primitive to use when there is a known amount of work to do that is being done by different workers that all have common synchronization points. However, if the amount of work needing to be done is not well known beforehand, Barrier is not the greatest primitive to use. Next we’ll look at another new synchronization primitive coming in .NET Framework 4.0 that is great to use when the amount of work is not known: the CountdownEvent.

I hope you all enjoyed this first stage in our Tour de BCL. Until the next stage, we’ll see you later.


Sunday, January 04, 2009 #

Here's a sneak peek at a project/library I'm currently working on:

include Microsoft::Xna::Framework
include Microsoft::Xna::Framework::Graphics
include RubyXna

class FluxxGame < RubyXnaGame
    def initialize
        self.background_color = Color.black
    end

    def load(loader)
        @font = loader.load_font("Title")
    end

    def render(renderer)
        renderer.draw_text(@font, "Fluxx, v0.1", Vector2.zero, Color.white)
    end
end

The combination of IronRuby and XNA Game Studio is proving to be pretty darn fun!


Wednesday, December 24, 2008 #

Happy Holidays, everybody! First of all, thanks to everybody who helped get the word out on the launch of this new show on Channel 9 by blogging, tweeting, or spreading the word using other ways. There is no doubt in our minds that the launch of this new show wouldn’t have been nearly as successful without all of you!

In this second episode of 10-4, we’ll take a very high-level look at Visual Studio 2010. We’ll discuss what types of features you can expect to see in Visual Studio 2010 and .NET Framework 4.0 depending on what type of developer you are. And lastly we’ll go ahead and dive into a revised area of Visual Studio: the Start Page. The Start Page in Visual Studio 2010 has been revised to be XAML-based and allows full customization.

In future episodes we’ll dive more deeply into the technical underpinnings of Visual Studio 2010 and the .NET Framework 4.0, but for this second episode we want to make sure everybody gains an understanding of what different areas are being focused in Visual Studio 2010. While an overview is given, there is only so much time and so many features we can talk about. So if you don't see something mentioned, don't worry as we have a lot more information to dive into in the coming weeks and months.

For more 10-4 episodes, be sure to visit:
http://channel9.msdn.com/Shows/10-4/

Over and out!


Sunday, December 14, 2008 #

This question/conversation has come up quite often when chatting with friends and other developers. With the introduction of the "var" keyword in C# now, when should it be used (and, perhaps more importantly, when should it not be used). Here is a quick summary on my thoughts...

My personal feeling is that it's a problem if I have to rely on Intellisense in order to be able to tell what type a variable is when it is declared. It's important to keep the future audience in mind while you are writing code (this could be future developers needing to maintain your code, or even yourself years down the road). After all, writing code is the easy part. Reading it (and hence, reverse engineering it to a degree) is the hard part.

So, when you write code that requires another developer to use Intellisense (or go poking around other pieces of code that may be contextual irrelevant) to understand a piece of code, I would contend that you are not writing good code.

This brings us to the var keyword. Good usage? Using it to automatically infer variable types on declaration/instantiation lines of code:

var people = new List<People>();
var lookup = new Dictionary<string, SomeType>();

However, I really don't like when developers use the var keyword on a variable declaration based on the return of a method call. Without Intellisense, how am I supposed to be able to tell what type the variable is?

// This is *bad*, don't do this
var local = SomeMethod(...);

How about loop variables? My personal take (though I could go either way) is to not use the var keyword on loops:

// I prefer this
for (int i = 0; i < 10; i++)
{
...
}

// over this
for (var i = 0; i < 10; i++)
{
...
}

In general, I think it is important not to be a lazy coder and do something just because it makes it easier on you at the time. As a developer, you need to think about those that will come after you and maintain your code. Don't make it any harder on them than it needs to be. Their jobs are already hard enough.

What are your thoughts? Where do you use the var keyword? Where do you NOT use the var keyword?


Tuesday, October 28, 2008 #

Great news everybody! I just got word that Day 2 of the PDC (that's Tuesday), at 12:45pm, Steven Sinofsky will be giving a 30 minute Open Space session on "Windows 7 Keynote Discussion."

Yes, that's correct. The Senior Vice President of Windows and Windows Live will be in Open Space at 12:45pm to discuss with you, our customers, what you think about the announcements made in Tuesday morning's keynote on Windows 7.

So if you want to get the ear of the man in charge of Windows (who only answers to Steve Ballmer himself), make sure to drop by Open Space Tuesday afternoon at 12:45pm to partake in what is sure to be a great discussion!


Thursday, October 23, 2008 #

I'm VERY excited to finally talk about this publicly. A while ago, I was able to start playing around with a programming language called SmallBasic being developed internally at Microsoft by Vijaye Raji. And now, it's finally been announced as part of our new Dev Labs effort.

So what is SmallBasic? From the website:

"Small Basic is a project that's aimed at bringing "fun" back to programming. By providing a small and easy to learn programming language in a friendly and inviting development environment, Small Basic makes programming a breeze. Ideal for kids and adults alike, Small Basic helps beginners take the first step into the wonderful world of programming."

Why develop SmallBasic? Well, I can tell you the reason that I'm excited about it. I personally feel that in the absence of QBASIC, GWBASIC, etc., there is no longer a programming language that is easy for non-programmers to get hooked with. All our modern .NET languages currently are more "professional" languages. But what is out there for 6-10 year olds (for example) to play around with easily in order to "get the bug" of programming. Nothing from Microsoft, if you ask me.

 

This is why I'm so ecstatic that SmallBasic is finally available publically (keep in mind, it's not an official product, simply DevLabs bits that you can play around with). So, even if you are a professional programmer, feel free to go over, download it, and play around for a bit.

One of my favorite things? For one, the editor. It's a pretty clever take on Intellisense and the editor is a great example of a "friendly" IDE.

Enjoy!