Tag Archives: .NET

Intel Threading Challenge #1

While I never participated in the Intel Threading Challenge, I still find the problems really intriguing. Why? Because they are problems designed to test threaded development which is not only cool but is also going to play a large part in the future of computing. I would call all this problem for the most part concurrent, not parallel. Why? Read this and you'll understand completely. Now, on to the challenge!

Problem # 1 states:

Problem description: Given a set of unsorted items with keys that can be considered as a binary representation of an integer, the bits within the key can be used to sort the set of items. This method of sorting is known as Radix Sort.

Write a program that includes a threaded version of a Radix Sort algorithm that sorts the keys read from an input file, then output the sorted keys to another file. The input and output file names shall be the first and second arguments on the command line of the application execution.

The first line of the input text file is the total number of keys (N) to be sorted; this is followed by N keys, one per line, in the file.  A key will be a seven-character string made up of printable characters not including the space character (ASCII 0x20). The number of keys within the file is less than 2^31 - 1.  Sorted output must be stored in a text file, one key per line.

Timing: If you put timing code into your application to time the sorting process and report the elapsed time, this time will be used for scoring.  If no timing code is added, the entire execution time (including time for input and output) will be used for scoring.


Example Input file:
8
H@skell
surVEYs
sysTEMS
HASKELL
Surveys
1234567
SURveys
systEMS

Example Output file:
1234567
H@skell
HASKELL
SURveys
Surveys
surVEYs
sysTEMS
systEMS

My solution (both serial and parallel):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Diagnostics;
using System.Threading;
using System.IO;
 
namespace RadixSort {
 
    class Program {
        static void Main(string[] args) {
            StreamReader sr;
            int length;
            TimeSpan serial, parallel;
            sr = File.OpenText(@"C:\Documents and Settings\rgreen\Desktop\Threading\Threading\rsTestK100.dat");
 
            length = Convert.ToInt32(sr.ReadLine().Trim());
            string[] values = new string[length];
            string[] newValues = new string[length];
            for(int x = 0; x < length; x++) {
                values[x] = sr.ReadLine().Trim();
            }
 
            Stopwatch sw = new Stopwatch();
 
            //
            // Serial
            //
            sw.Start();
            RadixSort(values).CopyTo(newValues, 0);
            sw.Stop();
            serial = sw.Elapsed;
 
            //
            // Parallel
            //
            sw.Reset();
            sw.Start();
            ParallelRadixSort(values).CopyTo(newValues, 0);
            sw.Stop();
            parallel = sw.Elapsed;
 
 
            Console.WriteLine("Serial Time: " + serial);
            Console.WriteLine("Parallel Time: " + parallel);
            Console.ReadLine();
        }
 
        public static string[] ParallelRadixSort(string[] array) {
            int length = array.Length;
 
            Parallel.For(0, array[0].Length - 1, delegate(int curRadix) {
                int index = array[0].Length - 1 - curRadix;
                array = new MergeSort(array, array[0].Length - 1 - index).Results;
            });
 
            return array;
        }
 
        public static string[] RadixSort(string[] array) {
 
            int length = array.Length;
 
            for(int curRadix = array[0].Length - 1; curRadix >= 0; curRadix--) {
                array = new MergeSort(array, array[0].Length - 1 - curRadix).Results;
            }
 
            return array;
        }
    }
}

And there you have it!

Project Euler #6

Project Euler problem #6 is

Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.

When I first began looking at this problem I wanted to experiment a bit with the Parallel Extensions for .NET, so I started where any parallel algorithm begins: with a sequential algorithm. The algorithm here is rather trivial: Loop over the natural numbers from 1 to 100. In order to sum the squares I will sum the square of each number. In order to square the sum I will sum the numbers and then square them. The difference is the answer. So, first, I came up with two functions: SumOfSquares and SquareOfSums.


My first attempt at these functions ended up something like this:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
static double SumOfSquares(int min, int max) {
    double result = 0;
    for(int x = min; x <= max; x++) {
        result += Math.Pow(x, 2);
     }
     return result;
}
static double SquareOfSums(int min, int max) {
    double result = 0;
    for(int x = min; x <= max; x++) {
        result += x;
    }
    return Math.Pow(result, 2);
}

Both of those functions are very straight forward. My first thought after writing these was, "Hey, why not use some LINQ?" So I did. Here is how the functions change:


1
2
3
4
5
6
static double LinqSumOfSquares(int min, int max) {
    return Enumerable.Range(min, max).Select(d => Math.Pow(d, 2)).Sum();           
}
static double LinqSquareOfSums(int min, int max) {
    return Math.Pow(Enumerable.Range(min, max).Select(d => d).Sum(), 2);    
}

Wow! Talk about incredible, shrinking functions! I love finding a way to make code more succinct, readable, and elegant and these changes seem to have hit the nail on the head! Anyways, that is basically all the pieces for the sequential algorithm. All you have to do is call each of those functions and take the difference. Simple, huh? But the real question is how can we parallelize these bad boys? I have a few thoughts.

  1. Call each function in parallel. In other words let the sumOfSquares and SquareOfSum function run at the same time in different threads.
  2. Parallelize each function invidvidually. In other words leverage the loops inside of each function in order to parallelize them.

So let's take a look at each method using the Parallel Extensions for .NET. The first method is calling each function in parallel. My first thought here was to use Parallel.Invoke in order to call each function at the same time. A little further research quickly revealed that Parallel.Invoke cannot return any values. My initial response to that: "Well that sucks." Luckily there's another class in the Parallel library called Futures. What's a future? From DevX -

"In TPL terms, a Future is basically a task that returns a value. It's like a deferred function. You start it running and then use its value later. If the Future hasn't finished calculating its value by the time you need it, it makes you wait while it finishes."

Sounds good to me. So how do we use futures? Like this:


1
2
3
4
5
6
7
8
static long Problem6Futures(int min, int max) {
 
    Future<double> fSumOfSquares = Future.Create(() => SumOfSquares(min, max));
    Future<double> fSquareOfSums = Future.Create(() => SquareOfSums(min, max));
    double result = fSquareOfSums.Value - fSumOfSquares.Value;
 
    return result;
}

Easy, huh? All you have to remember is that Futures are deferred functions that return values. The result of Future operation gets stored in an object of type Future and the value is stored in Object.Value. So how about the second method of parallelizing this algorithm? Well, it's even easier because of PLINQ - or Parallel LINQ. Let's see what it looks like.


1
2
3
4
5
6
static double ParallelSumOfSquares(int min, int max) {
    return Enumerable.Range(min, max).AsParallel().Select(d => Math.Pow(d, 2)).Sum();
}
static double ParallelSquareOfSums(int min, int max) {
    return Math.Pow(Enumerable.Range(min, max).AsParallel().Sum(), 2);
}

Hah! Even easier! All that I did was add .AsParrallel() to our data. That tells LINQ to do the processing in parallel!

So, there's Project Euler Problem #6 for you. Was it a hard problem? Not really. Are you going to see major performance results through the parallelization of this algorithm? No. But you gotta' start somewhere when you're learning how to parallelize algorithms using a new library. Today I used Futures and PLINQ and that sound's like a pretty solid start to me.

The Future of Web Pages

This morning I came across this jQuery library and it got me to thinking a little bit. All of the web sites that I currently have coded ( I don't really do the design) tend to use .NET or PHP that is mashed up with some Javascript here and there.


I'm wondering why we tend to still use PHP and .NET except for things that are absolutely on the back end. Why would I use .NET or PHP to manipulate elements in a page when I can just use JavaScript?


Maybe the web community should start developing pages that are JavaScript run and call PHP/.NET WebServices. That puts all the processing for display on the JS and all the data manipulation and retrieval where it should be - on the server.


Any thoughts?

Content-Disposition attachment vs inline

Today I ran into an interesting issue. We have some legacy code in .NET 1.1 that exports an HTML table to Microsoft Excel. This export occurs by simply rendering the table via Response.Write and setting the header content-disposition to "attachment; filename=FileName.xls". The original code looked something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
Response.Clear()
Response.AddHeader("content-disposition", "attachment;filename=SalesByProductReport.xls")
Response.Charset = "utf-8"
Response.Cache.SetCacheability(HttpCacheability.NoCache)
Response.ContentType = "application/vnd.ms-excel"
 
Dim stringWrite As IO.StringWriter = New System.IO.StringWriter
Dim htmlWrite As HtmlTextWriter = New HtmlTextWriter(stringWrite)
 
tblTable.RenderControl(htmlWrite)
 
Response.Write(stringWrite.ToString())
Response.Flush()
Response.End()

The problem that occurred was that any user using Internet Explorer (surprise, surprise!) would get a prompt to download the file but the file would not download! The file worked properly in all other browsers. The solution is to change

1
Response.AddHeader("content-disposition", "attachment;filename=SalesByProductReport.xls")

to

1
Response.AddHeader("content-disposition", "inline;filename=SalesByProductReport.xls")

Now, why exactly does this work? I'm not sure, so if you know please tell me.

ASP .NET Nested Forms (Or why do I get the error 'Invalid postack or callback argument')

I have now come across a few situations where using nested forms in ASP .NET causes problems. The typical error produced in this case is a 'Invalid postback or callback argument' error. This occurs because ASP .NET allows the rendering of multiple, nested forms but fails to validate the forms when a postback occurs. Thus when ASP .NET recognizes nested forms in a page, it marks the page as not valid (Page.IsValid returns false).

The reason that this error occurs is because multiple, nested forms cannot reside on a single aspx page .

The solution is very simple: The submit button for the user created form must contain the following attribute: onclick="this.form.submit();". A recent example I came across is below. The basic form that was developed and placed inside an aspx page as a nested form was as follows (only the beginning and ending of this form is shown. The content is not necessary or relevant):

1
2
3
4
5
6
7
8
9
10
11
12
<form accept-charset="UTF-8" action="http://www.response-o-matic.com/mail.php" method="post" enctype="multipart/form-data">
<table>
<tbody>
...
<tr>
<td colspan="2" align="center">
<input value="Submit Form" type="submit" />
</td>
</tr>
</tbody>
</table>
</form>

The following code demonstrates the proper way of entering this form so that nested forms will work. Please note that I reiterated all of the attributes (action, method, enctype, etc...) of the form in the JavaScript call.

1
2
3
4
5
6
7
8
9
10
11
12
<form accept-charset="UTF-8" action="http://www.response-o-matic.com/mail.php" method="post" enctype="multipart/form-data">
<table>
<tbody>
...
<tr>
<td>
<input onclick="this.form.action='http://www.response-o-matic.com/mail.php'; this.form.method='post';this.form.enctype='multipart/form-data';this.form.submit();" value=" Submit Form " type="submit" />
</td>
</tr>
</tbody>
</table>
</form>

SEO 301 Redirects for ASP .NET 1.1

I recently worked on a project where any URL following the form of http://www.mydomain.com/subDomain/Default.aspx needed to 301 redirect to http://www.mydomain.com/subDomain/. Basically this calls for stripping the 'Default.aspx' off of any request that has it. The project was to be completed using Visual Basic .NET 1.1.

The solution is  is to add a few lines of code in the Global.asax.vb file inside of the  Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs) function. The final product looks like this:

1
2
3
4
5
6
7
8
9
Sub Application_BeginRequest(ByVal sender As Object, ByVal e AsEventArgs)
    Dim oPath As String = Request.CurrentExecutionFilePath.ToLower
    If Not oPath.EndsWith("default.aspx") Then Return
 
    Response.Status = "301 Moved Permanently"
    Response.AddHeader("Location", "http://www.myDomain.com" +  
    oPath.Substring(0, oPath.IndexOf("default.aspx")))
 
End Sub

Debugging .NET 1.1 With Visual Studio 2003 on Windows XP x64

In order to debug .NET 1.1 properly, it is necessary to uninstall the .NET 2.0 framework when you want to debug. The command to uninstall is:

C:WINDOWSMicrosoft.NETFrameworkv2.0.50727aspnet_regiis.exe -u

The command to reinstall when your are done is:

C:WINDOWSMicrosoft.NETFrameworkv2.0.50727aspnet_regiis.exe -i

Upload Large Files in ASP.NET

Here's the simple solution to uploading large files in ASP .net.

Add a new key in the web.config. The fileSize (maxRequestLength) is in kilobytes with a default size of 4.

The key to add is

1
<httpRuntime maxRequestLength="2000000"/>