Monday, July 04, 2011

TPL Dataflow Presentation


With quite good responses to my presentation about .NET Task Parallel Library (TPL) to both the office SunGard and the client BoA/ML, I am asked to give another presentation to the first meeting of SunGard Houston .NET interested group. This time I am going to add TPL Dataflow (Visual Studio Async) to the talk. There are a few good points about TPL dataflow which I am very pleased with. I am pretty sure the presentation will generate a lot of interests in the techie community of the office.

First of all, as we all know, in particular TPL did not focus on problems best expressed with agent-based models or those based on message-passing paradigms. TPL Dataflow is focused on providing building blocks for message passing and parallelizing CPU- and I/O-intensive applications with high-throughput and low-latency.


Secondly, you can actually use Rx in Task Dataflow. Rx is predominantly focused on coordination and composition of event streams with a LINQ-based API, providing a rich set of combinators for manipulating IObservable<T>s of data. You can let data flow blocks to be exposed as both observable and observers, therefore enabling direct integration of Rx library.


The implementation is extremely flexible and give the developers a lot of options. TPL Dataflow is comprised of "dataflow blocks," data structures that buffer, process, and propagate data. They can be either sources, targets, or both, in which case they're referred to as propagators, e.g.
ActionBlock, BufferBlock, BoadcastBlock, WriteOnceBlock, TransformBlock, BatchBlock, JoinBlock, and BatchedJoinBlock.

Tuesday, March 29, 2011

.NET 4.0 TPL Rocks!

I’ve been working with multi-threaded programming since C# v1.0 with Visual Studio 2003. Before that, I have a few years working on multi-threaded programming in Java and C++.

There were a few things I hated on multi-threaded programming: imperative programming, implicit contract, and heavy thread composing; Thread interacting with WaitHandles like ManualResetEvent and AutoResetEvent; concerns about the race conditions, dead locks, live locks, thread starving etc. The last but not the least is the debugging. When there is something going wrong, it is really hard to debug without a good tool.

Task Parallel Library, or TPL, is a new .NET 4.0 new library, an component of the .NET 4.0 Parallel Frameworks. After I used it in my project, I felt Microsoft really hit the ball off the park this time.

TPL is EASY. TPL is just like the easy button. It is easy to use. It is easy to debug. It is easy to boost the performance of your application. If you have not started to use it in your project, I would strongly recommend you to start to use it or encourage your developers to use it.

Tasks vs Threads -> Tasks don’t equal to threads. You can think of Tasks are threads without all the threads’ drawbacks. Threads are single core, expensive on resources, such as 2M user mode memory plus kernel constructs. Thread needs time to startup and dispose. The most important is that the context switching the performance killer.

TPL is better in performance than using thread:

· Task scheduler has been highly optimized to utilize multi-processors with the brand new thread pool.

· There are less startup and tear down (mostly I/O) time for tasks than for threads.

· There is less time spending on context switching for tasks than for threads when threads are more than cores. Performance will degrade if the thread manager is not multi-core aware and thread running time aware.

· Memory (I/O) has been pre-allocated for the thread pool.

Here are some tips I learned to use TPL:

-- -- TPL Tips – Creating Task

· You can pass delegate to the constructor, either Action or Func. You can pass LINQ expression as well.

· You can create Task and run it right away. Or you can just create a task and declare it’s running condition.

· You can define TaskCreateOptions, such as LongRunning, or AttachedParent

· You can pass in Cancellation token.

-- -- TPL Tips – Passing data

· You can pass any object inherited from System.Object

-- -- TPL Tips – Returning

· This is way better than old TheadPool.QueueUserItem

-- -- TPL Tips – Waiting

· Waiting is a good way to declare parallelism

· Wait, WaitAll, WaitAny

-- -- TPL Tips – Cancelling

· Cancelling is cooperative

· CancellationTokenSource can be used once.

· Cancellation Token can be passed into task creation or waiting

· The beauty of the cancellation token is once the cancellation token source is cancelled, if the task has not been scheduled, it will not be executed.

-- -- TPL Tips – Continuing

· Task.ContinueWith

· Task.Factory.ContinueWhenAll()

· ContinuationOptions (OnlyOnFaulted, OnlyOnCancelled, NotOnRanToComplete)

· Continue with ParentTask.Result

-- -- TPL Tips – Composing

· AttachedToParent

8. -- TPL Tips – AggregateException

9 . -- TPL Tips – Cool Tools

· Parallel Tasks (see the location of code)

· Parallel Stacks (see the stack of the code)

What is hot in the coming releases in .NET 5.0?

Task DataFlow (TDF) – is based on Concurrency and Coordination Runtime and Visual C++ Asynchronous Agency 2010. Currently it is based on.

Visual Studio Async - async and await keywords. You can await anything, not only Task and Task.


Like I mentioned, if you haven't started to use TPL, you should start now. TPL is easy to use, easy to debug, easy to boost the performance of your application.

Saturday, October 02, 2010

TFS 2010 Basic is My Choice for Personal Use

Team Foundation Server (commonly abbreviated to TFS) is a Microsoft product offering source control, data collection, reporting, and project tracking, and is intended for collaborative software development projects. It is good choice for big company like Chevron. But I wouldn’t choose TFS 2005 or TFS 2008 as my choice for personal use or for small companies. But with the compact features of TFS 2010 Basic: Source Control, TFS Build, and Work Items; and the power tools to easily backup and restore,  I found TFS is more attractive to me than other tools as Subversion or CVS as my personal source control system along with Application Life Cycle Management system.

 

So I picked TFS 2010 Basic to install with SQL Server Express on my Windows 7. The installation was very pleasant and smooth. It works like a charm.

 

TFS 2010 Basic is my choice now for personal use. It can be yours too.

 

References:

·         Reference: Mahesh Mitkari's Blog  My Coffee cup: Installing TFS 2010 on Windows 7

·         Reference: How to Backup / Restore TFS 2010

·         Download TFS Power Tools September 2010

 

 

Wednesday, June 16, 2010

SortedSet vs HashSet


HashSet<T> is very good at add and search operations. Any search operation (Contains, Remove, and similar operations) are O(1). That's great. However, on the minus side, the HashSet<T> is not a sorted collection. Therefore, enumerating the elements in a sorted order forces you to copy the items to a different collection (like a List<T>) and sort the resulting list. You could construct a LINQ query to order the elements, however internally that query will likely use some form of temporary storage to create the sorted sequence. That means every sort will be an expensive operation. Sort is typically an O(n ln n) operation, Also, because the HashSet<T> does not have a sort method, you'll also have increased memory pressure and time cost to copy the elements.

SortedSet is new to .NET 4.0 System.Collections.Generic namespace. SortedSet<T> has different characteristics. The sorted set ensures that the elements in the set are always in sorted order. Every Add operation places the new element in the correct location in the set. That means Add is an O(ln n) operation. The SortedSet<T> must perform a binary search to find the correct location for the new element. The search happens on any of the search actions (Contains, Remove, etc). Those operations also have an O(ln n) performance characteristic. That sounds like the SortedSet<T> is always slower than the HashSet<T>. No one would use it if it was always slower. SortedSet<T> is much faster for iterating the set in sorted order. It's already in the correct order, so the enumeration becomes an O(n) operation.

Conclusion
SortedSet<T> will typically be faster than HashSet<T> when the majority of your operations require enumerating the set in one particular order. If, instead, most of the operations are searching, you'll find better performance using the HashSet<T>. The frequency of insert operations also has an effect on which collection would be better. The more frequently insert operations occur, the more likely HashSet<T> will be faster.

Thursday, February 25, 2010

System.OutOfMemoryException on WCF Web Service

Recently ran into OutOfMemoryException from a .NET 3.0 WCF web service whenever the w3wp.exe reaches ~1.395 GB memory. WCF web service is hosted in IIS 6.0. After poking around, the problem was found...

IIS has limitations and warts when it comes to memory handling, and if your WCF service really must use more than 1.4 GB of memory on the server, then you need to host that WCF service yourself, in a console app, a NT Service, a Winforms app - whichever way to you choose to go.
Quick question though: how is your server going to handle 10 simultaneous requests if handling each request will use up 1.4 GB of memory....

Keep in mind that you don't get access to all memory if you're running in asp.net, it'll only allow you 2gigs with a standard configuration. Maybe you should farm this out to a windows service, or a console app.


See here: "Fact: In a standard setup your worker process always have 2GB Virtual memory available (no matter if you have 1, 2 or 4GB physical memory in the machine)."

http://jesperen.wordpress.com/2007/05/23/understanding-aspnet-memory/

In that case, I am going to check out
WCF streaming which allows you to substantially reduce the size of buffer memory needed on the server. Let me get back to this after I try WCF streaming out.

Tuesday, December 29, 2009

Apply FxCop Rules to Multiple Solutions

It's easy to apply FxCop into the projects with Visual Studio. You don't need to manually change each project settings using the project Properties dialogue. You can just copy and paste the settings to each .csproj file which is the MSBuild file.

The steps to applied the same FxCop rules to the multiple projects at the same time are:

  • Unload the projects in a batch.
  • Edit the csproj files in a batch. This will automatically check the .csproject files out. Past the following lines into the .csproj file in each configuration files.

<PropertyGroup Condition=" '$(Configuration)$(Platform)' == 'DebugAnyCPU' ">

<RunCodeAnalysis>true</RunCodeAnalysis>

<CodeAnalysisRules>-Microsoft.Design#CA1005;-Microsoft.Design#CA1011;-Microsoft.Design#CA1009;-Microsoft.Design#CA1019;-Microsoft.Design#CA1000;-Microsoft.Design#CA1006;-Microsoft.Design#CA1046;-Microsoft.Design#CA1035;-Microsoft.Design#CA1033;-Microsoft.Design#CA1014;-Microsoft.Design#CA1017;-Microsoft.Design#CA1018;-Microsoft.Design#CA1060;-Microsoft.Design#CA1034;-Microsoft.Design#CA1052;-Microsoft.Design#CA1057;-Microsoft.Design#CA1030;-Microsoft.Design#CA1003;-Microsoft.Design#CA1007;-Microsoft.Globalization#CA1301;-Microsoft.Globalization#CA1306;-Microsoft.Globalization#CA1305;-Microsoft.Globalization#CA1300;-Microsoft.Globalization#CA1309;-Microsoft.Interoperability#CA1403;-Microsoft.Interoperability#CA1406;-Microsoft.Interoperability#CA1413;-Microsoft.Interoperability#CA1402;-Microsoft.Interoperability#CA1407;-Microsoft.Interoperability#CA1404;-Microsoft.Interoperability#CA1410;-Microsoft.Interoperability#CA1411;-Microsoft.Interoperability#CA1405;-Microsoft.Interoperability#CA1409;-Microsoft.Interoperability#CA1415;-Microsoft.Interoperability#CA1408;-Microsoft.Interoperability#CA1414;-Microsoft.Interoperability#CA1412;-Microsoft.Interoperability#CA1400;-Microsoft.Interoperability#CA1401;-Microsoft.Mobility#CA1600;-Microsoft.Mobility#CA1601;-Microsoft.Performance#CA1812;-Microsoft.Performance#CA1824;-Microsoft.Portability#CA1901;-Microsoft.Portability#CA1900;-Microsoft.Security#CA2116;-Microsoft.Security#CA2117;-Microsoft.Security#CA2115;-Microsoft.Security#CA2102;-Microsoft.Security#CA2122;-Microsoft.Security#CA2114;-Microsoft.Security#CA2123;-Microsoft.Security#CA2108;-Microsoft.Security#CA2107;-Microsoft.Security#CA2103;-Microsoft.Security#CA2118;-Microsoft.Security#CA2109;-Microsoft.Security#CA2119;-Microsoft.Security#CA2106;-Microsoft.Security#CA2112;-Microsoft.Security#CA2120;-Microsoft.Security#CA2126;-Microsoft.Security#CA2124;-Microsoft.Security#CA2127;-Microsoft.Security#CA2128;-Microsoft.Security#CA2129;-Microsoft.Usage#CA2227;-Microsoft.Usage#CA2212;-Microsoft.Usage#CA2219;-Microsoft.Usage#CA2228;-Microsoft.Usage#CA2240;-Microsoft.Usage#CA2229;-Microsoft.Usage#CA2238;-Microsoft.Usage#CA2239;-Microsoft.Usage#CA2242;-Microsoft.Usage#CA2230</CodeAnalysisRules>

<PropertyGroup>

Load the projects again in a batch.

Undo the .sln file if the solution file has been checked out by visual studio.

Tuesday, December 22, 2009

To-do list in TFS Project Migration

I can think of at least two reasons why project migration is not avoidable in TFS 2008.

  1. You want to rename your project. For complicated reasons, renaming is not supported in TFS. So the only you can achieve this is to create a new project then migrate everything.
  2. You want to change the process template you used in TFS project. Changing the template is not supported in TFS 2008 either.

Migrating the team project, you can easily think of three parts.

  1. Source control code
  2. Work Items
  3. SharePoint Documents

However there are other things you need to consider too.

  1. TFS groups and membership
  2. TFS security settings
  3. TFS queries

Some of these items are manual processes.

  • Like work items migration, you need to use TFS query to get the work items you need, then export to excel then import to the new TFS project.
  • SharePoint Documents.
  • TFS queries
  • TFS security settings.

Some of these items you can accomplish using scripts.

  • Group membership migration. I found one good tool you can utilize to migrate group membership in CodePlex
  • Source code migration. You can simply use TF.EXE to compose a batch file.

Before the migration you should consider to stop TFS SQL server's transaction log jobs. Then migrate. Restart the SQL transaction log job. Re-index the database to achieve the ultimate performance.

Is High Quality Software Worth the Cost?

I can't agree more with Martin Fowler's post on "Is High Quality Software Worth the Cost?". Here is just a repost. https:/...